10.3. Fleshing Out the Application: The ClientNow that the simple application is in place, we need to make it do something interesting. We know how to create a skeleton, and we have a sense of how to debug, but we're a long way from having a working ZipCodes application. We'll start by working on the client. The source code for the rudimentary client that GWT built is in the src/com/oreilly/ajax/client/ZipCodes.java file. This file looks a lot like a Java GUI application. There's a listener, there's a button, you can set text on elements with a call to setText( ), and so on. It's not all that different from AWT or Swing. The main points to note are that the ZipCodes class implements EntryPoint and overrides the onModuleLoad( ) method. The onModuleLoad( ) method configures the GUI widgets, and at the end of this method, the widgets are added to elements in the HTML page. The HTML file (Example 10-3) is located in the project's public subdirectory. Example 10-3. ZipCodes.html
The ZipCodes.html file contains a hook to the Web Toolkit module, which is defined by the <meta> tag. You'll also see that the JavaScript reference to gwt.js is in the document's body rather than inside the <head> tag, where you would normally expect it. That's supposed to make it faster to load. Finally, note that the <td> elements in the table have ids of "slot1" and "slot2". These are the places where GWT puts the button and the label. If you look back at ZipCodes.java (Example 10-2), you'll see that "slot1" and "slot2" are used to identify elements in the root panel; the button and the label are added to these elements. The Java client uses these ids to access elements in the HTML page. 10.3.1. Customizing the ClientNow let's get started with the ZipCodes client. We only need three fields: zip code, city, and state. Those fields will be in a <div> with the id of "gridholder" (see Example 10-4). Example 10-4. Modified ZipCodes.html
The status div is used to pass error messages and other information from the server, as you'll see shortly.
Now let's modify the ZipCodes class to populate the HTML page (Example 10-5). Example 10-5. ZipCodes updated with a grid and widgets
The ZipCodes class now looks like a GUI application. It has widgets and a grid, and the widgets have methods like setText( ) and listeners like the FocusListener. GWT will translate these widgets into JavaScript, but you really don't need to worry about that while developing your code. Just treat the client like a Java GUI application. The ZipCodes class defines widgets at the beginning of the class; for this application, TextBox and Label widgets are used within a Grid layout. The widgets are configured in the onModuleLoaded( ) method: the visible length of the text fields is set with setVisibleLength( ), the maximum length of the fields is set with setMaxLength( ), and the widgets are formatted in a grid with grid.setWidget( ). The Grid object is initialized as a three-row, two-column array of widgets: private Grid grid = new Grid(3, 2); The DOM library that GWT supplies is used to format a message that can be sent to the client. Look at the displayFormattedMessage( ) method. The DOM object can be used to retrieve elements from the client and set styles on the elements (for example, setting the color). Those style attributes match the styles that can be set with stylesheets. Look at a good CSS reference to see the different styles that can be set on an element. To display the message, call setInnerHTML( ). If you are familiar with dynamically changing HTML text with JavaScript, you will recognize this call. It is very similar to setting the innerHTML parameter with JavaScript. At this point, you can run this application in hosted mode (Figure 10-10). Try entering a zip code and then pressing the Tab key to move to the next field. Figure 10-10. ZipCodes application clientThe application has no services yet, so a message is sent to the client to inform the user that the services are not yet available. The next section shows how to make a service available to the client. |