ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Working with the Google Web Toolkit
Pages: 1, 2, 3, 4, 5

Revisionist History

As anyone who has used (most) Ajax or RIA apps knows, the back button can be the bane of your existence. Fortunately, GWT includes a means of dealing with it. The History class allows you to store a single String token that you can use to capture the display state and redraw as needed. This is accomplished by inserting the token into the anchor part of the URL on the page and writing the location.history as needed.

Let's modify our simple example to support the back button. First, we want to capture state changes and store them in the History object. To do this, we will add a new TableEventListener to our table in our onModuleLoad():

//Add a new listener to record the row history
table.addTableListener( new TableListener(){
    public void onCellClicked(SourcesTableEvents sender, 
        int row, int cell) {
        History.newItem( ""+row );

Now, we want to capture changes to the back button and update our selection on our table appropriately. So we will make our EntryPoint a HistoryListener:

public class TableEntryPoint 
    implements EntryPoint, HistoryListener {

    public void onHistoryChanged(String historyToken){
        table.onCellClicked( table, 
            Integer.parseInt( historyToken ), 0);

Now, whenever you click the back button, it will track back to the last selected item. The last step is to make sure our initialization works in case the page is bookmarked or gets a hard reload. To do this, we go back to our getData() method on the EntryPoint and modify the onSuccess() handler of the AsyncCallback. This will check to see if there is a stored token in the History object and reset the state of the table to the selected row stored in the history token.

public void onSuccess(Object result) {
    table.setSource( new 
        SimpleDataSource( (Person[]) result ) );
    if(History.getToken().length() != 0){
        table.onCellClicked( table, 
            Integer.parseInt( History.getToken()), 0);

The final step is to go back and modify our HTML host page with a hidden iframe that the GWT script uses for history manipulation:

<iframe id="__gwt_historyFrame" 

This is a pretty primitive example, but hopefully you understand enough to make use of the History class in a more complicated application. Figure 3 shows the sample application's table display.

Figure 3. Our finished table widget in the GWT browser.

That's Not All!

There is still more coolness in the GWT. There is still more not covered here, included a JavaDoc based-annotation system for typing collections (Java SE 5 generics aren't supported for IsSerializable), JUnit testing frameworks, and lots of cool widgets to use and extend. Also, be sure to check out the GWT provided examples. Although I admit I find some of the programming idioms a bit strange -- and it may simply be my history as "Not a GUI developer guy" -- they definitely give you a feel for what can be done with the framework. In fact the DynaTable example is a much more robust version of what we have built here.

Pages: 1, 2, 3, 4, 5

Next Pagearrow