Merge from mainline.
[official-gcc.git] / libjava / classpath / java / awt / AWTEventMulticaster.java
blobf7b9163cf67d15f0363dd82e93e256007dacf2a3
1 /* AWTEventMulticaster.java -- allows multicast chaining of listeners
2 Copyright (C) 1999, 2000, 2002 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.AdjustmentEvent;
44 import java.awt.event.AdjustmentListener;
45 import java.awt.event.ComponentEvent;
46 import java.awt.event.ComponentListener;
47 import java.awt.event.ContainerEvent;
48 import java.awt.event.ContainerListener;
49 import java.awt.event.FocusEvent;
50 import java.awt.event.FocusListener;
51 import java.awt.event.HierarchyBoundsListener;
52 import java.awt.event.HierarchyEvent;
53 import java.awt.event.HierarchyListener;
54 import java.awt.event.InputMethodEvent;
55 import java.awt.event.InputMethodListener;
56 import java.awt.event.ItemEvent;
57 import java.awt.event.ItemListener;
58 import java.awt.event.KeyEvent;
59 import java.awt.event.KeyListener;
60 import java.awt.event.MouseEvent;
61 import java.awt.event.MouseListener;
62 import java.awt.event.MouseMotionListener;
63 import java.awt.event.MouseWheelEvent;
64 import java.awt.event.MouseWheelListener;
65 import java.awt.event.TextEvent;
66 import java.awt.event.TextListener;
67 import java.awt.event.WindowEvent;
68 import java.awt.event.WindowFocusListener;
69 import java.awt.event.WindowListener;
70 import java.awt.event.WindowStateListener;
71 import java.io.IOException;
72 import java.io.ObjectOutputStream;
73 import java.io.Serializable;
74 import java.lang.reflect.Array;
75 import java.util.ArrayList;
76 import java.util.EventListener;
78 /**
79 * This class is used to implement a chain of event handlers. Dispatching
80 * using this class is thread safe. Here is a quick example of how to
81 * add and delete listeners using this class. For this example, we will
82 * assume are firing <code>AdjustmentEvent</code>'s. However, this
83 * same approach is useful for all events in the <code>java.awt.event</code>
84 * package, and more if this class is subclassed.
86 * <p><code>
87 * AdjustmentListener al;
88 * public void addAdjustmentListener(AdjustmentListener listener)
89 * {
90 * al = AWTEventMulticaster.add(al, listener);
91 * }
92 * public void removeAdjustmentListener(AdjustmentListener listener)
93 * {
94 * al = AWTEventMulticaster.remove(al, listener);
95 * }
96 * </code>
98 * <p>When it come time to process an event, simply call <code>al</code>,
99 * assuming it is not <code>null</code>, and all listeners in the chain will
100 * be fired.
102 * <p>The first time <code>add</code> is called it is passed
103 * <code>null</code> and <code>listener</code> as its arguments. This
104 * starts building the chain. This class returns <code>listener</code>
105 * which becomes the new <code>al</code>. The next time, <code>add</code>
106 * is called with <code>al</code> and <code>listener</code> and the
107 * new listener is then chained to the old.
109 * @author Bryce McKinlay
110 * @author Aaron M. Renn (arenn@urbanophile.com)
111 * @author Eric Blake (ebb9@email.byu.edu)
112 * @since 1.1
113 * @status updated to 1.4
115 public class AWTEventMulticaster
116 implements ComponentListener, ContainerListener, FocusListener, KeyListener,
117 MouseListener, MouseMotionListener, WindowListener,
118 WindowFocusListener, WindowStateListener, ActionListener,
119 ItemListener, AdjustmentListener, TextListener,
120 InputMethodListener, HierarchyListener, HierarchyBoundsListener,
121 MouseWheelListener
124 * A variable in the event chain.
126 protected final EventListener a;
129 * A variable in the event chain.
131 protected final EventListener b;
134 * Initializes a new instance of <code>AWTEventMulticaster</code> with
135 * the specified event listener parameters. The parameters should not be
136 * null, although it is not required to enforce this with a
137 * NullPointerException.
139 * @param a the "a" listener object
140 * @param b the "b" listener object
142 protected AWTEventMulticaster(EventListener a, EventListener b)
144 this.a = a;
145 this.b = b;
149 * Removes one instance of the specified listener from this multicaster
150 * chain. This descends recursively if either child is a multicaster, and
151 * returns a multicaster chain with the old listener removed.
153 * @param oldl the object to remove from this multicaster
154 * @return the resulting multicaster with the specified listener removed
156 protected EventListener remove(EventListener oldl)
158 // If oldl is an immediate child, return the other child.
159 if (a == oldl)
160 return b;
161 if (b == oldl)
162 return a;
163 // If a and/or b are Multicaster's, search them recursively.
164 if (a instanceof AWTEventMulticaster)
166 EventListener newa = ((AWTEventMulticaster) a).remove(oldl);
167 if (newa != a)
168 return new AWTEventMulticaster(newa, b);
170 if (b instanceof AWTEventMulticaster)
172 EventListener newb = ((AWTEventMulticaster) b).remove(oldl);
173 if (newb != b)
174 return new AWTEventMulticaster(a, newb);
176 // oldl was not found.
177 return this;
181 * Handles this event by dispatching it to the "a" and "b" listener
182 * instances.
184 * @param e the event to handle
186 public void componentResized(ComponentEvent e)
188 ((ComponentListener) a).componentResized(e);
189 ((ComponentListener) b).componentResized(e);
193 * Handles this event by dispatching it to the "a" and "b" listener
194 * instances.
196 * @param e the event to handle
198 public void componentMoved(ComponentEvent e)
200 ((ComponentListener) a).componentMoved(e);
201 ((ComponentListener) b).componentMoved(e);
205 * Handles this event by dispatching it to the "a" and "b" listener
206 * instances.
208 * @param e the event to handle
210 public void componentShown(ComponentEvent e)
212 ((ComponentListener) a).componentShown(e);
213 ((ComponentListener) b).componentShown(e);
217 * Handles this event by dispatching it to the "a" and "b" listener
218 * instances.
220 * @param e the event to handle
222 public void componentHidden(ComponentEvent e)
224 ((ComponentListener) a).componentHidden(e);
225 ((ComponentListener) b).componentHidden(e);
229 * Handles this event by dispatching it to the "a" and "b" listener
230 * instances.
232 * @param e the event to handle
234 public void componentAdded(ContainerEvent e)
236 ((ContainerListener) a).componentAdded(e);
237 ((ContainerListener) b).componentAdded(e);
241 * Handles this event by dispatching it to the "a" and "b" listener
242 * instances.
244 * @param e the event to handle
246 public void componentRemoved(ContainerEvent e)
248 ((ContainerListener) a).componentRemoved(e);
249 ((ContainerListener) b).componentRemoved(e);
253 * Handles this event by dispatching it to the "a" and "b" listener
254 * instances.
256 * @param e the event to handle
258 public void focusGained(FocusEvent e)
260 ((FocusListener) a).focusGained(e);
261 ((FocusListener) b).focusGained(e);
265 * Handles this event by dispatching it to the "a" and "b" listener
266 * instances.
268 * @param e the event to handle
270 public void focusLost(FocusEvent e)
272 ((FocusListener) a).focusLost(e);
273 ((FocusListener) b).focusLost(e);
277 * Handles this event by dispatching it to the "a" and "b" listener
278 * instances.
280 * @param e the event to handle
282 public void keyTyped(KeyEvent e)
284 ((KeyListener) a).keyTyped(e);
285 ((KeyListener) b).keyTyped(e);
289 * Handles this event by dispatching it to the "a" and "b" listener
290 * instances.
292 * @param e the event to handle
294 public void keyPressed(KeyEvent e)
296 ((KeyListener) a).keyPressed(e);
297 ((KeyListener) b).keyPressed(e);
301 * Handles this event by dispatching it to the "a" and "b" listener
302 * instances.
304 * @param e the event to handle
306 public void keyReleased(KeyEvent e)
308 ((KeyListener) a).keyReleased(e);
309 ((KeyListener) b).keyReleased(e);
313 * Handles this event by dispatching it to the "a" and "b" listener
314 * instances.
316 * @param e the event to handle
318 public void mouseClicked(MouseEvent e)
320 ((MouseListener) a).mouseClicked(e);
321 ((MouseListener) b).mouseClicked(e);
325 * Handles this event by dispatching it to the "a" and "b" listener
326 * instances.
328 * @param e the event to handle
330 public void mousePressed(MouseEvent e)
332 ((MouseListener) a).mousePressed(e);
333 ((MouseListener) b).mousePressed(e);
337 * Handles this event by dispatching it to the "a" and "b" listener
338 * instances.
340 * @param e the event to handle
342 public void mouseReleased(MouseEvent e)
344 ((MouseListener) a).mouseReleased(e);
345 ((MouseListener) b).mouseReleased(e);
349 * Handles this event by dispatching it to the "a" and "b" listener
350 * instances.
352 * @param e the event to handle
354 public void mouseEntered(MouseEvent e)
356 ((MouseListener) a).mouseEntered(e);
357 ((MouseListener) b).mouseEntered(e);
361 * Handles this event by dispatching it to the "a" and "b" listener
362 * instances.
364 * @param e the event to handle
366 public void mouseExited(MouseEvent e)
368 ((MouseListener) a).mouseExited(e);
369 ((MouseListener) b).mouseExited(e);
373 * Handles this event by dispatching it to the "a" and "b" listener
374 * instances.
376 * @param e the event to handle
378 public void mouseDragged(MouseEvent e)
380 ((MouseMotionListener) a).mouseDragged(e);
381 ((MouseMotionListener) b).mouseDragged(e);
385 * Handles this event by dispatching it to the "a" and "b" listener
386 * instances.
388 * @param e the event to handle
390 public void mouseMoved(MouseEvent e)
392 ((MouseMotionListener) a).mouseMoved(e);
393 ((MouseMotionListener) b).mouseMoved(e);
397 * Handles this event by dispatching it to the "a" and "b" listener
398 * instances.
400 * @param e the event to handle
402 public void windowOpened(WindowEvent e)
404 ((WindowListener) a).windowOpened(e);
405 ((WindowListener) b).windowOpened(e);
409 * Handles this event by dispatching it to the "a" and "b" listener
410 * instances.
412 * @param e the event to handle
414 public void windowClosing(WindowEvent e)
416 ((WindowListener) a).windowClosing(e);
417 ((WindowListener) b).windowClosing(e);
421 * Handles this event by dispatching it to the "a" and "b" listener
422 * instances.
424 * @param e the event to handle
426 public void windowClosed(WindowEvent e)
428 ((WindowListener) a).windowClosed(e);
429 ((WindowListener) b).windowClosed(e);
433 * Handles this event by dispatching it to the "a" and "b" listener
434 * instances.
436 * @param e the event to handle
438 public void windowIconified(WindowEvent e)
440 ((WindowListener) a).windowIconified(e);
441 ((WindowListener) b).windowIconified(e);
445 * Handles this event by dispatching it to the "a" and "b" listener
446 * instances.
448 * @param e the event to handle
450 public void windowDeiconified(WindowEvent e)
452 ((WindowListener) a).windowDeiconified(e);
453 ((WindowListener) b).windowDeiconified(e);
457 * Handles this event by dispatching it to the "a" and "b" listener
458 * instances.
460 * @param e the event to handle
462 public void windowActivated(WindowEvent e)
464 ((WindowListener) a).windowActivated(e);
465 ((WindowListener) b).windowActivated(e);
469 * Handles this event by dispatching it to the "a" and "b" listener
470 * instances.
472 * @param e the event to handle
474 public void windowDeactivated(WindowEvent e)
476 ((WindowListener) a).windowDeactivated(e);
477 ((WindowListener) b).windowDeactivated(e);
481 * Handles this event by dispatching it to the "a" and "b" listener
482 * instances.
484 * @param e the event to handle
485 * @since 1.4
487 public void windowStateChanged(WindowEvent e)
489 ((WindowStateListener) a).windowStateChanged(e);
490 ((WindowStateListener) b).windowStateChanged(e);
494 * Handles this event by dispatching it to the "a" and "b" listener
495 * instances.
497 * @param e the event to handle
498 * @since 1.4
500 public void windowGainedFocus(WindowEvent e)
502 ((WindowFocusListener) a).windowGainedFocus(e);
503 ((WindowFocusListener) b).windowGainedFocus(e);
507 * Handles this event by dispatching it to the "a" and "b" listener
508 * instances.
510 * @param e the event to handle
511 * @since 1.4
513 public void windowLostFocus(WindowEvent e)
515 ((WindowFocusListener) a).windowLostFocus(e);
516 ((WindowFocusListener) b).windowLostFocus(e);
520 * Handles this event by dispatching it to the "a" and "b" listener
521 * instances.
523 * @param e the event to handle
525 public void actionPerformed(ActionEvent e)
527 ((ActionListener) a).actionPerformed(e);
528 ((ActionListener) b).actionPerformed(e);
532 * Handles this event by dispatching it to the "a" and "b" listener
533 * instances.
535 * @param e the event to handle
537 public void itemStateChanged(ItemEvent e)
539 ((ItemListener) a).itemStateChanged(e);
540 ((ItemListener) b).itemStateChanged(e);
544 * Handles this event by dispatching it to the "a" and "b" listener
545 * instances.
547 * @param e the event to handle
549 public void adjustmentValueChanged(AdjustmentEvent e)
551 ((AdjustmentListener) a).adjustmentValueChanged(e);
552 ((AdjustmentListener) b).adjustmentValueChanged(e);
556 * Handles this event by dispatching it to the "a" and "b" listener
557 * instances.
559 * @param e the event to handle
561 public void textValueChanged(TextEvent e)
563 ((TextListener) a).textValueChanged(e);
564 ((TextListener) b).textValueChanged(e);
568 * Handles this event by dispatching it to the "a" and "b" listener
569 * instances.
571 * @param e the event to handle
572 * @since 1.2
574 public void inputMethodTextChanged(InputMethodEvent e)
576 ((InputMethodListener) a).inputMethodTextChanged(e);
577 ((InputMethodListener) b).inputMethodTextChanged(e);
581 * Handles this event by dispatching it to the "a" and "b" listener
582 * instances.
584 * @param e the event to handle
585 * @since 1.2
587 public void caretPositionChanged(InputMethodEvent e)
589 ((InputMethodListener) a).caretPositionChanged(e);
590 ((InputMethodListener) b).caretPositionChanged(e);
594 * Handles this event by dispatching it to the "a" and "b" listener
595 * instances.
597 * @param e the event to handle
598 * @since 1.3
600 public void hierarchyChanged(HierarchyEvent e)
602 ((HierarchyListener) a).hierarchyChanged(e);
603 ((HierarchyListener) b).hierarchyChanged(e);
607 * Handles this event by dispatching it to the "a" and "b" listener
608 * instances.
610 * @param e the event to handle
611 * @since 1.3
613 public void ancestorMoved(HierarchyEvent e)
615 ((HierarchyBoundsListener) a).ancestorMoved(e);
616 ((HierarchyBoundsListener) b).ancestorMoved(e);
620 * Handles this event by dispatching it to the "a" and "b" listener
621 * instances.
623 * @param e the event to handle
624 * @since 1.3
626 public void ancestorResized(HierarchyEvent e)
628 ((HierarchyBoundsListener) a).ancestorResized(e);
629 ((HierarchyBoundsListener) b).ancestorResized(e);
633 * Handles this event by dispatching it to the "a" and "b" listener
634 * instances.
636 * @param e the event to handle
637 * @since 1.4
639 public void mouseWheelMoved(MouseWheelEvent e)
641 ((MouseWheelListener) a).mouseWheelMoved(e);
642 ((MouseWheelListener) b).mouseWheelMoved(e);
646 * Chain <code>ComponentListener</code> a and b.
648 * @param a the "a" listener, may be null
649 * @param b the "b" listener, may be null
650 * @return latest entry in the chain
652 public static ComponentListener add(ComponentListener a, ComponentListener b)
654 return (ComponentListener) addInternal(a, b);
658 * Chain <code>ContainerListener</code> a and b.
660 * @param a the "a" listener, may be null
661 * @param b the "b" listener, may be null
662 * @return latest entry in the chain
664 public static ContainerListener add(ContainerListener a, ContainerListener b)
666 return (ContainerListener) addInternal(a, b);
670 * Chain <code>FocusListener</code> a and b.
672 * @param a the "a" listener, may be null
673 * @param b the "b" listener, may be null
674 * @return latest entry in the chain
676 public static FocusListener add(FocusListener a, FocusListener b)
678 return (FocusListener) addInternal(a, b);
682 * Chain <code>KeyListener</code> a and b.
684 * @param a the "a" listener, may be null
685 * @param b the "b" listener, may be null
686 * @return latest entry in the chain
688 public static KeyListener add(KeyListener a, KeyListener b)
690 return (KeyListener) addInternal(a, b);
694 * Chain <code>MouseListener</code> a and b.
696 * @param a the "a" listener, may be null
697 * @param b the "b" listener, may be null
698 * @return latest entry in the chain
700 public static MouseListener add(MouseListener a, MouseListener b)
702 return (MouseListener) addInternal(a, b);
706 * Chain <code>MouseMotionListener</code> a and b.
708 * @param a the "a" listener, may be null
709 * @param b the "b" listener, may be null
710 * @return latest entry in the chain
712 public static MouseMotionListener add(MouseMotionListener a,
713 MouseMotionListener b)
715 return (MouseMotionListener) addInternal(a, b);
719 * Chain <code>WindowListener</code> a and b.
721 * @param a the "a" listener, may be null
722 * @param b the "b" listener, may be null
723 * @return latest entry in the chain
725 public static WindowListener add(WindowListener a, WindowListener b)
727 return (WindowListener) addInternal(a, b);
731 * Chain <code>WindowStateListener</code> a and b.
733 * @param a the "a" listener, may be null
734 * @param b the "b" listener, may be null
735 * @return latest entry in the chain
736 * @since 1.4
738 public static WindowStateListener add(WindowStateListener a,
739 WindowStateListener b)
741 return (WindowStateListener) addInternal(a, b);
745 * Chain <code>WindowFocusListener</code> a and b.
747 * @param a the "a" listener, may be null
748 * @param b the "b" listener, may be null
749 * @return latest entry in the chain
750 * @since 1.4
752 public static WindowFocusListener add(WindowFocusListener a,
753 WindowFocusListener b)
755 return (WindowFocusListener) addInternal(a, b);
759 * Chain <code>ActionListener</code> a and b.
761 * @param a the "a" listener, may be null
762 * @param b the "b" listener, may be null
763 * @return latest entry in the chain
765 public static ActionListener add(ActionListener a, ActionListener b)
767 return (ActionListener) addInternal(a, b);
771 * Chain <code>ItemListener</code> a and b.
773 * @param a the "a" listener, may be null
774 * @param b the "b" listener, may be null
775 * @return latest entry in the chain
777 public static ItemListener add(ItemListener a, ItemListener b)
779 return (ItemListener) addInternal(a, b);
783 * Chain <code>AdjustmentListener</code> a and b.
785 * @param a the "a" listener, may be null
786 * @param b the "b" listener, may be null
787 * @return latest entry in the chain
789 public static AdjustmentListener add(AdjustmentListener a,
790 AdjustmentListener b)
792 return (AdjustmentListener) addInternal(a, b);
796 * Chain <code>AdjustmentListener</code> a and b.
798 * @param a the "a" listener, may be null
799 * @param b the "b" listener, may be null
800 * @return latest entry in the chain
802 public static TextListener add(TextListener a, TextListener b)
804 return (TextListener) addInternal(a, b);
808 * Chain <code>InputMethodListener</code> a and b.
810 * @param a the "a" listener, may be null
811 * @param b the "b" listener, may be null
812 * @return latest entry in the chain
813 * @since 1.2
815 public static InputMethodListener add(InputMethodListener a,
816 InputMethodListener b)
818 return (InputMethodListener) addInternal(a, b);
822 * Chain <code>HierarchyListener</code> a and b.
824 * @param a the "a" listener, may be null
825 * @param b the "b" listener, may be null
826 * @return latest entry in the chain
827 * @since 1.3
829 public static HierarchyListener add(HierarchyListener a, HierarchyListener b)
831 return (HierarchyListener) addInternal(a, b);
835 * Chain <code>HierarchyBoundsListener</code> a and b.
837 * @param a the "a" listener, may be null
838 * @param b the "b" listener, may be null
839 * @return latest entry in the chain
840 * @since 1.3
842 public static HierarchyBoundsListener add(HierarchyBoundsListener a,
843 HierarchyBoundsListener b)
845 return (HierarchyBoundsListener) addInternal(a, b);
849 * Chain <code>MouseWheelListener</code> a and b.
851 * @param a the "a" listener, may be null
852 * @param b the "b" listener, may be null
853 * @return latest entry in the chain
854 * @since 1.4
856 public static MouseWheelListener add(MouseWheelListener a,
857 MouseWheelListener b)
859 return (MouseWheelListener) addInternal(a, b);
863 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
865 * @param l the listener chain to reduce
866 * @param oldl the listener to remove
867 * @return the resulting listener chain
869 public static ComponentListener remove(ComponentListener l,
870 ComponentListener oldl)
872 return (ComponentListener) removeInternal(l, oldl);
876 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
878 * @param l the listener chain to reduce
879 * @param oldl the listener to remove
880 * @return the resulting listener chain
882 public static ContainerListener remove(ContainerListener l,
883 ContainerListener oldl)
885 return (ContainerListener) removeInternal(l, oldl);
889 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
891 * @param l the listener chain to reduce
892 * @param oldl the listener to remove
893 * @return the resulting listener chain
895 public static FocusListener remove(FocusListener l, FocusListener oldl)
897 return (FocusListener) removeInternal(l, oldl);
901 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
903 * @param l the listener chain to reduce
904 * @param oldl the listener to remove
905 * @return the resulting listener chain
907 public static KeyListener remove(KeyListener l, KeyListener oldl)
909 return (KeyListener) removeInternal(l, oldl);
913 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
915 * @param l the listener chain to reduce
916 * @param oldl the listener to remove
917 * @return the resulting listener chain
919 public static MouseListener remove(MouseListener l, MouseListener oldl)
921 return (MouseListener) removeInternal(l, oldl);
925 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
927 * @param l the listener chain to reduce
928 * @param oldl the listener to remove
929 * @return the resulting listener chain
931 public static MouseMotionListener remove(MouseMotionListener l,
932 MouseMotionListener oldl)
934 return (MouseMotionListener) removeInternal(l, oldl);
938 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
940 * @param l the listener chain to reduce
941 * @param oldl the listener to remove
942 * @return the resulting listener chain
944 public static WindowListener remove(WindowListener l, WindowListener oldl)
946 return (WindowListener) removeInternal(l, oldl);
950 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
952 * @param l the listener chain to reduce
953 * @param oldl the listener to remove
954 * @return the resulting listener chain
955 * @since 1.4
957 public static WindowStateListener remove(WindowStateListener l,
958 WindowStateListener oldl)
960 return (WindowStateListener) removeInternal(l, oldl);
964 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
966 * @param l the listener chain to reduce
967 * @param oldl the listener to remove
968 * @return the resulting listener chain
969 * @since 1.4
971 public static WindowFocusListener remove(WindowFocusListener l,
972 WindowFocusListener oldl)
974 return (WindowFocusListener) removeInternal(l, oldl);
978 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
980 * @param l the listener chain to reduce
981 * @param oldl the listener to remove
982 * @return the resulting listener chain
984 public static ActionListener remove(ActionListener l, ActionListener oldl)
986 return (ActionListener) removeInternal(l, oldl);
990 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
992 * @param l the listener chain to reduce
993 * @param oldl the listener to remove
994 * @return the resulting listener chain
996 public static ItemListener remove(ItemListener l, ItemListener oldl)
998 return (ItemListener) removeInternal(l, oldl);
1002 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
1004 * @param l the listener chain to reduce
1005 * @param oldl the listener to remove
1006 * @return the resulting listener chain
1008 public static AdjustmentListener remove(AdjustmentListener l,
1009 AdjustmentListener oldl)
1011 return (AdjustmentListener) removeInternal(l, oldl);
1015 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
1017 * @param l the listener chain to reduce
1018 * @param oldl the listener to remove
1019 * @return the resulting listener chain
1021 public static TextListener remove(TextListener l, TextListener oldl)
1023 return (TextListener) removeInternal(l, oldl);
1027 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
1029 * @param l the listener chain to reduce
1030 * @param oldl the listener to remove
1031 * @return the resulting listener chain
1032 * @since 1.2
1034 public static InputMethodListener remove(InputMethodListener l,
1035 InputMethodListener oldl)
1037 return (InputMethodListener) removeInternal(l, oldl);
1041 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
1043 * @param l the listener chain to reduce
1044 * @param oldl the listener to remove
1045 * @return the resulting listener chain
1046 * @since 1.3
1048 public static HierarchyListener remove(HierarchyListener l,
1049 HierarchyListener oldl)
1051 return (HierarchyListener) removeInternal(l, oldl);
1055 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
1057 * @param l the listener chain to reduce
1058 * @param oldl the listener to remove
1059 * @return the resulting listener chain
1060 * @since 1.3
1062 public static HierarchyBoundsListener remove(HierarchyBoundsListener l,
1063 HierarchyBoundsListener oldl)
1065 return (HierarchyBoundsListener) removeInternal(l, oldl);
1069 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
1071 * @param l the listener chain to reduce
1072 * @param oldl the listener to remove
1073 * @return the resulting listener chain
1074 * @since 1.4
1076 public static MouseWheelListener remove(MouseWheelListener l,
1077 MouseWheelListener oldl)
1079 return (MouseWheelListener) removeInternal(l, oldl);
1083 * Chain <code>EventListener</code> a and b.
1085 * @param a the "a" listener, may be null
1086 * @param b the "b" listener, may be null
1087 * @return latest entry in the chain
1089 protected static EventListener addInternal(EventListener a, EventListener b)
1091 if (a == null)
1092 return b;
1093 if (b == null)
1094 return a;
1095 return new AWTEventMulticaster(a, b);
1099 * Removes the listener <code>oldl</code> from the listener <code>l</code>.
1101 * @param l the listener chain to reduce
1102 * @param oldl the listener to remove
1103 * @return the resulting listener chain
1105 protected static EventListener removeInternal(EventListener l,
1106 EventListener oldl)
1108 if (l == oldl)
1109 return null;
1110 if (l instanceof AWTEventMulticaster)
1111 return ((AWTEventMulticaster) l).remove(oldl);
1112 return l;
1116 * Saves all Serializable listeners to a serialization stream.
1118 * @param s the stream to save to
1119 * @param k a prefix stream put before each serializable listener
1120 * @throws IOException if serialization fails
1122 protected void saveInternal(ObjectOutputStream s, String k)
1123 throws IOException
1125 // This is not documented by Sun, but I think it is correct.
1126 if (a instanceof AWTEventMulticaster)
1127 ((AWTEventMulticaster) a).saveInternal(s, k);
1128 else if (a instanceof Serializable)
1130 s.writeObject(k);
1131 s.writeObject(a);
1133 if (b instanceof AWTEventMulticaster)
1134 ((AWTEventMulticaster) b).saveInternal(s, k);
1135 else if (b instanceof Serializable)
1137 s.writeObject(k);
1138 s.writeObject(b);
1143 * Saves a Serializable listener chain to a serialization stream.
1145 * @param s the stream to save to
1146 * @param k a prefix stream put before each serializable listener
1147 * @param l the listener chain to save
1148 * @throws IOException if serialization fails
1150 protected static void save(ObjectOutputStream s, String k, EventListener l)
1151 throws IOException
1153 // This is not documented by Sun, but I think it is correct.
1154 if (l instanceof AWTEventMulticaster)
1155 ((AWTEventMulticaster) l).saveInternal(s, k);
1156 else if (l instanceof Serializable)
1158 s.writeObject(k);
1159 s.writeObject(l);
1164 * Returns an array of all chained listeners of the specified type in the
1165 * given chain. A null listener returns an empty array, and a listener
1166 * which is not an AWTEventMulticaster returns an array of one element. If
1167 * no listeners in the chain are of the specified type, an empty array is
1168 * returned.
1170 * @param l the listener chain to convert to an array
1171 * @param type the type of listeners to collect
1172 * @return an array of the listeners of that type in the chain
1173 * @throws ClassCastException if type is not assignable from EventListener
1174 * @throws NullPointerException if type is null
1175 * @throws IllegalArgumentException if type is Void.TYPE
1176 * @since 1.4
1178 public static EventListener[] getListeners(EventListener l, Class type)
1180 ArrayList list = new ArrayList();
1181 if (l instanceof AWTEventMulticaster)
1182 ((AWTEventMulticaster) l).getListeners(list, type);
1183 else if (type.isInstance(l))
1184 list.add(l);
1185 EventListener[] r = (EventListener[]) Array.newInstance(type, list.size());
1186 list.toArray(r);
1187 return r;
1191 * Collects all instances of the given type in the chain into the list.
1193 * @param l the list to collect into
1194 * @param type the type of listeners to collect
1195 * @throws NullPointerException if type is null
1196 * @see #getListeners(EventListener, Class)
1198 private void getListeners(ArrayList l, Class type)
1200 if (a instanceof AWTEventMulticaster)
1201 ((AWTEventMulticaster) a).getListeners(l, type);
1202 else if (type.isInstance(a))
1203 l.add(a);
1204 if (b instanceof AWTEventMulticaster)
1205 ((AWTEventMulticaster) b).getListeners(l, type);
1206 else if (type.isInstance(b))
1207 l.add(b);
1209 } // class AWTEventMulticaster