|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.ceryle.util.event.EventManager
public class EventManager
A singleton class that manages the addition and removal of ChangeListeners to
a event source, as well as the firing of events to those listeners. An "event
source" is the class delegating its event handling to an implementation of the
EventDelegate interface, in this case, the (inner) delegating class
EventManager.EventDelegateImpl supplied by this manager. The class being serviced
is considered a "client" of the EventDelegate.
This class was originally written for a generic EventObject but there was then no useful listener signature, so ChangeEvents were settled on.
This permits consolidation of all event listeners and their associated methods in one place rather than having them attached to specific subcomponents of an application, and avoids a great deal of event-related cut-and-paste code. Convenience methods that call the EventManager for event delegation can be written to maintain existing APIs.
If you want to provide delegate service outside the bounds of this manager,
you can use EventSourceDelegate.
Basically, rather than have all manner of client classes maintain their own
listener lists, add and remove listener methods, any class wanting to attach
listeners can simply request a delegate object to provide that service. The
delegate handles the listener list, the add and remove listener methods.
Firing events is then a matter of calling the EventManager's
fireEvent(Object,ChangeEvent) method, where the Object is the client.
Prior to instantiating the event object, the client can call
isListening(Object) to see there are any listeners attached to its
delegate.
Adding a ChangeListener to an object is very simple:
EventManager.addChangeListener(object,
new ChangeListener() {
public void stateChanged( ChangeEvent e ) {
YourObject o = (YourObject)e.getSource();
// react to event...
}
});
To fire an event from your class, use something akin to this:
private final void fireEvent()
{
if ( EventManager.isListening(this) ) {
EventManager.fireEvent(this,new ChangeEvent(this));
}
}
Removing a ChangeListener from an object is very simple:
EventManager.removeChangeListener(object,listener);
If you only have a reference to the listener, the following
method will remove it from any clients managed by the
EventManager:
EventManager.removeChangeListener(listener);
This may be used to create listeners for objects that don't yet exist,
particularly designed for embedded applications that need to be able
to listen for the instantiation of an Object, by maintaining a cache
of client-less event sources that set their client upon being popped
from the cache. Each time the getDelegateFor(Object) method
is called with a null parameter it will preload an internal cache with
a client-less EventDelegate that will be popped and returned in
preference to creating a new object. This can have unwanted side effects
if there are multiple clients populating the cache with listeners. The
only check is for a Class match, so be aware if others might be
populating the client-less cache with listeners.
| Nested Class Summary | |
|---|---|
class |
EventManager.EventDelegateImpl
Inner delegating class that manages event listener addition and removal. |
| Method Summary | |
|---|---|
static void |
addChangeListener(Object client,
ChangeListener listener)
Registers a ChangeListener with a EventDelegate for the provided client object. |
static void |
fireEvent(Object client,
ChangeEvent event)
Notify all listeners of the EventDelegate having a registered interest in change events of the supplied Event. |
static Set |
getChangeListeners(Object client)
Return the Set containing the ChangeListeners attached to the delegate for the supplied client. |
EventDelegate |
getDelegateFor(Object client)
Returns a EventDelegate for the provided client Object. |
static EventManager |
getInstance()
As this is a singleton class, this returns the single instance of this class provided with the property file filename and bit-wise application settings. |
static boolean |
isListening(Object client)
Returns true if there are one or more listeners registered with the provided client Object (undelegated event source). |
static boolean |
removeChangeListener(ChangeListener listener)
Un-registers a ChangeListener from any EventDelegate client managed by this EventManager. |
static void |
removeChangeListener(Object client,
ChangeListener listener)
Un-registers a ChangeListener with the EventDelegate for the provided client object. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static EventManager getInstance()
public static final void addChangeListener(Object client,
ChangeListener listener)
client - the client of the event sourcelistener - the event listener
public static final void removeChangeListener(Object client,
ChangeListener listener)
client - the client of the event sourcelistener - the event listenerpublic static final Set getChangeListeners(Object client)
This method returns a Set rather than an Iterator because of the high
likelihood of the Set being modified while an Iterator might be active.
This returns an unmodifiable reference to the actual Set containing
the delegate's listeners. Any attempt to modify the Set will throw an
UnsupportedOperationException. This method is not
synchronized and it should be understood that the composition of the
backing Set may change at any time.
client - the client of the event source
UnsupportedOperationException - if any attempt is made to modify the Setpublic static final boolean removeChangeListener(ChangeListener listener)
listener - the event listener
public static boolean isListening(Object client)
client - the client Object
public static void fireEvent(Object client,
ChangeEvent event)
client - the client initiating the event.event - the Event to fire.public EventDelegate getDelegateFor(Object client)
client - the client Object, or alternately a Class reference
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||