defining custom events without the boilerplate code?

16 views
Skip to first unread message

Elhanan

unread,
Jun 19, 2011, 10:23:16 AM6/19/11
to google-we...@googlegroups.com
hi..
after reading about custom events in gwt i understand that for each custom events you must:
1. define an event handler which events EventHandler
2.  create an event class which extends Event
3. define a static Type<MyHandler> inside the event (return that type on the method)
4. add the handler to the event bus
5. fire the event

BUT
why shouldn't i do the following:
1. create a generic eventHandler interface like so:
public interface GenericEventHandler<P>  extends EventHandler{
 public void onEvent(P p);
}
create an event factory which extends type

public class EventFactory<P> extends Event.Type<GenericEventHandler<P>>
{
    public Event create(P p){
      return new Event(){
      public Type<GenericEventHandler<P>> getType(){
   return MessageFactory.this;
     }
   public dispatch (GenericEventHandler h){
   h.onEvent(p);
  }
}
}
}

from then on all i would need to do this for each new event

final static EventFactory<String> myEvent1 =new EventFactoy<String>();
eventbus.addHandler(myEvent,new GenericEventHandler<Sting>(){
..
}
fire( myEvent1.create("sds"));
Reply all
Reply to author
Forward
0 new messages