KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > i18n > client > NumberFormatExampleController


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc 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 JavaDoc doGetPattern(String JavaDoc 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 JavaDoc("Unknown pattern key '" + patternKey
59         + "'");
60   }
61
62   protected void doParseAndRememberPattern(String JavaDoc pattern) {
63     activeFormat = NumberFormat.getFormat(pattern);
64   }
65
66   protected void doParseInput(String JavaDoc toParse, HasText output, HasText error) {
67     if (!"".equals(toParse)) {
68       try {
69         double x = Double.parseDouble(toParse);
70         String JavaDoc s = activeFormat.format(x);
71         output.setText(s);
72       } catch (NumberFormatException JavaDoc e) {
73         String JavaDoc errMsg = constants.failedToParseInput();
74         error.setText(errMsg);
75       }
76     } else {
77       output.setText("<None>");
78     }
79   }
80 }
81
Popular Tags