Writing a simple test case
In JUnit, a test case is a class that may contain one or more tests. The purpose of the test case is to group a set of tests together, typically based on their function. For example, you may want to place all tests for a calendar widget in the same class, potentially allowing you to write private methods that can be shared among the tests. In the example, you'll write a test case that tests basic math functionality, giving you a feel for how to use JUnit. You begin by creating a new class named...
Implementing a continuously updating component
In this section, we'll revisit the Server Status component and allow it to periodically update itself. To do this, you'll add the ability to set the refresh rate for the data, as well as add the ability to change or stop the automatic updates. In doing so, we'll need to look at the GWT Timer class, which allows you to trigger timed events. If you've been following along with us, your Server Status component should be working and have a method that can be called to have it update its information...
Simple RPC with RequestBuilder
The RequestBuilder class allows you to call a URL and register a handler to receive the result. The handler works in a similar fashion as to how it worked with GWT-RPC in chapter 11, except that you can only send and receive text-only data. Any data that isn't in a text form, like a Date value, must be converted to text when the data is sent. The data can be sent by either GET or POST, which implies URL-encoded data when using a GET but for a POST, you can pass any sort of text-based data. For...
Exploring events 1
Event handling in web applications ties visual elements the user sees to the functionality of the application. This could be through clicking a button, dragging a component, changing a value, or a number of other events. GWT supports all the events that a browser can manage, as shown in table 6.1. Table 6.1 Browser events that a GWT application can handle, together with internal GWT values that may be assigned to them Table 6.1 Browser events that a GWT application can handle, together with...
Sinking events
The first way a widget can register interest in events is to use the sinkEvents method. Using this method, you can tell the GWT application and browser what types of events the widget is interested in. A widget can sink all browser events shown in table 6.1 when it does so, it registers the wnd._dispatchEvent method for events the widget should sink. It also updates the DOM element representation of the widget to show which events it's interested in. In the Dashboard, you want the...
Injecting resources into an application at runtime
GWT provides the ability to inject into applications JavaScript and CSS resources at runtime as opposed to defining them in the applications HTML file . You may want to do this, for example, if you're embedding a GWT application into an existing web page and you want to keep the application's CSS separate from the web page HTML. We'll look at both types of injection in turn first, injecting JavaScript resources into applications and second, injecting style-sheet resources into applications at...
Using dynamic string internationalization
The dynamic approach was originally designed to allow existing i18n approaches to be quickly incorporated into GWT applications. If your existing approach used JavaScript associative array objects containing sets of key-value pairs, like those shown in table 15.5, then dynamic-string i18n would potentially work for you. Table 15.5 Two JavaScript associative array objects containing English and Swedish user interface constants Table 15.5 Two JavaScript associative array objects containing...
Understanding the GWT event model
One of the first things a GWT application does when it loads is to register a single global event handler at the browser's document level. It performs this through the init method of the DOM class, which has two implementations one for IE and one for all the other browsers GWT supports. For IE, this is performed in the DOMImplIE6 class doc.body.onclick doc.body.onmousedown doc.body.onmouseup doc.body.onmousemove doc.body.onkeydown doc.body.onkeypress doc.body.onkeyup doc.body.onfocus...
Writing custom field serializers 1
With GWT's basic serialization system, you can create data objects that implement the IsSerializable interface, which can be passed between the client and server. When you implement this interface, GWT handles all the details of serialization and deserialization for you. This is usually sufficient for most work with GWT, but occasionally this mechanism isn't sufficient for a given project. Three common reasons for needing to write a custom field serializer are as follows The default...
Managing sunk events with the onBrowserEvent method
Once you've indicated that a widget should sink particular events, you must set up the application to handle those events. You do that in the widget's code by overriding the onBrowserEvent method. The default implementation of this method is defined in the Widget class as an empty method, consistent with the GWT model of not handling events unless explicitly directed to. For the DashboardPanel, which extends DialogBox, you must handle all the DialogBox sunk events plus the onDblClick event you...
Using JSON with Perl on the server
Perl is one of the old workhorses of the Web, and it's still popular today. In this section, we'll provide the code required to get the server portion of your Yahoo-based search service project up and running in Perl. In the course of doing so, you'll see how Perl can work with JSON messages, as well as its ability to act as a proxy between the client and a remote service. As with the Java version, you need to get a few external libraries, the first of which provides support for JSON. As with...
Examining GWTs internationalization and configuration tools
GWT provides several techniques that can aid you with internationalization and configuration issues. This may seem like an odd pair, but they're similar in that you want the ability to store text strings or numeric values in a properties file and to access them from your application. GWT provides two primary mechanisms that should handle most needs static inclusion at compile time, and dynamic inclusion at runtime. The static method is accomplished by implementing the Constants or Messages...
Introducing GWT
High-level overview of GWT's toolset Comparison of GWT to similar technologies An example of a GWT application In May 2006, Google released the Google Web Toolkit GWT , a set of development tools, programming utilities, and widgets that let you create rich Internet applications differently than you may have done before. The difference between GWT and all those other frameworks is that with GWT you write your browser-side code in Java instead of JavaScript. For those of us who rely on Java as a...
Step Implementing the right GWT Java interfaces
GWT provides a number of interfaces that widgets can claim to implement. A GWT Label, for instance, implements the HasText interface, which means it provides setText and getText methods. When you use widgets in a composite widget, these interfaces become buried. It's possible to make the widgets your composite uses public then, a user can call the interface methods on those widgets, but the user needs to know the structure of your composite. To improve on that and maintain consistency across...
Using HTML to load a JavaScript library
The first of the two ways we'll look at for loading a JavaScript library is through the HTML file the application lives in. You do so by using the normal way of linking JavaScript files to a web page, using the lt script gt tag. In the head section of the Dashboard.html file, you place the following text type text javascript gt lt script gt You need to replace the X with a Google Ajax Search API key, which you can obtained from http code.google.com apis ajaxsearch when using the API in hosted...
Parsing and validating a JSON server response
When you receive the JSON message results from the server, you should add comprehensive validation to the message. Because this requires a bit of code, you'll break up the functionality into several bite-sized pieces. Listing 13.4 is the shell for the response handler it contains four methods, three of which aren't currently defined. class SearchResultsHandler implements RequestCallback public void onError Request request, Throwable exception GWT.log Search request failed, exception...
Listing An example of overriding the TestCase setUp method
protected void setUp throws Exception protected void tearDown throws Exception Although managing a connection pool is more relevant to testing server-side code than it is to GWT code, it's a concept that most developers can understand. You initialize or reset the services needed by the test in the setUp method, and you shut down or release the services in tearDown . Although you may not use these methods often, it's good to know that JUnit makes it easy to do. At this point, you know everything...
Summary Amx
That concludes the first part of the journey through the GWT basics, covering widgets. You've seen that GWT provides quite a few standard widgets but where the functionality you need isn't present, then it's relatively simple to create your own widgets. You can do this either by starting from scratch or by extending an existing widget if you need more complicated functionality, which is better implemented as two or more simple widgets put together, then you should consider composite widgets,...
Wrapping a complex JavaScript library 1
The previous section wrapped a simple JavaScript library where there was only one JavaScript object. As you saw, this is a real-life example, but nowadays it's often the case that the JavaScript libraries you may want to wrap contain more than one object that you need to manage. Let's look at the Google Ajax Search API, which you'll use to present the user with a Dashboard search component similar to that shown in figure 8.15. We won't go into as much detail about the wrapping in this section,...
Altering the FormPanel target
There is a good reason why you may want to change the target of the form submission, and it goes back to how traditional HTML forms work. Typically, when you submit a form on the Web, the browser loads a completely new page. Although the idea behind Ajax techniques is that the page doesn't need to reload, you still may need to do this. With the FormPanel, you can set the target of the form with the constructor only, and you can't change it once it has been set. There are three constructor...
Calling the remote server service
Now that you have the remote service interface defined and implemented, you've created the remote asynchronous interface, and you've created a serializable object to pass between the server and client, all that remains is to call the service from the client browser. To do this, you need to do the following 1 Instantiate a proxy object that will forward method calls to the server. 2 Specify the URL of the service. 3 Create a callback method to handle the result of the asynchronous method call....
GWT vs Echo
Echo2 is another popular web toolkit in the same problem space as GWT, and it's similar to GWT in how it's used to create the UI. You use the API to create instances of components and then add them to the display. Listing 1.4 shows the Echo2 version of the GWT reference example the two versions look nearly identical. Listing 1.4 An implementation of a simple Button event in Echo2 final TextField text new TextField text.setText text box final Button button new Button button.setText Click Me...
Accessing the JRE emulation library
We mentioned earlier that the GWT compiler needs access to the Java source code for any class you're using in your code. This requirement doesn't stop with the use ofjust external libraries it includes the JRE as well. To provide developers with the ability to use some of the JRE classes, GWT provides the JRE Emulation Library. This library contains the most commonly used parts of the full JRE, which you can use in your projects and compile to JavaScript. Tables 1.1 and 1.2 enumerate the...
Implementing custom field serialization
Thus far, we've looked at the method signatures for the custom field serializer, but you haven't yet implemented them. Let's inspect each method individually and explain its purpose and how it should be implemented. Implementing the instantiate method When GWT serializes or deserializes an object using your custom field serializer class, it calls instantiate . The purpose of this method is to create a new instance of your object, including passing any values to the constructor required for its...
Listening to FormPanel events
The FormPanel allows you to register an event listener, allowing you to write handler code for exposed events. The two events are the submission of the form and the completion of the submission form.addFormHandler new FormHandler public void onSubmit FormSubmitEvent event The onSubmit handler is triggered just before the form submission, allowing you to validate the form and even cancel the submission. To cancel the submission, you call setCancelled true on the event object passed to the...
GWT vs Ruby on Rails
The title of this section is a little misleading because GWT and Ruby on Rails don't compete, although they overlap in some respects. Ruby on Rails is a rapid development framework that uses the Ruby language. It provides the server side of the equation and is specifically designed to handle a lot of the back-end work automatically for you. On the client side, Ruby on Rails provides some support for Ajax, allowing you to use the Java equivalent of a tag library in your HTML code. The end result...
Access JavaScript window variable through GWT wnd variable
Access JavaScript document variable through GWT doc C variable You use both the wnd and doc variables to access y offset properties of the browser. At O, you retrieve the pageYOffset value for non-IE browsers, and at Q you need to use the doc variable to retrieve the body.scrollTop variable for IE. Whichever value you eventually retrieve is the one returned to the caller of the method. This is also a great example to point out the limitation of JSNI we spoke of earlier managing browser...
Accessing properties of the context
When the GWT system creates the bindings required for internationalization, the generator needs to know which locale the current context is all about in order to select the appropriate properties file this is why the files must be named in the particular manner they have been . Similarly, for the Dashboard, you wish to know if you're generating for the Internet or intranet version. In chapter 15, we'll introduce a user-defined property called externalvisibility that you can set as either...





