GWT Generic Event Bus

I was looking at GWT Event Bus implementation on MVP Design Pattern.I think it's good but I'm too lazy to write the event and eventhandler classes for each type of event.So I decided to wrote a helper to simplify the implementation.

May be someone is disagree with this solution.

EventBusGenericEventHandler.java
package net.vcari.webipc.client;

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

public interface EventBusGenericEventHandler  extends EventHandler{
  void  onEventTrigger(EventBusGenericEvent event);
}

The event bus event handler.

EventBusGenericEvent.java
package net.vcari.webipc.client;

import java.util.List;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.event.shared.GwtEvent;

public class EventBusGenericEvent extends GwtEvent<EventBusGenericEventHandler>{
  public Type<EventBusGenericEventHandler> eventType;

  public ModelData dataModel;
  public List<ModelData> dataModelList;
  
  @Override
  public Type<EventBusGenericEventHandler> getAssociatedType() {
    return eventType;
  }

  @Override
  protected void dispatch(EventBusGenericEventHandler handler) {
    handler.onEventTrigger(this);    
  }

}

Event bus event. I use GXT modeldata in my application. I put extra variable for single data(ModelData) and List. I use 2 variable to avoid confusion between List and single ModelData (^&%@&YY(&@& - dirty solution. Hahahaha).


EventBusTypes.java
package net.vcari.webipc.client;

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

public class EventBusTypes {
  
  /**
   * Set current location event type. Use to trigger data update for other location like in image viewer
   */
  public static Type<EventBusGenericEventHandler> setCurrentLocationEvent=new Type<EventBusGenericEventHandler>();
  
  /**
   * Set current location process updated event type
   */
  public static Type<EventBusGenericEventHandler> setCurrentLocationUpdatedEvent=new Type<EventBusGenericEventHandler>();
  
  /**
   * Set other location event type. Use to trigger data update for other location like in image viewer
   */
  public static Type<EventBusGenericEventHandler> setOtherLocationEvent=new Type<EventBusGenericEventHandler>();
  
  /**
   * Set other location process updated event type
   */
  public static Type<EventBusGenericEventHandler> setOtherLocationUpdatedEvent=new Type<EventBusGenericEventHandler>();
  
}
The registers of event types.

I  declare the event bus as static. It can be called from other class.
public static EventBus EVENT_BUS = GWT.create(SimpleEventBus.class);


To trigger the event:
EventBusGenericEvent event = new EventBusGenericEvent();
event.eventType=EventBusTypes.setCurrentLocationEvent;
event.dataModel=pagingGrid.getGrid().getSelectionModel().getSelectedItem();
Application.EVENT_BUS.fireEvent(event);
Declare the event, set the type, set the data and fire the event.

To handle the event:

I think it's easier to declare the HandlerRegistration variable in module/class scope as it can be accessed anywhere inside the module/class. This is because it's required to remove the event handler when the container (like form or panel) is closed. If not, the event handler will be handled even the container has ben closed.
HandlerRegistration onSetOtherLocationHandlerRegistration;

Put the handler (may be in onRender function).
onSetOtherLocationHandlerRegistration=Application.EVENT_BUS.addHandler(EventBusTypes.setOtherLocationUpdatedEvent, new EventBusGenericEventHandler() {

      @Override
      public void onEventTrigger(EventBusGenericEvent event) {
        onSetOtherLocationEvent(event);
      }
    });

Finally, remove the handler when the form is closed / unloaded.
@Override
  protected void onUnload() {
    super.onUnload();
    onSetOtherLocationHandlerRegistration.removeHandler();
  }

Voila !!!

0 comments:

 
Copyright © peyotest