001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.sample.i18n.client;
017:
018: import com.google.gwt.user.client.ui.ChangeListener;
019: import com.google.gwt.user.client.ui.HasText;
020: import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
021: import com.google.gwt.user.client.ui.Label;
022: import com.google.gwt.user.client.ui.ListBox;
023: import com.google.gwt.user.client.ui.TextBox;
024: import com.google.gwt.user.client.ui.Widget;
025:
026: import java.util.Map;
027:
028: /**
029: * Abstract base class used to implement {@link NumberFormatExampleController}
030: * and {@link DateTimeFormatExampleController}.
031: */
032: public abstract class AbstractFormatExampleController {
033:
034: private static final String PATTERN_KEY_CUSTOM = "custom";
035: public final TextBox txtCurrentPattern = new TextBox();
036: public final Label lblFormattedOutput = new Label();
037: public final Label lblPatternError = new Label();
038: public final Label lblParseError = new Label();
039: public final ListBox lstSamplePatterns = new ListBox();
040: public final TextBox txtInput = new TextBox();
041: private String prevPattern;
042: private String prevInput;
043:
044: protected AbstractFormatExampleController(String defaultText,
045: Map<String, String> patterns) {
046: initWidgetsForPattern(patterns);
047: initWidgetsForInput();
048: txtInput.setText(defaultText);
049: tryToParseInput(false);
050: }
051:
052: protected abstract String doGetPattern(String patternKey);
053:
054: /**
055: * Parses the specified pattern and remembers it for formatting input later.
056: *
057: * @param pattern
058: * @throws IllegalArgumentException if the pattern could not be parsed
059: */
060: protected abstract void doParseAndRememberPattern(String pattern);
061:
062: protected abstract void doParseInput(String toParse,
063: HasText output, HasText error);
064:
065: private void initWidgetsForInput() {
066: txtInput.addKeyboardListener(new KeyboardListenerAdapter() {
067: @Override
068: public void onKeyUp(Widget sender, char keyCode,
069: int modifiers) {
070: tryToParseInput(false);
071: }
072: });
073: }
074:
075: private void initWidgetsForPattern(Map<String, String> patternMap) {
076: txtCurrentPattern
077: .addKeyboardListener(new KeyboardListenerAdapter() {
078: @Override
079: public void onKeyUp(Widget sender, char keyCode,
080: int modifiers) {
081: String pattern = txtCurrentPattern.getText();
082:
083: // Update the active pattern.
084: tryToActivatePattern(pattern);
085: }
086: });
087:
088: // Load pattern choices.
089: for (Map.Entry<String, String> entry : patternMap.entrySet()) {
090: String patternKey = entry.getKey();
091: String caption = entry.getValue();
092:
093: lstSamplePatterns.addItem(caption, patternKey);
094: }
095:
096: lstSamplePatterns.addChangeListener(new ChangeListener() {
097: public void onChange(Widget sender) {
098: syncPatternToList();
099: }
100: });
101:
102: lstSamplePatterns.setSelectedIndex(0);
103:
104: syncPatternToList();
105: }
106:
107: private void syncPatternToList() {
108: int sel = lstSamplePatterns.getSelectedIndex();
109: assert (sel >= 0) && (sel < lstSamplePatterns.getItemCount());
110:
111: // Update the current pattern.
112: String patternKey = lstSamplePatterns.getValue(sel);
113: String pattern;
114: if (PATTERN_KEY_CUSTOM.equals(patternKey)) {
115: // Make the pattern text box editable.
116: txtCurrentPattern.setReadOnly(false);
117: pattern = txtCurrentPattern.getText();
118: txtCurrentPattern.setText(pattern);
119: txtCurrentPattern.selectAll();
120: txtCurrentPattern.setFocus(true);
121: } else {
122: // Make the pattern text box read only.
123: txtCurrentPattern.setReadOnly(true);
124: pattern = doGetPattern(patternKey);
125: txtCurrentPattern.setText(pattern);
126: }
127:
128: // Make the new pattern active.
129: tryToActivatePattern(pattern);
130: }
131:
132: private void tryToActivatePattern(String pattern) {
133: if (!pattern.equals(prevPattern)) {
134: prevPattern = pattern;
135: lblPatternError.setText("");
136: try {
137: // Allow the subclass to parse the pattern.
138: doParseAndRememberPattern(pattern);
139:
140: // Parse and format the input again since the pattern changed.
141: tryToParseInput(true);
142: } catch (IllegalArgumentException e) {
143: lblPatternError.setText(e.getMessage());
144: }
145: }
146: }
147:
148: private void tryToParseInput(boolean forceReparse) {
149: String toParse = txtInput.getText();
150: if (forceReparse || !toParse.equals(prevInput)) {
151: prevInput = toParse;
152: lblParseError.setText("");
153: doParseInput(toParse, lblFormattedOutput, lblParseError);
154: }
155: }
156: }
|