KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > AWTEventMulticaster


1 /*
2  * @(#)AWTEventMulticaster.java 1.37 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.awt.event.*;
10 import java.lang.reflect.Array JavaDoc;
11 import java.util.EventListener JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.EventListener JavaDoc;
16
17
18 /**
19  * A class which implements efficient and thread-safe multi-cast event
20  * dispatching for the AWT events defined in the java.awt.event package.
21  * This class will manage an immutable structure consisting of a chain of
22  * event listeners and will dispatch events to those listeners. Because
23  * the structure is immutable, it is safe to use this API to add/remove
24  * listeners during the process of an event dispatch operation.
25  * However, event listeners added during the process of an event dispatch
26  * operation will not be notified of the event currently being dispatched.
27  *
28  * An example of how this class could be used to implement a new
29  * component which fires "action" events:
30  *
31  * <pre><code>
32  * public myComponent extends Component {
33  * ActionListener actionListener = null;
34  *
35  * public synchronized void addActionListener(ActionListener l) {
36  * actionListener = AWTEventMulticaster.add(actionListener, l);
37  * }
38  * public synchronized void removeActionListener(ActionListener l) {
39  * actionListener = AWTEventMulticaster.remove(actionListener, l);
40  * }
41  * public void processEvent(AWTEvent e) {
42  * // when event occurs which causes "action" semantic
43  * ActionListener listener = actionListener;
44  * if (listener != null) {
45  * listener.actionPerformed(new ActionEvent());
46  * }
47  * }
48  * }
49  * </code></pre>
50  *
51  * @author John Rose
52  * @author Amy Fowler
53  * @version 1.37, 05/05/04
54  * @since 1.1
55  */

