Class EventMulticaster
- All Implemented Interfaces:
EventListener
This class will manage an immutable structure consisting of a chain of event listeners and will dispatch events to those listeners. Because the structure is immutable, it is safe to use this API to add/remove listeners during the process of an event dispatch operation.
An example of how this class could be used to implement a new component which fires "action" events:
public MyComponent extends Component
{
EventListener listener;
public void addPropertyValueListener(PropertyListener l)
{
listener=EventMulticaster.add(listener,l);
}
public void removePropertyListener(PropertyListener l)
{
listener=EventMulticaster.remove(listener,l);
}
public void someMethod()
{
// When event occurs which causes "onEvent".
EventListener listener=this.listener;
if ( listener!=null )
listener.onEvent(new SomeEvent(...));
}
}
- Author:
- Christopher Mindus
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final EventListenerThe two event listeners.protected final EventListenerThe two event listeners.Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates an event multicaster instance which chains listener-a with listener-b.Method Summary
Modifier and TypeMethodDescriptionstatic EventListeneradd(EventListener a, EventListener b) Adds listener-a with listener-b and returns the resulting multicast listener.protected static EventListenerReturns the resulting multicast listener from adding listener-a and listener-b together.voidCalled when the event is fired.voidpopulate(ArrayList<EventListener> listeners) Populates the array with listeners.protected EventListenerremove(EventListener old) Removes a listener from this multicaster and returns the resulting multicast listener.static EventListenerremove(EventListener l, EventListener old) Removes the old listener from listener-l and returns the resulting multicast listener.protected static EventListenerremoveInternal(EventListener l, EventListener old) Returns the resulting multicast listener after removing the old listener from listener-l.
Field Details
a
The two event listeners.b
The two event listeners.
Constructor Details
EventMulticaster
Creates an event multicaster instance which chains listener-a with listener-b.- Parameters:
a- listener-a.b- listener-b.
Method Details
remove
Removes a listener from this multicaster and returns the resulting multicast listener.- Parameters:
old- the listener to be removed.
populate
Populates the array with listeners.onEvent
Called when the event is fired.- Specified by:
onEventin interfaceEventListener- Parameters:
event- The event.
add
Adds listener-a with listener-b and returns the resulting multicast listener.No duplicate check is performed, following the
java.awt.AWTEventMulticastercontract this class is modelled on. Adding the same listener twice chains it twice, and the event is then delivered to it twice for every fire. One call toremove(EventListener,EventListener)undoes one registration, not all of them, so a listener added twice keeps receiving events after being removed once. Callers that cannot guarantee a single registration must track that themselves.- Parameters:
a- listener-a.b- listener-b.
remove
Removes the old listener from listener-l and returns the resulting multicast listener.- Parameters:
l- listener-l.old- the listener being removed.
addInternal
Returns the resulting multicast listener from adding listener-a and listener-b together.- If listener-a is null, it returns listener-b.
- If listener-b is null, it returns listener-a.
- If neither are null, then it creates and returns a new EventMulticaster instance which chains a with b.
- Parameters:
a- event-listener-a.b- event-listener-b.
removeInternal
Returns the resulting multicast listener after removing the old listener from listener-l.- If listener-l equals the old listener OR listener-l is null, returns null.
- If listener-l is an instance of EventMulticaster, then it removes the old listener from it.
- Returns listener l.
- Parameters:
l- the listener being removed from.old- the listener being removed.