Home‎ > ‎JSNI‎ > ‎Wrapping‎ > ‎

Event Binding


Here is how I wired events for the event bus in the GWT 2.4+ framework. I'll link to the code once I'm ready to setup a repository.

This is my Maps V3 javascript bindings event architecture

HandlerRegistation class is the heart of the Event binding. I have quite a bit of generification going on, so it may be hard to follow at first. 

public class MapWidget extends Composite implements RequiresResize {

 
//...

 
public HandlerRegistration addClickHandler(ClickMapHandler handler) {
   
return MapHandlerRegistration.addHandler(this, "click", handler, new ClickEventFormatter());
 
}

 
//...  
}
public class MapHandlerRegistration {

 
/**
   * setup handler registration for event bus to use
   * @param w
   * @param eventName
   * @param handler
   * @param formatter
   * @return
   */

 
public static <E extends MapEvent> HandlerRegistration addHandler(MapWidget w, String eventName, MapHandler<E> handler, MapEventFormatter<E> formatter) {
   
final JavaScriptObject listener = addHandlerImpl(w, eventName, handler, formatter);
   
HandlerRegistration registration = new HandlerRegistration() {
     
public void removeHandler() {
        removeHandlerImpl
(listener);
     
}
   
};
   
return registration;
 
}

 
/**
   * add maps jso handler for event name
   * @param w
   * @param eventName
   * @param handler
   * @param formatter
   * @return
   */

 
private static native <E extends MapEvent> JavaScriptObject addHandlerImpl(MapWidget w, String eventName, MapHandler<E> handler, MapEventFormatter<E> formatter) /*-{
    var jso = w.@com.gonevertical.apis.googlemaps.client.MapWidget::getJso()();
    var callback = function(event) {
      @com.gonevertical.apis.googlemaps.client.events.MapHandlerRegistration::onCallback(Lcom/gonevertical/apis/googlemaps/client/events/MapHandler;Lcom/google/gwt/ajaxloader/client/Properties;Lcom/gonevertical/apis/googlemaps/client/events/MapEventFormatter;)(handler, event, formatter);
    };          
    return $wnd.google.maps.event.addListener(jso, eventName, callback)
  }-*/
;
 
 
/**
   * HandlerRegistration call when finished
   * @param listener
   */

 
private static native void removeHandlerImpl(JavaScriptObject listener) /*-{
    $wnd.google.maps.MapsEventListener.addListener(listener);
  }-*/
;

 
/**
   * process generic handler callback
   * @param handler
   * @param properties
   * @param formatter
   */

 
@SuppressWarnings("unused")
 
protected static <E extends MapEvent> void onCallback(final MapHandler<E> handler, final Properties properties, final MapEventFormatter<E> formatter) {
   
try {
      formatEvent
(handler, properties, formatter);
   
} catch (Throwable x) {
      GWT
.getUncaughtExceptionHandler().onUncaughtException(x);
   
}
 
}

 
/**
   * create a map event
   * @param handler
   * @param properties
   * @param formatter
   */

 
private static <E extends MapEvent> void formatEvent(MapHandler<E> handler, Properties properties,  MapEventFormatter<E> formatter) {
    E
event = formatter.createEvent(properties);
    handler
.onEvent(event);
 
}

public abstract class MapEvent<H extends MapHandler<E>, E> extends GwtEvent<H> {

 
protected Properties properties;

 
public MapEvent(Properties properties) {
   
this.properties = properties;
 
}
 
 
public Properties getProperties() {
   
return properties;
 
}
 
 
@Override
 
public abstract Type<H> getAssociatedType();

 
@Override
 
protected abstract void dispatch(H handler);
   
}
public interface MapEventFormatter<E extends MapEvent> {
 
public E createEvent(Properties properties);
}
public interface MapHandler<E> extends EventHandler {
 
public void onEvent(E event);
}
public class ClickEventFormatter implements MapEventFormatter<ClickMapEvent> {

 
@Override
 
public ClickMapEvent createEvent(Properties properties) {
   
return new ClickMapEvent(properties);
 
}

}
public interface ClickMapHandler extends MapHandler<ClickMapEvent> {  
}
public class ClickMapEvent extends MapEvent<ClickMapHandler, ClickMapEvent> {

 
public static Type<ClickMapHandler> TYPE = new Type<ClickMapHandler>();
 
 
public ClickMapEvent(Properties properties) {
   
super(properties);
 
}

 
@Override
 
public com.google.gwt.event.shared.GwtEvent.Type<ClickMapHandler> getAssociatedType() {
   
return TYPE;
 
}

 
@Override
 
protected void dispatch(ClickMapHandler handler) {
    handler
.onEvent(this);
 
}

 
public MouseEvent getMouseEvent() {
   
return new MouseEvent(properties);
 
}
 
}







Comments