2011-12-06 15 views
0

フルーツ{アップル、バナナ、チェリー}という列挙があります。私は、次のパターンを持つことができるように、自分のアプリケーションのためのイベント・サブシステムを書きたい:列挙型を使用したGWTイベントシステムのデマルチプレクス/デリゲート

class AppleListener implements HasFruitPriceChangeListener<Apple> { 
    onFruitPriceChange(int newPrice) { 
     // do stuff 
    } 
} 

と、次の形式でタスクを委任することができ、単一のリスナー:

class FruitPriceListener { 
    public void onPriceChange(EnumMap<Fruits, Integer> pricePoints) { 
     // for each fruit, throw the event as 
     // FruitPriceChange.fire(Apple, newPrice); 
    } 
} 

があります上記の方法でそれを行う方法?おそらくValueChangeEventを使用したいと思いますが、もう1つのイベントとハンドラを作成することもできます。私がしたくないことは、AppleFruitPriceChangeEventのような各項目のイベント/クラス定義を持つことです。

答えて

0

だから私が思いついた解決策をした

  1. 列挙型を作成し、GwtEvent.Typeを関連付けますそれらと:

    enum Fruits { 
        Apple, Banana, Cherry; 
    
        public GwtEvent.Type getGwtEventType() { 
        return new GwtEvent.Type(); 
        } 
    } 
    
  2. Crea新しい出来事。

    class FruitPriceChangeEvent extends GwtEvent<?> { 
    
        private final Fruit fruit; 
        FruitPriceChangeEvent(Fruit fruitEnum) { 
        this.fruit = fruitEnum; 
        } 
    
        @Override 
        public GwtEvent.Type<?> getAssociatedType() { 
        return fruit.getGwtEventType(); 
        } 
    
        // ... other stuff... 
    
    } 
    

そして@Stefanが述べたように、全体のイベントハンドラループを通してそれを渡します。このアプローチの美しさ/ハックは、SimpleEventBusがイベントを取得するためのHashMap<GwtEvent.Type, List<HasHandlers>>を保持し、新しいGwtEvent.Typeを作成するたびに、一意のハッシュコードを生成することです(詳細は実装をチェックしてください)。

参考文献:

http://grepcode.com/file/repo1.maven.org/maven2/com.google.gwt/gwt-servlet/2.1.1-rc1/com/google/gwt/event/shared/GwtEvent.java?av=f

http://grepcode.com/file/repo1.maven.org/maven2/com.google.gwt/gwt-servlet/2.1.1-rc1/com/google/gwt/event/shared/SimpleEventBus.java?av=f

1

このようなものについては、EventBusを使用できます.Googleでは(http://www.youtube.com/watch?v=PDuhR18-EdM)の使用方法を提案しています。

あなたglobl Eventbus

のpublic static SimpleEventBusバス=新しいSimpleEventBus();

あなたの変更イベント:

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

import eyeweb.client.gwtMessages.JSPollingEntry; 

public class EventModified extends GwtEvent<EventModifiedHandler> { 

    public final static Type<EventModifiedHandler> TYPE = new Type<EventModifiedHandler>(); 

    private final Fruits fruits; 
    public final JSPollingEntry getPollingMessage(){ 
     return fruits; 
    } 

    public EventModified(Fruits fruits) { 
     this.fruits = fruits; 
    } 

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

    @Override 
    protected void dispatch(EventModifiedHandler handler) { 
     handler.onUpdateRecivde(this); 
    } 
} 

イベントを取得するイベント

package eyeweb.client.eventbus; 

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

public interface EventModifiedHandler extends EventHandler { 

    public void onUpdateRecivde(EventModified handler); 
} 

何かが

EventBus.bus.fireEvent(new EventModified(fruite)); 

を変更するイベントハンドラのハンドラ

01すべてのことsould
EventBus.bus.addHandler(EventModified .TYPE, new EventModifiedHandler() { 
      @Override 
      public void onMessageSend(EventSendData e) { 
       //... do stuff   } 
     }); 

まあ;)

よろしく、 ステファン

+0

こんにちはステファン、私は直後にそれを行うための方法を発見していないので、決して質問自体をチェックするようになりました。私はすぐに私のソリューションを掲載します。 – Arindam

関連する問題