GWT’s new Event Model – Handlers in GWT 1.6

Theres a lot of concern and worry around the “new event model” in GWT 1.6: throwing out Listeners (a well know and used Java construct) and replacing them with Handlers (a GWT team invention). Let me put your mind at rest, Handlers will make your GWT life much easier.

So what are the differences between Listeners and Handlers?

Listeners Handlers
Have one Listener per event source (ie: “mouse”, “keyboard”, etc) Have one Handler per event type (ie: “mouse press”, “mouse down”, “mouse up”)
Are uncategorized, and the Widget is responsible for sinking / un-sinking the DOM events Are categorized into 2 types: DOM and Logical, adding a DOM Handler to any Widget deferrers to Widget.addDomHandler which sinks events as required
Each Widget uses a ListenerCollection for each class of Listener added All Handlers are stored with a HandlerManager which also manages dispatching any type of event (including new ones you create yourself)
Listeners are removed with the pattern Button .removeClickListener(listener); Adding a Handler returns a HandlerRegistration object, removing a Handler is done by calling HandlerRegistration .removeHandler();
GWT Listeners accept a parameter for each bit of relative event information (ie: sender, mouse x, mouse y) Handlers (more like normal Java Event Listeners) are passed an Event object with all the Event details as their only parameter

So what do Handlers actually buy you in code?

Possibly the most important thing to notice is they take up a lot less space when compiled to GWT. This in mainly due to the fact that their management is centralized in HandlerManager instead of requiring a separate ListenerCollection for each Listener type, and that an implementation only needs a single method overridden (instead of having to listen for events you’re not interested in).

Another point to take note of is that all DOM related Event objects extend from DomEvent. Why is this important? Because DomEvent includes the methods:

  • preventDefault()
  • stopPropagation()
  • getNativeEvent()

I find the last (getNativeEvent()) to be the most useful, it works much the same as Event.getCurrentEvent() and returns a NativeEvent (the new parent class to Event). The advantage of this mechanism over Event.getCurrentEvent() is that you have all the access to the underlying event information (mouse button, modifiers, etc) but attached directly to the dispatched event.

Summary

Handlers are a good step forward for GWT, and make abundant sense in a Web environment where code size is very important. They also make it much easier to add you own events since you no longer need to manage your own Listener registration / deregistration cycle or firing of the events to all of the registered Listeners (HandlerManager does it all for you). I think there will be some interesting new patterns emerging with regards to managing events on generated structures (built based on data fetched from the server for example).

I’m looking forward to seeing what comes of these changes.

About these ads

8 Responses to “GWT’s new Event Model – Handlers in GWT 1.6”

  1. GWT’s new Event Model - Handlers in GWT 1.6 « Lemming Technology Blog « try {} catch () Says:

    […] GWT’s new Event Model – Handlers in GWT 1.6 « Lemming Technology Blog. […]

  2. code » Learning GWT 1.6, part 1: Managing events Says:

    […] classes, and replaces them with the new Handler classes. For a comparison of the two, Jason from Lemming Technology Blog has a good […]

  3. code » Building a GWT 1.6 app, part 1: Managing events Says:

    […] classes, and replaces them with the new Handler classes. For a comparison of the two, Jason from Lemming Technology Blog has a good […]

  4. Using Event Handlers in GWT-1.6 « Lemming Technology Blog Says:

    […] Event Handlers in GWT-1.6 March 12, 2009 — Jason I’ve already blogged about Event Handlers in GWT-1.6. Everyone coding Java knows how an event Listener works. How to attach one to an event source, how […]

  5. links for 2009-11-18 « Amy G. Dala Says:

    […] GWT’s new Event Model – Handlers in GWT 1.6 « Lemming Technology Blog (tags: md gwt reference events) […]

  6. Andre Bonner Says:

    Great article

  7. Moving to GWT 2.1 « Stephen Smith's Blog Says:

    […] big change for us going to GWT 2 was a change in the event model. Up until 1.7, you added a listener to receive any event. Basically you just registered the object […]

  8. mallaudinqazi Says:

    nice explanation … thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: