AWTEventMulticaster.java 33.8 KB
Newer Older
1 2
/* AWTEventMulticaster.java -- allows multicast chaining of listeners
   Copyright (C) 1999, 2000, 2002 Free Software Foundation
Bryce McKinlay committed
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */
Bryce McKinlay committed
37 38 39 40


package java.awt;

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.HierarchyBoundsListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.io.IOException;
Bryce McKinlay committed
72
import java.io.ObjectOutputStream;
73 74 75 76
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.EventListener;
Bryce McKinlay committed
77 78

/**
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
 * This class is used to implement a chain of event handlers.  Dispatching
 * using this class is thread safe.  Here is a quick example of how to
 * add and delete listeners using this class.  For this example, we will
 * assume are firing <code>AdjustmentEvent</code>'s.  However, this
 * same approach is useful for all events in the <code>java.awt.event</code>
 * package, and more if this class is subclassed.
 *
 * <p><code>
 * AdjustmentListener al;
 * public void addAdjustmentListener(AdjustmentListener listener)
 * {
 *   al = AWTEventMulticaster.add(al, listener);
 * }
 * public void removeAdjustmentListener(AdjustmentListener listener)
 * {
 *   al = AWTEventMulticaster.remove(al, listener);
 * }
 * </code>
 *
 * <p>When it come time to process an event, simply call <code>al</code>,
 * assuming it is not <code>null</code>, and all listeners in the chain will
 * be fired.
 *
 * <p>The first time <code>add</code> is called it is passed
 * <code>null</code> and <code>listener</code> as its arguments.  This
 * starts building the chain.  This class returns <code>listener</code>
 * which becomes the new <code>al</code>.  The next time, <code>add</code>
 * is called with <code>al</code> and <code>listener</code> and the
 * new listener is then chained to the old.
 *
 * @author Bryce McKinlay
 * @author Aaron M. Renn <arenn@urbanophile.com>
 * @author Eric Blake <ebb9@email.byu.edu>
 * @since 1.1
 * @status updated to 1.4
 */
public class AWTEventMulticaster
  implements ComponentListener, ContainerListener, FocusListener, KeyListener,
             MouseListener, MouseMotionListener, WindowListener,
             WindowFocusListener, WindowStateListener, ActionListener,
             ItemListener, AdjustmentListener, TextListener,
             InputMethodListener, HierarchyListener, HierarchyBoundsListener,
             MouseWheelListener
