1 16 package com.google.gwt.sample.i18n.client; 17 18 import com.google.gwt.i18n.client.NumberFormat; 19 import com.google.gwt.user.client.ui.HasText; 20 21 24 public class NumberFormatExampleController extends 25 AbstractFormatExampleController { 26 27 private static final String DEFAULT_INPUT = "31415926535.897932"; 28 private NumberFormat activeFormat; 29 private final NumberFormatExampleConstants constants; 30 31 public NumberFormatExampleController( 32 final NumberFormatExampleConstants constants) { 33 super(DEFAULT_INPUT, constants.numberFormatPatterns()); 34 this.constants = constants; 35 } 36 37 public NumberFormatExampleConstants getConstants() { 38 return constants; 39 } 40 41 protected String doGetPattern(String patternKey) { 42 if ("currency".equals(patternKey)) { 43 return NumberFormat.getCurrencyFormat().getPattern(); 44 } 45 46 if ("decimal".equals(patternKey)) { 47 return NumberFormat.getDecimalFormat().getPattern(); 48 } 49 50 if ("scientific".equals(patternKey)) { 51 return NumberFormat.getScientificFormat().getPattern(); 52 } 53 54 if ("percent".equals(patternKey)) { 55 return NumberFormat.getPercentFormat().getPattern(); 56 } 57 58 throw new IllegalArgumentException ("Unknown pattern key '" + patternKey 59 + "'"); 60 } 61 62 protected void doParseAndRememberPattern(String pattern) { 63 activeFormat = NumberFormat.getFormat(pattern); 64 } 65 66 protected void doParseInput(String toParse, HasText output, HasText error) { 67 if (!"".equals(toParse)) { 68 try { 69 double x = Double.parseDouble(toParse); 70 String s = activeFormat.format(x); 71 output.setText(s); 72 } catch (NumberFormatException e) { 73 String errMsg = constants.failedToParseInput(); 74 error.setText(errMsg); 75 } 76 } else { 77 output.setText("<None>"); 78 } 79 } 80 } 81 | Popular Tags |