java.awt

Class AWTEventMulticaster

Implemented Interfaces:
ActionListener, AdjustmentListener, ComponentListener, ContainerListener, EventListener, FocusListener, HierarchyBoundsListener, HierarchyListener, InputMethodListener, ItemListener, KeyListener, MouseListener, MouseMotionListener, MouseWheelListener, TextListener, WindowFocusListener, WindowListener, WindowStateListener

public class AWTEventMulticaster
extends Object
implements ComponentListener, ContainerListener, FocusListener, KeyListener, MouseListener, MouseMotionListener, WindowListener, WindowFocusListener, WindowStateListener, ActionListener, ItemListener, AdjustmentListener, TextListener, InputMethodListener, HierarchyListener, HierarchyBoundsListener, MouseWheelListener

This class is used to implement a chain of event handlers. Dispatching using this class is thread safe. Here is a quick example of how to add and delete listeners using this class. For this example, we will assume are firing AdjustmentEvent's. However, this same approach is useful for all events in the java.awt.event package, and more if this class is subclassed.

AdjustmentListener al; public void addAdjustmentListener(AdjustmentListener listener) { al = AWTEventMulticaster.add(al, listener); } public void removeAdjustmentListener(AdjustmentListener listener) { al = AWTEventMulticaster.remove(al, listener); }

When it come time to process an event, simply call al, assuming it is not null, and all listeners in the chain will be fired.

The first time add is called it is passed null and listener as its arguments. This starts building the chain. This class returns listener which becomes the new al. The next time, add is called with al and listener and the new listener is then chained to the old.

Since:
1.1

Field Summary

protected EventListener
a
A variable in the event chain.
protected EventListener
b
A variable in the event chain.

Constructor Summary

AWTEventMulticaster(EventListener a, EventListener b)
Initializes a new instance of AWTEventMulticaster with the specified event listener parameters.

Method Summary