Bryce McKinlay committed
122
{
123 124 125 126 127 128
  /**
   * A variable in the event chain.
   */
  protected final EventListener a;

  /**
129
   * A variable in the event chain.
130 131 132 133 134
   */
  protected final EventListener b;

  /**
   * Initializes a new instance of <code>AWTEventMulticaster</code> with
135 136 137
   * the specified event listener parameters. The parameters should not be
   * null, although it is not required to enforce this with a
   * NullPointerException.
138
   *
139 140
   * @param a the "a" listener object
   * @param b the "b" listener object
141
   */
142
  protected AWTEventMulticaster(EventListener a, EventListener b)
Bryce McKinlay committed
143 144 145 146 147
  {
    this.a = a;
    this.b = b;
  }

148
  /**
149 150 151
   * Removes one instance of the specified listener from this multicaster
   * chain. This descends recursively if either child is a multicaster, and
   * returns a multicaster chain with the old listener removed.
152
   *
153 154
   * @param oldl the object to remove from this multicaster
   * @return the resulting multicaster with the specified listener removed
155
   */
Bryce McKinlay committed
156 157 158 159 160 161 162
  protected EventListener remove(EventListener oldl)
  {
    // If oldl is an immediate child, return the other child.
    if (a == oldl)
      return b;
    if (b == oldl)
      return a;
163
    // If a and/or b are Multicaster's, search them recursively.
Bryce McKinlay committed
164 165
    if (a instanceof AWTEventMulticaster)
      {
166 167 168 169
        EventListener newa = ((AWTEventMulticaster) a).remove(oldl);
        if (newa != a)
          return new AWTEventMulticaster(newa, b);
      }
Bryce McKinlay committed
170 171
    if (b instanceof AWTEventMulticaster)
      {
172 173 174
        EventListener newb = ((AWTEventMulticaster) b).remove(oldl);
        if (newb != b)
          return new AWTEventMulticaster(a, newb);
Bryce McKinlay committed
175 176 177 178 179
      }
    // oldl was not found.
    return this;
  }

180
  /**
181 182
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
183
   *
184
   * @param event the event to handle
185
   */
186
  public void componentResized(ComponentEvent e)
Bryce McKinlay committed
187
  {
188 189
    ((ComponentListener) a).componentResized(e);
    ((ComponentListener) b).componentResized(e);
Bryce McKinlay committed
190
  }
191 192

  /**
193 194
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
195
   *
196
   * @param event the event to handle
197
   */
198
  public void componentMoved(ComponentEvent e)
Bryce McKinlay committed
199
  {
200 201 202
    ((ComponentListener) a).componentMoved(e);
    ((ComponentListener) b).componentMoved(e);
  }
203 204

  /**
205 206
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
207
   *
208
   * @param event the event to handle
209
   */
210
  public void componentShown(ComponentEvent e)
Bryce McKinlay committed
211
  {
212 213
    ((ComponentListener) a).componentShown(e);
    ((ComponentListener) b).componentShown(e);
Bryce McKinlay committed
214
  }
215 216

  /**
217 218
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
219
   *
220
   * @param event the event to handle
221
   */
222
  public void componentHidden(ComponentEvent e)
Bryce McKinlay committed
223
  {
224 225
    ((ComponentListener) a).componentHidden(e);
    ((ComponentListener) b).componentHidden(e);
Bryce McKinlay committed
226
  }
227 228

  /**
229 230
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
231
   *
232
   * @param event the event to handle
233
   */
234
  public void componentAdded(ContainerEvent e)
Bryce McKinlay committed
235
  {
236 237
    ((ContainerListener) a).componentAdded(e);
    ((ContainerListener) b).componentAdded(e);
Bryce McKinlay committed
238
  }
239 240

  /**
241 242
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
243
   *
244
   * @param event the event to handle
245
   */
246
  public void componentRemoved(ContainerEvent e)
Bryce McKinlay committed
247
  {
248 249
    ((ContainerListener) a).componentRemoved(e);
    ((ContainerListener) b).componentRemoved(e);
Bryce McKinlay committed
250
  }
251 252

  /**
253 254
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
255
   *
256
   * @param event the event to handle
257
   */
258
  public void focusGained(FocusEvent e)
Bryce McKinlay committed
259
  {
260 261
    ((FocusListener) a).focusGained(e);
    ((FocusListener) b).focusGained(e);
Bryce McKinlay committed
262 263
  }

264
  /**
265 266
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
267
   *
268
   * @param event the event to handle
269
   */
270
  public void focusLost(FocusEvent e)
Bryce McKinlay committed
271
  {
272 273
    ((FocusListener) a).focusLost(e);
    ((FocusListener) b).focusLost(e);
Bryce McKinlay committed
274
  }
275 276

  /**
277 278
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
279
   *
280
   * @param event the event to handle
281
   */
282
  public void keyTyped(KeyEvent e)
Bryce McKinlay committed
283
  {
284 285
    ((KeyListener) a).keyTyped(e);
    ((KeyListener) b).keyTyped(e);
Bryce McKinlay committed
286
  }
287 288

  /**
289 290
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
291
   *
292
   * @param event the event to handle
293
   */
294
  public void keyPressed(KeyEvent e)
Bryce McKinlay committed
295
  {
296 297
    ((KeyListener) a).keyPressed(e);
    ((KeyListener) b).keyPressed(e);
Bryce McKinlay committed
298
  }
299 300

  /**
301 302
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
303
   *
304
   * @param event the event to handle
305
   */
306
  public void keyReleased(KeyEvent e)
Bryce McKinlay committed
307
  {
308 309
    ((KeyListener) a).keyReleased(e);
    ((KeyListener) b).keyReleased(e);
Bryce McKinlay committed
310
  }
311 312

  /**
313 314
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
315
   *
316
   * @param event the event to handle
317
   */
318
  public void mouseClicked(MouseEvent e)
Bryce McKinlay committed
319
  {
320 321
    ((MouseListener) a).mouseClicked(e);
    ((MouseListener) b).mouseClicked(e);
Bryce McKinlay committed
322
  }
323 324

  /**
325 326
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
327
   *
328
   * @param event the event to handle
329
   */
330
  public void mousePressed(MouseEvent e)
Bryce McKinlay committed
331
  {
332 333
    ((MouseListener) a).mousePressed(e);
    ((MouseListener) b).mousePressed(e);
Bryce McKinlay committed
334 335
  }

336
  /**
337 338
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
339
   *
340
   * @param event the event to handle
341
   */
342
  public void mouseReleased(MouseEvent e)
Bryce McKinlay committed
343
  {
344 345
    ((MouseListener) a).mouseReleased(e);
    ((MouseListener) b).mouseReleased(e);
Bryce McKinlay committed
346 347
  }

348
  /**
349 350
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
351
   *
352
   * @param event the event to handle
353
   */
354
  public void mouseEntered(MouseEvent e)
Bryce McKinlay committed
355
  {
356 357
    ((MouseListener) a).mouseEntered(e);
    ((MouseListener) b).mouseEntered(e);
Bryce McKinlay committed
358 359
  }

360
  /**
361 362
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
363
   *
364
   * @param event the event to handle
365
   */
366
  public void mouseExited(MouseEvent e)
Bryce McKinlay committed
367
  {
368 369
    ((MouseListener) a).mouseExited(e);
    ((MouseListener) b).mouseExited(e);
Bryce McKinlay committed
370 371
  }

372
  /**
373 374
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
375
   *
376
   * @param event the event to handle
377
   */
378
  public void mouseDragged(MouseEvent e)
Bryce McKinlay committed
379
  {
380 381
    ((MouseMotionListener) a).mouseDragged(e);
    ((MouseMotionListener) b).mouseDragged(e);
Bryce McKinlay committed
382 383
  }

384
  /**
385 386
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
387
   *
388
   * @param event the event to handle
389
   */
390
  public void mouseMoved(MouseEvent e)
Bryce McKinlay committed
391
  {
392 393
    ((MouseMotionListener) a).mouseMoved(e);
    ((MouseMotionListener) b).mouseMoved(e);
Bryce McKinlay committed
394 395
  }

396
  /**
397 398
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
399
   *
400
   * @param event the event to handle
401
   */
402
  public void windowOpened(WindowEvent e)
Bryce McKinlay committed
403
  {
404 405
    ((WindowListener) a).windowOpened(e);
    ((WindowListener) b).windowOpened(e);
Bryce McKinlay committed
406 407
  }

408
  /**
409 410
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
411
   *
412
   * @param event the event to handle
413
   */
414
  public void windowClosing(WindowEvent e)
Bryce McKinlay committed
415
  {
416 417
    ((WindowListener) a).windowClosing(e);
    ((WindowListener) b).windowClosing(e);
Bryce McKinlay committed
418 419
  }

420
  /**
421 422
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
423
   *
424
   * @param event the event to handle
425
   */
426
  public void windowClosed(WindowEvent e)
Bryce McKinlay committed
427
  {
428 429
    ((WindowListener) a).windowClosed(e);
    ((WindowListener) b).windowClosed(e);
Bryce McKinlay committed
430 431
  }

432
  /**
433 434
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
435
   *
436
   * @param event the event to handle
437
   */
438
  public void windowIconified(WindowEvent e)
Bryce McKinlay committed
439
  {
440 441
    ((WindowListener) a).windowIconified(e);
    ((WindowListener) b).windowIconified(e);
Bryce McKinlay committed
442 443
  }

444 445 446 447
  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
448
   * @param event the event to handle
449
   */
450
  public void windowDeiconified(WindowEvent e)
Bryce McKinlay committed
451
  {
452 453
    ((WindowListener) a).windowDeiconified(e);
    ((WindowListener) b).windowDeiconified(e);
Bryce McKinlay committed
454
  }
455 456 457 458 459

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
460
   * @param event the event to handle
461
   */
462
  public void windowActivated(WindowEvent e)
Bryce McKinlay committed
463
  {
464 465
    ((WindowListener) a).windowActivated(e);
    ((WindowListener) b).windowActivated(e);
Bryce McKinlay committed
466
  }
467 468 469 470 471

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
472
   * @param event the event to handle
473
   */
474
  public void windowDeactivated(WindowEvent e)
Bryce McKinlay committed
475
  {
476 477
    ((WindowListener) a).windowDeactivated(e);
    ((WindowListener) b).windowDeactivated(e);
Bryce McKinlay committed
478
  }
479 480 481 482 483

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
484 485
   * @param event the event to handle
   * @since 1.4
486
   */
487
  public void windowStateChanged(WindowEvent e)
Bryce McKinlay committed
488
  {
489 490
    ((WindowStateListener) a).windowStateChanged(e);
    ((WindowStateListener) b).windowStateChanged(e);
Bryce McKinlay committed
491
  }
492 493 494 495 496

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
497 498
   * @param event the event to handle
   * @since 1.4
499
   */
500
  public void windowGainedFocus(WindowEvent e)
Bryce McKinlay committed
501
  {
502 503
    ((WindowFocusListener) a).windowGainedFocus(e);
    ((WindowFocusListener) b).windowGainedFocus(e);
Bryce McKinlay committed
504
  }
505 506 507 508 509

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
510 511
   * @param event the event to handle
   * @since 1.4
512
   */
513
  public void windowLostFocus(WindowEvent e)
Bryce McKinlay committed
514
  {
515 516
    ((WindowFocusListener) a).windowLostFocus(e);
    ((WindowFocusListener) b).windowLostFocus(e);
Bryce McKinlay committed
517
  }
518 519 520 521 522

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
523
   * @param event the event to handle
524
   */
525
  public void actionPerformed(ActionEvent e)
Bryce McKinlay committed
526
  {
527 528
    ((ActionListener) a).actionPerformed(e);
    ((ActionListener) b).actionPerformed(e);
Bryce McKinlay committed
529 530
  }

531 532 533 534
  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
535
   * @param event the event to handle
536
   */
537
  public void itemStateChanged(ItemEvent e)
Bryce McKinlay committed
538
  {
539 540
    ((ItemListener) a).itemStateChanged(e);
    ((ItemListener) b).itemStateChanged(e);
Bryce McKinlay committed
541
  }
542 543 544 545 546

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
547
   * @param event the event to handle
548
   */
549
  public void adjustmentValueChanged(AdjustmentEvent e)
Bryce McKinlay committed
550
  {
551 552
    ((AdjustmentListener) a).adjustmentValueChanged(e);
    ((AdjustmentListener) b).adjustmentValueChanged(e);
Bryce McKinlay committed
553
  }
554 555 556 557 558

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
559
   * @param event the event to handle
560
   */
561
  public void textValueChanged(TextEvent e)
Bryce McKinlay committed
562
  {
563 564
    ((TextListener) a).textValueChanged(e);
    ((TextListener) b).textValueChanged(e);
Bryce McKinlay committed
565 566
  }

567 568 569 570
  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
571 572
   * @param event the event to handle
   * @since 1.2
573
   */
574
  public void inputMethodTextChanged(InputMethodEvent e)
Bryce McKinlay committed
575
  {
576 577
    ((InputMethodListener) a).inputMethodTextChanged(e);
    ((InputMethodListener) b).inputMethodTextChanged(e);
Bryce McKinlay committed
578
  }
579 580 581 582 583

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
584 585
   * @param event the event to handle
   * @since 1.2
586
   */
587
  public void caretPositionChanged(InputMethodEvent e)
Bryce McKinlay committed
588
  {
589 590
    ((InputMethodListener) a).caretPositionChanged(e);
    ((InputMethodListener) b).caretPositionChanged(e);
Bryce McKinlay committed
591
  }
592 593 594 595 596

  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
597 598
   * @param event the event to handle
   * @since 1.3
599
   */
600
  public void hierarchyChanged(HierarchyEvent e)
Bryce McKinlay committed
601 602 603 604 605
  {
    ((HierarchyListener) a).hierarchyChanged(e);
    ((HierarchyListener) b).hierarchyChanged(e);
  }

606 607 608 609
  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
610 611
   * @param event the event to handle
   * @since 1.3
612
   */
613
  public void ancestorMoved(HierarchyEvent e)
Bryce McKinlay committed
614
  {
615 616
    ((HierarchyBoundsListener) a).ancestorMoved(e);
    ((HierarchyBoundsListener) b).ancestorMoved(e);
Bryce McKinlay committed
617 618
  }

619 620 621 622
  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
623 624
   * @param event the event to handle
   * @since 1.3
625
   */
626
  public void ancestorResized(HierarchyEvent e)
Bryce McKinlay committed
627
  {
628 629
    ((HierarchyBoundsListener) a).ancestorResized(e);
    ((HierarchyBoundsListener) b).ancestorResized(e);
Bryce McKinlay committed
630 631
  }

632 633 634 635
  /**
   * Handles this event by dispatching it to the "a" and "b" listener
   * instances.
   *
636 637
   * @param event the event to handle
   * @since 1.4
638
   */
639
  public void mouseWheelMoved(MouseWheelEvent e)
Bryce McKinlay committed
640
  {
641 642 643
    ((MouseWheelListener) a).mouseWheelMoved(e);
    ((MouseWheelListener) b).mouseWheelMoved(e);
  }
Bryce McKinlay committed
644

645
  /**
646
   * Chain <code>ComponentListener</code> a and b.
647
   *
648 649 650
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
651
   */
652
  public static ComponentListener add(ComponentListener a, ComponentListener b)
Bryce McKinlay committed
653
  {
654
    return (ComponentListener) addInternal(a, b);
Bryce McKinlay committed
655
  }
656 657

  /**
658
   * Chain <code>ContainerListener</code> a and b.
659
   *
660 661 662
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
663
   */
664
  public static ContainerListener add(ContainerListener a, ContainerListener b)
Bryce McKinlay committed
665
  {
666
    return (ContainerListener) addInternal(a, b);
Bryce McKinlay committed
667
  }
668 669

  /**
670
   * Chain <code>FocusListener</code> a and b.
671
   *
672 673 674
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
675
   */
676
  public static FocusListener add(FocusListener a, FocusListener b)
Bryce McKinlay committed
677
  {
678
    return (FocusListener) addInternal(a, b);
Bryce McKinlay committed
679
  }
680 681

  /**
682
   * Chain <code>KeyListener</code> a and b.
683
   *
684 685 686
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
687
   */
688
  public static KeyListener add(KeyListener a, KeyListener b)
Bryce McKinlay committed
689
  {
690
    return (KeyListener) addInternal(a, b);
Bryce McKinlay committed
691
  }
692 693

  /**
694
   * Chain <code>MouseListener</code> a and b.
695
   *
696 697 698
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
699
   */
700
  public static MouseListener add(MouseListener a, MouseListener b)
Bryce McKinlay committed
701
  {
702
    return (MouseListener) addInternal(a, b);
Bryce McKinlay committed
703
  }
704 705

  /**
706
   * Chain <code>MouseMotionListener</code> a and b.
707
   *
708 709 710
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
711
   */
712 713
  public static MouseMotionListener add(MouseMotionListener a,
                                        MouseMotionListener b)
Bryce McKinlay committed
714
  {
715
    return (MouseMotionListener) addInternal(a, b);
Bryce McKinlay committed
716 717
  }

718
  /**
719
   * Chain <code>WindowListener</code> a and b.
720
   *
721 722 723
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null 
   * @return latest entry in the chain
724
   */
725
  public static WindowListener add(WindowListener a, WindowListener b)
Bryce McKinlay committed
726
  {
727
    return (WindowListener) addInternal(a, b);
Bryce McKinlay committed
728
  }
729 730

  /**
731
   * Chain <code>WindowStateListener</code> a and b.
732
   *
733 734 735 736
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null 
   * @return latest entry in the chain
   * @since 1.4
737
   */
738 739
  public static WindowStateListener add(WindowStateListener a,
                                        WindowStateListener b)
Bryce McKinlay committed
740
  {
741
    return (WindowStateListener) addInternal(a, b);
Bryce McKinlay committed
742
  }
743 744

  /**
745
   * Chain <code>WindowFocusListener</code> a and b.
746
   *
747 748 749 750
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null 
   * @return latest entry in the chain
   * @since 1.4
751
   */
752 753
  public static WindowFocusListener add(WindowFocusListener a,
                                        WindowFocusListener b)
Bryce McKinlay committed
754
  {
755
    return (WindowFocusListener) addInternal(a, b);
Bryce McKinlay committed
756 757
  }

758
  /**
759
   * Chain <code>ActionListener</code> a and b.
760
   *
761 762 763
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
764
   */
765
  public static ActionListener add(ActionListener a, ActionListener b)
Bryce McKinlay committed
766
  {
767
    return (ActionListener) addInternal(a, b);
Bryce McKinlay committed
768
  }
769 770

  /**
771
   * Chain <code>ItemListener</code> a and b.
772
   *
773 774 775
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
776
   */
777
  public static ItemListener add(ItemListener a, ItemListener b)
Bryce McKinlay committed
778
  {
779
    return (ItemListener) addInternal(a, b);
Bryce McKinlay committed
780
  }
781 782

  /**
783
   * Chain <code>AdjustmentListener</code> a and b.
784
   *
785 786 787
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
788
   */
789 790
  public static AdjustmentListener add(AdjustmentListener a,
                                       AdjustmentListener b)
Bryce McKinlay committed
791
  {
792
    return (AdjustmentListener) addInternal(a, b);
Bryce McKinlay committed
793
  }
794 795

  /**
796
   * Chain <code>AdjustmentListener</code> a and b.
797
   *
798 799 800
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
801
   */
802
  public static TextListener add(TextListener a, TextListener b)
Bryce McKinlay committed
803
  {
804
    return (TextListener) addInternal(a, b);
Bryce McKinlay committed
805
  }
806 807

  /**
808
   * Chain <code>InputMethodListener</code> a and b.
809
   *
810 811 812 813
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
   * @since 1.2
814
   */
815 816
  public static InputMethodListener add(InputMethodListener a,
                                        InputMethodListener b)
Bryce McKinlay committed
817
  {
818
    return (InputMethodListener) addInternal(a, b);
Bryce McKinlay committed
819
  }
820 821

  /**
822
   * Chain <code>HierarchyListener</code> a and b.
823
   *
824 825 826 827
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
   * @since 1.3
828
   */
829
  public static HierarchyListener add(HierarchyListener a, HierarchyListener b)
Bryce McKinlay committed
830
  {
831
    return (HierarchyListener) addInternal(a, b);
Bryce McKinlay committed
832
  }
833 834

  /**
835
   * Chain <code>HierarchyBoundsListener</code> a and b.
836
   *
837 838 839 840
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
   * @since 1.3
841
   */
842 843
  public static HierarchyBoundsListener add(HierarchyBoundsListener a,
                                            HierarchyBoundsListener b)
Bryce McKinlay committed
844
  {
845
    return (HierarchyBoundsListener) addInternal(a, b);
Bryce McKinlay committed
846
  }
847 848

  /**
849
   * Chain <code>MouseWheelListener</code> a and b.
850
   *
851 852 853 854
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null
   * @return latest entry in the chain
   * @since 1.4
855
   */
856 857
  public static MouseWheelListener add(MouseWheelListener a,
                                       MouseWheelListener b)
Bryce McKinlay committed
858
  {
859
    return (MouseWheelListener) addInternal(a, b);
Bryce McKinlay committed
860
  }
861 862

  /**
863
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
864
   *
865 866 867
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
868
   */
869 870
  public static ComponentListener remove(ComponentListener l,
                                         ComponentListener oldl)
Bryce McKinlay committed
871
  {
872
    return (ComponentListener) removeInternal(l, oldl);
Bryce McKinlay committed
873
  }
874

875 876 877 878 879 880 881 882 883
  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static ContainerListener remove(ContainerListener l,
                                         ContainerListener oldl)
Bryce McKinlay committed
884
  {
885
    return (ContainerListener) removeInternal(l, oldl);
Bryce McKinlay committed
886 887
  }

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static FocusListener remove(FocusListener l, FocusListener oldl)
  {
    return (FocusListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static KeyListener remove(KeyListener l, KeyListener oldl)
  {
    return (KeyListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static MouseListener remove(MouseListener l, MouseListener oldl)
  {
    return (MouseListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static MouseMotionListener remove(MouseMotionListener l,
                                           MouseMotionListener oldl)
  {
    return (MouseMotionListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static WindowListener remove(WindowListener l, WindowListener oldl)
  {
    return (WindowListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   * @since 1.4
   */
  public static WindowStateListener remove(WindowStateListener l,
                                           WindowStateListener oldl)
  {
    return (WindowStateListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   * @since 1.4
   */
  public static WindowFocusListener remove(WindowFocusListener l,
                                           WindowFocusListener oldl)
  {
    return (WindowFocusListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static ActionListener remove(ActionListener l, ActionListener oldl)
  {
    return (ActionListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static ItemListener remove(ItemListener l, ItemListener oldl)
  {
    return (ItemListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static AdjustmentListener remove(AdjustmentListener l,
                                          AdjustmentListener oldl)
  {
    return (AdjustmentListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  public static TextListener remove(TextListener l, TextListener oldl)
  {
    return (TextListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   * @since 1.2
   */
  public static InputMethodListener remove(InputMethodListener l,
                                           InputMethodListener oldl)
  {
    return (InputMethodListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   * @since 1.3
   */
  public static HierarchyListener remove(HierarchyListener l,
                                         HierarchyListener oldl)
  {
    return (HierarchyListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   * @since 1.3
   */
  public static HierarchyBoundsListener remove(HierarchyBoundsListener l,
                                               HierarchyBoundsListener oldl)
  {
    return (HierarchyBoundsListener) removeInternal(l, oldl);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   * @since 1.4
   */
  public static MouseWheelListener remove(MouseWheelListener l,
                                          MouseWheelListener oldl)
  {
    return (MouseWheelListener) removeInternal(l, oldl);
  }

  /**
   * Chain <code>EventListener</code> a and b.
   *
   * @param a the "a" listener, may be null
   * @param b the "b" listener, may be null 
   * @return latest entry in the chain
   */
  protected static EventListener addInternal(EventListener a, EventListener b)
  {
    if (a == null)
      return b;
    if (b == null)
      return a;
    return new AWTEventMulticaster(a, b);
  }

  /**
   * Removes the listener <code>oldl</code> from the listener <code>l</code>.
   *
   * @param l the listener chain to reduce
   * @param oldl the listener to remove
   * @return the resulting listener chain
   */
  protected static EventListener removeInternal(EventListener l,
                                                EventListener oldl)
  {
    if (l == oldl)
      return null;
    if (l instanceof AWTEventMulticaster)
      return ((AWTEventMulticaster) l).remove(oldl);
    return l;
  }

  /**
   * Saves all Serializable listeners to a serialization stream.
   *
   * @param s the stream to save to
   * @param k a prefix stream put before each serializable listener
   * @throws IOException if serialization fails
   */
Bryce McKinlay committed
1122
  protected void saveInternal(ObjectOutputStream s, String k)
1123
    throws IOException
Bryce McKinlay committed
1124
  {
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    // This is not documented by Sun, but I think it is correct.
    if (a instanceof AWTEventMulticaster)
      ((AWTEventMulticaster) a).saveInternal(s, k);
    else if (a instanceof Serializable)
      {
        s.writeObject(k);
        s.writeObject(a);
      }
    if (b instanceof AWTEventMulticaster)
      ((AWTEventMulticaster) b).saveInternal(s, k);
    else if (b instanceof Serializable)
      {
        s.writeObject(k);
        s.writeObject(b);
      }
  }

  /**
   * Saves a Serializable listener chain to a serialization stream.
   *
   * @param s the stream to save to
   * @param k a prefix stream put before each serializable listener
   * @param l the listener chain to save
   * @throws IOException if serialization fails
   */
  protected static void save(ObjectOutputStream s, String k, EventListener l)
    throws IOException
  {
    // This is not documented by Sun, but I think it is correct.
    if (l instanceof AWTEventMulticaster)
      ((AWTEventMulticaster) l).saveInternal(s, k);
    else if (l instanceof Serializable)
      {
        s.writeObject(k);
        s.writeObject(l);
      }
  }

  /**
   * Returns an array of all chained listeners of the specified type in the
   * given chain. A null listener returns an empty array, and a listener
   * which is not an AWTEventMulticaster returns an array of one element. If
   * no listeners in the chain are of the specified type, an empty array is
   * returned.
   *
   * @param l the listener chain to convert to an array
   * @param type the type of listeners to collect
   * @return an array of the listeners of that type in the chain
   * @throws ClassCastException if type is not assignable from EventListener
   * @throws NullPointerException if type is null
   * @throws IllegalArgumentException if type is Void.TYPE
   * @since 1.4
   */
  public static EventListener[] getListeners(EventListener l, Class type)
  {
    ArrayList list = new ArrayList();
    if (l instanceof AWTEventMulticaster)
      ((AWTEventMulticaster) l).getListeners(list, type);
    else if (type.isInstance(l))
      list.add(l);
    EventListener[] r = (EventListener[]) Array.newInstance(type, list.size());
    list.toArray(r);
    return r;
  }

  /**
   * Collects all instances of the given type in the chain into the list.
   *
   * @param l the list to collect into
   * @param type the type of listeners to collect
   * @throws NullPointerException if type is null
   * @see #getListeners(EventListener, Class)
   */
  private void getListeners(ArrayList l, Class type)
  {
    if (a instanceof AWTEventMulticaster)
      ((AWTEventMulticaster) a).getListeners(l, type);
    else if (type.isInstance(a))
      l.add(a);
    if (b instanceof AWTEventMulticaster)
      ((AWTEventMulticaster) b).getListeners(l, type);
    else if (type.isInstance(b))
      l.add(b);
Bryce McKinlay committed
1208
  }
1209
} // class AWTEventMulticaster