Advertisement
mitrakov

Simple Event Bus with GWT

Sep 14th, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. public class MyClass {
  2.     public static interface MyHandler extends EventHandler {
  3.         void onEvent(MyEvent event);
  4.     }
  5.     public static class MyEvent extends GwtEvent<MyHandler> {
  6.         private static Type<MyHandler> type = new Type<>();
  7.         @Override
  8.         public Type<MyHandler> getAssociatedType() {
  9.             return type;
  10.         }
  11.         @Override
  12.         protected void dispatch(MyHandler handler) {
  13.             handler.onEvent(this);
  14.         }
  15.     }
  16.  
  17.     protected final EventBus bus = new SimpleEventBus();
  18.     public MyClass withCellSelectHandler(MyHandler handler) {
  19.         bus.addHandler(MyEvent.type, handler);
  20.         return this;
  21.     }
  22.  
  23.     // somewhere in MyClass:
  24.     bus.fireEvent(new MyEvent())
  25. }
  26.  
  27. // usage:
  28. new MyClass().withCellSelectHandler(event -> GWT.log("Hey"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement