FileDocCategorySizeDatePackage
Zipcodes.javaAPI DocExample4412Sun Jan 28 14:59:58 GMT 2007com.oreilly.ajax.client

Zipcodes

public class Zipcodes extends Object implements com.google.gwt.core.client.EntryPoint
Entry point classes define onModuleLoad().

Fields Summary
private com.google.gwt.user.client.ui.Label
zipCodeLabel
private com.google.gwt.user.client.ui.TextBox
zipCodeBox
private com.google.gwt.user.client.ui.Label
stateLabel
private com.google.gwt.user.client.ui.TextBox
stateTextBox
private com.google.gwt.user.client.ui.Label
cityLabel
private com.google.gwt.user.client.ui.TextBox
cityTextBox
private com.google.gwt.user.client.ui.Grid
grid
Constructors Summary
Methods Summary
private voiddisplayFormattedMessage(java.lang.String message, java.lang.String color)

    Element el = DOM.getElementById("status");
    if (el != null)
    {
      DOM.setStyleAttribute(el, "font-family", "ariel, san-serif");
      DOM.setStyleAttribute(el, "font-size", "1.2em");
      DOM.setStyleAttribute(el, "color", color);
      DOM.setInnerHTML(el, message);
    }
  
public voidonModuleLoad()
This is the entry point method.


           
    
  

    zipCodeLabel.setText("Zip Code:");
    zipCodeBox.setVisibleLength(5);
    zipCodeBox.setMaxLength(5);

    stateTextBox.setVisibleLength(2);
    stateTextBox.setMaxLength(2);
    cityTextBox.setMaxLength(40);
    cityTextBox.setVisibleLength(40);

    grid.setWidget(0, 0, zipCodeLabel);
    grid.setWidget(0, 1, zipCodeBox);
    grid.setWidget(1, 0, cityLabel);
    grid.setWidget(1, 1, cityTextBox);
    grid.setWidget(2, 0, stateLabel);
    grid.setWidget(2, 1, stateTextBox);

    zipCodeBox.addFocusListener(new FocusListener()
    {
      public void onFocus(Widget sender)
      {
      }

      public void onLostFocus(Widget sender)
      {
        ResponseServiceAsync respService = (ResponseServiceAsync) GWT
            .create(com.oreilly.ajax.client.ResponseService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) respService;

        endpoint.setServiceEntryPoint("/responseService");


        displayFormattedMessage("getting data...", "blue");
        AsyncCallback callback = new AsyncCallback()
        {
          public void onSuccess(Object result)
          {

            JSONObject jsonObject;
            try
            {
              displayFormattedMessage("Parsing JSON data...", "blue");
              jsonObject = JSONParser.parse((String) result);
              String[] keys = jsonObject.getKeys();
              if (keys.length >= 2)
              {
                String city = jsonObject.get("city").toString();
                String state = jsonObject.get("state").toString();
                cityTextBox.setText(city);
                stateTextBox.setText(state);
                displayFormattedMessage("", "blue");
              }
              else
              {
                cityTextBox.setText("");
                stateTextBox.setText("");
                displayFormattedMessage("This zip code was not found in the database.", "red");
              }

            } catch (JSONException e)
            {
              displayFormattedMessage("Error parsing JSON data \n"
                  + e.getMessage(), "red");
            }
          }

          public void onFailure(Throwable caught)
          {

            displayFormattedMessage(
                "Server request raised an error; Java exception : " + caught == null ? "An unknown exception"
                    : caught.getMessage(), "red");
          }
        };
        // Call the service method, validating the form
        // values first.
        try
        {
          displayFormattedMessage("getting request", "green");
          respService.displayResponse(zipCodeBox.getText(), callback);
        } catch (Exception e)
        {
          displayFormattedMessage("Server request raised an error: "
              + e.getMessage(), "red");
        } finally
        {
          displayFormattedMessage("", "green");
        }
      }
    });
    RootPanel.get("gridholder").add(grid);