KickJava   Java API By Example, From Geeks To Geeks.

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


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.user.client.ui.ChangeListener;
19 import com.google.gwt.user.client.ui.HasText;
20 import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
21 import com.google.gwt.user.client.ui.Label;
22 import com.google.gwt.user.client.ui.ListBox;
23 import com.google.gwt.user.client.ui.TextBox;
24 import com.google.gwt.user.client.ui.Widget;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * Abstract base class used to implement {@link NumberFormatExampleController}
31  * and {@link DateTimeFormatExampleController}.
32  */

33 public abstract class AbstractFormatExampleController {
34
35   private static final String JavaDoc PATTERN_KEY_CUSTOM = "custom";
36   public final TextBox txtCurrentPattern = new TextBox();
37   public final Label lblFormattedOutput = new Label();
38   public final Label lblPatternError = new Label();
39   public final Label lblParseError = new Label();
40   public final ListBox lstSamplePatterns = new ListBox();
41   public final TextBox txtInput = new TextBox();
42   private String JavaDoc prevPattern;
43   private String JavaDoc prevInput;
44
45   protected AbstractFormatExampleController(String JavaDoc defaultText, Map JavaDoc patterns) {
46     initWidgetsForPattern(patterns);
47     initWidgetsForInput();
48     txtInput.setText(defaultText);
49     tryToParseInput(false);
50   }
51
52   protected abstract String JavaDoc doGetPattern(String JavaDoc patternKey);
53
54   /**
55    * Parses the specified pattern and remembers it for formatting input later.
56    *
57    * @param pattern
58    * @throws IllegalArgumentException if the pattern could not be parsed
59    */

60   protected abstract void doParseAndRememberPattern(String JavaDoc pattern);
61
62   protected abstract void doParseInput(String JavaDoc toParse, HasText output,
63       HasText error);
64
65   private void initWidgetsForInput() {
66     txtInput.addKeyboardListener(new KeyboardListenerAdapter() {
67       public void onKeyUp(Widget sender, char keyCode, int modifiers) {
68         tryToParseInput(false);
69       }
70     });
71   }
72
73   private void initWidgetsForPattern(Map JavaDoc patternMap) {
74     txtCurrentPattern.addKeyboardListener(new KeyboardListenerAdapter() {
75       public void onKeyUp(Widget sender, char keyCode, int modifiers) {
76         String JavaDoc pattern = txtCurrentPattern.getText();
77
78         // Update the active pattern.
79
tryToActivatePattern(pattern);
80       }
81     });
82
83     // Load pattern choices.
84
for (Iterator JavaDoc iter = patternMap.entrySet().iterator(); iter.hasNext();) {
85       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
86       String JavaDoc patternKey = (String JavaDoc) entry.getKey();
87       String JavaDoc caption = (String JavaDoc) entry.getValue();
88
89       lstSamplePatterns.addItem(caption, patternKey);
90     }
91
92     lstSamplePatterns.addChangeListener(new ChangeListener() {
93       public void onChange(Widget sender) {
94         syncPatternToList();
95       }
96     });
97
98     lstSamplePatterns.setSelectedIndex(0);
99
100     syncPatternToList();
101   }
102
103   private void syncPatternToList() {
104     int sel = lstSamplePatterns.getSelectedIndex();
105     assert (sel >= 0) && (sel < lstSamplePatterns.getItemCount());
106
107     // Update the current pattern.
108
String JavaDoc patternKey = lstSamplePatterns.getValue(sel);
109     String JavaDoc pattern;
110     if (PATTERN_KEY_CUSTOM.equals(patternKey)) {
111       // Make the pattern text box editable.
112
txtCurrentPattern.setReadOnly(false);
113       pattern = txtCurrentPattern.getText();
114       txtCurrentPattern.setText(pattern);
115       txtCurrentPattern.selectAll();
116       txtCurrentPattern.setFocus(true);
117     } else {
118       // Make the pattern text box read only.
119
txtCurrentPattern.setReadOnly(true);
120       pattern = doGetPattern(patternKey);
121       txtCurrentPattern.setText(pattern);
122     }
123
124     // Make the new pattern active.
125
tryToActivatePattern(pattern);
126   }
127
128   private void tryToActivatePattern(String JavaDoc pattern) {
129     if (!pattern.equals(prevPattern)) {
130       prevPattern = pattern;
131       lblPatternError.setText("");
132       try {
133         // Allow the subclass to parse the pattern.
134
doParseAndRememberPattern(pattern);
135
136         // Parse and format the input again since the pattern changed.
137
tryToParseInput(true);
138       } catch (IllegalArgumentException JavaDoc e) {
139         lblPatternError.setText(e.getMessage());
140       }
141     }
142   }
143
144   private void tryToParseInput(boolean forceReparse) {
145     String JavaDoc toParse = txtInput.getText();
146     if (forceReparse || !toParse.equals(prevInput)) {
147       prevInput = toParse;
148       lblParseError.setText("");
149       doParseInput(toParse, lblFormattedOutput, lblParseError);
150     }
151   }
152 }
153
Popular Tags