Tuesday, November 14, 2006

Google Web Toolkit (GWT) using VistaFei w/ RPC, etc.

I started looking into GWT and after many revelations about what it does and doesn't do, I fell in love. :)

I went thru several tutorials and projects on my own to learn a bunch of stuff and thanks to the many forums and such for helping.

I wound up checking out VistaFei and it's been a great help.


I checked out Googlipse which was cool too, but w/o the GUI editing I wasn't convinced I needed it.

Also this tutorial by Robert Hanson was VERY helpful to get started.

To download VistaFei, you have to register w/ the forum and then go to this post (or this post for mac). It only shows the links if you're logged into the forum.

You'll need some things (see this post) like Java5 and the GWT of course.

So I went thru the tutorials and such from the forum. Very cool and played around with it on my own.

A few things that were helpful to learn...
- the GWT page on IsSerializable doesn't mention that a Serializable class needs an empty/default constructor
- GWT seems to only allow/support JUnit testing for the 'client' package
- VistaFei regenerates all the generated code so back up your changes
- RPC callbacks need a way to communicate their results back to the entry point class

These last 2 points are my focus here...

Generated code

When VistaFei generates the code, it overwrites the existing code which makes sense. However, if you've made changes to that code, you'll need to back it up somewhere. Currently, I put all my code at the bottom (and fully qualify the references so that I don't have to change any imports) and back it up to a file named <file name>_code.txt. I intend to write a script to find those files and put the code w/in the last curly-bracket in the appropriate file (or you can and send it to me unless someone has a better option???).

Similarly, the RPC server implementation classes are overwriten w/ the default (relatively) empty methods. I wrote 2 .bat files that back these files up and restores them... when I remember to do it. :)

Using the VistaFei properties area, I added code to the onClick event and such. I tried to keep it simple like 'handleAddEvent();' so that it doesn't ever need to change. Then I add that method at the bottom.

RPC Callbacks

When it uses RPC, I usually call a class like ControllerRPC w/ static methods to handle the RPC. I pass in the caller ('this') for any callback processing. The ControllerRPC gets the appropriate ServiceManager class that VistaFei generated and runs the request method. I create the callback w/ the success and failure methods.

Since I have the calling class, I can call whatever method I need to on it (generally something I write since most of the members/methods of the caller are private since it's the entry point class: generated, gui).

So the entry point (MyApp.java) gets the onClick event for the 'Add' button and calls handleAdd(). That method calls ControllerRPC.addItem(sName, this) which gets the AddItemServiceManager.INSTANCE and calls requestAddItem(sName, callback) where the callback is defined w/ the 'accept' and 'failed' methods. Something like this (forgive the formatting)...


public static void addItem(final String sName, final MyApp theApp)
{
AddItemServiceManager asm = AddItemServiceManager .INSTANCE;
asm .requestaddItem(sName, new AddItemIResponse() {

public void accept(Object result) {
// TODO Auto-generated method stub
System.out.println("accept");
theApp.setItemAdded((AddedItem)result);
}

public void failed(Throwable caught) {
// TODO Auto-generated method stub
System.out.println("failed: " + caught.getMessage());
}
});
}


The parameters need to be final if you're going to manipulate them. The above method is the way it's done in the VistaFei RPC tutorial but I cdn't tell it from the website. I had to download the source which is on that same page.

Dig around the VistaFei forum and the GWT doco - in particular the Developer's Guide. Also, the blog, forum, developer's groups, etc. are helpful of course.

BTW, I'm using db4o for my backend, server side database and it rocks.... HARD!

I hope some of the above is useful to someone. Please leave comments/questions either way.

Also, if anyone has better solutions for protecting your custom code in the generated classes, let me know.