public class DispatchQueue
extends java.lang.Object
Dispatcher d = new Dispatcher() { public void dispatch(EventListener l, Object eventObj) { ((FooListener) l).fooXXX((FooEvent) eventObj); } }; FooEvent event = new FooEvent(this); dispatchQueue.dispatch(d, FooListener.class, event);In the above code substitute a specific listener and event for the Foo listener and event. Since Dispatchers are reusable, it is probably a good idea to create one for each type of event to be delivered and just reuse them everytime to avoid unnecessary memory allocation.
Currently, the DispatchQueue creates an internal thread with which all events are delivered; this means that events are never delivered using the caller's thread.
Constructor and Description |
---|
DispatchQueue()
Constructs a dispatch queue and starts a dispather thread if
necessary.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(java.lang.Class clazz,
java.util.EventListener l)
Adds a listener to the dispatch queue's listener list; the listener
is then able to receive events.
|
void |
dispatch(Dispatcher d,
java.lang.Class clazz,
java.util.EventObject eventObj)
Dispatches an event to a set of event listeners using a specified
dispatcher object.
|
protected void |
dispatch(java.lang.Object[] listeners,
Dispatcher d,
java.lang.Class clazz,
java.util.EventObject eventObj) |
java.util.EventListener |
getListener(java.lang.Class clazz,
java.util.EventListener l)
Returns the listener if it is already in the dispatch queue.
|
java.lang.Object[] |
getListeners()
Returns a pointer to the array of event listeners.
|
void |
removeListener(java.lang.Class clazz,
java.util.EventListener l)
Removes a listener from the dispatch queue's listener list; the listener
is no longer able to receive events.
|
static void |
shutdown()
Terminates the dispatching thread for a graceful shutdown
of the dispatching queue; the caller will block until the
dispatching thread has completed all pending dispatches.
|
public DispatchQueue()
public static void shutdown()
public java.lang.Object[] getListeners()
public java.util.EventListener getListener(java.lang.Class clazz, java.util.EventListener l)
clazz
- the class of the listener to find.l
- the listener instance to find.public void addListener(java.lang.Class clazz, java.util.EventListener l)
clazz
- the class object associated with the event listener type.l
- the instance of the event listener to add.public void removeListener(java.lang.Class clazz, java.util.EventListener l)
clazz
- the class object associated with the event listener type.l
- the instance of the event listener to remove.public void dispatch(Dispatcher d, java.lang.Class clazz, java.util.EventObject eventObj)
d
- the dispatcher used to actually dispatch the event; this
varies according to the type of event listener.clazz
- the class associated with the target event listener type;
only event listeners of this type will receive the event.eventObj
- the actual event object to dispatch.protected void dispatch(java.lang.Object[] listeners, Dispatcher d, java.lang.Class clazz, java.util.EventObject eventObj)