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.

Disable Context Menu in GWT

To make use of the right mouse button, it is necessary to disable the native browser context menu (the popup menu appearing 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 possible to make use of the right mouse button for example like this:

someWidget.addDomHandler(new MouseMoveHandler() {

	@Override public void onMouseMove(MouseMoveEvent event) {
		if (event.getNativeButton() == NativeEvent.BUTTON_RIGHT) {
		...

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) {“.

Authentication with FlickrJ

When you know about username and password logins, the whole Flickr authentication process for web applications seems a little weird on first sight, nevertheless it is logical and necessary after you have done some reading (for example the official Flickr WebApp Auth HowTo).
To get started and into coding quickly (using FlickrJ and Java) I recommend this page. Especially the code example is excellent in my opinion! Thanks Andy Sacher!