eu.gressly.util.event
Class EventDispatcher<L>

java.lang.Object
  extended by eu.gressly.util.event.EventDispatcher<L>

public class EventDispatcher<L>
extends java.lang.Object

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 ??

Author:
Philipp Gressly (phi@gressly.ch)

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

EventDispatcher

public EventDispatcher()
Generate a listener List (initialcapacity 2) of ActionListeners

Method Detail

addListener

public void addListener(L listener)
Add a listener to the list. This listener will be notified by calling the "dispatch()" method

Parameters:
listener - to be added. Do not forgett to remove after use!

removeListener

public void removeListener(L listener)
Remove a previously added listener.

Parameters:
listener - The listener to be removed from the list.

clearListenerList

public void clearListenerList()
Remove all listeners from this list.


dispatch

public void dispatch(java.lang.String eventMethod,
                     java.lang.Object... params)
Fire an event (normally the first parameter of 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.