Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/AWTEventMulticaster.java
38829 views
/*1* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package java.awt;2526import java.awt.event.*;27import java.lang.reflect.Array;28import java.util.EventListener;29import java.io.Serializable;30import java.io.ObjectOutputStream;31import java.io.IOException;32import java.util.EventListener;333435/**36* {@code AWTEventMulticaster} implements efficient and thread-safe multi-cast37* event dispatching for the AWT events defined in the {@code java.awt.event}38* package.39* <p>40* The following example illustrates how to use this class:41*42* <pre><code>43* public myComponent extends Component {44* ActionListener actionListener = null;45*46* public synchronized void addActionListener(ActionListener l) {47* actionListener = AWTEventMulticaster.add(actionListener, l);48* }49* public synchronized void removeActionListener(ActionListener l) {50* actionListener = AWTEventMulticaster.remove(actionListener, l);51* }52* public void processEvent(AWTEvent e) {53* // when event occurs which causes "action" semantic54* ActionListener listener = actionListener;55* if (listener != null) {56* listener.actionPerformed(new ActionEvent());57* }58* }59* }60* </code></pre>61* The important point to note is the first argument to the {@code62* add} and {@code remove} methods is the field maintaining the63* listeners. In addition you must assign the result of the {@code add}64* and {@code remove} methods to the field maintaining the listeners.65* <p>66* {@code AWTEventMulticaster} is implemented as a pair of {@code67* EventListeners} that are set at construction time. {@code68* AWTEventMulticaster} is immutable. The {@code add} and {@code69* remove} methods do not alter {@code AWTEventMulticaster} in70* anyway. If necessary, a new {@code AWTEventMulticaster} is71* created. In this way it is safe to add and remove listeners during72* the process of an event dispatching. However, event listeners73* added during the process of an event dispatch operation are not74* notified of the event currently being dispatched.75* <p>76* All of the {@code add} methods allow {@code null} arguments. If the77* first argument is {@code null}, the second argument is returned. If78* the first argument is not {@code null} and the second argument is79* {@code null}, the first argument is returned. If both arguments are80* {@code non-null}, a new {@code AWTEventMulticaster} is created using81* the two arguments and returned.82* <p>83* For the {@code remove} methods that take two arguments, the following is84* returned:85* <ul>86* <li>{@code null}, if the first argument is {@code null}, or87* the arguments are equal, by way of {@code ==}.88* <li>the first argument, if the first argument is not an instance of89* {@code AWTEventMulticaster}.90* <li>result of invoking {@code remove(EventListener)} on the91* first argument, supplying the second argument to the92* {@code remove(EventListener)} method.93* </ul>94* <p>Swing makes use of95* {@link javax.swing.event.EventListenerList EventListenerList} for96* similar logic. Refer to it for details.97*98* @see javax.swing.event.EventListenerList99*100* @author John Rose101* @author Amy Fowler102* @since 1.1103*/104105public class AWTEventMulticaster implements106ComponentListener, ContainerListener, FocusListener, KeyListener,107MouseListener, MouseMotionListener, WindowListener, WindowFocusListener,108WindowStateListener, ActionListener, ItemListener, AdjustmentListener,109TextListener, InputMethodListener, HierarchyListener,110HierarchyBoundsListener, MouseWheelListener {111112protected final EventListener a, b;113114/**115* Creates an event multicaster instance which chains listener-a116* with listener-b. Input parameters <code>a</code> and <code>b</code>117* should not be <code>null</code>, though implementations may vary in118* choosing whether or not to throw <code>NullPointerException</code>119* in that case.120* @param a listener-a121* @param b listener-b122*/123protected AWTEventMulticaster(EventListener a, EventListener b) {124this.a = a; this.b = b;125}126127/**128* Removes a listener from this multicaster.129* <p>130* The returned multicaster contains all the listeners in this131* multicaster with the exception of all occurrences of {@code oldl}.132* If the resulting multicaster contains only one regular listener133* the regular listener may be returned. If the resulting multicaster134* is empty, then {@code null} may be returned instead.135* <p>136* No exception is thrown if {@code oldl} is {@code null}.137*138* @param oldl the listener to be removed139* @return resulting listener140*/141protected EventListener remove(EventListener oldl) {142if (oldl == a) return b;143if (oldl == b) return a;144EventListener a2 = removeInternal(a, oldl);145EventListener b2 = removeInternal(b, oldl);146if (a2 == a && b2 == b) {147return this; // it's not here148}149return addInternal(a2, b2);150}151152/**153* Handles the componentResized event by invoking the154* componentResized methods on listener-a and listener-b.155* @param e the component event156*/157public void componentResized(ComponentEvent e) {158((ComponentListener)a).componentResized(e);159((ComponentListener)b).componentResized(e);160}161162/**163* Handles the componentMoved event by invoking the164* componentMoved methods on listener-a and listener-b.165* @param e the component event166*/167public void componentMoved(ComponentEvent e) {168((ComponentListener)a).componentMoved(e);169((ComponentListener)b).componentMoved(e);170}171172/**173* Handles the componentShown event by invoking the174* componentShown methods on listener-a and listener-b.175* @param e the component event176*/177public void componentShown(ComponentEvent e) {178((ComponentListener)a).componentShown(e);179((ComponentListener)b).componentShown(e);180}181182/**183* Handles the componentHidden event by invoking the184* componentHidden methods on listener-a and listener-b.185* @param e the component event186*/187public void componentHidden(ComponentEvent e) {188((ComponentListener)a).componentHidden(e);189((ComponentListener)b).componentHidden(e);190}191192/**193* Handles the componentAdded container event by invoking the194* componentAdded methods on listener-a and listener-b.195* @param e the component event196*/197public void componentAdded(ContainerEvent e) {198((ContainerListener)a).componentAdded(e);199((ContainerListener)b).componentAdded(e);200}201202/**203* Handles the componentRemoved container event by invoking the204* componentRemoved methods on listener-a and listener-b.205* @param e the component event206*/207public void componentRemoved(ContainerEvent e) {208((ContainerListener)a).componentRemoved(e);209((ContainerListener)b).componentRemoved(e);210}211212/**213* Handles the focusGained event by invoking the214* focusGained methods on listener-a and listener-b.215* @param e the focus event216*/217public void focusGained(FocusEvent e) {218((FocusListener)a).focusGained(e);219((FocusListener)b).focusGained(e);220}221222/**223* Handles the focusLost event by invoking the224* focusLost methods on listener-a and listener-b.225* @param e the focus event226*/227public void focusLost(FocusEvent e) {228((FocusListener)a).focusLost(e);229((FocusListener)b).focusLost(e);230}231232/**233* Handles the keyTyped event by invoking the234* keyTyped methods on listener-a and listener-b.235* @param e the key event236*/237public void keyTyped(KeyEvent e) {238((KeyListener)a).keyTyped(e);239((KeyListener)b).keyTyped(e);240}241242/**243* Handles the keyPressed event by invoking the244* keyPressed methods on listener-a and listener-b.245* @param e the key event246*/247public void keyPressed(KeyEvent e) {248((KeyListener)a).keyPressed(e);249((KeyListener)b).keyPressed(e);250}251252/**253* Handles the keyReleased event by invoking the254* keyReleased methods on listener-a and listener-b.255* @param e the key event256*/257public void keyReleased(KeyEvent e) {258((KeyListener)a).keyReleased(e);259((KeyListener)b).keyReleased(e);260}261262/**263* Handles the mouseClicked event by invoking the264* mouseClicked methods on listener-a and listener-b.265* @param e the mouse event266*/267public void mouseClicked(MouseEvent e) {268((MouseListener)a).mouseClicked(e);269((MouseListener)b).mouseClicked(e);270}271272/**273* Handles the mousePressed event by invoking the274* mousePressed methods on listener-a and listener-b.275* @param e the mouse event276*/277public void mousePressed(MouseEvent e) {278((MouseListener)a).mousePressed(e);279((MouseListener)b).mousePressed(e);280}281282/**283* Handles the mouseReleased event by invoking the284* mouseReleased methods on listener-a and listener-b.285* @param e the mouse event286*/287public void mouseReleased(MouseEvent e) {288((MouseListener)a).mouseReleased(e);289((MouseListener)b).mouseReleased(e);290}291292/**293* Handles the mouseEntered event by invoking the294* mouseEntered methods on listener-a and listener-b.295* @param e the mouse event296*/297public void mouseEntered(MouseEvent e) {298((MouseListener)a).mouseEntered(e);299((MouseListener)b).mouseEntered(e);300}301302/**303* Handles the mouseExited event by invoking the304* mouseExited methods on listener-a and listener-b.305* @param e the mouse event306*/307public void mouseExited(MouseEvent e) {308((MouseListener)a).mouseExited(e);309((MouseListener)b).mouseExited(e);310}311312/**313* Handles the mouseDragged event by invoking the314* mouseDragged methods on listener-a and listener-b.315* @param e the mouse event316*/317public void mouseDragged(MouseEvent e) {318((MouseMotionListener)a).mouseDragged(e);319((MouseMotionListener)b).mouseDragged(e);320}321322/**323* Handles the mouseMoved event by invoking the324* mouseMoved methods on listener-a and listener-b.325* @param e the mouse event326*/327public void mouseMoved(MouseEvent e) {328((MouseMotionListener)a).mouseMoved(e);329((MouseMotionListener)b).mouseMoved(e);330}331332/**333* Handles the windowOpened event by invoking the334* windowOpened methods on listener-a and listener-b.335* @param e the window event336*/337public void windowOpened(WindowEvent e) {338((WindowListener)a).windowOpened(e);339((WindowListener)b).windowOpened(e);340}341342/**343* Handles the windowClosing event by invoking the344* windowClosing methods on listener-a and listener-b.345* @param e the window event346*/347public void windowClosing(WindowEvent e) {348((WindowListener)a).windowClosing(e);349((WindowListener)b).windowClosing(e);350}351352/**353* Handles the windowClosed event by invoking the354* windowClosed methods on listener-a and listener-b.355* @param e the window event356*/357public void windowClosed(WindowEvent e) {358((WindowListener)a).windowClosed(e);359((WindowListener)b).windowClosed(e);360}361362/**363* Handles the windowIconified event by invoking the364* windowIconified methods on listener-a and listener-b.365* @param e the window event366*/367public void windowIconified(WindowEvent e) {368((WindowListener)a).windowIconified(e);369((WindowListener)b).windowIconified(e);370}371372/**373* Handles the windowDeiconfied event by invoking the374* windowDeiconified methods on listener-a and listener-b.375* @param e the window event376*/377public void windowDeiconified(WindowEvent e) {378((WindowListener)a).windowDeiconified(e);379((WindowListener)b).windowDeiconified(e);380}381382/**383* Handles the windowActivated event by invoking the384* windowActivated methods on listener-a and listener-b.385* @param e the window event386*/387public void windowActivated(WindowEvent e) {388((WindowListener)a).windowActivated(e);389((WindowListener)b).windowActivated(e);390}391392/**393* Handles the windowDeactivated event by invoking the394* windowDeactivated methods on listener-a and listener-b.395* @param e the window event396*/397public void windowDeactivated(WindowEvent e) {398((WindowListener)a).windowDeactivated(e);399((WindowListener)b).windowDeactivated(e);400}401402/**403* Handles the windowStateChanged event by invoking the404* windowStateChanged methods on listener-a and listener-b.405* @param e the window event406* @since 1.4407*/408public void windowStateChanged(WindowEvent e) {409((WindowStateListener)a).windowStateChanged(e);410((WindowStateListener)b).windowStateChanged(e);411}412413414/**415* Handles the windowGainedFocus event by invoking the windowGainedFocus416* methods on listener-a and listener-b.417* @param e the window event418* @since 1.4419*/420public void windowGainedFocus(WindowEvent e) {421((WindowFocusListener)a).windowGainedFocus(e);422((WindowFocusListener)b).windowGainedFocus(e);423}424425/**426* Handles the windowLostFocus event by invoking the windowLostFocus427* methods on listener-a and listener-b.428* @param e the window event429* @since 1.4430*/431public void windowLostFocus(WindowEvent e) {432((WindowFocusListener)a).windowLostFocus(e);433((WindowFocusListener)b).windowLostFocus(e);434}435436/**437* Handles the actionPerformed event by invoking the438* actionPerformed methods on listener-a and listener-b.439* @param e the action event440*/441public void actionPerformed(ActionEvent e) {442((ActionListener)a).actionPerformed(e);443((ActionListener)b).actionPerformed(e);444}445446/**447* Handles the itemStateChanged event by invoking the448* itemStateChanged methods on listener-a and listener-b.449* @param e the item event450*/451public void itemStateChanged(ItemEvent e) {452((ItemListener)a).itemStateChanged(e);453((ItemListener)b).itemStateChanged(e);454}455456/**457* Handles the adjustmentValueChanged event by invoking the458* adjustmentValueChanged methods on listener-a and listener-b.459* @param e the adjustment event460*/461public void adjustmentValueChanged(AdjustmentEvent e) {462((AdjustmentListener)a).adjustmentValueChanged(e);463((AdjustmentListener)b).adjustmentValueChanged(e);464}465public void textValueChanged(TextEvent e) {466((TextListener)a).textValueChanged(e);467((TextListener)b).textValueChanged(e);468}469470/**471* Handles the inputMethodTextChanged event by invoking the472* inputMethodTextChanged methods on listener-a and listener-b.473* @param e the item event474*/475public void inputMethodTextChanged(InputMethodEvent e) {476((InputMethodListener)a).inputMethodTextChanged(e);477((InputMethodListener)b).inputMethodTextChanged(e);478}479480/**481* Handles the caretPositionChanged event by invoking the482* caretPositionChanged methods on listener-a and listener-b.483* @param e the item event484*/485public void caretPositionChanged(InputMethodEvent e) {486((InputMethodListener)a).caretPositionChanged(e);487((InputMethodListener)b).caretPositionChanged(e);488}489490/**491* Handles the hierarchyChanged event by invoking the492* hierarchyChanged methods on listener-a and listener-b.493* @param e the item event494* @since 1.3495*/496public void hierarchyChanged(HierarchyEvent e) {497((HierarchyListener)a).hierarchyChanged(e);498((HierarchyListener)b).hierarchyChanged(e);499}500501/**502* Handles the ancestorMoved event by invoking the503* ancestorMoved methods on listener-a and listener-b.504* @param e the item event505* @since 1.3506*/507public void ancestorMoved(HierarchyEvent e) {508((HierarchyBoundsListener)a).ancestorMoved(e);509((HierarchyBoundsListener)b).ancestorMoved(e);510}511512/**513* Handles the ancestorResized event by invoking the514* ancestorResized methods on listener-a and listener-b.515* @param e the item event516* @since 1.3517*/518public void ancestorResized(HierarchyEvent e) {519((HierarchyBoundsListener)a).ancestorResized(e);520((HierarchyBoundsListener)b).ancestorResized(e);521}522523/**524* Handles the mouseWheelMoved event by invoking the525* mouseWheelMoved methods on listener-a and listener-b.526* @param e the mouse event527* @since 1.4528*/529public void mouseWheelMoved(MouseWheelEvent e) {530((MouseWheelListener)a).mouseWheelMoved(e);531((MouseWheelListener)b).mouseWheelMoved(e);532}533534/**535* Adds component-listener-a with component-listener-b and536* returns the resulting multicast listener.537* @param a component-listener-a538* @param b component-listener-b539*/540public static ComponentListener add(ComponentListener a, ComponentListener b) {541return (ComponentListener)addInternal(a, b);542}543544/**545* Adds container-listener-a with container-listener-b and546* returns the resulting multicast listener.547* @param a container-listener-a548* @param b container-listener-b549*/550public static ContainerListener add(ContainerListener a, ContainerListener b) {551return (ContainerListener)addInternal(a, b);552}553554/**555* Adds focus-listener-a with focus-listener-b and556* returns the resulting multicast listener.557* @param a focus-listener-a558* @param b focus-listener-b559*/560public static FocusListener add(FocusListener a, FocusListener b) {561return (FocusListener)addInternal(a, b);562}563564/**565* Adds key-listener-a with key-listener-b and566* returns the resulting multicast listener.567* @param a key-listener-a568* @param b key-listener-b569*/570public static KeyListener add(KeyListener a, KeyListener b) {571return (KeyListener)addInternal(a, b);572}573574/**575* Adds mouse-listener-a with mouse-listener-b and576* returns the resulting multicast listener.577* @param a mouse-listener-a578* @param b mouse-listener-b579*/580public static MouseListener add(MouseListener a, MouseListener b) {581return (MouseListener)addInternal(a, b);582}583584/**585* Adds mouse-motion-listener-a with mouse-motion-listener-b and586* returns the resulting multicast listener.587* @param a mouse-motion-listener-a588* @param b mouse-motion-listener-b589*/590public static MouseMotionListener add(MouseMotionListener a, MouseMotionListener b) {591return (MouseMotionListener)addInternal(a, b);592}593594/**595* Adds window-listener-a with window-listener-b and596* returns the resulting multicast listener.597* @param a window-listener-a598* @param b window-listener-b599*/600public static WindowListener add(WindowListener a, WindowListener b) {601return (WindowListener)addInternal(a, b);602}603604/**605* Adds window-state-listener-a with window-state-listener-b606* and returns the resulting multicast listener.607* @param a window-state-listener-a608* @param b window-state-listener-b609* @since 1.4610*/611public static WindowStateListener add(WindowStateListener a,612WindowStateListener b) {613return (WindowStateListener)addInternal(a, b);614}615616/**617* Adds window-focus-listener-a with window-focus-listener-b618* and returns the resulting multicast listener.619* @param a window-focus-listener-a620* @param b window-focus-listener-b621* @since 1.4622*/623public static WindowFocusListener add(WindowFocusListener a,624WindowFocusListener b) {625return (WindowFocusListener)addInternal(a, b);626}627628/**629* Adds action-listener-a with action-listener-b and630* returns the resulting multicast listener.631* @param a action-listener-a632* @param b action-listener-b633*/634public static ActionListener add(ActionListener a, ActionListener b) {635return (ActionListener)addInternal(a, b);636}637638/**639* Adds item-listener-a with item-listener-b and640* returns the resulting multicast listener.641* @param a item-listener-a642* @param b item-listener-b643*/644public static ItemListener add(ItemListener a, ItemListener b) {645return (ItemListener)addInternal(a, b);646}647648/**649* Adds adjustment-listener-a with adjustment-listener-b and650* returns the resulting multicast listener.651* @param a adjustment-listener-a652* @param b adjustment-listener-b653*/654public static AdjustmentListener add(AdjustmentListener a, AdjustmentListener b) {655return (AdjustmentListener)addInternal(a, b);656}657public static TextListener add(TextListener a, TextListener b) {658return (TextListener)addInternal(a, b);659}660661/**662* Adds input-method-listener-a with input-method-listener-b and663* returns the resulting multicast listener.664* @param a input-method-listener-a665* @param b input-method-listener-b666*/667public static InputMethodListener add(InputMethodListener a, InputMethodListener b) {668return (InputMethodListener)addInternal(a, b);669}670671/**672* Adds hierarchy-listener-a with hierarchy-listener-b and673* returns the resulting multicast listener.674* @param a hierarchy-listener-a675* @param b hierarchy-listener-b676* @since 1.3677*/678public static HierarchyListener add(HierarchyListener a, HierarchyListener b) {679return (HierarchyListener)addInternal(a, b);680}681682/**683* Adds hierarchy-bounds-listener-a with hierarchy-bounds-listener-b and684* returns the resulting multicast listener.685* @param a hierarchy-bounds-listener-a686* @param b hierarchy-bounds-listener-b687* @since 1.3688*/689public static HierarchyBoundsListener add(HierarchyBoundsListener a, HierarchyBoundsListener b) {690return (HierarchyBoundsListener)addInternal(a, b);691}692693/**694* Adds mouse-wheel-listener-a with mouse-wheel-listener-b and695* returns the resulting multicast listener.696* @param a mouse-wheel-listener-a697* @param b mouse-wheel-listener-b698* @since 1.4699*/700public static MouseWheelListener add(MouseWheelListener a,701MouseWheelListener b) {702return (MouseWheelListener)addInternal(a, b);703}704705/**706* Removes the old component-listener from component-listener-l and707* returns the resulting multicast listener.708* @param l component-listener-l709* @param oldl the component-listener being removed710*/711public static ComponentListener remove(ComponentListener l, ComponentListener oldl) {712return (ComponentListener) removeInternal(l, oldl);713}714715/**716* Removes the old container-listener from container-listener-l and717* returns the resulting multicast listener.718* @param l container-listener-l719* @param oldl the container-listener being removed720*/721public static ContainerListener remove(ContainerListener l, ContainerListener oldl) {722return (ContainerListener) removeInternal(l, oldl);723}724725/**726* Removes the old focus-listener from focus-listener-l and727* returns the resulting multicast listener.728* @param l focus-listener-l729* @param oldl the focus-listener being removed730*/731public static FocusListener remove(FocusListener l, FocusListener oldl) {732return (FocusListener) removeInternal(l, oldl);733}734735/**736* Removes the old key-listener from key-listener-l and737* returns the resulting multicast listener.738* @param l key-listener-l739* @param oldl the key-listener being removed740*/741public static KeyListener remove(KeyListener l, KeyListener oldl) {742return (KeyListener) removeInternal(l, oldl);743}744745/**746* Removes the old mouse-listener from mouse-listener-l and747* returns the resulting multicast listener.748* @param l mouse-listener-l749* @param oldl the mouse-listener being removed750*/751public static MouseListener remove(MouseListener l, MouseListener oldl) {752return (MouseListener) removeInternal(l, oldl);753}754755/**756* Removes the old mouse-motion-listener from mouse-motion-listener-l757* and returns the resulting multicast listener.758* @param l mouse-motion-listener-l759* @param oldl the mouse-motion-listener being removed760*/761public static MouseMotionListener remove(MouseMotionListener l, MouseMotionListener oldl) {762return (MouseMotionListener) removeInternal(l, oldl);763}764765/**766* Removes the old window-listener from window-listener-l and767* returns the resulting multicast listener.768* @param l window-listener-l769* @param oldl the window-listener being removed770*/771public static WindowListener remove(WindowListener l, WindowListener oldl) {772return (WindowListener) removeInternal(l, oldl);773}774775/**776* Removes the old window-state-listener from window-state-listener-l777* and returns the resulting multicast listener.778* @param l window-state-listener-l779* @param oldl the window-state-listener being removed780* @since 1.4781*/782public static WindowStateListener remove(WindowStateListener l,783WindowStateListener oldl) {784return (WindowStateListener) removeInternal(l, oldl);785}786787/**788* Removes the old window-focus-listener from window-focus-listener-l789* and returns the resulting multicast listener.790* @param l window-focus-listener-l791* @param oldl the window-focus-listener being removed792* @since 1.4793*/794public static WindowFocusListener remove(WindowFocusListener l,795WindowFocusListener oldl) {796return (WindowFocusListener) removeInternal(l, oldl);797}798799/**800* Removes the old action-listener from action-listener-l and801* returns the resulting multicast listener.802* @param l action-listener-l803* @param oldl the action-listener being removed804*/805public static ActionListener remove(ActionListener l, ActionListener oldl) {806return (ActionListener) removeInternal(l, oldl);807}808809/**810* Removes the old item-listener from item-listener-l and811* returns the resulting multicast listener.812* @param l item-listener-l813* @param oldl the item-listener being removed814*/815public static ItemListener remove(ItemListener l, ItemListener oldl) {816return (ItemListener) removeInternal(l, oldl);817}818819/**820* Removes the old adjustment-listener from adjustment-listener-l and821* returns the resulting multicast listener.822* @param l adjustment-listener-l823* @param oldl the adjustment-listener being removed824*/825public static AdjustmentListener remove(AdjustmentListener l, AdjustmentListener oldl) {826return (AdjustmentListener) removeInternal(l, oldl);827}828public static TextListener remove(TextListener l, TextListener oldl) {829return (TextListener) removeInternal(l, oldl);830}831832/**833* Removes the old input-method-listener from input-method-listener-l and834* returns the resulting multicast listener.835* @param l input-method-listener-l836* @param oldl the input-method-listener being removed837*/838public static InputMethodListener remove(InputMethodListener l, InputMethodListener oldl) {839return (InputMethodListener) removeInternal(l, oldl);840}841842/**843* Removes the old hierarchy-listener from hierarchy-listener-l and844* returns the resulting multicast listener.845* @param l hierarchy-listener-l846* @param oldl the hierarchy-listener being removed847* @since 1.3848*/849public static HierarchyListener remove(HierarchyListener l, HierarchyListener oldl) {850return (HierarchyListener) removeInternal(l, oldl);851}852853/**854* Removes the old hierarchy-bounds-listener from855* hierarchy-bounds-listener-l and returns the resulting multicast856* listener.857* @param l hierarchy-bounds-listener-l858* @param oldl the hierarchy-bounds-listener being removed859* @since 1.3860*/861public static HierarchyBoundsListener remove(HierarchyBoundsListener l, HierarchyBoundsListener oldl) {862return (HierarchyBoundsListener) removeInternal(l, oldl);863}864865/**866* Removes the old mouse-wheel-listener from mouse-wheel-listener-l867* and returns the resulting multicast listener.868* @param l mouse-wheel-listener-l869* @param oldl the mouse-wheel-listener being removed870* @since 1.4871*/872public static MouseWheelListener remove(MouseWheelListener l,873MouseWheelListener oldl) {874return (MouseWheelListener) removeInternal(l, oldl);875}876877/**878* Returns the resulting multicast listener from adding listener-a879* and listener-b together.880* If listener-a is null, it returns listener-b;881* If listener-b is null, it returns listener-a882* If neither are null, then it creates and returns883* a new AWTEventMulticaster instance which chains a with b.884* @param a event listener-a885* @param b event listener-b886*/887protected static EventListener addInternal(EventListener a, EventListener b) {888if (a == null) return b;889if (b == null) return a;890return new AWTEventMulticaster(a, b);891}892893/**894* Returns the resulting multicast listener after removing the895* old listener from listener-l.896* If listener-l equals the old listener OR listener-l is null,897* returns null.898* Else if listener-l is an instance of AWTEventMulticaster,899* then it removes the old listener from it.900* Else, returns listener l.901* @param l the listener being removed from902* @param oldl the listener being removed903*/904protected static EventListener removeInternal(EventListener l, EventListener oldl) {905if (l == oldl || l == null) {906return null;907} else if (l instanceof AWTEventMulticaster) {908return ((AWTEventMulticaster)l).remove(oldl);909} else {910return l; // it's not here911}912}913914915/* Serialization support.916*/917918protected void saveInternal(ObjectOutputStream s, String k) throws IOException {919if (a instanceof AWTEventMulticaster) {920((AWTEventMulticaster)a).saveInternal(s, k);921}922else if (a instanceof Serializable) {923s.writeObject(k);924s.writeObject(a);925}926927if (b instanceof AWTEventMulticaster) {928((AWTEventMulticaster)b).saveInternal(s, k);929}930else if (b instanceof Serializable) {931s.writeObject(k);932s.writeObject(b);933}934}935936protected static void save(ObjectOutputStream s, String k, EventListener l) throws IOException {937if (l == null) {938return;939}940else if (l instanceof AWTEventMulticaster) {941((AWTEventMulticaster)l).saveInternal(s, k);942}943else if (l instanceof Serializable) {944s.writeObject(k);945s.writeObject(l);946}947}948949/*950* Recursive method which returns a count of the number of listeners in951* EventListener, handling the (common) case of l actually being an952* AWTEventMulticaster. Additionally, only listeners of type listenerType953* are counted. Method modified to fix bug 4513402. -bchristi954*/955private static int getListenerCount(EventListener l, Class<?> listenerType) {956if (l instanceof AWTEventMulticaster) {957AWTEventMulticaster mc = (AWTEventMulticaster)l;958return getListenerCount(mc.a, listenerType) +959getListenerCount(mc.b, listenerType);960}961else {962// Only count listeners of correct type963return listenerType.isInstance(l) ? 1 : 0;964}965}966967/*968* Recusive method which populates EventListener array a with EventListeners969* from l. l is usually an AWTEventMulticaster. Bug 4513402 revealed that970* if l differed in type from the element type of a, an ArrayStoreException971* would occur. Now l is only inserted into a if it's of the appropriate972* type. -bchristi973*/974private static int populateListenerArray(EventListener[] a, EventListener l, int index) {975if (l instanceof AWTEventMulticaster) {976AWTEventMulticaster mc = (AWTEventMulticaster)l;977int lhs = populateListenerArray(a, mc.a, index);978return populateListenerArray(a, mc.b, lhs);979}980else if (a.getClass().getComponentType().isInstance(l)) {981a[index] = l;982return index + 1;983}984// Skip nulls, instances of wrong class985else {986return index;987}988}989990/**991* Returns an array of all the objects chained as992* <code><em>Foo</em>Listener</code>s by the specified993* <code>java.util.EventListener</code>.994* <code><em>Foo</em>Listener</code>s are chained by the995* <code>AWTEventMulticaster</code> using the996* <code>add<em>Foo</em>Listener</code> method.997* If a <code>null</code> listener is specified, this method returns an998* empty array. If the specified listener is not an instance of999* <code>AWTEventMulticaster</code>, this method returns an array which1000* contains only the specified listener. If no such listeners are chained,1001* this method returns an empty array.1002*1003* @param l the specified <code>java.util.EventListener</code>1004* @param listenerType the type of listeners requested; this parameter1005* should specify an interface that descends from1006* <code>java.util.EventListener</code>1007* @return an array of all objects chained as1008* <code><em>Foo</em>Listener</code>s by the specified multicast1009* listener, or an empty array if no such listeners have been1010* chained by the specified multicast listener1011* @exception NullPointerException if the specified1012* {@code listenertype} parameter is {@code null}1013* @exception ClassCastException if <code>listenerType</code>1014* doesn't specify a class or interface that implements1015* <code>java.util.EventListener</code>1016*1017* @since 1.41018*/1019@SuppressWarnings("unchecked")1020public static <T extends EventListener> T[]1021getListeners(EventListener l, Class<T> listenerType)1022{1023if (listenerType == null) {1024throw new NullPointerException ("Listener type should not be null");1025}10261027int n = getListenerCount(l, listenerType);1028T[] result = (T[])Array.newInstance(listenerType, n);1029populateListenerArray(result, l, 0);1030return result;1031}1032}103310341035