java.lang.Object eu.gressly.util.event.EventDispatcher<L>
public class EventDispatcher<L>
Add or remove Listenres to this objects. They get notified when calling "dispatch()".
In addition to the PropertyChangeSupport
this class is not restricted to a given event type.
In addition to the EventListenerList
this supports a dispatch()
method, which
notifies all registered handlers.
Example:
// initialize: EventDispatcher<ActionListener> ged = new EventDispatcher<ActionListener>(); // add some listeners ged.addListener(myActionListenerImplementationInstance); ... // and some time later: ActionEvent ae = new ActionEvent(this, 0, "MyEventCommand"); ged.dispatch("actionPerformed", ae); // here myActionListenerImplementationInstance.actionPerformed(ae) will be called
This class could replace EventListenerList
and PropertyChangeSupport
in most of theyr applications ??
Constructor Summary | |
---|---|
EventDispatcher()
Generate a listener List (initialcapacity 2) of ActionListeners |
Method Summary | |
---|---|
void |
addListener(L listener)
Add a listener to the list. |
void |
clearListenerList()
Remove all listeners from this list. |
void |
dispatch(java.lang.String eventMethod,
java.lang.Object... params)
Fire an event (normally the first parameter of params ) to all registered listeners. |
void |
removeListener(L listener)
Remove a previously added listener. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public EventDispatcher()
Method Detail |
---|
public void addListener(L listener)
listener
- to be added. Do not forgett to remove after use!public void removeListener(L listener)
listener
- The listener to be removed from the list.public void clearListenerList()
public void dispatch(java.lang.String eventMethod, java.lang.Object... params)
params
) to all registered listeners.
This behavour is called like "fire", "fireEvents", "notifyListeners" or "notifyAllListeners" in other Observer/State Patterns.
First, a copy of the ListenerList is made, so that a simultaneous adding and removing of the elements does not disturb the
actual process of dispatching.
Then the method eventMethod()
is called on every listener using the paramter list.
See above for an ActionListener
-example.