When using the EventBus, for each event type in your system you will
create a Class (a GwtEvent) and an Interface (the corresponding
EventHandler).

It is a bit of a nuisance maintaining two java files for each event.
So I propose to simplify it by having one abstract event class and
then ONLY ONE class for each event, instead of two. Note that your
actual usage of your new style event class stays the same, so there is
no refactoring required there.


___________________________________________
1.
In your com.company.project.shared package create this file:

import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;

public abstract class AbstractEvent
        <E extends GwtEvent<H>, H extends AbstractEvent.AbstractHandler<E>>
extends GwtEvent<H> {

        public static interface AbstractHandler<E> extends EventHandler {
                void handleEvent(E event);
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void dispatch(H handler) {
                handler.handleEvent((E) this);
        }

}


___________________________________________
2.
This is what an actual event class will look like. I think you'll
agree that this is much simpler than before. Notice we've even got rid
of getter methods for the attributes and replaced them with public
final fields instead; this is because we're never changing the event
data, so it CAN be declared public final, AND that reduces the bulk of
the class too:

import com.company.project.shared.AbstractEvent;

public class CalendarChangeRequestEvent
extends AbstractEvent<CalendarChangeRequestEvent,
CalendarChangeRequestEvent.Handler> {

        public interface Handler extends
AbstractHandler<CalendarChangeRequestEvent>{}
        public static final Type<Handler> TYPE = new Type<Handler>();
        @Override public Type<Handler> getAssociatedType(){return TYPE;}

        public final Date       dateFrom;
        public final Date       dateTill;

        public CalendarChangeRequestEvent(Date dateFrom, Date dateTill) {
                this.dateFrom = dateFrom;
                this.dateTill = dateTill;
        }

}


___________________________________________
3.
Firing the event:

        eventBus.fireEvent(new CalendarChangeRequestEvent(dateFrom,
dateTill));


___________________________________________
4.
Handling the event:

                eventBus.addHandler(CalendarChangeRequestEvent.TYPE, new
CalendarChangeRequestEvent.Handler(){
                        @Override
                        public void handleEvent(CalendarChangeRequestEvent 
event) {

                                //note that we're just referencing the public 
final field instead
of superfluous getter methods
                                GWT.log("From: " + event.dateFrom);
                                GWT.log("Till: " + event.dateTill);

                        }
                });

-- 
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