01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
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: /**
22: * Demonstrates how to use {@link NumberFormat}.
23: */
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: @Override
42: protected String doGetPattern(String patternKey) {
43: if ("currency".equals(patternKey)) {
44: return NumberFormat.getCurrencyFormat().getPattern();
45: }
46:
47: if ("decimal".equals(patternKey)) {
48: return NumberFormat.getDecimalFormat().getPattern();
49: }
50:
51: if ("scientific".equals(patternKey)) {
52: return NumberFormat.getScientificFormat().getPattern();
53: }
54:
55: if ("percent".equals(patternKey)) {
56: return NumberFormat.getPercentFormat().getPattern();
57: }
58:
59: throw new IllegalArgumentException("Unknown pattern key '"
60: + patternKey + "'");
61: }
62:
63: @Override
64: protected void doParseAndRememberPattern(String pattern) {
65: activeFormat = NumberFormat.getFormat(pattern);
66: }
67:
68: @Override
69: protected void doParseInput(String toParse, HasText output,
70: HasText error) {
71: if (!"".equals(toParse)) {
72: try {
73: double x = Double.parseDouble(toParse);
74: String s = activeFormat.format(x);
75: output.setText(s);
76: } catch (NumberFormatException e) {
77: String errMsg = constants.failedToParseInput();
78: error.setText(errMsg);
79: }
80: } else {
81: output.setText("<None>");
82: }
83: }
84: }
|