1 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 ; 27 import java.util.Map ; 28 29 33 public abstract class AbstractFormatExampleController { 34 35 private static final String 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 prevPattern; 43 private String prevInput; 44 45 protected AbstractFormatExampleController(String defaultText, Map patterns) { 46 initWidgetsForPattern(patterns); 47 initWidgetsForInput(); 48 txtInput.setText(defaultText); 49 tryToParseInput(false); 50 } 51 52 protected abstract String doGetPattern(String patternKey); 53 54 60 protected abstract void doParseAndRememberPattern(String pattern); 61 62 protected abstract void doParseInput(String 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 patternMap) { 74 txtCurrentPattern.addKeyboardListener(new KeyboardListenerAdapter() { 75 public void onKeyUp(Widget sender, char keyCode, int modifiers) { 76 String pattern = txtCurrentPattern.getText(); 77 78 tryToActivatePattern(pattern); 80 } 81 }); 82 83 for (Iterator iter = patternMap.entrySet().iterator(); iter.hasNext();) { 85 Map.Entry entry = (Map.Entry ) iter.next(); 86 String patternKey = (String ) entry.getKey(); 87 String caption = (String ) 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 String patternKey = lstSamplePatterns.getValue(sel); 109 String pattern; 110 if (PATTERN_KEY_CUSTOM.equals(patternKey)) { 111 txtCurrentPattern.setReadOnly(false); 113 pattern = txtCurrentPattern.getText(); 114 txtCurrentPattern.setText(pattern); 115 txtCurrentPattern.selectAll(); 116 txtCurrentPattern.setFocus(true); 117 } else { 118 txtCurrentPattern.setReadOnly(true); 120 pattern = doGetPattern(patternKey); 121 txtCurrentPattern.setText(pattern); 122 } 123 124 tryToActivatePattern(pattern); 126 } 127 128 private void tryToActivatePattern(String pattern) { 129 if (!pattern.equals(prevPattern)) { 130 prevPattern = pattern; 131 lblPatternError.setText(""); 132 try { 133 doParseAndRememberPattern(pattern); 135 136 tryToParseInput(true); 138 } catch (IllegalArgumentException e) { 139 lblPatternError.setText(e.getMessage()); 140 } 141 } 142 } 143 144 private void tryToParseInput(boolean forceReparse) { 145 String 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 |