vendredi 5 août 2011

EventBus code

If something need to be clarified(and I supposed it should be) don't hesistate to write comments

(Update on 2012 October the 2nd)
MainPanel which implements the interface ActionChangedEventHandler to handle all the business events which are related to new action taken by the user.:
public class MainPanel extends VLayout implements ActionChangedEventHandler {

 ------
   

    public MainPanel(){
       
  //My MainPanel "subscribe" to the business event ACTION_CHANGED
 MainPanel.eventBus.addHandler(ActionChangedEvent.TYPE, this);     

 .........
ActionChanged event handler implementation in MainPanel: Everytime this business event will be triggered, my MainPanel will excute this bit of code.
I have several "subevent categories" defined by the property action of the event and which are defeined in the enum BusEventsAction .
@Override
    public void onActionChanged(ActionChangedEvent event) {
        switch(event.getAction()){
        case CATALOG:
            ContentPanelType1 type1= new ContentPanelType1 ();
            chargePanel(type1);
            break;
        case CATEGORY:
           ContentPanelType2 type2= new ContentPanelType2 ();
            chargePanel(type2);
            break;
        case VARIABLES:
........

            break;

ContentPanel:
public class ContentPanelType1 extends MyAppContentPanel implements Type1EventEventHandler
public ContentPanelType1 (){
MainPanel.eventBus.addHandler(Type1Event.TYPE, this);            initializeComponent()
    }

Business event handler (in this case the interface define one method called processRequest):
@Override
    public void processRequest(Type1Event event) {
        SC.say("Coucou");
       
    }

ActionChanged Event Handler interface:
public interface ActionChangedEventHandler  extends  EventHandler {
    void onActionChanged(ActionChangedEvent event);

}
ActionChanged  Event class:
public class ActionChangedEvent extends  GwtEvent<ActionChangedEventHandler> {
    public static final Type<ActionChangedEventHandler> TYPE = new Type<ActionChangedEventHandler>();
   
    private BusEventsAction action;

    public BusEventsAction getAction() {
        return action;
    }

    public void setAction(BusEventsAction action) {
        this.action = action;
    }

    @Override
    public Type getAssociatedType() {       
        return TYPE;
    }

   
    @Override
    protected void dispatch(ActionChangedEventHandler handler) {
        handler.onActionChanged(this);
       
    }

}

Bus events   Enum
public enum BusEvents { 
  ACTION_CHANGED(0),
  CATEGORY_CHANGED(1),
  VARIABLE_CHANGED(2),
  BASES_CHANGED(3),
  USERS_CHANGED(4),
  NEWS_CHANGED(5),
.....
 final private int  value;
   BusEvents( int value) {        
        this.value = value;
    }
   
    public int getValue(){
        return value;
    }
}
......
Bus events actions  Enum
public enum BusEventsAction { 
  CATALOGUE_VARIABLES(0),
  CATALOGUE_CATEGORIES(1),
  USERS(2),
      
.....
   

Business event Triggering:
Suppose I have a button which need to bring the variables catalog panel as the content I will do the following:
btnCatalog.addClickHandler(new ClickHandler() {
@Override
  public void onClick(ClickEvent event) {
//Instantiate an event
    ActionChangedEvent e = new ActionChangedEvent();
//set the sub event type
    e.setAction(BusEventsAction. CATALOGUE_VARIABLES);
//get the EventBus instance and fire the event    
    MainControl.getEventBus().fireEvent(e);
 }
});

Any panel implementing the ActionCHangeEventHandler will  process this event in its onActionChanged(ActionChangedEvent event) method and decide depending of the type of action if it will react to the event or not.

Aucun commentaire:

Enregistrer un commentaire

It's your turn / Exprimez vous
What do you think, what would you like to be exposed, on which topic you don't agree....
Qu'en pensez vous, quels sujets aimeriez vous voir traités, sur quels point n'êtes vous pas d'accord....