Path: blob/master/src/java.desktop/share/classes/java/awt/AWTEventMulticaster.java
41152 views
/*1* Copyright (c) 1996, 2015, 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 {111112/**113* A variable in the event chain (listener-a)114*/115protected final EventListener a;116117/**118* A variable in the event chain (listener-b)119*/120protected final EventListener b;121122/**123* Creates an event multicaster instance which chains listener-a124* with listener-b. Input parameters {@code a} and {@code b}125* should not be {@code null}, though implementations may vary in126* choosing whether or not to throw {@code NullPointerException}127* in that case.128* @param a listener-a129* @param b listener-b130*/131protected AWTEventMulticaster(EventListener a, EventListener b) {132this.a = a; this.b = b;133}134135/**136* Removes a listener from this multicaster.137* <p>138* The returned multicaster contains all the listeners in this139* multicaster with the exception of all occurrences of {@code oldl}.140* If the resulting multicaster contains only one regular listener141* the regular listener may be returned. If the resulting multicaster142* is empty, then {@code null} may be returned instead.143* <p>144* No exception is thrown if {@code oldl} is {@code null}.145*146* @param oldl the listener to be removed147* @return resulting listener148*/149protected EventListener remove(EventListener oldl) {150if (oldl == a) return b;151if (oldl == b) return a;152EventListener a2 = removeInternal(a, oldl);153EventListener b2 = removeInternal(b, oldl);154if (a2 == a && b2 == b) {155return this; // it's not here156}157return addInternal(a2, b2);158}159160/**161* Handles the componentResized event by invoking the162* componentResized methods on listener-a and listener-b.163* @param e the component event164*/165public void componentResized(ComponentEvent e) {166((ComponentListener)a).componentResized(e);167((ComponentListener)b).componentResized(e);168}169170/**171* Handles the componentMoved event by invoking the172* componentMoved methods on listener-a and listener-b.173* @param e the component event174*/175public void componentMoved(ComponentEvent e) {176((ComponentListener)a).componentMoved(e);177((ComponentListener)b).componentMoved(e);178}179180/**181* Handles the componentShown event by invoking the182* componentShown methods on listener-a and listener-b.183* @param e the component event184*/185public void componentShown(ComponentEvent e) {186((ComponentListener)a).componentShown(e);187((ComponentListener)b).componentShown(e);188}189190/**191* Handles the componentHidden event by invoking the192* componentHidden methods on listener-a and listener-b.193* @param e the component event194*/195public void componentHidden(ComponentEvent e) {196((ComponentListener)a).componentHidden(e);197((ComponentListener)b).componentHidden(e);198}199200/**201* Handles the componentAdded container event by invoking the202* componentAdded methods on listener-a and listener-b.203* @param e the component event204*/205public void componentAdded(ContainerEvent e) {206((ContainerListener)a).componentAdded(e);207((ContainerListener)b).componentAdded(e);208}209210/**211* Handles the componentRemoved container event by invoking the212* componentRemoved methods on listener-a and listener-b.213* @param e the component event214*/215public void componentRemoved(ContainerEvent e) {216((ContainerListener)a).componentRemoved(e);217((ContainerListener)b).componentRemoved(e);218}219220/**221* Handles the focusGained event by invoking the222* focusGained methods on listener-a and listener-b.223* @param e the focus event224*/225public void focusGained(FocusEvent e) {226((FocusListener)a).focusGained(e);227((FocusListener)b).focusGained(e);228}229230/**231* Handles the focusLost event by invoking the232* focusLost methods on listener-a and listener-b.233* @param e the focus event234*/235public void focusLost(FocusEvent e) {236((FocusListener)a).focusLost(e);237((FocusListener)b).focusLost(e);238}239240/**241* Handles the keyTyped event by invoking the242* keyTyped methods on listener-a and listener-b.243* @param e the key event244*/245public void keyTyped(KeyEvent e) {246((KeyListener)a).keyTyped(e);247((KeyListener)b).keyTyped(e);248}249250/**251* Handles the keyPressed event by invoking the252* keyPressed methods on listener-a and listener-b.253* @param e the key event254*/255public void keyPressed(KeyEvent e) {256((KeyListener)a).keyPressed(e);257((KeyListener)b).keyPressed(e);258}259260/**261* Handles the keyReleased event by invoking the262* keyReleased methods on listener-a and listener-b.263* @param e the key event264*/265public void keyReleased(KeyEvent e) {266((KeyListener)a).keyReleased(e);267((KeyListener)b).keyReleased(e);268}269270/**271* Handles the mouseClicked event by invoking the272* mouseClicked methods on listener-a and listener-b.273* @param e the mouse event274*/275public void mouseClicked(MouseEvent e) {276((MouseListener)a).mouseClicked(e);277((MouseListener)b).mouseClicked(e);278}279280/**281* Handles the mousePressed event by invoking the282* mousePressed methods on listener-a and listener-b.283* @param e the mouse event284*/285public void mousePressed(MouseEvent e) {286((MouseListener)a).mousePressed(e);287((MouseListener)b).mousePressed(e);288}289290/**291* Handles the mouseReleased event by invoking the292* mouseReleased methods on listener-a and listener-b.293* @param e the mouse event294*/295public void mouseReleased(MouseEvent e) {296((MouseListener)a).mouseReleased(e);297((MouseListener)b).mouseReleased(e);298}299300/**301* Handles the mouseEntered event by invoking the302* mouseEntered methods on listener-a and listener-b.303* @param e the mouse event304*/305public void mouseEntered(MouseEvent e) {306((MouseListener)a).mouseEntered(e);307((MouseListener)b).mouseEntered(e);308}309310/**311* Handles the mouseExited event by invoking the312* mouseExited methods on listener-a and listener-b.313* @param e the mouse event314*/315public void mouseExited(MouseEvent e) {316((MouseListener)a).mouseExited(e);317((MouseListener)b).mouseExited(e);318}319320/**321* Handles the mouseDragged event by invoking the322* mouseDragged methods on listener-a and listener-b.323* @param e the mouse event324*/325public void mouseDragged(MouseEvent e) {326((MouseMotionListener)a).mouseDragged(e);327((MouseMotionListener)b).mouseDragged(e);328}329330/**331* Handles the mouseMoved event by invoking the332* mouseMoved methods on listener-a and listener-b.333* @param e the mouse event334*/335public void mouseMoved(MouseEvent e) {336((MouseMotionListener)a).mouseMoved(e);337((MouseMotionListener)b).mouseMoved(e);338}339340/**341* Handles the windowOpened event by invoking the342* windowOpened methods on listener-a and listener-b.343* @param e the window event344*/345public void windowOpened(WindowEvent e) {346((WindowListener)a).windowOpened(e);347((WindowListener)b).windowOpened(e);348}349350/**351* Handles the windowClosing event by invoking the352* windowClosing methods on listener-a and listener-b.353* @param e the window event354*/355public void windowClosing(WindowEvent e) {356((WindowListener)a).windowClosing(e);357((WindowListener)b).windowClosing(e);358}359360/**361* Handles the windowClosed event by invoking the362* windowClosed methods on listener-a and listener-b.363* @param e the window event364*/365public void windowClosed(WindowEvent e) {366((WindowListener)a).windowClosed(e);367((WindowListener)b).windowClosed(e);368}369370/**371* Handles the windowIconified event by invoking the372* windowIconified methods on listener-a and listener-b.373* @param e the window event374*/375public void windowIconified(WindowEvent e) {376((WindowListener)a).windowIconified(e);377((WindowListener)b).windowIconified(e);378}379380/**381* Handles the windowDeiconified event by invoking the382* windowDeiconified methods on listener-a and listener-b.383* @param e the window event384*/385public void windowDeiconified(WindowEvent e) {386((WindowListener)a).windowDeiconified(e);387((WindowListener)b).windowDeiconified(e);388}389390/**391* Handles the windowActivated event by invoking the392* windowActivated methods on listener-a and listener-b.393* @param e the window event394*/395public void windowActivated(WindowEvent e) {396((WindowListener)a).windowActivated(e);397((WindowListener)b).windowActivated(e);398}399400/**401* Handles the windowDeactivated event by invoking the402* windowDeactivated methods on listener-a and listener-b.403* @param e the window event404*/405public void windowDeactivated(WindowEvent e) {406((WindowListener)a).windowDeactivated(e);407((WindowListener)b).windowDeactivated(e);408}409410/**411* Handles the windowStateChanged event by invoking the412* windowStateChanged methods on listener-a and listener-b.413* @param e the window event414* @since 1.4415*/416public void windowStateChanged(WindowEvent e) {417((WindowStateListener)a).windowStateChanged(e);418((WindowStateListener)b).windowStateChanged(e);419}420421422/**423* Handles the windowGainedFocus event by invoking the windowGainedFocus424* methods on listener-a and listener-b.425* @param e the window event426* @since 1.4427*/428public void windowGainedFocus(WindowEvent e) {429((WindowFocusListener)a).windowGainedFocus(e);430((WindowFocusListener)b).windowGainedFocus(e);431}432433/**434* Handles the windowLostFocus event by invoking the windowLostFocus435* methods on listener-a and listener-b.436* @param e the window event437* @since 1.4438*/439public void windowLostFocus(WindowEvent e) {440((WindowFocusListener)a).windowLostFocus(e);441((WindowFocusListener)b).windowLostFocus(e);442}443444/**445* Handles the actionPerformed event by invoking the446* actionPerformed methods on listener-a and listener-b.447* @param e the action event448*/449public void actionPerformed(ActionEvent e) {450((ActionListener)a).actionPerformed(e);451((ActionListener)b).actionPerformed(e);452}453454/**455* Handles the itemStateChanged event by invoking the456* itemStateChanged methods on listener-a and listener-b.457* @param e the item event458*/459public void itemStateChanged(ItemEvent e) {460((ItemListener)a).itemStateChanged(e);461((ItemListener)b).itemStateChanged(e);462}463464/**465* Handles the adjustmentValueChanged event by invoking the466* adjustmentValueChanged methods on listener-a and listener-b.467* @param e the adjustment event468*/469public void adjustmentValueChanged(AdjustmentEvent e) {470((AdjustmentListener)a).adjustmentValueChanged(e);471((AdjustmentListener)b).adjustmentValueChanged(e);472}473public void textValueChanged(TextEvent e) {474((TextListener)a).textValueChanged(e);475((TextListener)b).textValueChanged(e);476}477478/**479* Handles the inputMethodTextChanged event by invoking the480* inputMethodTextChanged methods on listener-a and listener-b.481* @param e the item event482*/483public void inputMethodTextChanged(InputMethodEvent e) {484((InputMethodListener)a).inputMethodTextChanged(e);485((InputMethodListener)b).inputMethodTextChanged(e);486}487488/**489* Handles the caretPositionChanged event by invoking the490* caretPositionChanged methods on listener-a and listener-b.491* @param e the item event492*/493public void caretPositionChanged(InputMethodEvent e) {494((InputMethodListener)a).caretPositionChanged(e);495((InputMethodListener)b).caretPositionChanged(e);496}497498/**499* Handles the hierarchyChanged event by invoking the500* hierarchyChanged methods on listener-a and listener-b.501* @param e the item event502* @since 1.3503*/504public void hierarchyChanged(HierarchyEvent e) {505((HierarchyListener)a).hierarchyChanged(e);506((HierarchyListener)b).hierarchyChanged(e);507}508509/**510* Handles the ancestorMoved event by invoking the511* ancestorMoved methods on listener-a and listener-b.512* @param e the item event513* @since 1.3514*/515public void ancestorMoved(HierarchyEvent e) {516((HierarchyBoundsListener)a).ancestorMoved(e);517((HierarchyBoundsListener)b).ancestorMoved(e);518}519520/**521* Handles the ancestorResized event by invoking the522* ancestorResized methods on listener-a and listener-b.523* @param e the item event524* @since 1.3525*/526public void ancestorResized(HierarchyEvent e) {527((HierarchyBoundsListener)a).ancestorResized(e);528((HierarchyBoundsListener)b).ancestorResized(e);529}530531/**532* Handles the mouseWheelMoved event by invoking the533* mouseWheelMoved methods on listener-a and listener-b.534* @param e the mouse event535* @since 1.4536*/537public void mouseWheelMoved(MouseWheelEvent e) {538((MouseWheelListener)a).mouseWheelMoved(e);539((MouseWheelListener)b).mouseWheelMoved(e);540}541542/**543* Adds component-listener-a with component-listener-b and544* returns the resulting multicast listener.545* @param a component-listener-a546* @param b component-listener-b547* @return the resulting listener548*/549public static ComponentListener add(ComponentListener a, ComponentListener b) {550return (ComponentListener)addInternal(a, b);551}552553/**554* Adds container-listener-a with container-listener-b and555* returns the resulting multicast listener.556* @param a container-listener-a557* @param b container-listener-b558* @return the resulting listener559*/560public static ContainerListener add(ContainerListener a, ContainerListener b) {561return (ContainerListener)addInternal(a, b);562}563564/**565* Adds focus-listener-a with focus-listener-b and566* returns the resulting multicast listener.567* @param a focus-listener-a568* @param b focus-listener-b569* @return the resulting listener570*/571public static FocusListener add(FocusListener a, FocusListener b) {572return (FocusListener)addInternal(a, b);573}574575/**576* Adds key-listener-a with key-listener-b and577* returns the resulting multicast listener.578* @param a key-listener-a579* @param b key-listener-b580* @return the resulting listener581*/582public static KeyListener add(KeyListener a, KeyListener b) {583return (KeyListener)addInternal(a, b);584}585586/**587* Adds mouse-listener-a with mouse-listener-b and588* returns the resulting multicast listener.589* @param a mouse-listener-a590* @param b mouse-listener-b591* @return the resulting listener592*/593public static MouseListener add(MouseListener a, MouseListener b) {594return (MouseListener)addInternal(a, b);595}596597/**598* Adds mouse-motion-listener-a with mouse-motion-listener-b and599* returns the resulting multicast listener.600* @param a mouse-motion-listener-a601* @param b mouse-motion-listener-b602* @return the resulting listener603*/604public static MouseMotionListener add(MouseMotionListener a, MouseMotionListener b) {605return (MouseMotionListener)addInternal(a, b);606}607608/**609* Adds window-listener-a with window-listener-b and610* returns the resulting multicast listener.611* @param a window-listener-a612* @param b window-listener-b613* @return the resulting listener614*/615public static WindowListener add(WindowListener a, WindowListener b) {616return (WindowListener)addInternal(a, b);617}618619/**620* Adds window-state-listener-a with window-state-listener-b621* and returns the resulting multicast listener.622* @param a window-state-listener-a623* @param b window-state-listener-b624* @return the resulting listener625* @since 1.4626*/627@SuppressWarnings("overloads")628public static WindowStateListener add(WindowStateListener a,629WindowStateListener b) {630return (WindowStateListener)addInternal(a, b);631}632633/**634* Adds window-focus-listener-a with window-focus-listener-b635* and returns the resulting multicast listener.636* @param a window-focus-listener-a637* @param b window-focus-listener-b638* @return the resulting listener639* @since 1.4640*/641public static WindowFocusListener add(WindowFocusListener a,642WindowFocusListener b) {643return (WindowFocusListener)addInternal(a, b);644}645646/**647* Adds action-listener-a with action-listener-b and648* returns the resulting multicast listener.649* @param a action-listener-a650* @param b action-listener-b651* @return the resulting listener652*/653@SuppressWarnings("overloads")654public static ActionListener add(ActionListener a, ActionListener b) {655return (ActionListener)addInternal(a, b);656}657658/**659* Adds item-listener-a with item-listener-b and660* returns the resulting multicast listener.661* @param a item-listener-a662* @param b item-listener-b663* @return the resulting listener664*/665@SuppressWarnings("overloads")666public static ItemListener add(ItemListener a, ItemListener b) {667return (ItemListener)addInternal(a, b);668}669670/**671* Adds adjustment-listener-a with adjustment-listener-b and672* returns the resulting multicast listener.673* @param a adjustment-listener-a674* @param b adjustment-listener-b675* @return the resulting listener676*/677@SuppressWarnings("overloads")678public static AdjustmentListener add(AdjustmentListener a, AdjustmentListener b) {679return (AdjustmentListener)addInternal(a, b);680}681682/**683* Adds text-listener-a with text-listener-b and684* returns the resulting multicast listener.685*686* @param a text-listener-a687* @param b text-listener-b688* @return the resulting listener689*/690@SuppressWarnings("overloads")691public static TextListener add(TextListener a, TextListener b) {692return (TextListener)addInternal(a, b);693}694695/**696* Adds input-method-listener-a with input-method-listener-b and697* returns the resulting multicast listener.698* @param a input-method-listener-a699* @param b input-method-listener-b700* @return the resulting listener701*/702public static InputMethodListener add(InputMethodListener a, InputMethodListener b) {703return (InputMethodListener)addInternal(a, b);704}705706/**707* Adds hierarchy-listener-a with hierarchy-listener-b and708* returns the resulting multicast listener.709* @param a hierarchy-listener-a710* @param b hierarchy-listener-b711* @return the resulting listener712* @since 1.3713*/714@SuppressWarnings("overloads")715public static HierarchyListener add(HierarchyListener a, HierarchyListener b) {716return (HierarchyListener)addInternal(a, b);717}718719/**720* Adds hierarchy-bounds-listener-a with hierarchy-bounds-listener-b and721* returns the resulting multicast listener.722* @param a hierarchy-bounds-listener-a723* @param b hierarchy-bounds-listener-b724* @return the resulting listener725* @since 1.3726*/727public static HierarchyBoundsListener add(HierarchyBoundsListener a, HierarchyBoundsListener b) {728return (HierarchyBoundsListener)addInternal(a, b);729}730731/**732* Adds mouse-wheel-listener-a with mouse-wheel-listener-b and733* returns the resulting multicast listener.734* @param a mouse-wheel-listener-a735* @param b mouse-wheel-listener-b736* @return the resulting listener737* @since 1.4738*/739@SuppressWarnings("overloads")740public static MouseWheelListener add(MouseWheelListener a,741MouseWheelListener b) {742return (MouseWheelListener)addInternal(a, b);743}744745/**746* Removes the old component-listener from component-listener-l and747* returns the resulting multicast listener.748* @param l component-listener-l749* @param oldl the component-listener being removed750* @return the resulting listener751*/752public static ComponentListener remove(ComponentListener l, ComponentListener oldl) {753return (ComponentListener) removeInternal(l, oldl);754}755756/**757* Removes the old container-listener from container-listener-l and758* returns the resulting multicast listener.759* @param l container-listener-l760* @param oldl the container-listener being removed761* @return the resulting listener762*/763public static ContainerListener remove(ContainerListener l, ContainerListener oldl) {764return (ContainerListener) removeInternal(l, oldl);765}766767/**768* Removes the old focus-listener from focus-listener-l and769* returns the resulting multicast listener.770* @param l focus-listener-l771* @param oldl the focus-listener being removed772* @return the resulting listener773*/774public static FocusListener remove(FocusListener l, FocusListener oldl) {775return (FocusListener) removeInternal(l, oldl);776}777778/**779* Removes the old key-listener from key-listener-l and780* returns the resulting multicast listener.781* @param l key-listener-l782* @param oldl the key-listener being removed783* @return the resulting listener784*/785public static KeyListener remove(KeyListener l, KeyListener oldl) {786return (KeyListener) removeInternal(l, oldl);787}788789/**790* Removes the old mouse-listener from mouse-listener-l and791* returns the resulting multicast listener.792* @param l mouse-listener-l793* @param oldl the mouse-listener being removed794* @return the resulting listener795*/796public static MouseListener remove(MouseListener l, MouseListener oldl) {797return (MouseListener) removeInternal(l, oldl);798}799800/**801* Removes the old mouse-motion-listener from mouse-motion-listener-l802* and returns the resulting multicast listener.803* @param l mouse-motion-listener-l804* @param oldl the mouse-motion-listener being removed805* @return the resulting listener806*/807public static MouseMotionListener remove(MouseMotionListener l, MouseMotionListener oldl) {808return (MouseMotionListener) removeInternal(l, oldl);809}810811/**812* Removes the old window-listener from window-listener-l and813* returns the resulting multicast listener.814* @param l window-listener-l815* @param oldl the window-listener being removed816* @return the resulting listener817*/818public static WindowListener remove(WindowListener l, WindowListener oldl) {819return (WindowListener) removeInternal(l, oldl);820}821822/**823* Removes the old window-state-listener from window-state-listener-l824* and returns the resulting multicast listener.825* @param l window-state-listener-l826* @param oldl the window-state-listener being removed827* @return the resulting listener828* @since 1.4829*/830@SuppressWarnings("overloads")831public static WindowStateListener remove(WindowStateListener l,832WindowStateListener oldl) {833return (WindowStateListener) removeInternal(l, oldl);834}835836/**837* Removes the old window-focus-listener from window-focus-listener-l838* and returns the resulting multicast listener.839* @param l window-focus-listener-l840* @param oldl the window-focus-listener being removed841* @return the resulting listener842* @since 1.4843*/844public static WindowFocusListener remove(WindowFocusListener l,845WindowFocusListener oldl) {846return (WindowFocusListener) removeInternal(l, oldl);847}848849/**850* Removes the old action-listener from action-listener-l and851* returns the resulting multicast listener.852* @param l action-listener-l853* @param oldl the action-listener being removed854* @return the resulting listener855*/856@SuppressWarnings("overloads")857public static ActionListener remove(ActionListener l, ActionListener oldl) {858return (ActionListener) removeInternal(l, oldl);859}860861/**862* Removes the old item-listener from item-listener-l and863* returns the resulting multicast listener.864* @param l item-listener-l865* @param oldl the item-listener being removed866* @return the resulting listener867*/868@SuppressWarnings("overloads")869public static ItemListener remove(ItemListener l, ItemListener oldl) {870return (ItemListener) removeInternal(l, oldl);871}872873/**874* Removes the old adjustment-listener from adjustment-listener-l and875* returns the resulting multicast listener.876* @param l adjustment-listener-l877* @param oldl the adjustment-listener being removed878* @return the resulting listener879*/880@SuppressWarnings("overloads")881public static AdjustmentListener remove(AdjustmentListener l, AdjustmentListener oldl) {882return (AdjustmentListener) removeInternal(l, oldl);883}884885/**886* Removes the old text-listener from text-listener-l and887* returns the resulting multicast listener.888*889* @param l text-listener-l890* @param oldl the text-listener being removed891* @return the resulting listener892*/893@SuppressWarnings("overloads")894public static TextListener remove(TextListener l, TextListener oldl) {895return (TextListener) removeInternal(l, oldl);896}897898/**899* Removes the old input-method-listener from input-method-listener-l and900* returns the resulting multicast listener.901* @param l input-method-listener-l902* @param oldl the input-method-listener being removed903* @return the resulting listener904*/905public static InputMethodListener remove(InputMethodListener l, InputMethodListener oldl) {906return (InputMethodListener) removeInternal(l, oldl);907}908909/**910* Removes the old hierarchy-listener from hierarchy-listener-l and911* returns the resulting multicast listener.912* @param l hierarchy-listener-l913* @param oldl the hierarchy-listener being removed914* @return the resulting listener915* @since 1.3916*/917@SuppressWarnings("overloads")918public static HierarchyListener remove(HierarchyListener l, HierarchyListener oldl) {919return (HierarchyListener) removeInternal(l, oldl);920}921922/**923* Removes the old hierarchy-bounds-listener from924* hierarchy-bounds-listener-l and returns the resulting multicast925* listener.926* @param l hierarchy-bounds-listener-l927* @param oldl the hierarchy-bounds-listener being removed928* @return the resulting listener929* @since 1.3930*/931public static HierarchyBoundsListener remove(HierarchyBoundsListener l, HierarchyBoundsListener oldl) {932return (HierarchyBoundsListener) removeInternal(l, oldl);933}934935/**936* Removes the old mouse-wheel-listener from mouse-wheel-listener-l937* and returns the resulting multicast listener.938* @param l mouse-wheel-listener-l939* @param oldl the mouse-wheel-listener being removed940* @return the resulting listener941* @since 1.4942*/943@SuppressWarnings("overloads")944public static MouseWheelListener remove(MouseWheelListener l,945MouseWheelListener oldl) {946return (MouseWheelListener) removeInternal(l, oldl);947}948949/**950* Returns the resulting multicast listener from adding listener-a951* and listener-b together.952* If listener-a is null, it returns listener-b;953* If listener-b is null, it returns listener-a954* If neither are null, then it creates and returns955* a new AWTEventMulticaster instance which chains a with b.956* @param a event listener-a957* @param b event listener-b958* @return the resulting listener959*/960protected static EventListener addInternal(EventListener a, EventListener b) {961if (a == null) return b;962if (b == null) return a;963return new AWTEventMulticaster(a, b);964}965966/**967* Returns the resulting multicast listener after removing the968* old listener from listener-l.969* If listener-l equals the old listener OR listener-l is null,970* returns null.971* Else if listener-l is an instance of AWTEventMulticaster,972* then it removes the old listener from it.973* Else, returns listener l.974* @param l the listener being removed from975* @param oldl the listener being removed976* @return the resulting listener977*/978protected static EventListener removeInternal(EventListener l, EventListener oldl) {979if (l == oldl || l == null) {980return null;981} else if (l instanceof AWTEventMulticaster) {982return ((AWTEventMulticaster)l).remove(oldl);983} else {984return l; // it's not here985}986}987988989/**990* Serialization support. Saves all Serializable listeners991* to a serialization stream.992*993* @param s the stream to save to994* @param k a prefix stream to put before each serializable listener995* @throws IOException if serialization fails996*/997protected void saveInternal(ObjectOutputStream s, String k) throws IOException {998if (a instanceof AWTEventMulticaster) {999((AWTEventMulticaster)a).saveInternal(s, k);1000}1001else if (a instanceof Serializable) {1002s.writeObject(k);1003s.writeObject(a);1004}10051006if (b instanceof AWTEventMulticaster) {1007((AWTEventMulticaster)b).saveInternal(s, k);1008}1009else if (b instanceof Serializable) {1010s.writeObject(k);1011s.writeObject(b);1012}1013}10141015/**1016* Saves a Serializable listener chain to a serialization stream.1017*1018* @param s the stream to save to1019* @param k a prefix stream to put before each serializable listener1020* @param l the listener chain to save1021* @throws IOException if serialization fails1022*/1023protected static void save(ObjectOutputStream s, String k, EventListener l) throws IOException {1024if (l == null) {1025return;1026}1027else if (l instanceof AWTEventMulticaster) {1028((AWTEventMulticaster)l).saveInternal(s, k);1029}1030else if (l instanceof Serializable) {1031s.writeObject(k);1032s.writeObject(l);1033}1034}10351036/*1037* Recursive method which returns a count of the number of listeners in1038* EventListener, handling the (common) case of l actually being an1039* AWTEventMulticaster. Additionally, only listeners of type listenerType1040* are counted. Method modified to fix bug 4513402. -bchristi1041*/1042private static int getListenerCount(EventListener l, Class<?> listenerType) {1043if (l instanceof AWTEventMulticaster) {1044AWTEventMulticaster mc = (AWTEventMulticaster)l;1045return getListenerCount(mc.a, listenerType) +1046getListenerCount(mc.b, listenerType);1047}1048else {1049// Only count listeners of correct type1050return listenerType.isInstance(l) ? 1 : 0;1051}1052}10531054/*1055* Recursive method which populates EventListener array a with EventListeners1056* from l. l is usually an AWTEventMulticaster. Bug 4513402 revealed that1057* if l differed in type from the element type of a, an ArrayStoreException1058* would occur. Now l is only inserted into a if it's of the appropriate1059* type. -bchristi1060*/1061private static int populateListenerArray(EventListener[] a, EventListener l, int index) {1062if (l instanceof AWTEventMulticaster) {1063AWTEventMulticaster mc = (AWTEventMulticaster)l;1064int lhs = populateListenerArray(a, mc.a, index);1065return populateListenerArray(a, mc.b, lhs);1066}1067else if (a.getClass().getComponentType().isInstance(l)) {1068a[index] = l;1069return index + 1;1070}1071// Skip nulls, instances of wrong class1072else {1073return index;1074}1075}10761077/**1078* Returns an array of all the objects chained as1079* <code><em>Foo</em>Listener</code>s by the specified1080* {@code java.util.EventListener}.1081* <code><em>Foo</em>Listener</code>s are chained by the1082* {@code AWTEventMulticaster} using the1083* <code>add<em>Foo</em>Listener</code> method.1084* If a {@code null} listener is specified, this method returns an1085* empty array. If the specified listener is not an instance of1086* {@code AWTEventMulticaster}, this method returns an array which1087* contains only the specified listener. If no such listeners are chained,1088* this method returns an empty array.1089*1090* @param <T> the listener type1091* @param l the specified {@code java.util.EventListener}1092* @param listenerType the type of listeners requested; this parameter1093* should specify an interface that descends from1094* {@code java.util.EventListener}1095* @return an array of all objects chained as1096* <code><em>Foo</em>Listener</code>s by the specified multicast1097* listener, or an empty array if no such listeners have been1098* chained by the specified multicast listener1099* @exception NullPointerException if the specified1100* {@code listenertype} parameter is {@code null}1101* @exception ClassCastException if {@code listenerType}1102* doesn't specify a class or interface that implements1103* {@code java.util.EventListener}1104*1105* @since 1.41106*/1107@SuppressWarnings("unchecked")1108public static <T extends EventListener> T[]1109getListeners(EventListener l, Class<T> listenerType)1110{1111if (listenerType == null) {1112throw new NullPointerException ("Listener type should not be null");1113}11141115int n = getListenerCount(l, listenerType);1116T[] result = (T[])Array.newInstance(listenerType, n);1117populateListenerArray(result, l, 0);1118return result;1119}1120}112111221123