On 28 juin, 17:50, Paul Schwarz <paulsschw...@gmail.com> wrote:
> Thanks Phil, though what would this look like in this case?
>
> The design of the GwtEvent and TYPE being static is actually very
> powerful in its elegant simplicity. A call to the constructor of
> GwtEvent.Type() actually causes an int to be incremented and this
> becomes the hashCode "ID" of this event type, and hence assigning that
> to final static variable (TYPE) give a sort of mapping architecture
> where GwtEvent implementations are mapped to int IDs and that's how
> the event bus knows which event has been fired.
>
> So I'm interested to know what you mean by passing in the TYPE from
> elsewhere, because you'd have to be careful not to construct two
> GwtEvent.Type()s to represent a given event type. I'm sure you know
> this, but for the benefit of others who may try to fiddle with the
> mechanism and then wonder why their events aren't caught.

I suppose:
public class GenericEvent extends GwtEvent<GenericEvent.Handler> {
   public interface Handler extends EventHandler {
      void handleEvent(GenericEvent event);
   }

   private final Type<Handler> type;

   public GenericEvent(Type<Handler> type) {
      this.type = type;
   }

   @Override
   public Type<Handler> getAssociatedType() {
      return type;
   }

   @Override
   protected void dispatch(Handler handler) {
      handler.handleEvent(this);
   }
}

...

public static final Type<GenericEvent.Handler> MY_EVENT_TYPE = new
Type<GenericEvent.Handler>();

...

eventBus.addHandler(MY_EVENT_TYPE, new Handler() {
   public void handleEvent(GenericEvent event) {
      ...
   }
});

...

eventBus.fireEvent(new GenericEvent(MY_EVENT_TYPE));


Sure, there's the risk of defining 2 MY_EVENT_TYPE for the same
"logical" event, but not much more than the risk of defining 2
GwtEvent.

-- 
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-tool...@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