void
actionPerformed(ActionEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
static ActionListener
add(ActionListener a, ActionListener b)
Chain ActionListener a and b.
static AdjustmentListener
add(AdjustmentListener a, AdjustmentListener b)
Chain AdjustmentListener a and b.
static ComponentListener
add(ComponentListener a, ComponentListener b)
Chain ComponentListener a and b.
static ContainerListener
add(ContainerListener a, ContainerListener b)
Chain ContainerListener a and b.
static FocusListener
add(FocusListener a, FocusListener b)
Chain FocusListener a and b.
static HierarchyBoundsListener
add(HierarchyBoundsListener a, HierarchyBoundsListener b)
Chain HierarchyBoundsListener a and b.
static HierarchyListener
add(HierarchyListener a, HierarchyListener b)
Chain HierarchyListener a and b.
static InputMethodListener
add(InputMethodListener a, InputMethodListener b)
Chain InputMethodListener a and b.
static ItemListener
add(ItemListener a, ItemListener b)
Chain ItemListener a and b.
static KeyListener
add(KeyListener a, KeyListener b)
Chain KeyListener a and b.
static MouseListener
add(MouseListener a, MouseListener b)
Chain MouseListener a and b.
static MouseMotionListener
add(MouseMotionListener a, MouseMotionListener b)
Chain MouseMotionListener a and b.
static MouseWheelListener
add(MouseWheelListener a, MouseWheelListener b)
Chain MouseWheelListener a and b.
static TextListener
add(TextListener a, TextListener b)
Chain AdjustmentListener a and b.
static WindowFocusListener
add(WindowFocusListener a, WindowFocusListener b)
Chain WindowFocusListener a and b.
static WindowListener
add(WindowListener a, WindowListener b)
Chain WindowListener a and b.
static WindowStateListener
add(WindowStateListener a, WindowStateListener b)
Chain WindowStateListener a and b.
protected static EventListener
addInternal(EventListener a, EventListener b)
Chain EventListener a and b.
void
adjustmentValueChanged(AdjustmentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
ancestorMoved(HierarchyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
ancestorResized(HierarchyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
caretPositionChanged(InputMethodEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
componentAdded(ContainerEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
componentHidden(ComponentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
componentMoved(ComponentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
componentRemoved(ContainerEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
componentResized(ComponentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
componentShown(ComponentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
static
extends EventListener> T[] getListeners(EventListener l, Class type)
Returns an array of all chained listeners of the specified type in the given chain.
void
focusGained(FocusEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
focusLost(FocusEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
hierarchyChanged(HierarchyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
inputMethodTextChanged(InputMethodEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
itemStateChanged(ItemEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
keyPressed(KeyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
keyReleased(KeyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
keyTyped(KeyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
mouseClicked(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
mouseDragged(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
mouseEntered(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
mouseExited(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
mouseMoved(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
mousePressed(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
mouseReleased(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
mouseWheelMoved(MouseWheelEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
static ActionListener
remove(ActionListener l, ActionListener oldl)
Removes the listener oldl from the listener l.
static AdjustmentListener
remove(AdjustmentListener l, AdjustmentListener oldl)
Removes the listener oldl from the listener l.
static ComponentListener
remove(ComponentListener l, ComponentListener oldl)
Removes the listener oldl from the listener l.
static ContainerListener
remove(ContainerListener l, ContainerListener oldl)
Removes the listener oldl from the listener l.
static FocusListener
remove(FocusListener l, FocusListener oldl)
Removes the listener oldl from the listener l.
static HierarchyBoundsListener
remove(HierarchyBoundsListener l, HierarchyBoundsListener oldl)
Removes the listener oldl from the listener l.
static HierarchyListener
remove(HierarchyListener l, HierarchyListener oldl)
Removes the listener oldl from the listener l.
static InputMethodListener
remove(InputMethodListener l, InputMethodListener oldl)
Removes the listener oldl from the listener l.
static ItemListener
remove(ItemListener l, ItemListener oldl)
Removes the listener oldl from the listener l.
static KeyListener
remove(KeyListener l, KeyListener oldl)
Removes the listener oldl from the listener l.
static MouseListener
remove(MouseListener l, MouseListener oldl)
Removes the listener oldl from the listener l.
static MouseMotionListener
remove(MouseMotionListener l, MouseMotionListener oldl)
Removes the listener oldl from the listener l.
static MouseWheelListener
remove(MouseWheelListener l, MouseWheelListener oldl)
Removes the listener oldl from the listener l.
static TextListener
remove(TextListener l, TextListener oldl)
Removes the listener oldl from the listener l.
static WindowFocusListener
remove(WindowFocusListener l, WindowFocusListener oldl)
Removes the listener oldl from the listener l.
static WindowListener
remove(WindowListener l, WindowListener oldl)
Removes the listener oldl from the listener l.
static WindowStateListener
remove(WindowStateListener l, WindowStateListener oldl)
Removes the listener oldl from the listener l.
protected EventListener
remove(EventListener oldl)
Removes one instance of the specified listener from this multicaster chain.
protected static EventListener
removeInternal(EventListener l, EventListener oldl)
Removes the listener oldl from the listener l.
protected static void
save(ObjectOutputStream s, String k, EventListener l)
Saves a Serializable listener chain to a serialization stream.
protected void
saveInternal(ObjectOutputStream s, String k)
Saves all Serializable listeners to a serialization stream.
void
textValueChanged(TextEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowActivated(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowClosed(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowClosing(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowDeactivated(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowDeiconified(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowGainedFocus(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowIconified(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowLostFocus(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowOpened(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
void
windowStateChanged(WindowEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

a

protected final EventListener a
A variable in the event chain.

b

protected final EventListener b
A variable in the event chain.

Constructor Details

AWTEventMulticaster

protected AWTEventMulticaster(EventListener a,
                              EventListener b)
Initializes a new instance of AWTEventMulticaster with the specified event listener parameters. The parameters should not be null, although it is not required to enforce this with a NullPointerException.
Parameters:
a - the "a" listener object
b - the "b" listener object

Method Details

actionPerformed

public void actionPerformed(ActionEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
actionPerformed in interface ActionListener
Parameters:
e - the event to handle

add

public static ActionListener add(ActionListener a,
                                 ActionListener b)
Chain ActionListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static AdjustmentListener add(AdjustmentListener a,
                                     AdjustmentListener b)
Chain AdjustmentListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static ComponentListener add(ComponentListener a,
                                    ComponentListener b)
Chain ComponentListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static ContainerListener add(ContainerListener a,
                                    ContainerListener b)
Chain ContainerListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static FocusListener add(FocusListener a,
                                FocusListener b)
Chain FocusListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static HierarchyBoundsListener add(HierarchyBoundsListener a,
                                          HierarchyBoundsListener b)
Chain HierarchyBoundsListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain
Since:
1.3

add

public static HierarchyListener add(HierarchyListener a,
                                    HierarchyListener b)
Chain HierarchyListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain
Since:
1.3

add

public static InputMethodListener add(InputMethodListener a,
                                      InputMethodListener b)
Chain InputMethodListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain
Since:
1.2

add

public static ItemListener add(ItemListener a,
                               ItemListener b)
Chain ItemListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static KeyListener add(KeyListener a,
                              KeyListener b)
Chain KeyListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static MouseListener add(MouseListener a,
                                MouseListener b)
Chain MouseListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static MouseMotionListener add(MouseMotionListener a,
                                      MouseMotionListener b)
Chain MouseMotionListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static MouseWheelListener add(MouseWheelListener a,
                                     MouseWheelListener b)
Chain MouseWheelListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain
Since:
1.4

add

public static TextListener add(TextListener a,
                               TextListener b)
Chain AdjustmentListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static WindowFocusListener add(WindowFocusListener a,
                                      WindowFocusListener b)
Chain WindowFocusListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain
Since:
1.4

add

public static WindowListener add(WindowListener a,
                                 WindowListener b)
Chain WindowListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

add

public static WindowStateListener add(WindowStateListener a,
                                      WindowStateListener b)
Chain WindowStateListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain
Since:
1.4

addInternal

protected static EventListener addInternal(EventListener a,
                                           EventListener b)
Chain EventListener a and b.
Parameters:
a - the "a" listener, may be null
b - the "b" listener, may be null
Returns:
latest entry in the chain

adjustmentValueChanged

public void adjustmentValueChanged(AdjustmentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
adjustmentValueChanged in interface AdjustmentListener
Parameters:
e - the event to handle

ancestorMoved

public void ancestorMoved(HierarchyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
ancestorMoved in interface HierarchyBoundsListener
Parameters:
e - the event to handle
Since:
1.3

ancestorResized

public void ancestorResized(HierarchyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
ancestorResized in interface HierarchyBoundsListener
Parameters:
e - the event to handle
Since:
1.3

caretPositionChanged

public void caretPositionChanged(InputMethodEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
caretPositionChanged in interface InputMethodListener
Parameters:
e - the event to handle
Since:
1.2

componentAdded

public void componentAdded(ContainerEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
componentAdded in interface ContainerListener
Parameters:
e - the event to handle

componentHidden

public void componentHidden(ComponentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
componentHidden in interface ComponentListener
Parameters:
e - the event to handle

componentMoved

public void componentMoved(ComponentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
componentMoved in interface ComponentListener
Parameters:
e - the event to handle

componentRemoved

public void componentRemoved(ContainerEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
componentRemoved in interface ContainerListener
Parameters:
e - the event to handle

componentResized

public void componentResized(ComponentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
componentResized in interface ComponentListener
Parameters:
e - the event to handle

componentShown

public void componentShown(ComponentEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
componentShown in interface ComponentListener
Parameters:
e - the event to handle

extends EventListener> T[] getListeners

public static extends EventListener> T[] getListeners(EventListener l,
                                                         Class type)
Returns an array of all chained listeners of the specified type in the given chain. A null listener returns an empty array, and a listener which is not an AWTEventMulticaster returns an array of one element. If no listeners in the chain are of the specified type, an empty array is returned.
Parameters:
l - the listener chain to convert to an array
type - the type of listeners to collect
Returns:
an array of the listeners of that type in the chain
Throws:
ClassCastException - if type is not assignable from EventListener
NullPointerException - if type is null
IllegalArgumentException - if type is Void.TYPE
Since:
1.4

focusGained

public void focusGained(FocusEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
focusGained in interface FocusListener
Parameters:
e - the event to handle

focusLost

public void focusLost(FocusEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
focusLost in interface FocusListener
Parameters:
e - the event to handle

hierarchyChanged

public void hierarchyChanged(HierarchyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
hierarchyChanged in interface HierarchyListener
Parameters:
e - the event to handle
Since:
1.3

inputMethodTextChanged

public void inputMethodTextChanged(InputMethodEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
inputMethodTextChanged in interface InputMethodListener
Parameters:
e - the event to handle
Since:
1.2

itemStateChanged

public void itemStateChanged(ItemEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
itemStateChanged in interface ItemListener
Parameters:
e - the event to handle

keyPressed

public void keyPressed(KeyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
keyPressed in interface KeyListener
Parameters:
e - the event to handle

keyReleased

public void keyReleased(KeyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
keyReleased in interface KeyListener
Parameters:
e - the event to handle

keyTyped

public void keyTyped(KeyEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
keyTyped in interface KeyListener
Parameters:
e - the event to handle

mouseClicked

public void mouseClicked(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
mouseClicked in interface MouseListener
Parameters:
e - the event to handle

mouseDragged

public void mouseDragged(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
mouseDragged in interface MouseMotionListener
Parameters:
e - the event to handle

mouseEntered

public void mouseEntered(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
mouseEntered in interface MouseListener
Parameters:
e - the event to handle

mouseExited

public void mouseExited(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
mouseExited in interface MouseListener
Parameters:
e - the event to handle

mouseMoved

public void mouseMoved(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
mouseMoved in interface MouseMotionListener
Parameters:
e - the event to handle

mousePressed

public void mousePressed(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
mousePressed in interface MouseListener
Parameters:
e - the event to handle

mouseReleased

public void mouseReleased(MouseEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
mouseReleased in interface MouseListener
Parameters:
e - the event to handle

mouseWheelMoved

public void mouseWheelMoved(MouseWheelEvent e)
Handles this event by dispatching it to the "a" and "b" listener instances.
Specified by:
mouseWheelMoved in interface MouseWheelListener
Parameters:
e - the event to handle
Since:
1.4

remove

public static ActionListener remove(ActionListener l,
                                    ActionListener oldl)
Removes the listener oldl from the listener l.
Parameters:
l - the listener chain to reduce
oldl - the listener to remove
Returns:
the resulting listener chain

remove

public static AdjustmentListener remove(AdjustmentListener l,
                                        AdjustmentListener oldl)
Removes the listener oldl from the listener l.
Parameters:
l - the listener chain to reduce
oldl - the listener to remove
Returns:
the resulting listener chain

remove

public static ComponentListener remove(ComponentListener l,
                                       ComponentListener oldl)
Removes the listener oldl from the listener l.
Parameters:
l - the listener chain to reduce
oldl - the listener to remove
Returns:
the resulting listener chain

remove

public static