Section 8.3. Java Source


8.3. Java Source

This is the Java source code that creates all of the visible widgets, except for one h2 tag, for our application. The code represents a Java file like most others: a package statement followed by the importation of several classes, which are parts of the GWT API, followed by a class definition.

Like a Java file that uses Swing, this file includes a lot of GUI-related code. The source code is commented, but you might look at Figure 7 or 8 to recall what the window looks like in order to help you understand what is going on here.

 package com.parkerriver.gwt.intro.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.HistoryListener; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.HTTPRequest; import com.google.gwt.user.client.ResponseTextHandler; import com.google.gwt.user.client.ui.Widget; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.Grid; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.FormPanel; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.json.client.JSONObject; import com.google.gwt.json.client.JSONParser; import com.google.gwt.json.client.JSONValue; import com.google.gwt.json.client.JSONException; /**  * Entry point classes define <code>onModuleLoad()</code>.  */ public class GwtAjax implements EntryPoint,     HistoryListener {   //The URL that the HTTPRequest.asyncGet() method uses   public static final String TARGET_URL =       "http://bwpmacmini.local/~bruceperry/info2.php";   private final FormPanel form = new FormPanel();   //A Grid, or HTML table, with 5 rows and 3 columns   private final Grid formGrid = new Grid(5, 3);   //The server name goes in this read-only TextBox   private final TextBox servBox =       createTextBox(50, 50, "", null);   //The request time goes in this read-only TextBox   private final TextBox timeBox =       createTextBox(50, 50, "", null);   //The browser's user-agent string fills this TextBox   private final TextBox browserBox =       createTextBox(50, 50, "", null);   //Check the return value against this regular expression   private final static String regexCheck =     "^\\{\"(server|browser|date)\":\".+\"\\,\"(server|browser|date)\":"+   "\".+\"\\,\"(server|browser|date)\":\".+\"\\}$";   /**    * This is the entry point method, called when    * the user loads the application into the browser.    */   public void onModuleLoad() {     final Label servLab = createLabel("Server name: ");     //The textboxes are not editable;     // they just display values     servBox.setEnabled(false);     //Add these widgets to the Grid's first row     formGrid.setWidget(0, 0, servLab);     formGrid.setWidget(0, 1, servBox);     final Label timeLab = createLabel(         "Time of your request: ");     timeBox.setEnabled(false);     //Add the request-time label and TextBox widgets     //to the Grid's second row     formGrid.setWidget(1, 0, timeLab);     formGrid.setWidget(1, 1, timeBox);     final Label browserLab = createLabel(         "User Agent string: ");     browserBox.setEnabled(false);     //Add the user-agent label/TextBox to the Grid's third row     formGrid.setWidget(2, 0, browserLab);     formGrid.setWidget(2, 1, browserBox);     //Finally, add the submit button     Button but = new Button("Request Info");     //The GwtIntroListener is a nested class; see below     but.addClickListener(new GwtIntroListener());     //Position the button in the grid's last row     formGrid.setWidget(3, 0, but);     //Add the Grid to the form     form.setWidget(formGrid);     //Add the entire form/grid widget to a div     // with id "container"     RootPanel root = RootPanel.get("container");     if (root != null) root.add(form);  }   /* A convenience method for creating labels */   protected Label createLabel(String labText) {     Label lab = new Label();     lab.setText(labText);     return lab;   }   /* A convenience method for creating TextBoxes */   protected TextBox createTextBox(int len, int visibleLen, String name,                                   String width) {     TextBox tb = new TextBox();     tb.setMaxLength(len);     tb.setVisibleLength(visibleLen);     if (! (name == null || name.equals(""))) tb.setName(name);     //the object's new width, in CSS units (e.g. "10px", "1em")     if (! (width == null || width.equals(""))) tb.setWidth(width);     return tb;   }   public void onHistoryChanged(String string) {     RpcStatus stat = new RpcStatus();     stat.setStatusDivId("err_message");     stat.showStatus(true, "token: "+string, "purple");   }   private boolean checkResponseValue(String val) {     return val.matches(regexCheck);   }   private class GwtIntroListener implements       ClickListener{     public void onClick(Widget widg) {       //Use this object to show the status of the request       final RpcStatus status = new RpcStatus();       //Allow different translations of these messages       //by using GWT's i18n interfaces       final GwtAjaxConstants myConstants =           (GwtAjaxConstants) GWT.               create(GwtAjaxConstants.class);       status.showStatus(true,           myConstants.formSubmissionMsg(), "green");       //Send the asynchronous request for the server info.       //Use the com.google.gwt.http.client package for       //more functionality.       HTTPRequest.asyncGet(           TARGET_URL,           new ResponseTextHandler() {             public void onCompletion(                 String responseText) {               //debug return value...               //Window.alert(responseText);               if (responseText == null ||                   responseText.equalsIgnoreCase("")) {                 status.setStatusDivId("err_message");                 status.showStatus(true,                     "The server appears to be unavailable right now." +                         " Please try again in a moment.",                     "red");                 return;               }               try {                 //Check the return value against our regular expression                 if (! checkResponseValue(responseText))                   throw new JSONException(                       "The return value was invalid.");                 JSONValue jval = JSONParser.parse(                     responseText);                 JSONObject jobj = jval.isObject();                 if (jobj != null) {                   //Write the info into the textboxes                   servBox.setText(jobj.get("server").                       isString().stringValue());                   timeBox.setText(jobj.get("date").                       isString().stringValue());                   browserBox.setText(jobj.get("browser").                       isString().stringValue());                   status.showStatus(true,                       myConstants.JsonMsg(), "green");                 }               } catch (JSONException je) {                 status.setStatusDivId("err_message");                 status.showStatus(true, "Exception message: " +                     je.getMessage(), "red");               }             }           });//end asyncGet     }//end onclick   }//end ClickListener nested class   //Various Getters for using in JUnit tests   public TextBox getServBox() {     return servBox;   }   public TextBox getTimeBox() {     return timeBox;   }   public TextBox getBrowserBox() {     return browserBox;   }   public FormPanel getForm() {     return form;   }   public Grid getFormGrid() {     return formGrid;   } } 




Google Web Toolkit for Ajax
Google Web Toolkit GWT Java AJAX Programming: A step-by-step to Google Web Toolkit for creating Ajax applications fast
ISBN: 1847191002
EAN: 2147483647
Year: 2006
Pages: 29

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net