On 1 avr, 11:31, Jason Morris <lem...@gmail.com> wrote:
> On the surface, a Handler is just a Listener, but with only 1 event method. 
> Event Handlers also
> don't have a removeOnClickHandler method, instead the add method returns a 
> registration object that
> can be used to de-register that handler.
>
> Underneath, Handlers are a very different beast. All of the Handlers for a 
> specific Widget are
> registered with the same HandlerManager, thus no more needs for a 
> ClickListenerCollection and a
> MouseListenerCollection and a KeyboardListenerCollection, etc.

...and note that the now-deprecated listeners have been "rewritten" on
top of this new "handlers" system, with some overhead (compensating
the removal of the XXXListenerCollections). Notably, adding a listener
creates a wrapper class and adds it as a handler for each event
handled by the listener, even if you don't handle it; and removing a
listener is quite costly (the wrapper doesn't keep a reference to the
HandlerRegistration objects, as it could potentially be added as a
handler to more than one widget –even if that's not the case when
using the now-deprecated add/removeXXXListener methods–).

In other words: if you switch to 1.6, switch to handlers at the same
time!
(and if you made your own listener interfaces, transform them to event
classes and handler interfaces)


A side effect of this new "layout" is also that it makes it possible/
easier, in composite widgets, to attach a KeyUpHandler to one child
widget and a KeyDownHandler to another child widget and, for
performance (to reduce the number of classes, see [1]), implement them
at the composite (or rather an internal class) level: onKeyUp will
only ever be called by one child widget and onKeyDown by the other.

In GWT 1.5:
class MyWidget extends Composite {
   TextBox a;
   TextBox b;
   KeyboardListener listener = new KeyboardListener() {
      public  void onKeyDown(Widget sender, char keyCode, int
modifiers) {
         if (sender.equals(a)) {
            // a fired a "key down" event
         }
      }

      public  void onKeyPress(Widget sender, char keyCode, int
modifiers) {
         // I don't need "key press" events
      }

      public  void onKeyUp(Widget sender, char keyCode, int modifiers)
{
         if (sender.equals(b)) {
            // b fired a "key up" event
         }
      }
   };

   MyWidget() {
      ...
      a.addKeyboardListener(listener);
      b.addKeyboardListener(listener);
   }
}

In GWT 1.6: (note that the "keypress" event is never ever sinked at
the DOM level, which also helps in improving performances a bit)
public class MyWidget extends Composite {
   TextBox a;
   TextBox b;
   Handlers handlers = new Handlers();

   MyWidget() {
      ...
      a.addKeyDownHandler(handlers);
      b.addKeyUpHandler(handlers);
   }

   private class Handlers implements KeyUpHandler, KeyDownHandler {
      public void onKeyDown(KeyDownEvent event) {
         // no need to check: a fired a "key down" event
      }

      public void onKeyUp(KeyUpEvent event) {
         // no need to check: b fired a "key up" event
      }
   }
}

[1] 
http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&t=FAQ_UIUseOneListener
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to