View Javadoc

1   package org.appfuse.webapp.client.application.base.request;
2   
3   import com.google.gwt.event.shared.EventHandler;
4   import com.google.gwt.event.shared.GwtEvent;
5   import com.google.web.bindery.event.shared.EventBus;
6   import com.google.web.bindery.event.shared.HandlerRegistration;
7   
8   /**
9    * An event posted whenever an RPC request is sent or its response is received.
10   */
11  public class RequestEvent extends GwtEvent<RequestEvent.Handler> {
12  
13      /**
14       * Implemented by handlers of this type of event.
15       */
16      public interface Handler extends EventHandler {
17  
18          /**
19           * Called when a {@link RequestEvent} is fired.
20           *
21           * @param requestEvent
22           *            a {@link RequestEvent} instance
23           */
24          void onRequestEvent(RequestEvent requestEvent);
25      }
26  
27      /**
28       * The request state.
29       */
30      public enum State {
31          SENT, RECEIVED
32      }
33  
34      private static final Type<Handler> TYPE = new Type<Handler>();
35  
36      /**
37       * Register a {@link RequestEvent.Handler} on an {@link EventBus}.
38       *
39       * @param eventBus
40       *            the {@link EventBus}
41       * @param handler
42       *            a {@link RequestEvent.Handler}
43       * @return a {@link HandlerRegistration} instance
44       */
45      public static HandlerRegistration register(EventBus eventBus, RequestEvent.Handler handler) {
46          return eventBus.addHandler(TYPE, handler);
47      }
48  
49      private final State state;
50  
51      /**
52       * Constructs a new @{link RequestEvent}.
53       *
54       * @param state
55       *            a {@link State} instance
56       */
57      public RequestEvent(State state) {
58          this.state = state;
59      }
60  
61      @Override
62      public GwtEvent.Type<Handler> getAssociatedType() {
63          return TYPE;
64      }
65  
66      /**
67       * Returns the {@link State} associated with this event.
68       *
69       * @return a {@link State} instance
70       */
71      public State getState() {
72          return state;
73      }
74  
75      @Override
76      protected void dispatch(Handler handler) {
77          handler.onRequestEvent(this);
78      }
79  }