java.io.Serializable
public class EventListenerList
extends java.lang.Object
implements java.io.Serializable
This class is an efficient repository for EventListeners based on javax.swing.EventListenerList.
This modification of javax.swing.EventListenerList retains the core functionality of that class but changes the basic API and adds a few more features, as summarized below:
add(EventListener, int)
,
which allows the addition of a listener at a specific position in
the array.
As is the case with javax.swing.EventListenerList, this class provides multi-threaded safety through a copy-on-modify strategy. It is optimized to provide high performance when events are being fired, with slightly slower performance than the Collection API when listeners are being added and removed. Like its predecessor, this class will never return a null array from getListenerList.
The most important thing to keep in mind when using this class is that the array returned by getListenerList is the actual internal array of this class and MUST NOT BE MODIFIED UNDER ANY CIRCUMSTANCES. Below is an example of how to use this class, borrowed (and slightly modified) from the javax.swing.EventListenerList documentation:
Usage example: Say one is defining a class that sends out FooEvents, and one wants to allow users of the class to register FooListeners and receive notification when FooEvents occur. The following should be added to the class definition:
EventListenerList listenerList = new EventListenerList(); FooEvent fooEvent = null; public void addFooListener(FooListener l) { listenerList.add(l); } public void removeFooListener(FooListener l) { listenerList.remove(l); } // Notify all listeners that have registered interest for // notification on this event type. The event instance // is lazily created using the parameters passed into // the fire method. protected void fireFooXXX() { // Guaranteed to return a non-null array EventListener[] listeners = listenerList.getListenerList(); // Process the listeners last to first, notifying // those that are interested in this event for (int i = 0 ; i < listeners.length ; i++) { // Lazily create the event: if (fooEvent == null) fooEvent = new FooEvent(this); ((FooListener)listeners[i]).fooXXX(fooEvent); } }
foo should be changed to the appropriate name, and fireFooXxx to the appropriate method name. One fire method should exist for each notification method in the FooListener interface.
The authors of javax.swing.EventListenerList are Georges Saab, Hans Muller, and James Gosling.
Modifier and Type | Field | Description |
---|---|---|
protected java.util.EventListener[] |
listenerList |
The internal list of listeners that is returned from
getListenerList
|
Constructor | Description |
---|---|
EventListenerList() |
Modifier and Type | Method | Description |
---|---|---|
void |
add(java.util.EventListener newListener) |
Adds the listener to the end of the listener list.
|
void |
add(java.util.EventListener newListener,
int index) |
Adds the listener at the specified index in the listener
list.
|
int |
getListenerCount() |
Returns the total number of listeners in this listener
list.
|
java.util.EventListener[] |
getListenerList() |
Passes back the event listener list as an array of
EventListeners.
|
boolean |
remove(java.util.EventListener listenerToRemove) |
Removes the listener as a listener of the specified type.
|
java.lang.String |
toString() |
Returns a string representation of the EventListenerList.
|
protected transient java.util.EventListener[] listenerList
public java.util.EventListener[] getListenerList()
Passes back the event listener list as an array of EventListeners.
Note that for performance reasons, this implementation passes back the actual data structure in which the listener data is stored internally! This method is guaranteed to pass back a non-null array, so that no null-checking is required in fire methods. A zero-length array of Object will be returned if there are currently no listeners.
WARNING!!! Absolutely NO modification of the data contained in this array should be made -- if any such manipulation is necessary, it should be done on a copy of the array returned rather than the array itself.public int getListenerCount()
Returns the total number of listeners in this listener list.
public void add(java.util.EventListener newListener)
Adds the listener to the end of the listener list.
newListener
- the listener to be addedjava.lang.IllegalArgumentException
- if the specified newListener
is nullpublic void add(java.util.EventListener newListener, int index)
Adds the listener at the specified index in the listener list.
newListener
- the listener to be addedjava.lang.IllegalArgumentException
- if the specified newListener
is null, or the specified index is less than zero or greater than
the length of the listener list array.public boolean remove(java.util.EventListener listenerToRemove)
listenerToRemove
- the listener to be removedjava.lang.IllegalArgumentException
- if the specified listener is
nullpublic java.lang.String toString()
toString
in class java.lang.Object
Intalio Inc. (C) 1999-2008. All rights reserved http://www.intalio.com