Friday, April 16, 2010

Generics in UiBinder


One of the most exciting things about GWT is that you get to use Java. And one of the most exciting things about Java is that you get to use Generics.



Another great thing about GWT is the UiBinder, a tool that makes it really easy to create and (most importantly) modify and maintain ui layouts.


For a long time, though, I was stuck, because I was creating a bunch of fancy UI widgets that used Generics, but I didn't know how to specify them in my .ui.xml files.

There wasn't any mention of generics in the official google ui:binder tutorial, so I just sort of assumed it was impossible. As a workaround, I created <g:SimplePanel> instances in the .ui.xml files, and then filled them dynamically in the corresponding java files when calling createAndBindUi().

The other day, though, I was surfing through our app's DOM via Firebug (thank you OOPHM!) and I was getting a little annoyed at all the extra DIVs that were being created due to the wrapping SimplePanel instances.

So I looked a little deeper and I found a hit on the "google-web-toolkit" google code page entitled GWT UIBinder Use Cases.

Comment by, Dec 13, 2009

@johan.rydberg, generics support is anemic, trying only not to break things. You can't specify generics in your ui.xml file, and you'll need to instantiate your Table in your owner class.

I'm pretty sure this should work (making some presumptions about your typed Table class):

 ui:field='rowTypeTable' />
@UiField(provided = true)

final Table<RowType> rowTypeTable = new Table<RowType>() {}

Very happily, I don't have a single thing to add here, the solution above works perfectly, no modifications needed.

Probably would be a good addition to the UiBinder tutorial page on the gwt homepage, but in the meantime, I figure putting it up here might help others who hit this same problem.

No comments:

Post a Comment