Wednesday, August 17, 2011

GWT - Tips and Tricks

Scroll Bar of browser does not works in Firefox and Chrome

If you have a dialog box and I believe Popup panel also, the scroll bar of firefox and chrome stop working. The scroll bar work fine in IE. This is a known bug in GWT. See http://code.google.com/p/google-web-toolkit/issues/detail?id=399.

To solve this (the solution is given in the bug description itself):
Disable the modal attribute of dialog box and enable the glass panel

dialogBox.setGlassEnabled(true);
dialogBox.setModal(false);

Firing native event from GWT

//Creating a key press event
NativeEvent nativeEvent =  Document.get().
       createKeyPressEvent(false,false,false,false,KeyCodes.KEY_TAB);

//Firing the event
DomEvent.fireNativeEvent(nativeEvent, this);

However note that this is still not equivalent to firing by user actions. For example you can fire a tab event programmatically, but this will not lead to the tab behvaior of moving into next widget. The limitation is put by Javascript for security reasons.

How can we stop GWT scaffolding code using Spring Roo?

Using Spring roo with GWT, to generate the basic structure we use gwt:setup which generates the initial scaffolding but after that If we want to stop the scaffolding, particularly when the domain class are annotated with @RooEntity.

Rename the generated directory from gwt to something else. Than roo stops generating the scaffolding code when class is marked with RooEntity

Can GWT work with pure HTML in UIBinder?

GWT cannot work with pure HTML. It needs g tag for widgets which than are mapped to widget objects in Java class. In fact HTML is used by GWT and compiled and converted into Javascript and other artifacts for the browser. However it still is a convenient way to handle layout markups.

TabLayoutPanel has Problems

GWT version - 2.1.0.M3
TabLayout Panel has some of the problems outlined below:

The content of the tab does not shows up often. To solve that some of things that should be taken care of-
Provide width and height explicitly

<g:TabLayoutPanel ui:field="rightTabPanel" width="100%" 
                     height="100%" barUnit="EM" barHeight="3">

Use standards mode (using <!DOCTYPE html>).In UIBinder this is on top of ui.xml file

Make sure that the TabLayoutPanel is attached to RootLayoutPanel and not RootPanel.
Inspite of this it does not shows graph widget inside the tab. This might be true for other widgets type also.
The only way I could solve it was to move to g:TabPanel. GWT keeps showing the deprecated warning but it works.(That's more important for my customer :-)). Still though I would like to move to TabLayoutPanel once it is stable. See http://stackoverflow.com/questions/3777213/gwt-tablayoutpanel-do-not-renders-the-graph-content also.

Tomcat Deployment Issues

If you see one of the following issues:

java.lang.ClassCastException: org.apache.jasper.servlet.JspServlet cannot be cast to javax.servlet.Servlet
Tomcat6 error : jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

Make sure the gwt-dev.jar is not part of war. This is required to run it in debug mode. One change that can be made to pom is

<dependency>
   <groupId>com.google.gwt</groupId>
  <artifactId>gwt-dev</artifactId>
  <version>2.1-SNAPSHOT</version>
  <scope>test</scope>
</dependency>

The basic reason for this problem is that the war are not allowed to override J2SE or Java servlet API classes. It seems for dev mode to work gwt-dev redefined the javax.servlet.Servlet and hence the problem comes.

Out of Memory Problem in GWT

If you are using GWT with maven, then if code becomes large, it throws out of memory error. To solve that, changing the maven memory options, which essentially is Java options does not helps. To solve this, provide extraJvmArgs to the gwt compiler as follows

<execution>
  <id>gwtcompile</id>
  <phase>prepare-package</phase>
  <goals>
    <goal>compile</goal>
  </goals>
  <configuration>
    <extraJvmArgs>-Xmx1g</extraJvmArgs>
  </configuration>
</execution>

Note the extraJvmargs which is set to 1 GB in this case.

Performance Issues with GWT collection emulation library

This is from the blog of Nikos which contains even benchmark data. As per the blog, the recommendation is to use the native arrays for performance. The problem shows up more in IE.

GWT Introduction

No comments:

Post a Comment