Home > Gwt, Java > How to Intercept GWT Events…

How to Intercept GWT Events…

In reality it can’t be done with the stock code. In our application we would like to be able to “stop” an event from being fired further.

One example, we have three event handlers that can be added A, B, C (validation, required check, and save rpc). We add the handlers to a button in that order when needed (sub class determine which handlers to add). We want to be able to specify in A or B handler that the event should stop propagating to other handlers.

We tried a couple of different things (including starting to add our own event handling logic) and we settled on two changes to GWT itself. We added a method to GWTEvent:
- public void stop(); /* record that the event should not be passed to more handlers */
- public void isStopped(); /* is the event stopped for further processing */
- public void unStop(); /* reset the stop flag to be false – did in case event is reused */

In our event handlers we can now call event.stop() to stop further propagation.

In HandlerManager.HandlerRegister we modified

private <H extends EventHandler> void fireEvent(GwtEvent<H> event,
        boolean isReverseOrder) {
      Type<H> type = event.getAssociatedType();
      int count = getHandlerCount(type);
      if (isReverseOrder) {
        for (int i = count - 1; i >= 0; i--) {
          H handler = this.<H> getHandler(type, i);
          event.dispatch(handler);
          if(event.isStopped()) { // CUSTOM: event stop processing
            event.unStop(); // did this in case events are reused
            break;
          }
        }
      } else {
        for (int i = 0; i < count; i++) {
          H handler = this.<H> getHandler(type, i);
          event.dispatch(handler);
          if(event.isStopped()) { // CUSTOM: event stop processing
            event.unStop(); // did this in case events are reused
            break;
          }
        }
      }
    }

While this is a very specific use case, hopefully with enough examples, some thought will be put into an extension mechanism for HandlerManager. Until then, we get to live with our solution.

EDIT: After a post on the GWT list, it looks like this type of use case (developer extensions for the event handling) where considered but deemed too much/too varied to include anything in the GWT 2.0 timeframe. Here is the discussion.

 
Categories: Gwt, Java
  1. Chris Hane
    September 4th, 2009 at 17:06 | #2

    @Juan Liska

    Event.cancelBubble does not stop firing subsequent handlers when multiple handlers are registered on the same widget. Event.cancelbubble will prevent parent widgets (e.g., containers) from receiving the event.

    Modifying GWT is the the best method I have found to “break” the event processing cycle. Another way is to throw an exception – yuck!

    I looked at Event.preventDefault also; no joy here either. It is meant to prevent the native widget default event from being fired.

  1. No trackbacks yet.

Spam Protection by WP-SpamFree