L
- the type of event listener that is supported by this proxy.public class EventListenerSupport<L>
extends java.lang.Object
implements java.io.Serializable
An EventListenerSupport object can be used to manage a list of event
listeners of a particular type. The class provides
addListener(Object)
and removeListener(Object)
methods
for registering listeners, as well as a fire()
method for firing
events to the listeners.
To use this class, suppose you want to support ActionEvents. You would do:
public class MyActionEventSource
{
private EventListenerSupport<ActionListener> actionListeners =
EventListenerSupport.create(ActionListener.class);
public void someMethodThatFiresAction()
{
ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "somethingCool");
actionListeners.fire().actionPerformed(e);
}
}
Serializing an EventListenerSupport
instance will result in any
non-Serializable
listeners being silently dropped.
Modifier and Type | Class and Description |
---|---|
protected class |
EventListenerSupport.ProxyInvocationHandler
An invocation handler used to dispatch the event(s) to all the listeners.
|
Modifier and Type | Field and Description |
---|---|
private java.util.List<L> |
listeners
The list used to hold the registered listeners.
|
private L[] |
prototypeArray
Empty typed array for #getListeners().
|
private L |
proxy
The proxy representing the collection of listeners.
|
private static long |
serialVersionUID
Serialization version
|
Modifier | Constructor and Description |
---|---|
private |
EventListenerSupport()
Create a new EventListenerSupport instance.
|
|
EventListenerSupport(java.lang.Class<L> listenerInterface)
Creates an EventListenerSupport object which supports the provided
listener interface.
|
|
EventListenerSupport(java.lang.Class<L> listenerInterface,
java.lang.ClassLoader classLoader)
Creates an EventListenerSupport object which supports the provided
listener interface using the specified class loader to create the JDK
dynamic proxy.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(L listener)
Registers an event listener.
|
void |
addListener(L listener,
boolean allowDuplicate)
Registers an event listener.
|
static <T> EventListenerSupport<T> |
create(java.lang.Class<T> listenerInterface)
Creates an EventListenerSupport object which supports the specified
listener type.
|
protected java.lang.reflect.InvocationHandler |
createInvocationHandler()
Create the
InvocationHandler responsible for broadcasting calls
to the managed listeners. |
private void |
createProxy(java.lang.Class<L> listenerInterface,
java.lang.ClassLoader classLoader)
Create the proxy object.
|
L |
fire()
Returns a proxy object which can be used to call listener methods on all
of the registered event listeners.
|
(package private) int |
getListenerCount()
Returns the number of registered listeners.
|
L[] |
getListeners()
Get an array containing the currently registered listeners.
|
private void |
initializeTransientFields(java.lang.Class<L> listenerInterface,
java.lang.ClassLoader classLoader)
Initialize transient fields.
|
private void |
readObject(java.io.ObjectInputStream objectInputStream)
Deserialize.
|
void |
removeListener(L listener)
Unregisters an event listener.
|
private void |
writeObject(java.io.ObjectOutputStream objectOutputStream)
Serialize.
|
private static final long serialVersionUID
private java.util.List<L> listeners
private transient L proxy
private transient L[] prototypeArray
public EventListenerSupport(java.lang.Class<L> listenerInterface)
listenerInterface
- the type of listener interface that will receive
events posted using this class.java.lang.NullPointerException
- if listenerInterface
is
null
.java.lang.IllegalArgumentException
- if listenerInterface
is
not an interface.public EventListenerSupport(java.lang.Class<L> listenerInterface, java.lang.ClassLoader classLoader)
listenerInterface
- the listener interface.classLoader
- the class loader.java.lang.NullPointerException
- if listenerInterface
or
classLoader
is null
.java.lang.IllegalArgumentException
- if listenerInterface
is
not an interface.private EventListenerSupport()
public static <T> EventListenerSupport<T> create(java.lang.Class<T> listenerInterface)
T
- the type of the listener interfacelistenerInterface
- the type of listener interface that will receive
events posted using this class.java.lang.NullPointerException
- if listenerInterface
is
null
.java.lang.IllegalArgumentException
- if listenerInterface
is
not an interface.public L fire()
public void addListener(L listener)
listener
- the event listener (may not be null
).java.lang.NullPointerException
- if listener
is
null
.public void addListener(L listener, boolean allowDuplicate)
allowDuplicate
is false.listener
- the event listener (may not be null
).allowDuplicate
- the flag for determining if duplicate listener
objects are allowed to be registered.java.lang.NullPointerException
- if listener
is null
.int getListenerCount()
public void removeListener(L listener)
listener
- the event listener (may not be null
).java.lang.NullPointerException
- if listener
is
null
.public L[] getListeners()
EventListenerSupport
instance.private void writeObject(java.io.ObjectOutputStream objectOutputStream) throws java.io.IOException
objectOutputStream
- the output streamjava.io.IOException
- if an IO error occursprivate void readObject(java.io.ObjectInputStream objectInputStream) throws java.io.IOException, java.lang.ClassNotFoundException
objectInputStream
- the input streamjava.io.IOException
- if an IO error occursjava.lang.ClassNotFoundException
- if the class cannot be resolvedprivate void initializeTransientFields(java.lang.Class<L> listenerInterface, java.lang.ClassLoader classLoader)
listenerInterface
- the class of the listener interfaceclassLoader
- the class loader to be usedprivate void createProxy(java.lang.Class<L> listenerInterface, java.lang.ClassLoader classLoader)
listenerInterface
- the class of the listener interfaceclassLoader
- the class loader to be usedprotected java.lang.reflect.InvocationHandler createInvocationHandler()
InvocationHandler
responsible for broadcasting calls
to the managed listeners. Subclasses can override to provide custom behavior.