de.sciss.app
Class EventManager

java.lang.Object
  extended by de.sciss.app.EventManager
All Implemented Interfaces:
Runnable

public class EventManager
extends Object
implements Runnable

A custom event dispatcher which carefully deals with synchronization issues. Assuming, the synchronization requests specified for some methods are fulfilled, this class is completely thread safe.

It is constructed using a second object, the manager's processor which will be invoked whenever new events are available in the event FIFO queue. the processor is then responsible for querying all registered listeners and calling their appropriate event listening methods.

Event dispatching is deferred to the Swing thread execution time since this makes the whole application much more predictable and easily synchronizable.

Version:
0.18, 08-Apr-08
Author:
Hanns Holger Rutz

Nested Class Summary
static interface EventManager.Processor
          Callers of the EventManager constructor must provide an object implementing this interface
 
Field Summary
static boolean DEBUG_EVENTS
           
protected  EventManager.Processor eventProcessor
           
 
Constructor Summary
protected EventManager()
           
  EventManager(EventManager.Processor eventProcessor)
           
 
Method Summary
 void addListener(Object listener)
          Adds a new listener.
 int countListeners()
          Get the number of listeners
 void debugDump()
           
 void dispatchEvent(BasicEvent e)
          Puts a new event in the queue.
 void dispose()
           
 Object getListener(int index)
          Gets a listener from the list
 void pause()
          Pauses event dispatching.
 void removeListener(Object listener)
          Removes a listener.
 void resume()
          Resumes event dispatching.
 void run()
          Called by add/removeListener and dispatchEvent.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG_EVENTS

public static final boolean DEBUG_EVENTS
See Also:
Constant Field Values

eventProcessor

protected EventManager.Processor eventProcessor
Constructor Detail

EventManager

public EventManager(EventManager.Processor eventProcessor)

EventManager

protected EventManager()
Method Detail

dispose

public void dispose()

addListener

public void addListener(Object listener)
Adds a new listener. The listener will receive all events queued after this method is called. Events already in queue at the moment this method is called are not passed to the listener.

Parameters:
listener - the listener to add

removeListener

public void removeListener(Object listener)
Removes a listener. Similar to the adding process, the listener won't receive any events queued after this method is called. However, when there are events in the queue at the moment when this method is called, they will still be past to the old listener.

Parameters:
listener - the listener to remove. null is allowed (no op).

run

public void run()
Called by add/removeListener and dispatchEvent. This method makes the postponed collection modifications permanent. It calls the eventProcessor as long as there are events in the queue.

Specified by:
run in interface Runnable

getListener

public Object getListener(int index)
Gets a listener from the list

Synchronization:
MUST BE CALLED FROM THE EVENT DISPATCH THREAD

countListeners

public int countListeners()
Get the number of listeners

Synchronization:
MUST BE CALLED FROM THE EVENT DISPATCH THREAD

debugDump

public void debugDump()

dispatchEvent

public void dispatchEvent(BasicEvent e)
Puts a new event in the queue. If the most recent event can be incorporated by the new event, it will be replaced, otherwise the new one is appended to the end. The eventProcessor is invoked asynchronously in the Swing event thread

Parameters:
e - the event to add to the queue. before it's added, the event's incorporate method will be checked against the most recent event in the queue.

pause

public void pause()
Pauses event dispatching. Events will still be queued, but the dispatcher will wait to call any processors until resume() is called.


resume

public void resume()
Resumes event dispatching. Any events in the queue will be distributed as normal.