如何在handlerManager中捆绑处理程序?

时间:2011-11-29 19:08:26

标签: events gwt handler

是否可以选择将不同的eventhandler捆绑在一个javafile中?

像:

public interface MyHandlerr extends EventHandler {
void myEvent1(Event1 event);
void myEvent2(Event2 event);
}

在我为每个活动提供一个处理程序的那一刻......但我对此并不满意。

格尔茨

1 个答案:

答案 0 :(得分:1)

您可以创建自己的EventHandler界面来处理多个事件

public interface MultipleEventsHandler extends EventHandler {
    void onMyEvent(MyEvent event);
    void onMyOtherEvent(MyOtherEvent event);
}

然后在您的事件类中,您可以定义应该调用哪些方法

public class MyEvent extends GwtEvent<MultipleEventsHandler> {
    public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();

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

    @Override
    protected void dispatch(MultipleEventsHandler handler) {
            handler.onMyEvent(this);
    }
}

public class MyOtherEvent extends GwtEvent<MultipleEventsHandler> {
    public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();

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

    @Override
    protected void dispatch(MultipleEventsHandler handler) {
            handler.onMyOtherEvent(this);
    }
}

如果您只想减少类/接口的数量,那么您可以将EventHandler放入事件类中,例如

public class MyEvent extends GwtEvent<MyEvent.Handler> {

    public interface Handler extends EventHandler {
        void onMyEvent(SomeEvent event);
    }

    public static final Type<MyEvent.Handler> TYPE = new Type<MyEvent.Handler>();


    @Override
    public Type<MyEvent.Handler> getAssociatedType() {
            return TYPE;
    }

    @Override
    protected void dispatch(MyEvent.Handler handler) {
            handler.onMyOtherEvent(this);
    }
}