56
57 public class AWTEventMulticaster implements
58     ComponentListener, ContainerListener, FocusListener, KeyListener,
59     MouseListener, MouseMotionListener, WindowListener, WindowFocusListener,
60     WindowStateListener, ActionListener, ItemListener, AdjustmentListener,
61     TextListener, InputMethodListener, HierarchyListener,
62     HierarchyBoundsListener, MouseWheelListener {
63
64     protected final EventListener JavaDoc a, b;
65
66     /**
67      * Creates an event multicaster instance which chains listener-a
68      * with listener-b. Input parameters <code>a</code> and <code>b</code>
69      * should not be <code>null</code>, though implementations may vary in
70      * choosing whether or not to throw <code>NullPointerException</code>
71      * in that case.
72      * @param a listener-a
73      * @param b listener-b
74      */

75     protected AWTEventMulticaster(EventListener JavaDoc a, EventListener JavaDoc b) {
76     this.a = a; this.b = b;
77     }
78
79     /**
80      * Removes a listener from this multicaster and returns the
81      * resulting multicast listener.
82      * @param oldl the listener to be removed
83      */

84     protected EventListener JavaDoc remove(EventListener JavaDoc oldl) {
85     if (oldl == a) return b;
86     if (oldl == b) return a;
87     EventListener JavaDoc a2 = removeInternal(a, oldl);
88     EventListener JavaDoc b2 = removeInternal(b, oldl);
89     if (a2 == a && b2 == b) {
90         return this; // it's not here
91
}
92     return addInternal(a2, b2);
93     }
94
95     /**
96      * Handles the componentResized event by invoking the
97      * componentResized methods on listener-a and listener-b.
98      * @param e the component event
99      */

100     public void componentResized(ComponentEvent e) {
101         ((ComponentListener)a).componentResized(e);
102         ((ComponentListener)b).componentResized(e);
103     }
104
105     /**
106      * Handles the componentMoved event by invoking the
107      * componentMoved methods on listener-a and listener-b.
108      * @param e the component event
109      */

110     public void componentMoved(ComponentEvent e) {
111         ((ComponentListener)a).componentMoved(e);
112         ((ComponentListener)b).componentMoved(e);
113     }
114
115     /**
116      * Handles the componentShown event by invoking the
117      * componentShown methods on listener-a and listener-b.
118      * @param e the component event
119      */

120     public void componentShown(ComponentEvent e) {
121         ((ComponentListener)a).componentShown(e);
122         ((ComponentListener)b).componentShown(e);
123     }
124
125     /**
126      * Handles the componentHidden event by invoking the
127      * componentHidden methods on listener-a and listener-b.
128      * @param e the component event
129      */

130     public void componentHidden(ComponentEvent e) {
131         ((ComponentListener)a).componentHidden(e);
132         ((ComponentListener)b).componentHidden(e);
133     }
134
135     /**
136      * Handles the componentAdded container event by invoking the
137      * componentAdded methods on listener-a and listener-b.
138      * @param e the component event
139      */

140     public void componentAdded(ContainerEvent e) {
141         ((ContainerListener)a).componentAdded(e);
142         ((ContainerListener)b).componentAdded(e);
143     }
144
145     /**
146      * Handles the componentRemoved container event by invoking the
147      * componentRemoved methods on listener-a and listener-b.
148      * @param e the component event
149      */

150     public void componentRemoved(ContainerEvent e) {
151         ((ContainerListener)a).componentRemoved(e);
152         ((ContainerListener)b).componentRemoved(e);
153     }
154
155     /**
156      * Handles the focusGained event by invoking the
157      * focusGained methods on listener-a and listener-b.
158      * @param e the focus event
159      */

160     public void focusGained(FocusEvent e) {
161         ((FocusListener)a).focusGained(e);
162         ((FocusListener)b).focusGained(e);
163     }
164
165     /**
166      * Handles the focusLost event by invoking the
167      * focusLost methods on listener-a and listener-b.
168      * @param e the focus event
169      */

170     public void focusLost(FocusEvent e) {
171         ((FocusListener)a).focusLost(e);
172         ((FocusListener)b).focusLost(e);
173     }
174
175     /**
176      * Handles the keyTyped event by invoking the
177      * keyTyped methods on listener-a and listener-b.
178      * @param e the key event
179      */

180     public void keyTyped(KeyEvent e) {
181         ((KeyListener)a).keyTyped(e);
182         ((KeyListener)b).keyTyped(e);
183     }
184
185     /**
186      * Handles the keyPressed event by invoking the
187      * keyPressed methods on listener-a and listener-b.
188      * @param e the key event
189      */

190     public void keyPressed(KeyEvent e) {
191         ((KeyListener)a).keyPressed(e);
192         ((KeyListener)b).keyPressed(e);
193     }
194
195     /**
196      * Handles the keyReleased event by invoking the
197      * keyReleased methods on listener-a and listener-b.
198      * @param e the key event
199      */

200     public void keyReleased(KeyEvent e) {
201         ((KeyListener)a).keyReleased(e);
202         ((KeyListener)b).keyReleased(e);
203     }
204
205     /**
206      * Handles the mouseClicked event by invoking the
207      * mouseClicked methods on listener-a and listener-b.
208      * @param e the mouse event
209      */

210     public void mouseClicked(MouseEvent e) {
211         ((MouseListener)a).mouseClicked(e);
212         ((MouseListener)b).mouseClicked(e);
213     }
214
215     /**
216      * Handles the mousePressed event by invoking the
217      * mousePressed methods on listener-a and listener-b.
218      * @param e the mouse event
219      */

220     public void mousePressed(MouseEvent e) {
221         ((MouseListener)a).mousePressed(e);
222         ((MouseListener)b).mousePressed(e);
223     }
224
225     /**
226      * Handles the mouseReleased event by invoking the
227      * mouseReleased methods on listener-a and listener-b.
228      * @param e the mouse event
229      */

230     public void mouseReleased(MouseEvent e) {
231         ((MouseListener)a).mouseReleased(e);
232         ((MouseListener)b).mouseReleased(e);
233     }
234
235     /**
236      * Handles the mouseEntered event by invoking the
237      * mouseEntered methods on listener-a and listener-b.
238      * @param e the mouse event
239      */

240     public void mouseEntered(MouseEvent e) {
241         ((MouseListener)a).mouseEntered(e);
242         ((MouseListener)b).mouseEntered(e);
243     }
244
245     /**
246      * Handles the mouseExited event by invoking the
247      * mouseExited methods on listener-a and listener-b.
248      * @param e the mouse event
249      */

250     public void mouseExited(MouseEvent e) {
251         ((MouseListener)a).mouseExited(e);
252         ((MouseListener)b).mouseExited(e);
253     }
254
255     /**
256      * Handles the mouseDragged event by invoking the
257      * mouseDragged methods on listener-a and listener-b.
258      * @param e the mouse event
259      */

260     public void mouseDragged(MouseEvent e) {
261         ((MouseMotionListener)a).mouseDragged(e);
262         ((MouseMotionListener)b).mouseDragged(e);
263     }
264
265     /**
266      * Handles the mouseMoved event by invoking the
267      * mouseMoved methods on listener-a and listener-b.
268      * @param e the mouse event
269      */

270     public void mouseMoved(MouseEvent e) {
271         ((MouseMotionListener)a).mouseMoved(e);
272         ((MouseMotionListener)b).mouseMoved(e);
273     }
274
275     /**
276      * Handles the windowOpened event by invoking the
277      * windowOpened methods on listener-a and listener-b.
278      * @param e the window event
279      */

280     public void windowOpened(WindowEvent e) {
281         ((WindowListener)a).windowOpened(e);
282         ((WindowListener)b).windowOpened(e);
283     }
284
285     /**
286      * Handles the windowClosing event by invoking the
287      * windowClosing methods on listener-a and listener-b.
288      * @param e the window event
289      */

290     public void windowClosing(WindowEvent e) {
291         ((WindowListener)a).windowClosing(e);
292         ((WindowListener)b).windowClosing(e);
293     }
294
295     /**
296      * Handles the windowClosed event by invoking the
297      * windowClosed methods on listener-a and listener-b.
298      * @param e the window event
299      */

300     public void windowClosed(WindowEvent e) {
301         ((WindowListener)a).windowClosed(e);
302         ((WindowListener)b).windowClosed(e);
303     }
304
305     /**
306      * Handles the windowIconified event by invoking the
307      * windowIconified methods on listener-a and listener-b.
308      * @param e the window event
309      */

310     public void windowIconified(WindowEvent e) {
311         ((WindowListener)a).windowIconified(e);
312         ((WindowListener)b).windowIconified(e);
313     }
314
315     /**
316      * Handles the windowDeiconfied event by invoking the
317      * windowDeiconified methods on listener-a and listener-b.
318      * @param e the window event
319      */

320     public void windowDeiconified(WindowEvent e) {
321         ((WindowListener)a).windowDeiconified(e);
322         ((WindowListener)b).windowDeiconified(e);
323     }
324
325     /**
326      * Handles the windowActivated event by invoking the
327      * windowActivated methods on listener-a and listener-b.
328      * @param e the window event
329      */

330     public void windowActivated(WindowEvent e) {
331         ((WindowListener)a).windowActivated(e);
332         ((WindowListener)b).windowActivated(e);
333     }
334
335     /**
336      * Handles the windowDeactivated event by invoking the
337      * windowDeactivated methods on listener-a and listener-b.
338      * @param e the window event
339      */

340     public void windowDeactivated(WindowEvent e) {
341         ((WindowListener)a).windowDeactivated(e);
342         ((WindowListener)b).windowDeactivated(e);
343     }
344
345     /**
346      * Handles the windowStateChanged event by invoking the
347      * windowStateChanged methods on listener-a and listener-b.
348      * @param e the window event
349      */

350     public void windowStateChanged(WindowEvent e) {
351         ((WindowStateListener)a).windowStateChanged(e);
352         ((WindowStateListener)b).windowStateChanged(e);
353     }
354
355
356     /**
357      * Handles the windowGainedFocus event by invoking the windowGainedFocus
358      * methods on listener-a and listener-b.
359      * @param e the window event
360      */

361     public void windowGainedFocus(WindowEvent e) {
362         ((WindowFocusListener)a).windowGainedFocus(e);
363         ((WindowFocusListener)b).windowGainedFocus(e);
364     }
365
366     /**
367      * Handles the windowLostFocus event by invoking the windowLostFocus
368      * methods on listener-a and listener-b.
369      * @param e the window event
370      */

371     public void windowLostFocus(WindowEvent e) {
372         ((WindowFocusListener)a).windowLostFocus(e);
373         ((WindowFocusListener)b).windowLostFocus(e);
374     }
375
376     /**
377      * Handles the actionPerformed event by invoking the
378      * actionPerformed methods on listener-a and listener-b.
379      * @param e the action event
380      */

381     public void actionPerformed(ActionEvent e) {
382         ((ActionListener)a).actionPerformed(e);
383         ((ActionListener)b).actionPerformed(e);
384     }
385
386     /**
387      * Handles the itemStateChanged event by invoking the
388      * itemStateChanged methods on listener-a and listener-b.
389      * @param e the item event
390      */

391     public void itemStateChanged(ItemEvent e) {
392         ((ItemListener)a).itemStateChanged(e);
393         ((ItemListener)b).itemStateChanged(e);
394     }
395
396     /**
397      * Handles the adjustmentValueChanged event by invoking the
398      * adjustmentValueChanged methods on listener-a and listener-b.
399      * @param e the adjustment event
400      */

401     public void adjustmentValueChanged(AdjustmentEvent e) {
402         ((AdjustmentListener)a).adjustmentValueChanged(e);
403         ((AdjustmentListener)b).adjustmentValueChanged(e);
404     }
405     public void textValueChanged(TextEvent e) {
406         ((TextListener)a).textValueChanged(e);
407         ((TextListener)b).textValueChanged(e);
408     }
409
410     /**
411      * Handles the inputMethodTextChanged event by invoking the
412      * inputMethodTextChanged methods on listener-a and listener-b.
413      * @param e the item event
414      */

415     public void inputMethodTextChanged(InputMethodEvent e) {
416        ((InputMethodListener)a).inputMethodTextChanged(e);
417        ((InputMethodListener)b).inputMethodTextChanged(e);
418     }
419
420     /**
421      * Handles the caretPositionChanged event by invoking the
422      * caretPositionChanged methods on listener-a and listener-b.
423      * @param e the item event
424      */

425     public void caretPositionChanged(InputMethodEvent e) {
426        ((InputMethodListener)a).caretPositionChanged(e);
427        ((InputMethodListener)b).caretPositionChanged(e);
428     }
429
430     /**
431      * Handles the hierarchyChanged event by invoking the
432      * hierarchyChanged methods on listener-a and listener-b.
433      * @param e the item event
434      */

435     public void hierarchyChanged(HierarchyEvent e) {
436         ((HierarchyListener)a).hierarchyChanged(e);
437         ((HierarchyListener)b).hierarchyChanged(e);
438     }
439
440     /**
441      * Handles the ancestorMoved event by invoking the
442      * ancestorMoved methods on listener-a and listener-b.
443      * @param e the item event
444      */

445     public void ancestorMoved(HierarchyEvent e) {
446         ((HierarchyBoundsListener)a).ancestorMoved(e);
447         ((HierarchyBoundsListener)b).ancestorMoved(e);
448     }
449
450     /**
451      * Handles the ancestorResized event by invoking the
452      * ancestorResized methods on listener-a and listener-b.
453      * @param e the item event
454      */

455     public void ancestorResized(HierarchyEvent e) {
456         ((HierarchyBoundsListener)a).ancestorResized(e);
457         ((HierarchyBoundsListener)b).ancestorResized(e);
458     }
459
460     /**
461      * Handles the mouseWheelMoved event by invoking the
462      * mouseWheelMoved methods on listener-a and listener-b.
463      * @param e the mouse event
464      * @since 1.4
465      */

466     public void mouseWheelMoved(MouseWheelEvent e) {
467         ((MouseWheelListener)a).mouseWheelMoved(e);
468         ((MouseWheelListener)b).mouseWheelMoved(e);
469     }
470
471     /**
472      * Adds component-listener-a with component-listener-b and
473      * returns the resulting multicast listener.
474      * @param a component-listener-a
475      * @param b component-listener-b
476      */

477     public static ComponentListener add(ComponentListener a, ComponentListener b) {
478         return (ComponentListener)addInternal(a, b);
479     }
480
481     /**
482      * Adds container-listener-a with container-listener-b and
483      * returns the resulting multicast listener.
484      * @param a container-listener-a
485      * @param b container-listener-b
486      */

487     public static ContainerListener add(ContainerListener a, ContainerListener b) {
488         return (ContainerListener)addInternal(a, b);
489     }
490
491     /**
492      * Adds focus-listener-a with focus-listener-b and
493      * returns the resulting multicast listener.
494      * @param a focus-listener-a
495      * @param b focus-listener-b
496      */

497     public static FocusListener add(FocusListener a, FocusListener b) {
498         return (FocusListener)addInternal(a, b);
499     }
500
501     /**
502      * Adds key-listener-a with key-listener-b and
503      * returns the resulting multicast listener.
504      * @param a key-listener-a
505      * @param b key-listener-b
506      */

507     public static KeyListener add(KeyListener a, KeyListener b) {
508         return (KeyListener)addInternal(a, b);
509     }
510
511     /**
512      * Adds mouse-listener-a with mouse-listener-b and
513      * returns the resulting multicast listener.
514      * @param a mouse-listener-a
515      * @param b mouse-listener-b
516      */

517     public static MouseListener add(MouseListener a, MouseListener b) {
518         return (MouseListener)addInternal(a, b);
519     }
520
521     /**
522      * Adds mouse-motion-listener-a with mouse-motion-listener-b and
523      * returns the resulting multicast listener.
524      * @param a mouse-motion-listener-a
525      * @param b mouse-motion-listener-b
526      */

527     public static MouseMotionListener add(MouseMotionListener a, MouseMotionListener b) {
528         return (MouseMotionListener)addInternal(a, b);
529     }
530
531     /**
532      * Adds window-listener-a with window-listener-b and
533      * returns the resulting multicast listener.
534      * @param a window-listener-a
535      * @param b window-listener-b
536      */

537     public static WindowListener add(WindowListener a, WindowListener b) {
538         return (WindowListener)addInternal(a, b);
539     }
540
541     /**
542      * Adds window-state-listener-a with window-state-listener-b
543      * and returns the resulting multicast listener.
544      * @param a window-state-listener-a
545      * @param b window-state-listener-b
546      */

547     public static WindowStateListener add(WindowStateListener a,
548                                           WindowStateListener b) {
549         return (WindowStateListener)addInternal(a, b);
550     }
551
552     /**
553      * Adds window-focus-listener-a with window-focus-listener-b
554      * and returns the resulting multicast listener.
555      * @param a window-focus-listener-a
556      * @param b window-focus-listener-b
557      */

558     public static WindowFocusListener add(WindowFocusListener a,
559                                           WindowFocusListener b) {
560         return (WindowFocusListener)addInternal(a, b);
561     }
562
563     /**
564      * Adds action-listener-a with action-listener-b and
565      * returns the resulting multicast listener.
566      * @param a action-listener-a
567      * @param b action-listener-b
568      */

569     public static ActionListener add(ActionListener a, ActionListener b) {
570         return (ActionListener)addInternal(a, b);
571     }
572
573     /**
574      * Adds item-listener-a with item-listener-b and
575      * returns the resulting multicast listener.
576      * @param a item-listener-a
577      * @param b item-listener-b
578      */

579     public static ItemListener add(ItemListener a, ItemListener b) {
580         return (ItemListener)addInternal(a, b);
581     }
582
583     /**
584      * Adds adjustment-listener-a with adjustment-listener-b and
585      * returns the resulting multicast listener.
586      * @param a adjustment-listener-a
587      * @param b adjustment-listener-b
588      */

589     public static AdjustmentListener add(AdjustmentListener a, AdjustmentListener b) {
590         return (AdjustmentListener)addInternal(a, b);
591     }
592     public static TextListener add(TextListener a, TextListener b) {
593         return (TextListener)addInternal(a, b);
594     }
595
596     /**
597      * Adds input-method-listener-a with input-method-listener-b and
598      * returns the resulting multicast listener.
599      * @param a input-method-listener-a
600      * @param b input-method-listener-b
601      */

602      public static InputMethodListener add(InputMethodListener a, InputMethodListener b) {
603         return (InputMethodListener)addInternal(a, b);
604      }
605
606     /**
607      * Adds hierarchy-listener-a with hierarchy-listener-b and
608      * returns the resulting multicast listener.
609      * @param a hierarchy-listener-a
610      * @param b hierarchy-listener-b
611      */

612      public static HierarchyListener add(HierarchyListener a, HierarchyListener b) {
613         return (HierarchyListener)addInternal(a, b);
614      }
615
616     /**
617      * Adds hierarchy-bounds-listener-a with hierarchy-bounds-listener-b and
618      * returns the resulting multicast listener.
619      * @param a hierarchy-bounds-listener-a
620      * @param b hierarchy-bounds-listener-b
621      */

622      public static HierarchyBoundsListener add(HierarchyBoundsListener a, HierarchyBoundsListener b) {
623         return (HierarchyBoundsListener)addInternal(a, b);
624      }
625
626     /**
627      * Adds mouse-wheel-listener-a with mouse-wheel-listener-b and
628      * returns the resulting multicast listener.
629      * @param a mouse-wheel-listener-a
630      * @param b mouse-wheel-listener-b
631      * @since 1.4
632      */

633     public static MouseWheelListener add(MouseWheelListener a,
634                                          MouseWheelListener b) {
635         return (MouseWheelListener)addInternal(a, b);
636     }
637
638     /**
639      * Removes the old component-listener from component-listener-l and
640      * returns the resulting multicast listener.
641      * @param l component-listener-l
642      * @param oldl the component-listener being removed
643      */

644     public static ComponentListener remove(ComponentListener l, ComponentListener oldl) {
645     return (ComponentListener) removeInternal(l, oldl);
646     }
647
648     /**
649      * Removes the old container-listener from container-listener-l and
650      * returns the resulting multicast listener.
651      * @param l container-listener-l
652      * @param oldl the container-listener being removed
653      */

654     public static ContainerListener remove(ContainerListener l, ContainerListener oldl) {
655     return (ContainerListener) removeInternal(l, oldl);
656     }
657
658     /**
659      * Removes the old focus-listener from focus-listener-l and
660      * returns the resulting multicast listener.
661      * @param l focus-listener-l
662      * @param oldl the focus-listener being removed
663      */

664     public static FocusListener remove(FocusListener l, FocusListener oldl) {
665     return (FocusListener) removeInternal(l, oldl);
666     }
667
668     /**
669      * Removes the old key-listener from key-listener-l and
670      * returns the resulting multicast listener.
671      * @param l key-listener-l
672      * @param oldl the key-listener being removed
673      */

674     public static KeyListener remove(KeyListener l, KeyListener oldl) {
675     return (KeyListener) removeInternal(l, oldl);
676     }
677
678     /**
679      * Removes the old mouse-listener from mouse-listener-l and
680      * returns the resulting multicast listener.
681      * @param l mouse-listener-l
682      * @param oldl the mouse-listener being removed
683      */

684     public static MouseListener remove(MouseListener l, MouseListener oldl) {
685     return (MouseListener) removeInternal(l, oldl);
686     }
687
688     /**
689      * Removes the old mouse-motion-listener from mouse-motion-listener-l
690      * and returns the resulting multicast listener.
691      * @param l mouse-motion-listener-l
692      * @param oldl the mouse-motion-listener being removed
693      */

694     public static MouseMotionListener remove(MouseMotionListener l, MouseMotionListener oldl) {
695     return (MouseMotionListener) removeInternal(l, oldl);
696     }
697
698     /**
699      * Removes the old window-listener from window-listener-l and
700      * returns the resulting multicast listener.
701      * @param l window-listener-l
702      * @param oldl the window-listener being removed
703      */

704     public static WindowListener remove(WindowListener l, WindowListener oldl) {
705     return (WindowListener) removeInternal(l, oldl);
706     }
707
708     /**
709      * Removes the old window-state-listener from window-state-listener-l
710      * and returns the resulting multicast listener.
711      * @param l window-state-listener-l
712      * @param oldl the window-state-listener being removed
713      */

714     public static WindowStateListener remove(WindowStateListener l,
715                                              WindowStateListener oldl) {
716         return (WindowStateListener) removeInternal(l, oldl);
717     }
718
719     /**
720      * Removes the old window-focus-listener from window-focus-listener-l
721      * and returns the resulting multicast listener.
722      * @param l window-focus-listener-l
723      * @param oldl the window-focus-listener being removed
724      */

725     public static WindowFocusListener remove(WindowFocusListener l,
726                                              WindowFocusListener oldl) {
727         return (WindowFocusListener) removeInternal(l, oldl);
728     }
729
730     /**
731      * Removes the old action-listener from action-listener-l and
732      * returns the resulting multicast listener.
733      * @param l action-listener-l
734      * @param oldl the action-listener being removed
735      */

736     public static ActionListener remove(ActionListener l, ActionListener oldl) {
737     return (ActionListener) removeInternal(l, oldl);
738     }
739
740     /**
741      * Removes the old item-listener from item-listener-l and
742      * returns the resulting multicast listener.
743      * @param l item-listener-l
744      * @param oldl the item-listener being removed
745      */

746     public static ItemListener remove(ItemListener l, ItemListener oldl) {
747     return (ItemListener) removeInternal(l, oldl);
748     }
749
750     /**
751      * Removes the old adjustment-listener from adjustment-listener-l and
752      * returns the resulting multicast listener.
753      * @param l adjustment-listener-l
754      * @param oldl the adjustment-listener being removed
755      */

756     public static AdjustmentListener remove(AdjustmentListener l, AdjustmentListener oldl) {
757     return (AdjustmentListener) removeInternal(l, oldl);
758     }
759     public static TextListener remove(TextListener l, TextListener oldl) {
760     return (TextListener) removeInternal(l, oldl);
761     }
762
763     /**
764      * Removes the old input-method-listener from input-method-listener-l and
765      * returns the resulting multicast listener.
766      * @param l input-method-listener-l
767      * @param oldl the input-method-listener being removed
768      */

769     public static InputMethodListener remove(InputMethodListener l, InputMethodListener oldl) {
770         return (InputMethodListener) removeInternal(l, oldl);
771     }
772
773     /**
774      * Removes the old hierarchy-listener from hierarchy-listener-l and
775      * returns the resulting multicast listener.
776      * @param l hierarchy-listener-l
777      * @param oldl the hierarchy-listener being removed
778      */

779     public static HierarchyListener remove(HierarchyListener l, HierarchyListener oldl) {
780         return (HierarchyListener) removeInternal(l, oldl);
781     }
782
783     /**
784      * Removes the old hierarchy-bounds-listener from
785      * hierarchy-bounds-listener-l and returns the resulting multicast
786      * listener.
787      * @param l hierarchy-bounds-listener-l
788      * @param oldl the hierarchy-bounds-listener being removed
789      */

790     public static HierarchyBoundsListener remove(HierarchyBoundsListener l, HierarchyBoundsListener oldl) {
791         return (HierarchyBoundsListener) removeInternal(l, oldl);
792     }
793
794     /**
795      * Removes the old mouse-wheel-listener from mouse-wheel-listener-l
796      * and returns the resulting multicast listener.
797      * @param l mouse-wheel-listener-l
798      * @param oldl the mouse-wheel-listener being removed
799      * @since 1.4
800      */

801     public static MouseWheelListener remove(MouseWheelListener l,
802                                             MouseWheelListener oldl) {
803       return (MouseWheelListener) removeInternal(l, oldl);
804     }
805
806     /**
807      * Returns the resulting multicast listener from adding listener-a
808      * and listener-b together.
809      * If listener-a is null, it returns listener-b;
810      * If listener-b is null, it returns listener-a
811      * If neither are null, then it creates and returns
812      * a new AWTEventMulticaster instance which chains a with b.
813      * @param a event listener-a
814      * @param b event listener-b
815      */

816     protected static EventListener JavaDoc addInternal(EventListener JavaDoc a, EventListener JavaDoc b) {
817     if (a == null) return b;
818     if (b == null) return a;
819     return new AWTEventMulticaster JavaDoc(a, b);
820     }
821
822     /**
823      * Returns the resulting multicast listener after removing the
824      * old listener from listener-l.
825      * If listener-l equals the old listener OR listener-l is null,
826      * returns null.
827      * Else if listener-l is an instance of AWTEventMulticaster,
828      * then it removes the old listener from it.
829      * Else, returns listener l.
830      * @param l the listener being removed from
831      * @param oldl the listener being removed
832      */

833     protected static EventListener JavaDoc removeInternal(EventListener JavaDoc l, EventListener JavaDoc oldl) {
834     if (l == oldl || l == null) {
835         return null;
836     } else if (l instanceof AWTEventMulticaster JavaDoc) {
837         return ((AWTEventMulticaster JavaDoc)l).remove(oldl);
838     } else {
839         return l; // it's not here
840
}
841     }
842       
843
844     /* Serialization support.
845      */

846
847     protected void saveInternal(ObjectOutputStream JavaDoc s, String JavaDoc k) throws IOException JavaDoc {
848         if (a instanceof AWTEventMulticaster JavaDoc) {
849         ((AWTEventMulticaster JavaDoc)a).saveInternal(s, k);
850         }
851         else if (a instanceof Serializable JavaDoc) {
852             s.writeObject(k);
853             s.writeObject(a);
854         }
855         
856         if (b instanceof AWTEventMulticaster JavaDoc) {
857         ((AWTEventMulticaster JavaDoc)b).saveInternal(s, k);
858         }
859         else if (b instanceof Serializable JavaDoc) {
860             s.writeObject(k);
861             s.writeObject(b);
862         }
863     }
864
865     protected static void save(ObjectOutputStream JavaDoc s, String JavaDoc k, EventListener JavaDoc l) throws IOException JavaDoc {
866       if (l == null) {
867           return;
868       }
869       else if (l instanceof AWTEventMulticaster JavaDoc) {
870           ((AWTEventMulticaster JavaDoc)l).saveInternal(s, k);
871       }
872       else if (l instanceof Serializable JavaDoc) {
873            s.writeObject(k);
874            s.writeObject(l);
875       }
876     }
877     
878     /*
879      * Recursive method which returns a count of the number of listeners in
880      * EventListener, handling the (common) case of l actually being an
881      * AWTEventMulticaster. Additionally, only listeners of type listenerType
882      * are counted. Method modified to fix bug 4513402. -bchristi
883      */

884     private static int getListenerCount(EventListener JavaDoc l, Class JavaDoc listenerType) {
885         if (l instanceof AWTEventMulticaster JavaDoc) {
886             AWTEventMulticaster JavaDoc mc = (AWTEventMulticaster JavaDoc)l;
887             return getListenerCount(mc.a, listenerType) +
888              getListenerCount(mc.b, listenerType);
889         }
890         else {
891             // Only count listeners of correct type
892
return listenerType.isInstance(l) ? 1 : 0;
893         }
894     }
895     
896     /*
897      * Recusive method which populates EventListener array a with EventListeners
898      * from l. l is usually an AWTEventMulticaster. Bug 4513402 revealed that
899      * if l differed in type from the element type of a, an ArrayStoreException
900      * would occur. Now l is only inserted into a if it's of the appropriate
901      * type. -bchristi
902      */

903     private static int populateListenerArray(EventListener JavaDoc[] a, EventListener JavaDoc l, int index) {
904         if (l instanceof AWTEventMulticaster JavaDoc) {
905             AWTEventMulticaster JavaDoc mc = (AWTEventMulticaster JavaDoc)l;
906             int lhs = populateListenerArray(a, mc.a, index);
907             return populateListenerArray(a, mc.b, lhs);
908         }
909         else if (a.getClass().getComponentType().isInstance(l)) {
910             a[index] = l;
911             return index + 1;
912         }
913         // Skip nulls, instances of wrong class
914
else {
915             return index;
916         }
917     }
918     
919     /**
920      * Returns an array of all the objects chained as
921      * <code><em>Foo</em>Listener</code>s by the specified
922      * <code>java.util.EventListener</code>.
923      * <code><em>Foo</em>Listener</code>s are chained by the
924      * <code>AWTEventMulticaster</code> using the
925      * <code>add<em>Foo</em>Listener</code> method.
926      * If a <code>null</code> listener is specified, this method returns an
927      * empty array. If the specified listener is not an instance of
928      * <code>AWTEventMulticaster</code>, this method returns an array which
929      * contains only the specified listener. If no such listeners are chanined,
930      * this method returns an empty array.
931      *
932      * @param l the specified <code>java.util.EventListener</code>
933      * @param listenerType the type of listeners requested; this parameter
934      * should specify an interface that descends from
935      * <code>java.util.EventListener</code>
936      * @return an array of all objects chained as
937      * <code><em>Foo</em>Listener</code>s by the specified multicast
938      * listener, or an empty array if no such listeners have been
939      * chained by the specified multicast listener
940      * @exception ClassCastException if <code>listenerType</code>
941      * doesn't specify a class or interface that implements
942      * <code>java.util.EventListener</code>
943      *
944      * @since 1.4
945      */

946     public static <T extends EventListener JavaDoc> T[]
947     getListeners(EventListener JavaDoc l, Class JavaDoc<T> listenerType)
948     {
949         int n = getListenerCount(l, listenerType);
950         T[] result = (T[])Array.newInstance(listenerType, n);
951         populateListenerArray(result, l, 0);
952         return result;
953     }
954 }
955
Popular Tags