public class EventDispatcher extends Task implements EventListener
The EventDispatcher can be configured to dispatch events in the current thread or via one or more background threads.
Listeners can also be added weakly, where a reference to them is kept only as long as they are referenced by other objects other than the EventDispatcher. So when they are garbage collected they will automatically be removed from the EventDispatcher.
Handle.ObjectDiscarder, Handle.WaitOnChange
Modifier and Type | Field and Description |
---|---|
boolean |
dontUseSeparateThread
If this is set true then events are not delivered via a separate
thread, but within the same thread that called dispatch().
|
boolean |
useSeparateThreadForEach
If this is set true then each event is delivered in its own thread.
|
Aborted, Changed, DEFAULT_CHANGE_ACCEPT_TIME, DEFAULT_PROGRESS_RESOLUTION, doing, error, Failed, Failure, OPTION_STOP_IF_NOT_SET, OPTION_TIMEOUT_IF_NOT_SET, progress, progressResolution, returnCode, returnValue, Running, shouldStop, startTime, state, Stopped, stopReason, subHandle, Succeeded, Success, waitChangeAcceptedTime
Constructor and Description |
---|
EventDispatcher() |
EventDispatcher(int maxParallelDeliveries)
Create an EventDispatcher specifying the number of threads used to deliver events.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(EventListener listener)
Add an event listener.
|
void |
addListener(EventListener listener,
boolean addToWeakSet)
Add an event listener.
|
static void |
addListenerFor(Object data,
EventListener listener,
boolean addWeakReference)
Add a listener to any object, using a global Hashtable of event listeners.
|
void |
close()
Close the EventDispatcher.
|
void |
dispatch(Event ev)
Send off the event to the listeners.
|
protected void |
doRun()
Override this to provide functionality for the Task.
|
void |
finalize()
This method (when overriden), will be called when the VM determines that the Object can be
garbage collected.
|
Vector |
getCurrentListeners(Vector destination)
Get all current listeners.
|
static EventDispatcher |
getDispatcher(Object data,
boolean createIt) |
boolean |
isEmpty()
Check if there are any listeners in the Dispatcher.
|
void |
onEvent(Event ev)
This method calls dispatch(ev) directly.
|
void |
removeListener(EventListener listener)
Remove an event listener.
|
static void |
removeListenerFor(Object data,
EventListener listener) |
static void |
sendToListeners(Object data,
Event ev) |
complete, getDaemon, getThread, interrupt, run, setDaemon, setThreadGroup, sleep, start, start
addParent, changeAccepted, changed, check, check, checkAbortFail, checkAny, checkChangeAccepted, checkFailure, convertError, doChangeTo, fail, getChangeState, getErrorText, getFinalReturnValue, getProperties, getReturnValue, getStoppableWaitOnSuccess, getUnwrappedReturnValue, hasStopped, newWaitOnChange, removeParent, resetProgress, set, setFlags, setProgress, setProgress, setResult, setReturnValue, startDoing, stop, succeed, succeeded, throwErrorIfNotStopped, throwRuntimeError, throwRuntimeError, throwRuntimeError, throwRuntimeError, timeout, timeout, waitChangeAccepted, waitForReturnValue, waitOn, waitOn, waitOn, waitOn, waitOnAny, waitOnAny, waitOnAny, waitOnAny, waitOnChange, waitOnResult, waitOnSuccess, waitOnSuccess, waitReturnValue, waitUntilCompletion, waitUntilCompletion, waitUntilStopped, waitUntilStopped, wasAborted, yield
public boolean dontUseSeparateThread
By default a single thread is used to dispatch all events - this thread waits until events comes in and then dispatches them to listeners one by one. Therefore if any listener blocks within the onEvent() method the rest of the events will not be delivered.
public boolean useSeparateThreadForEach
By default a single thread is used to dispatch all events - this thread waits until events comes in and then dispatches them to listeners one by one. Therefore if any listener blocks within the onEvent() method the rest of the events will not be delivered.
public EventDispatcher()
public EventDispatcher(int maxParallelDeliveries)
maxParallelDeliveries
- the maximum number of threads used to deliver evetns simultaneously.public static void addListenerFor(Object data, EventListener listener, boolean addWeakReference)
data
- any hashable Object to associate a listener with.listener
- an EventListener to listen to the data.addWeakReference
- true to add a weak reference to the listener.public static EventDispatcher getDispatcher(Object data, boolean createIt)
public static void removeListenerFor(Object data, EventListener listener)
protected final void doRun()
Task
public void addListener(EventListener listener, boolean addToWeakSet)
listener
- The listener to add.addToWeakSet
- If this is true the listener will be added to an internal
WeakSet. This means that if no other object is referring to that listener it will
eventually be garbage collected and removed from the list.public void addListener(EventListener listener)
listener
- The listener to add.public void removeListener(EventListener listener)
listener
- The listener to remove.public Vector getCurrentListeners(Vector destination)
destination
- A destination Vector, which will be cleared first, to hold the listeners. This can be null in which case a new one will be created.public void dispatch(Event ev)
ev
- the event to send.public boolean isEmpty()
public void close()
public void finalize()
Object
The finalize() method of a Class is only called if it overrides finalize() - the finalize() method java.lang.Object is never called by the Eve VM.
public void onEvent(Event ev)