gwt java: ant code code reuse eclipse example fix gwt library tutorial web application
leave a comment
Multiple Projects and GWT
When working on several large scale projects (in Eclipse), it’s convenient and of course more efficient to share and reuse code via common libraries. While those are in an early stage they need to be changed a lot and therefore it’s handy to link projects in instead of creating new jars each time the library has been updated.
Unfortunately, this standard approach for Java development in Eclipse does not work that straight forward as with plain old Java projects, it requires three steps in total:
- Link the library project to all relevant projects (“Project Preferences” -> “Java Build Path” -> “Projects” -> “Add…”)
- Then, add the client-side code of the library (by adding it as a module.) Therefore, edit the gwt.xml of your application and add for example
<inherits name="net.svenbuschbeck.gwt.lib.SuperLib "/>where SuperLib is the file name of the gwt.xml file in you library project and before that is the package it lies in. - Add the code to the project by linking a source folder. Unfortunately, this is required if you do not want to write an Ant script to compile your application. (If you prefer Ant check this out) I don’t like the idea of using such a script because if you forget to run it each time you make changes, you will end up in confusion—let’s go for the convenient, automatic way then.
- Add a folder to your application project; open the “advanced” section in the folder creation dialog, select “Link to alternate location” and pick the source folder (usually “src”) of your library project. (Hint: if you work within a team using a versioning system, you can create a variable for that folder and use this instead. This way, each of your colleagues can put the library project in a different folder and accommodate for that by adjusting the variable
) - Right click the folder, “Build Path” -> “Use as Source Folder”. Done.
- Add a folder to your application project; open the “advanced” section in the folder creation dialog, select “Link to alternate location” and pick the source folder (usually “src”) of your library project. (Hint: if you work within a team using a versioning system, you can create a variable for that folder and use this instead. This way, each of your colleagues can put the library project in a different folder and accommodate for that by adjusting the variable
Surprisingly, the GWT plugin for Eclipse does not honor the project linking, thus all the references need to be made explicit or you will end up with lots of the following: ClassNotFoundException.
gwt java javascript: bug code error fix gwt Java Javascript web application
leave a comment
“An invalid or illegal string was specified” Exception in GWT
While working with canvas (drawing stuff on it) in GWT, suddenly and in a seemingly unpredictable manner I got following error message now and then from within the GWT framework code:
com.google.gwt.core.client.JavaScriptException: (NS_ERROR_DOM_SYNTAX_ERR): An invalid or illegal string was specified;
Again, GWT tricked me into thinking I am writing Java code and made me forget about that it is going to be compiled into Javascript. And because of the latter, a division by zero does not throw an DevisionByZeroException, no, it returns NaN even for native data types (there is no differentiation between double and Double in Javascript — there is only the object-version of double, which can be of value Double.NaN).
But also GWT was not prepared to handle Double.NaN. When calling canvas.getContext().drawImage(image,x,y) and one of x and y or both were Double.NaN I got the error message shown above. If you got the same… you know what to do now: check all devisions in your code for potential devisions by zero!!
How to Execute Code When the GWT Application Is Going Down
My goal was to store the UI state of my application just before it gets terminated 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 getting detached from the DOM so that I can finalize my application. Surprisingly, that does not work in Chrome (tested Chrome and Firefox only).
But besides that this sounds like a bug to me, I found the “proper” way of doing things before the application ends:
Window.addWindowClosingHandler(new Window.ClosingHandler() {
@Override public void onWindowClosing(ClosingEvent event) { ... }
});
In the end, I think something like Document.addUnloadHandler would be more suggestive… closing the window or reloading a page is both exiting the application by unloading the DOM — not closing the window.
Disable an Anchor in GWT
Unexpectedly, calling setEnabled(false) does not prevent a link/anchor from being clicked. That means, the click events still get triggered.
The reason is more or less a bug in GWT as it updates the list of events that are going to be triggered 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 former enables a specific event to be triggered and the latter disables it).
I found a workaround by creating my own Anchor class and forcing the underlying 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 widget gets attached to the DOM the first time. It is required because setEnabled() could have been called before the anchor got attached.
If you encountered the same issue, please vote for this bug report.
Flickering Screen with ATI Radeon X1400 and Ubuntu
EDIT: ATI Radeon X1400 seems to work fine again since Ubuntu 11.04.
There is another fix to the “well known” issues with the ATI Radeon X1400 and Ubuntu 10.04 to 10.10 besides the one posted about one year ago (see here).
The previous fix kept the graphics card working normally but harmed the overall operating system’s stability, so here is another fix based on disabling all 3D features of the device to get rid of the flickering screen.
It’s up to you to choose what you are able to live without
a) The desktop settings panel and “Segmentation fault” errors now and then, or
b) 3D acceleration (does not harm video playback or any other basic OS functionalities)
For solution a) see this.
For b) create this file: /etc/modprobe.d/radeon-kms.conf — and add following content
options radeon modeset=0
Solution found here (in German).
Logitech Communicator STX Webcam vs. Ubuntu (64bit) & Skype 2.1
There are many tutorials out there solving the problem by making Skype use Video for Linux version 1 drivers instead of version 2, as that particular webcam does not seem to get along with the newer version. It boils down to the following lines:
Create a file in /usr/local/bin/skype and insert
#!/bin/bash LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so /usr/bin/skype
Finally, make it executable by sudo chmod a+x /usr/local/bin/skype
Use this file to start Skype from now on. Done.
But not for me. Skype refused to eat it: ERROR: ld.so: object '/usr/lib/libv4l/v4l1compat.so' from LD_PRELOAD cannot be preloaded: ignored.
Until I finally found this one here.
Just to cut a long story short, here is why: all the other solutions work for 32bit Linux only — but hey, I do not have any of my old laptop’s potential to be wasted — I am running 64bit Ubuntu (tested with version 9.10 and 10.10 64bit). And with a minor tweak, the fix will work for you, too.
Install the video4linux libraries:
sudo apt-get install lib32v4l-0
And change /usr/local/bin/skype to
LD_PRELOAD=/usr/lib32/libv4l/v4l1compat.so /usr/bin/skype
Its just about the “32” — and one good example more of why you should put meaningful error messages in whatever software!
Thanks Eoin Murphy.
Java Cookies from the Future Past
While working with cookies in Java/GWT and thus—to set the expire date—with Date, I found a doubtful Java behavior.
My goal was to set a cookie to expire in about one month from today like this:
Date expires = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 30);
Cookies.setCookie("myCookie", "myData", expires);
And kept wondering why the cookie never got stored.
And finally created a simple test case like this:
Date today = new Date(); Date tomorrow = new Date(today.getTime() + 1000 * 60 * 60 * 24); Date nextMonth = new Date(today.getTime() + 1000 * 60 * 60 * 24 * 30);
And got following dates:
today=Mon Feb 07 14:27:50 CET 2011 tomorrow=Tue Feb 08 14:27:50 CET 2011 nextMonth=Tue Jan 18 21:25:02 CET 2011
According to Java’s calculation, the cookie was expired already before even being set.
Took me a bit to understand why:
1000 * 60 * 60 * 24 * 30 = 2,592,000,000 = 0x9A7EC800
Thus, the first bit got set to one… a classical overflow causing the integer value to become negative — just try:
System.out.println(1000 * 60 * 60 * 24 * 30);
It will print out -1702967296.
Fix: Add a little L will solve the issue by forcing the compiler to calculate using the scope of long:
Date nextMonthLong = new Date(today.getTime() + 1000L * 60 * 60 * 24 * 30);
I guess I will fall for that one again sometime as the error is not obvious in my opinion — especially because getTime() returns a long and still, the compiler sticks with an int for the multiplication part.
GWT Error Message “Asked for attribute parser of no type”
Example:
public boolean isCollapsed() {
return panel.isVisible();
}
public void setCollapsed() {
panel.setVisible(false);
}
Seen it? Despite the complicated and cryptic error message, the mistake is rather simple: the setter method is missing a parameter, i.e. “public void setCollapsed(boolean collapsed) {“.
Casting collections in Java
Assuming two classes A and B as
class A {}
class B extends A {}
It is just logical that casting B to A works fine:
A anObjectOfClassA = new B();
But when it comes to collections of As and Bs, a strange phenomenon appears in Java:
Collection<B> collectionOfBs = new LinkedList<B>(); Collection<A> collectionOfAs = (Collection<A>)collectionOfBs; // this line does not compile!
That is, though the collection wrapper (the java.util.Collection class) is the same and B extends A, casting a collection of objects of class B to a collection of objects of class A throws an compile-time error.
But it works using generics (the whole class for the sake of completeness and reusability):
import java.util.Collection;
import java.util.LinkedList;
public class CollectionCastingExample {
class A {}
class B extends A {}
Collection<B> collectionOfBs = new LinkedList<B>();
// Collection<A> collectionOfAs = (Collection<A>)collectionOfBs;
Collection<A> collectionOfAs = downCastCollection(collectionOfBs, A.class);
/**
* Casts a collection of objects of class B where B extends A to a collection of objects of class A.
*
* @param <T> Base class
* @param collection Collection of objects of a class extending T
* @param aClass Representation of T
* @return Collection of objects of T casted from given collection of objects of a class extending T.
*/
@SuppressWarnings("unchecked")
public static <T> Collection<T> downCastCollection(Collection<? extends T> collection,
Class<T> aClass) {
return (Collection<T>) collection;
}
}
Despite the fact that a @SuppressWarnings("unchecked") is required, it avoids iterating over the whole collection of Bs and casting each of them from B to A plus adding them to a new collection of As.
Uploading File to Server: Access Denied
Simple task: upload a file to the server. Achieved so far: upload form works and the file gets transferred to the server.
But as soon as the file is written, I get one of that:
“java.security.AccessControlException: access denied (java.io.FilePermission /some/folder/upload__71c20601_12b1b66bc39__7ffa_00000000.tmp write)“
I am amazed how much searching is required to find some information about how to modify the file permissions for the local app engine! And in the end, it turns out that there is an issue with the GAE on Mac but without a way to configure the local Jetty server to allow write access. ![]()
As soon as I deploy the app to a Tomcat on Linux, it works like a charm!
GWT FileUpload: Adding Widgets to a FormPanel
If you build your first GWT form, for example something 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 console keeps telling you during runtime something like this: “java.lang.IllegalStateException: SimplePanel can only contain one child widget”. Instead of writing a long page of explanations and complaints like I did before, it’s simply like that:
“Just put all your widgets in a panel (like HorizontalPanel) and add that panel to the FormPanel.” (Jake − cf. comment below)
Thanks Jake!
GWT Does Not Load Module in Local AppEngine
The issue arose after I renamed the module file (ending with .gwt.xml) to better represent the module functionality. I also updated all relevant files in the project (search for files containing the old name to find them) accordingly.
Starting the application after that modifications ended up in an error (“[ERROR] Unable to find ‘<old module name>.gwt.xml’ on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?”) as the AppEngine tried loading the module by its old name.
Solution: Delete the launch profile for the project (by choosing “Run As…” -> “Run Configurations…” from the context menu).
Obviously the GWT does not check nor update the automatically generated launch profile thus you need to delete it to force the GWT to create a new profile from scratch taking the project changes into account. You might also adjust the profile according to the changes made, but deleting it is the safe and easy way.
First GWT Steps
Just started to work with GWT – a pretty interesting approach for web development compared to PHP or JSF. The whole Application engine is quite impressive especially allowing you to quickly test your applications locally by supporting automatic hot deployment after each code update.
One thing that took me a while was one of that “[ERROR] Unable to find ‘[some-file].xml’ on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?” errors. If you are sure the file is in place, I realized restarting the App Engine or Eclipse mostly solves that problem.
Flickering Screen After Upgrade to Kubuntu 10.04
First of all, I was amazed how smoothly the upgrade went. (K)Ubuntu and I guess other distributions as well have gone quite a far way to become platforms for everybody — with a lot of software but even more tools, helpers and automatic background services — like the nice upgrade service.
There was only one problem I ran into and I want to share the solution.
I am running Kubuntu 10.04 (just upgraded today) on a Lenovo IBM ThinkPad R60 (Yes, one of those with both brands on them
) with an ATI Radeon X1400 graphics device. But since upgraded, my display started flickering — not permanently but unbearable.
The solution that worked for me (at least partially — see below — and this is of cause depending on the graphics device type) was to install the proprietary ATI drivers. I know, it is not a good solution, I do not like to use them neither and if anyone out there is reading this with a better solution in mind, let me know it!! ![]()
Edit 2011-02-14: Added a new post with an additional, alternative solution here.
So what you need to do is to get your favorite package management tool (e.g. Synaptic or KPackageKit) — just hit Alt+F2 and type “package” and pick KPackageKit from the list (it will ask you for the super user password). Then, search for “fglrx”. In KPackageKit, you will find a result titled “Video driver for the ATI graphics accelerator”, and something below the title like “fglrx — 2:8.723.1-0ubuntu3” but the version number should not matter. Click the arrow to the right, hit “Apply” and after rebooting, everything should perfectly without any configuration.
Good luck.
Problems noticed so far: The driver causes “Segmentation Fault” error messages, e.g. when trying to open up the display settings. That’s ugly, I know. But you can read in several forum and blog posts, that the support for the former nicely working driver for the ATI X1400 has been discontinued since 10.04, hence, it’s just good luck that the fglrx works somehow — good to know *after* upgrading, right. But still, the flickering was unbearable for me, thus, I accept the issues for now. I will keep my eyes peeled hoping for a proper solution in the future.
Getting Drupal’s Access Control Module to Work Properly
After setting up some content types — some public, some internal. I installed the Access Control module, set up internal content not to be visible to anonymous users — but without any effect.
After some research, but without success, I realized the *Advanced* section at the bottom of the Access Control tab for each content type. And now the magic trick: Increase the weight and you are done. So I guess the build in access management was fighting the Access Control module, so it is up to you to make your favorite module stronger by giving it more weight. — I doubt this is intuitive. Additionally, it is for sure difficult to simply find the tiny little select box down there in a section, which is by default folded.
How to Make Jquerymenu for Drupal Keep its State on Page Reload
While setting up the website for my new project “Glocal” www.glocal-project.eu), I came across the problem of finding a proper menu module. Something easy to use, stable and efficient in the same time for the complex intranet structure (therefore, sorry, but you will not be able to see my solution there unless you are a project member). Something with a high usability in the end. Activemenu is still quite buggy and DHTML Menue requires a double click to actually open a page — unbearable in a non-doulbe-click environment like the Internet — who is supposed to guess, that this menu requires a double click?? Leaving me with JQuerymenu.
First impression: perfect! Open and close branches by clicking (+) or (-) — view page by clicking menu item label. Even the few styling issues could be fixed easily by using CSS. But as soon, as someone clicks a label, the menu collapses to its default status. It does not remember its last status after loading a different page with the same menu.
Is this it? All module have critical downsides like this? I was quite disappointed!
But I taught JQuerymenu to remember!
As it is quite some code, I will not post it here directly, but added it to the tracker page for this “feature request” or you can download the two updated files (jquerymenu.js and jquerymenu.module) here and replace the once in your /sites/all/modules/jquerymenu folder.
But please be careful, it should be considered an alpha version, there are quite some weaknesses (see tracker page). Any feedback or suggestions are very welcome!
fixing flex VideoDisplay CuePointManager
I was creating a Flex application to show slides and a presentation video of previously recorded presentations. Accordingly, each slide should appear at a certain point of time in the video — calls for cue points!
As all slides and there appearance are stored in a text file in my case, I started adding cue points with ActionScript. But as soon as the video can be controlled with a slider, allowing to shift for– and backwards, the event handler “cuePoint” was not triggered any more. Thus, the slides where not changed correctly as the user jumps ahead, as the cue points between the previous and the new position did not cause and cue point event.
Finally I wrote AdvCuePointManager, inheriting from CuePointManager, but which can deal with jumping back– and forward.
Copy it to “<your source folder>/net/svenbuschbeck/flex/video” and use it as follows:
<mx:VideoDisplay cuePointManagerClass="net.svenbuschbeck.flex.video.AdvCuePointManager" />