FileDocCategorySizeDatePackage
AbstractFormatExampleController.javaAPI DocExample5026Mon Aug 27 20:11:56 BST 2007com.google.gwt.sample.i18n.client

AbstractFormatExampleController

public abstract class AbstractFormatExampleController extends Object
Abstract base class used to implement {@link NumberFormatExampleController} and {@link DateTimeFormatExampleController}.

Fields Summary
private static final String
PATTERN_KEY_CUSTOM
public final com.google.gwt.user.client.ui.TextBox
txtCurrentPattern
public final com.google.gwt.user.client.ui.Label
lblFormattedOutput
public final com.google.gwt.user.client.ui.Label
lblPatternError
public final com.google.gwt.user.client.ui.Label
lblParseError
public final com.google.gwt.user.client.ui.ListBox
lstSamplePatterns
public final com.google.gwt.user.client.ui.TextBox
txtInput
private String
prevPattern
private String
prevInput
Constructors Summary
protected AbstractFormatExampleController(String defaultText, Map patterns)


       
    initWidgetsForPattern(patterns);
    initWidgetsForInput();
    txtInput.setText(defaultText);
    tryToParseInput(false);
  
Methods Summary
protected abstract java.lang.StringdoGetPattern(java.lang.String patternKey)

protected abstract voiddoParseAndRememberPattern(java.lang.String pattern)
Parses the specified pattern and remembers it for formatting input later.

param
pattern
throws
IllegalArgumentException if the pattern could not be parsed

protected abstract voiddoParseInput(java.lang.String toParse, com.google.gwt.user.client.ui.HasText output, com.google.gwt.user.client.ui.HasText error)

private voidinitWidgetsForInput()

    txtInput.addKeyboardListener(new KeyboardListenerAdapter() {
      public void onKeyUp(Widget sender, char keyCode, int modifiers) {
        tryToParseInput(false);
      }
    });
  
private voidinitWidgetsForPattern(java.util.Map patternMap)

    txtCurrentPattern.addKeyboardListener(new KeyboardListenerAdapter() {
      public void onKeyUp(Widget sender, char keyCode, int modifiers) {
        String pattern = txtCurrentPattern.getText();

        // Update the active pattern.
        tryToActivatePattern(pattern);
      }
    });

    // Load pattern choices.
    for (Iterator iter = patternMap.entrySet().iterator(); iter.hasNext();) {
      Map.Entry entry = (Map.Entry) iter.next();
      String patternKey = (String) entry.getKey();
      String caption = (String) entry.getValue();

      lstSamplePatterns.addItem(caption, patternKey);
    }

    lstSamplePatterns.addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        syncPatternToList();
      }
    });

    lstSamplePatterns.setSelectedIndex(0);

    syncPatternToList();
  
private voidsyncPatternToList()

    int sel = lstSamplePatterns.getSelectedIndex();
    assert (sel >= 0) && (sel < lstSamplePatterns.getItemCount());

    // Update the current pattern.
    String patternKey = lstSamplePatterns.getValue(sel);
    String pattern;
    if (PATTERN_KEY_CUSTOM.equals(patternKey)) {
      // Make the pattern text box editable.
      txtCurrentPattern.setReadOnly(false);
      pattern = txtCurrentPattern.getText();
      txtCurrentPattern.setText(pattern);
      txtCurrentPattern.selectAll();
      txtCurrentPattern.setFocus(true);
    } else {
      // Make the pattern text box read only.
      txtCurrentPattern.setReadOnly(true);
      pattern = doGetPattern(patternKey);
      txtCurrentPattern.setText(pattern);
    }

    // Make the new pattern active.
    tryToActivatePattern(pattern);
  
private voidtryToActivatePattern(java.lang.String pattern)

    if (!pattern.equals(prevPattern)) {
      prevPattern = pattern;
      lblPatternError.setText("");
      try {
        // Allow the subclass to parse the pattern.
        doParseAndRememberPattern(pattern);

        // Parse and format the input again since the pattern changed.
        tryToParseInput(true);
      } catch (IllegalArgumentException e) {
        lblPatternError.setText(e.getMessage());
      }
    }
  
private voidtryToParseInput(boolean forceReparse)

    String toParse = txtInput.getText();
    if (forceReparse || !toParse.equals(prevInput)) {
      prevInput = toParse;
      lblParseError.setText("");
      doParseInput(toParse, lblFormattedOutput, lblParseError);
    }