GWT 2.2.0

com.google.gwt.event.shared
Class EventBus

java.lang.Object
  extended by com.google.gwt.event.shared.EventBus
All Implemented Interfaces:
HasHandlers
Direct Known Subclasses:
CountingEventBus, ResettableEventBus, SimpleEventBus

public abstract class EventBus
extends java.lang.Object
implements HasHandlers

Dispatches GwtEvents to interested parties. Eases decoupling by allowing objects to interact without having direct dependencies upon one another, and without requiring event sources to deal with maintaining handler lists. There will typically be one EventBus per application, broadcasting events that may be of general interest.

See Also:
SimpleEventBus, ResettableEventBus, CountingEventBus

Constructor Summary
EventBus()
           
 
Method Summary
abstract
<H extends EventHandler>
HandlerRegistration
addHandler(GwtEvent.Type<H> type, H handler)
          Adds an unfiltered handler to receive events of this type from all sources.
abstract
<H extends EventHandler>
HandlerRegistration
addHandlerToSource(GwtEvent.Type<H> type, java.lang.Object source, H handler)
          Adds a handler to receive events of this type from the given source.
abstract  void fireEvent(GwtEvent<?> event)
          Fires the event from no source.
abstract  void fireEventFromSource(GwtEvent<?> event, java.lang.Object source)
          Fires the given event to the handlers listening to the event's type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EventBus

public EventBus()
Method Detail

addHandler

public abstract <H extends EventHandler> HandlerRegistration addHandler(GwtEvent.Type<H> type,
                                                                        H handler)
Adds an unfiltered handler to receive events of this type from all sources.

It is rare to call this method directly. More typically a GwtEvent subclass will provide a static register method, or a widget will accept handlers directly.

A tip: to make a handler de-register itself, the following works:

new MyHandler() {
  HandlerRegistration reg = MyEvent.register(eventBus, this);
 
  public void onMyThing(MyEvent event) {
    /* do your thing */
    reg.removeHandler();
  }
 };
 

Type Parameters:
H - The type of handler
Parameters:
type - the event type associated with this handler
handler - the handler
Returns:
the handler registration, can be stored in order to remove the handler later

addHandlerToSource

public abstract <H extends EventHandler> HandlerRegistration addHandlerToSource(GwtEvent.Type<H> type,
                                                                                java.lang.Object source,
                                                                                H handler)
Adds a handler to receive events of this type from the given source.

It is rare to call this method directly. More typically a GwtEvent subclass will provide a static register method, or a widget will accept handlers directly.

Type Parameters:
H - The type of handler
Parameters:
type - the event type associated with this handler
source - the source associated with this handler
handler - the handler
Returns:
the handler registration, can be stored in order to remove the handler later

fireEvent

public abstract void fireEvent(GwtEvent<?> event)
Fires the event from no source. Only unfiltered handlers will receive it.

Specified by:
fireEvent in interface HasHandlers
Parameters:
event - the event to fire

fireEventFromSource

public abstract void fireEventFromSource(GwtEvent<?> event,
                                         java.lang.Object source)
Fires the given event to the handlers listening to the event's type.

Any exceptions thrown by handlers will be bundled into a UmbrellaException and then re-thrown after all handlers have completed. An exception thrown by a handler will not prevent other handlers from executing.

Parameters:
event - the event to fire

GWT 2.2.0