15 Nov 2012, 5:37pm
gwt:

1 comment




  • Anzeige
  • GWTBridge Not Found in GWT2.5

    Just updated the GWT SDK and ended up with a bro­ken app:

    java.lang.NoClassDefFoundError: com/google/gwt/core/client/GWTBridge
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Class.java:169)
    	at com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)
    	at com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:64)
    

    After some searching/experimenting and a cup of cof­fee it finally occurred to me: the pack­age name says client! So all the time I have been using a client-package class in my server-side code. Kinda sur­pris­ing that it did work nicely all the while.

    Solu­tion: replace all com.google.gwt.core.client.GWT with com.google.gwt.core.shared.GWT in shared and server-side code.

    20 Sep 2012, 1:32pm
    gwt java:

    13 comments




  • Anzeige
  • Using GWT’s DateTimeFormat in Server-Side Code

    I was work­ing on some util­ity class to for­mat dates. As the for­mat­ting is the same on server and client the same class should be used on both, server and client. Within the GWT frame­work there is the DateTimeFormat class which seems to be sup­posed to do exactly that.

    But despite being in the shared pack­age (com.google.gwt.i18n.shared.DateTimeFormat) using it in the server-side code causes

    java.lang.UnsupportedOperationException: ERROR: GWT.create() is only usable in client code!  It cannot be called, for example, from server code. If you are running a unit test, check that your test case extends GWTTestCase and that GWT.create() is not called from within an initializer or constructor.

    Hav­ing a look into the code of the DateTimeFormat class (which is part of the shared pack­age of the GWT frame­work, thus can be used on server and client) reveals there is an import of com.google.gwt.i18n.client.LocaleInfo. But this class is part of the client pack­age — thus, it can not be used in the server-side code. It’s unclear to me why this was done like that because it sim­ply can not work by definition…

    Going down a bit fur­ther in the code of the DateTimeFormat class shows that the client pack­age class LocaleInfo is used only once like this:

    private static DateTimeFormatInfo getDefaultDateTimeFormatInfo() {
      // MUSTFIX(jat): implement
      return LocaleInfo.getCurrentLocale().getDateTimeFormatInfo();
    }

    The line is marked as “MUSTFIX” but for some rea­son it has not been fixed yet.
    There is a way to fix this issue that worked for me:

    • Copy the con­tent of DateTimeFormat to a new file in your own code
    • Remove the import of LocaleInfo
    • Chang­ing the fol­low­ing method (start­ing in line 656) like this:
    private static DateTimeFormatInfo getDefaultDateTimeFormatInfo() {
      return new DefaultDateTimeFormatInfo();
    }
    • And use this newly cre­ated class from now on in all your code.

    The fix does not seem to cause any major harm, but as John A. Tam­plin (cf. his com­ment below) clearly pointed out, apply­ing this fix will ren­der all dates using the default locale instead of using the user’s locale. Thus, this fix should be con­sid­ered a hack rather than a patch — but it does the trick until the bug has been fixed officially.

    The hack can be applied quickly — or sim­ply down­load the fixed class here.

    p.s. Unfor­tu­nately, it is not pos­si­ble to just over­write the method in a derived class because it’s a sta­tic method and pri­vate any­way.
    p.p.s. As sug­gested in the com­ments, you might also ini­tial­ize the DateTimeFormat with a new DefaultDateTimeFormatInfo() as the sec­ond para­menter. (Please note that I did not ver­ify this sug­ges­tion) In gen­eral, both ways of fix­ing this issue will cause that the date and time to be for­mated using the default locale rather than the user’s cur­rent locale, unfortunately.

    2 Jun 2012, 7:32pm
    gwt:

    3 comments




  • Anzeige
  • How to Use JQuery in GWT

    After adding the JQuery script to your project HTML file’s header section

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

    You will be able to use JQuery in native (JSNI) meth­ods by prepend­ing $wnd., e.g.:

    private native void someMethod() /*-{
    	$wnd.$(function() {
    		$wnd.$('#somediv').doSomething();
    	});
    }-*/;

    With­out $wnd. you will end up with fol­low­ing error: com.google.gwt.core.client.JavaScriptException: (ReferenceError): $ is not defined

    15 May 2012, 12:08pm
    gwt java:

    3 comments




  • Anzeige
  • JUnit-Testing and GAE APIs

    Some issues I encoun­tered when test­ing a new Java library for load­ing mul­ti­ple URLs in par­al­lel on the Google Apps Engine (GAE) (more about that library in another post).

    Here are some of the typ­i­cal errors you might encounter:

    Miss­ing time­out class

    java.lang.NoSuchMethodError: org.mortbay.thread.Timeout

    Con­text menu on project: Prop­er­ties -> Build Path -> Order and Export: move GWT SDK *above* App Engine SDK (cf. here)

    Com­pi­la­tion unit was not seen

    com.google.gwt.junit.JUnitFatalLaunchException: The test class 'com.test.client.MyTest' was not found in module 'com.text.Module'; no compilation unit for that type was seen

    • The test class needs to be within a pack­age that is con­fig­ured in the mod­ule file (*.gwt.xml). You should cre­ate a new source folder (Con­text menu on project -> New -> Source Folder) and cre­ate the whole pack­age struc­ture you use within your project up to client, e.g. com.company.client, and put your GWT tests in it. This way you make sure that the tests are not going to be deployed to GAE later on.
    • If you are test­ing client-side code, your test class needs imple­ment GWTU­nit *and* be run as GWTU­nitTest *not* as JUnitTest ;)

    API pack­age not found

    java.util.concurrent.ExecutionException: com.google.apphosting.api.ApiProxy$CallNotFoundException: The API package 'urlfetch' or call 'Fetch()' was not found.

    The GAE envi­ron­ment has not been initialized.

    • Set up the required libraries for testing:
      • Con­text menu on project -> Prop­er­ties -> Build Path -> Libraries -> Add Vari­able -> Con­fig­ure Vari­ables -> New…: Set “SDK_ROOT” as name and search you disk for “appengine-api-stubs.jar” and copy the part before “/lib/impl” into the text box named “Path”
      • Select “SDK_ROOT” from the pre­vi­ous dia­log, click “Extend…”, unfold lib and impl and select “…labs.jar”, “…stubs.jar”, and “…api.jar”; unfold “lib/testing” and select “…testing.jar” — done, whew.
    • Add fol­low­ing lines to your test class:
      private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalURLFetchServiceTestConfig());
      @Before public void setUp() { helper.setUp(); }
      @After public void tearDown() { helper.tearDown(); }
    • Have a look in com.google.appengine.tools.development.testing to see all the services/APIs avail­able for test­ing and replace LocalURLFetchServiceTestConfig with the one you need.

    I tried to keep it as brief as pos­si­ble, for more details have a look at this page.

    26 Apr 2012, 11:04am
    gwt:

    leave a comment




  • Anzeige
  • Improving Canvas Performance

    1. Time­out instead of Scheduler

    Sur­pris­ingly, using com.google.gwt.user.client.Timer seems much faster then rely­ing on com.google.gwt.core.client.Scheduler. I did not yet eval­u­ate the dif­fer­ences in detail but the lower CPU work­load speaks for itself.

    2. Ani­ma­tion­Sched­uler

    I am not really sure if this helps a lot, but you will find a lot of online doc­u­men­ta­tion explain­ing that you should use this:
    com.google.gwt.animation.client.AnimationScheduler.get(). requestAnimationFrame(callback, element);. The ele­ment para­me­ter is optional but said to improve per­for­mance as the browser can decide when to ren­der opti­mally.
    So when­ever your timer kicks in, and you are ready to ren­der the results, use the Ani­ma­tion­Sched­uler instead of call­ing your ren­der­ing rou­tines right away. It will only take very few mil­lisec­onds (depend­ing on the users screen frame rate—which is 60 Hz or more usu­ally) for the call­back to return to your code.

    3. Caching

    This parts requires much more effort and depends on your soft­ware archi­tec­ture and require­ments. The basic idea is not to redraw things that did not change. To do so we can draw each item on a hid­den can­vas once and then reuse this ren­der­ing dur­ing every draw­ing cycle.
    As the process is a bit more com­pli­cated, I will ded­i­cate it a sep­a­rate post—comming soon!

    4. Save on effects

    Yes, effects is what the can­vas is all about, but some­thing like a shadow can cost quite some CPU power. So if you run into prob­lems check if some of the effects can be achieved cheaper or even be removed.

    26 Apr 2012, 9:58am
    gwt java:

    leave a comment




  • Anzeige
  • Multiple Projects and GWT

    When work­ing on sev­eral large scale projects (in Eclipse), it’s con­ve­nient and of course more effi­cient to share and reuse code via com­mon libraries. While those are in an early stage they need to be changed a lot and there­fore it’s handy to link projects in instead of cre­at­ing new jars each time the library has been updated.
    Unfor­tu­nately, this stan­dard approach for Java devel­op­ment in Eclipse does not work that straight for­ward as with plain old Java projects, it requires three steps in total:

    1. Link the library project to all rel­e­vant projects (“Project Pref­er­ences” -> “Java Build Path” -> “Projects” -> “Add…”)
    2. Then, add the client-side code of the library (by adding it as a mod­ule.) There­fore, edit the gwt.xml of your appli­ca­tion and add for exam­ple <inherits name="net.svenbuschbeck.gwt.lib.SuperLib "/> where Super­Lib is the file name of the gwt.xml file in you library project and before that is the pack­age it lies in.
    3. Add the code to the project by link­ing a source folder. Unfor­tu­nately, this is required if you do not want to write an Ant script to com­pile your appli­ca­tion. (If you pre­fer Ant check this out) I don’t like the idea of using such a script because if you for­get to run it each time you make changes, you will end up in confusion—let’s go for the con­ve­nient, auto­matic way then.
      1. Add a folder to your appli­ca­tion project; open the “advanced” sec­tion in the folder cre­ation dia­log, select “Link to alter­nate loca­tion” and pick the source folder (usu­ally “src”) of your library project. (Hint: if you work within a team using a ver­sion­ing sys­tem, you can cre­ate a vari­able for that folder and use this instead. This way, each of your col­leagues can put the library project in a dif­fer­ent folder and accom­mo­date for that by adjust­ing the vari­able :) )
      2. Right click the folder, “Build Path” -> “Use as Source Folder”. Done.

    Sur­pris­ingly, the GWT plu­gin for Eclipse does not honor the project link­ing, thus all the ref­er­ences need to be made explicit or you will end up with lots of the fol­low­ing: ClassNotFoundException.

    31 Jan 2012, 10:53am
    gwt java javascript:

    leave a comment




  • Anzeige
  • An invalid or illegal string was specified” Exception in GWT

    While work­ing with can­vas (draw­ing stuff on it) in GWT, sud­denly and in a seem­ingly unpre­dictable man­ner I got fol­low­ing error mes­sage now and then from within the GWT frame­work code:

    com.google.gwt.core.client.JavaScriptException: (NS_ERROR_DOM_SYNTAX_ERR): An invalid or illegal string was specified;

    Again, GWT tricked me into think­ing I am writ­ing Java code and made me for­get about that it is going to be com­piled into Javascript. And because of the lat­ter, a divi­sion by zero does not throw an Devi­sion­ByZe­roEx­cep­tion, no, it returns NaN even for native data types (there is no dif­fer­en­ti­a­tion between dou­ble and Dou­ble in Javascript — there is only the object-version of dou­ble, which can be of value Double.NaN).

    But also GWT was not pre­pared to han­dle Double.NaN. When call­ing canvas.getContext().drawImage(image,x,y) and one of x and y or both were Double.NaN I got the error mes­sage shown above. If you got the same… you know what to do now: check all devi­sions in your code for poten­tial devi­sions by zero!!

    18 Jan 2012, 10:50am
    gwt java:

    leave a comment




  • Anzeige
  • Sorting Lists in GWT

    Quick one:
    Lists.sort(list, comparator) is not imple­mented in the GAE JVM.
    But, as a replacement/alternative, Collections.sort(list, comparator) is.

    28 Feb 2011, 11:10pm
    gwt java:

    leave a comment




  • Anzeige
  • How to Execute Code When the GWT Application Is Going Down

    My goal was to store the UI state of my appli­ca­tion just before it gets ter­mi­nated to be able to restore it next time the way the user left it the other day.

    I tried to add an addAttachHandler to the RootPanel to get informed about the root panel get­ting detached from the DOM so that I can final­ize my appli­ca­tion. Sur­pris­ingly, that does not work in Chrome (tested Chrome and Fire­fox only).

    But besides that this sounds like a bug to me, I found the “proper” way of doing things before the appli­ca­tion ends:

    Window.addWindowClosingHandler(new Window.ClosingHandler() {
    	@Override public void onWindowClosing(ClosingEvent event) { ... } 
    });

    In the end, I think some­thing like Document.addUnloadHandler would be more sug­ges­tive… clos­ing the win­dow or reload­ing a page is both exit­ing the appli­ca­tion by unload­ing the DOM — not clos­ing the window.

    18 Feb 2011, 1:27pm
    gwt:

    1 comment




  • Anzeige
  • Disable an Anchor in GWT

    Unex­pect­edly, call­ing setEnabled(false) does not pre­vent a link/anchor from being clicked. That means, the click events still get trig­gered.
    The rea­son is more or less a bug in GWT as it updates the list of events that are going to be trig­gered only at the moment when it gets attached to the DOM (Btw, in GWT, this process is called to sink and to unsink events, where the for­mer enables a spe­cific event to be trig­gered and the lat­ter dis­ables it).
    I found a workaround by cre­at­ing my own Anchor class and forc­ing the under­ly­ing GWT Anchor to update the list of events to be sunk.

    ...
    import com.google.gwt.event.dom.client.ClickHandler;
    import com.google.gwt.user.client.Event;
    
    public class Anchor extends com.google.gwt.user.client.ui.Anchor {
    
    	@Override public void setEnabled(boolean enabled) {
    		super.setEnabled(enabled);
    		if (isAttached()) {
    			onDetach();
    			if (enabled) {
    				sinkEvents(Event.ONCLICK);
    			} else {
    				unsinkEvents(Event.ONCLICK);
    			}
    			onAttach();
    		}
    	}
    
    	@Override protected void onLoad() {
    		super.onLoad();
    		if (isEnabled()) {
    			sinkEvents(Event.ONCLICK);
    		} else {
    			unsinkEvents(Event.ONCLICK);
    		}
    	}
    }

    Note the onLoad method, it sets up the state when the wid­get gets attached to the DOM the first time. It is required because setEnabled() could have been called before the anchor got attached.

    If you encoun­tered the same issue, please vote for this bug report.

    4 Feb 2011, 10:10pm
    gwt:

    1 comment




  • Anzeige
  • Disable Context Menu in GWT

    To make use of the right mouse but­ton, it is nec­es­sary to dis­able the native browser con­text menu (the popup menu appear­ing on right click). This can be achieved like this:

    RootLayoutPanel.get().addDomHandler(new ContextMenuHandler() {
    
    	@Override public void onContextMenu(ContextMenuEvent event) {
    		event.preventDefault();
    		event.stopPropagation();
    	}
    }, ContextMenuEvent.getType());

    Same should work for RootPanel.

    After that, it is pos­si­ble to make use of the right mouse but­ton for exam­ple like this:

    someWidget.addDomHandler(new MouseMoveHandler() {
    
    	@Override public void onMouseMove(MouseMoveEvent event) {
    		if (event.getNativeButton() == NativeEvent.BUTTON_RIGHT) {
    		...
    2 Feb 2011, 11:45pm
    gwt jobs:

    leave a comment




  • Anzeige
  • GWT Error Message “Asked for attribute parser of no type”

    Exam­ple:

    public boolean isCollapsed() {
    	return panel.isVisible();
    }
    
    public void setCollapsed() {
    	panel.setVisible(false);
    }

    Seen it? Despite the com­pli­cated and cryp­tic error mes­sage, the mis­take is rather sim­ple: the set­ter method is miss­ing a para­me­ter, i.e. “pub­lic void setCollapsed(boolean collapsed) {“.

    17 Jan 2011, 6:38pm
    gwt jobs:

    leave a comment




  • Anzeige
  • Testing GWT Application in Virtual Machine

    I am devel­op­ing on a Mac, but to test my GWT appli­ca­tions for cross-browser com­pat­i­bil­ity in Inter­net Explorer I need to use Win­dows, thus I got Win­dows 7 installed using Par­al­lels. Just by the way, to be able to test in dif­fer­ent Inter­net Explorer ver­sion, I am using a pretty handy appli­ca­tion called IETester.
    But try­ing to access local­host with IE in the vir­tual machine did not work. I got a “404 page not found” error instead of see­ing my app run­ning on the local App Engine instance. Obvi­ously, Par­al­lels does not auto­mat­i­cally for­ward local­host requests to OSX and maybe that is actu­ally a good idea security-wise.
    To fix the issue, you need to run Google App Engine on a pub­lic net­work inter­face, or in other words, bind the App Engine server to all avail­able IP addresses. The down side: every­body know­ing your IP address can see the GWT app now, but oth­er­wise you are not allowed to access it in the viru­tal machine as from your OSX’s point of view, that Win­dows machine is “some other guy access­ing from the out­side”, too. To make GAE acces­si­ble from the out­side, add the para­me­ter “-bindAd­dress 0.0.0.0″ when launch­ing you local GAE. Using Eclipse you can achieve that by right click­ing your project -> Run As -> Run Con­fig­u­ra­tions -> Choose “(x)= Argu­ments” tab; add the option to the top most box titled “Pro­gram argu­ments” in the options area (e.g. before “-port 8888″).

    The first part of the list of argu­ments should look some­thing like that:

    -remoteUI "${gwt_remote_ui_server_port}:${unique_id}" -startupUrl GlocalUiPg2.html -logLevel INFO -codeServerPort 9997 -bindAddress 0.0.0.0 -port 8888 ...

    Now, you can access you app using the OSX’s pub­lic IP address. (You can get to know your IP by hav­ing a look at the net­work pref­er­ences panel.) Launch­ing GAE from Eclipse, you will see a dif­fer­ent link (URL) in the “Devel­op­ment Mode” tab now, con­tain­ing the pub­lic IP already. Using that one in, say, your Fire­fox on Mac, it will ask you now whether you want to allow the debug­ger access. That is also due to the fact, that you are now using a pub­lic address, so it is not clear to your local debug server, whether that request came from the same com­puter or some­one else in the network.

    3 Dec 2010, 4:02pm
    gwt:

    2 comments




  • Anzeige
  • Adding a New Service (GWT)

    Adding a new servlet/service to you GWT appli­ca­tion is quite straight for­ward, e.g. by copy­ing the exam­ple “greet­ingSer­vice” or cre­at­ing a new servlet. But it’s easy to over­look a required change/adjustment of your project’s con­fig­u­ra­tion and you might end up with an error mes­sage like “Blocked attempt to access inter­face ‘some.package.SomeService’, which is not imple­mented by ‘some.other.package.SomeOtherServiceImpl’; this is either mis­con­fig­u­ra­tion or a hack attempt”.

    Check list (some should be replaced with what­ever you want to call your new service):

    • Copy or cre­ate files:
      SomeService.java and SomeServiceAsync.java in client pack­age
      SomeServiceImpl.java in server pack­age + change imple­men­ta­tion state­ment to SomeSer­vice
    • Adjust web.xml:
      <servlet>
      	<servlet-name>someServlet</servlet-name>
      	<servlet-class>some.package.SomeServiceImpl</servlet-class>
      </servlet>
      <servlet-mapping>
      	<servlet-name>someServlet</servlet-name>
      	<url-pattern>/[copy base directory name from other service declaration]/some</url-pattern>
      </servlet-mapping>
    • Anno­tate inter­face SomeService.java:
      @RemoteServiceRelativePath("some")
    • Con­nect to your new ser­vice in the client:
      private final SomeServiceAsync someService = GWT.create(SomeService.class);

    Def­i­nitely some pos­si­bil­i­ties to make a mis­takes or miss some­thing here.

    17 Sep 2010, 4:16pm
    jobs projects:

    1 comment




  • Anzeige
  • Using the XML Parser in GWT

    I tried using the XML pars­ing fea­tures of the GWT like that:

    form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
    	@Override
    	public void onSubmitComplete(SubmitCompleteEvent event) {
    		// One time upload only, to upload again, user needs to start the upload process from scatch – keeping it simple for now
    		panel.clear();
    		Window.alert(event.getResults());
    		Document result = XMLParser.parse(event.getResults());
    		...
    });

    But GWT kept telling me “No source code is avail­able for type com.google.gwt.xml.client.Document; did you for­get to inherit a required mod­ule?”. It turns out, you are required to explic­itly add the XML func­tion­al­i­ties to your project by adding fol­low­ing line to your .…gwt.xml file:

    <inherits name='com.google.gwt.xml.XML'/>

    Raises a ques­tion: What’s the point of AJAX (Asyn­chro­nous JavaScript and XML) with­out XML? Or in other words there is no AJAX with­out XML! So it’s up to you to add the AX part to GWT man­u­ally. What’s next?

    16 Sep 2010, 6:02pm
    jobs projects:

    3 comments




  • Anzeige
  • GWT FileUpload: Adding Widgets to a FormPanel

    If you build your first GWT form, for exam­ple some­thing like that:

    <!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
    <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">
    <g:HTMLPanel>
    <g:FormPanel ui:field="form"> 
    <g:FileUpload ui:field="uploadField" name="file"/>
    <g:SubmitButton ui:field="submitButton">Upload</g:SubmitButton>
    </g:FormPanel>
    </g:HTMLPanel>
    </ui:UiBinder>

    And your con­sole keeps telling you dur­ing run­time some­thing like this: “java.lang.IllegalStateException: Sim­plePanel can only con­tain one child wid­get”. Instead of writ­ing a long page of expla­na­tions and com­plaints like I did before, it’s sim­ply like that:

    Just put all your wid­gets in a panel (like Hor­i­zon­tal­Panel) and add that panel to the Form­Panel.” (Jake − cf. com­ment below)

    Thanks Jake! :)

    9 Sep 2010, 4:48pm
    jobs projects:

    1 comment




  • Anzeige
  • GWT Does Not Load Module in Local AppEngine

    The issue arose after I renamed the mod­ule file (end­ing with .gwt.xml) to bet­ter rep­re­sent the mod­ule func­tion­al­ity. I also updated all rel­e­vant files in the project (search for files con­tain­ing the old name to find them) accordingly.

    Start­ing the appli­ca­tion after that mod­i­fi­ca­tions ended up in an error (“[ERROR] Unable to find ‘<old mod­ule name>.gwt.xml’ on your class­path; could be a typo, or maybe you for­got to include a class­path entry for source?”) as the AppEngine tried load­ing the mod­ule by its old name.

    Solu­tion: Delete the launch pro­file for the project (by choos­ing “Run As…” -> “Run Con­fig­u­ra­tions…” from the con­text menu).

    Obvi­ously the GWT does not check nor update the auto­mat­i­cally gen­er­ated launch pro­file thus you need to delete it to force the GWT to cre­ate a new pro­file from scratch tak­ing the project changes into account. You might also adjust the pro­file accord­ing to the changes made, but delet­ing it is the safe and easy way.

    25 Aug 2010, 10:41am
    jobs projects:

    leave a comment




  • Anzeige
  • First GWT Steps

    Just started to work with GWT – a pretty inter­est­ing approach for web devel­op­ment com­pared to PHP or JSF. The whole Appli­ca­tion engine is quite impres­sive espe­cially allow­ing you to quickly test your appli­ca­tions locally by sup­port­ing auto­matic hot deploy­ment after each code update.
    One thing that took me a while was one of that “[ERROR] Unable to find ‘[some-file].xml’ on your class­path; could be a typo, or maybe you for­got to include a class­path entry for source?” errors. If you are sure the file is in place, I real­ized restart­ing the App Engine or Eclipse mostly solves that problem.