1 7 package java.awt; 8 9 import java.awt.event.*; 10 import java.lang.reflect.Array ; 11 import java.util.EventListener ; 12 import java.io.Serializable ; 13 import java.io.ObjectOutputStream ; 14 import java.io.IOException ; 15 import java.util.EventListener ; 16 17 18 56 57 public class AWTEventMulticaster implements 58 ComponentListener, ContainerListener, FocusListener, KeyListener, 59 MouseListener, MouseMotionListener, WindowListener, WindowFocusListener, 60 WindowStateListener, ActionListener, ItemListener, AdjustmentListener, 61 TextListener, InputMethodListener, HierarchyListener, 62 HierarchyBoundsListener, MouseWheelListener { 63 64 protected final EventListener a, b; 65 66 75 protected AWTEventMulticaster(EventListener a, EventListener b) { 76 this.a = a; this.b = b; 77 } 78 79 84 protected EventListener remove(EventListener oldl) { 85 if (oldl == a) return b; 86 if (oldl == b) return a; 87 EventListener a2 = removeInternal(a, oldl); 88 EventListener b2 = removeInternal(b, oldl); 89 if (a2 == a && b2 == b) { 90 return this; } 92 return addInternal(a2, b2); 93 } 94 95 100 public void componentResized(ComponentEvent e) { 101 ((ComponentListener)a).componentResized(e); 102 ((ComponentListener)b).componentResized(e); 103 } 104 105 110 public void componentMoved(ComponentEvent e) { 111 ((ComponentListener)a).componentMoved(e); 112 ((ComponentListener)b).componentMoved(e); 113 } 114 115 120 public void componentShown(ComponentEvent e) { 121 ((ComponentListener)a).componentShown(e); 122 ((ComponentListener)b).componentShown(e); 123 } 124 125 130 public void componentHidden(ComponentEvent e) { 131 ((ComponentListener)a).componentHidden(e); 132 ((ComponentListener)b).componentHidden(e); 133 } 134 135 140 public void componentAdded(ContainerEvent e) { 141 ((ContainerListener)a).componentAdded(e); 142 ((ContainerListener)b).componentAdded(e); 143 } 144 145 150 public void componentRemoved(ContainerEvent e) { 151 ((ContainerListener)a).componentRemoved(e); 152 ((ContainerListener)b).componentRemoved(e); 153 } 154 155 160 public void focusGained(FocusEvent e) { 161 ((FocusListener)a).focusGained(e); 162 ((FocusListener)b).focusGained(e); 163 } 164 165 170 public void focusLost(FocusEvent e) { 171 ((FocusListener)a).focusLost(e); 172 ((FocusListener)b).focusLost(e); 173 } 174 175 180 public void keyTyped(KeyEvent e) { 181 ((KeyListener)a).keyTyped(e); 182 ((KeyListener)b).keyTyped(e); 183 } 184 185 190 public void keyPressed(KeyEvent e) { 191 ((KeyListener)a).keyPressed(e); 192 ((KeyListener)b).keyPressed(e); 193 } 194 195 200 public void keyReleased(KeyEvent e) { 201 ((KeyListener)a).keyReleased(e); 202 ((KeyListener)b).keyReleased(e); 203 } 204 205 210 public void mouseClicked(MouseEvent e) { 211 ((MouseListener)a).mouseClicked(e); 212 ((MouseListener)b).mouseClicked(e); 213 } 214 215 220 public void mousePressed(MouseEvent e) { 221 ((MouseListener)a).mousePressed(e); 222 ((MouseListener)b).mousePressed(e); 223 } 224 225 230 public void mouseReleased(MouseEvent e) { 231 ((MouseListener)a).mouseReleased(e); 232 ((MouseListener)b).mouseReleased(e); 233 } 234 235 240 public void mouseEntered(MouseEvent e) { 241 ((MouseListener)a).mouseEntered(e); 242 ((MouseListener)b).mouseEntered(e); 243 } 244 245 250 public void mouseExited(MouseEvent e) { 251 ((MouseListener)a).mouseExited(e); 252 ((MouseListener)b).mouseExited(e); 253 } 254 255 260 public void mouseDragged(MouseEvent e) { 261 ((MouseMotionListener)a).mouseDragged(e); 262 ((MouseMotionListener)b).mouseDragged(e); 263 } 264 265 270 public void mouseMoved(MouseEvent e) { 271 ((MouseMotionListener)a).mouseMoved(e); 272 ((MouseMotionListener)b).mouseMoved(e); 273 } 274 275 280 public void windowOpened(WindowEvent e) { 281 ((WindowListener)a).windowOpened(e); 282 ((WindowListener)b).windowOpened(e); 283 } 284 285 290 public void windowClosing(WindowEvent e) { 291 ((WindowListener)a).windowClosing(e); 292 ((WindowListener)b).windowClosing(e); 293 } 294 295 300 public void windowClosed(WindowEvent e) { 301 ((WindowListener)a).windowClosed(e); 302 ((WindowListener)b).windowClosed(e); 303 } 304 305 310 public void windowIconified(WindowEvent e) { 311 ((WindowListener)a).windowIconified(e); 312 ((WindowListener)b).windowIconified(e); 313 } 314 315 320 public void windowDeiconified(WindowEvent e) { 321 ((WindowListener)a).windowDeiconified(e); 322 ((WindowListener)b).windowDeiconified(e); 323 } 324 325 330 public void windowActivated(WindowEvent e) { 331 ((WindowListener)a).windowActivated(e); 332 ((WindowListener)b).windowActivated(e); 333 } 334 335 340 public void windowDeactivated(WindowEvent e) { 341 ((WindowListener)a).windowDeactivated(e); 342 ((WindowListener)b).windowDeactivated(e); 343 } 344 345 350 public void windowStateChanged(WindowEvent e) { 351 ((WindowStateListener)a).windowStateChanged(e); 352 ((WindowStateListener)b).windowStateChanged(e); 353 } 354 355 356 361 public void windowGainedFocus(WindowEvent e) { 362 ((WindowFocusListener)a).windowGainedFocus(e); 363 ((WindowFocusListener)b).windowGainedFocus(e); 364 } 365 366 371 public void windowLostFocus(WindowEvent e) { 372 ((WindowFocusListener)a).windowLostFocus(e); 373 ((WindowFocusListener)b).windowLostFocus(e); 374 } 375 376 381 public void actionPerformed(ActionEvent e) { 382 ((ActionListener)a).actionPerformed(e); 383 ((ActionListener)b).actionPerformed(e); 384 } 385 386 391 public void itemStateChanged(ItemEvent e) { 392 ((ItemListener)a).itemStateChanged(e); 393 ((ItemListener)b).itemStateChanged(e); 394 } 395 396 401 public void adjustmentValueChanged(AdjustmentEvent e) { 402 ((AdjustmentListener)a).adjustmentValueChanged(e); 403 ((AdjustmentListener)b).adjustmentValueChanged(e); 404 } 405 public void textValueChanged(TextEvent e) { 406 ((TextListener)a).textValueChanged(e); 407 ((TextListener)b).textValueChanged(e); 408 } 409 410 415 public void inputMethodTextChanged(InputMethodEvent e) { 416 ((InputMethodListener)a).inputMethodTextChanged(e); 417 ((InputMethodListener)b).inputMethodTextChanged(e); 418 } 419 420 425 public void caretPositionChanged(InputMethodEvent e) { 426 ((InputMethodListener)a).caretPositionChanged(e); 427 ((InputMethodListener)b).caretPositionChanged(e); 428 } 429 430 435 public void hierarchyChanged(HierarchyEvent e) { 436 ((HierarchyListener)a).hierarchyChanged(e); 437 ((HierarchyListener)b).hierarchyChanged(e); 438 } 439 440 445 public void ancestorMoved(HierarchyEvent e) { 446 ((HierarchyBoundsListener)a).ancestorMoved(e); 447 ((HierarchyBoundsListener)b).ancestorMoved(e); 448 } 449 450 455 public void ancestorResized(HierarchyEvent e) { 456 ((HierarchyBoundsListener)a).ancestorResized(e); 457 ((HierarchyBoundsListener)b).ancestorResized(e); 458 } 459 460 466 public void mouseWheelMoved(MouseWheelEvent e) { 467 ((MouseWheelListener)a).mouseWheelMoved(e); 468 ((MouseWheelListener)b).mouseWheelMoved(e); 469 } 470 471 477 public static ComponentListener add(ComponentListener a, ComponentListener b) { 478 return (ComponentListener)addInternal(a, b); 479 } 480 481 487 public static ContainerListener add(ContainerListener a, ContainerListener b) { 488 return (ContainerListener)addInternal(a, b); 489 } 490 491 497 public static FocusListener add(FocusListener a, FocusListener b) { 498 return (FocusListener)addInternal(a, b); 499 } 500 501 507 public static KeyListener add(KeyListener a, KeyListener b) { 508 return (KeyListener)addInternal(a, b); 509 } 510 511 517 public static MouseListener add(MouseListener a, MouseListener b) { 518 return (MouseListener)addInternal(a, b); 519 } 520 521 527 public static MouseMotionListener add(MouseMotionListener a, MouseMotionListener b) { 528 return (MouseMotionListener)addInternal(a, b); 529 } 530 531 537 public static WindowListener add(WindowListener a, WindowListener b) { 538 return (WindowListener)addInternal(a, b); 539 } 540 541 547 public static WindowStateListener add(WindowStateListener a, 548 WindowStateListener b) { 549 return (WindowStateListener)addInternal(a, b); 550 } 551 552 558 public static WindowFocusListener add(WindowFocusListener a, 559 WindowFocusListener b) { 560 return (WindowFocusListener)addInternal(a, b); 561 } 562 563 569 public static ActionListener add(ActionListener a, ActionListener b) { 570 return (ActionListener)addInternal(a, b); 571 } 572 573 579 public static ItemListener add(ItemListener a, ItemListener b) { 580 return (ItemListener)addInternal(a, b); 581 } 582 583 589 public static AdjustmentListener add(AdjustmentListener a, AdjustmentListener b) { 590 return (AdjustmentListener)addInternal(a, b); 591 } 592 public static TextListener add(TextListener a, TextListener b) { 593 return (TextListener)addInternal(a, b); 594 } 595 596 602 public static InputMethodListener add(InputMethodListener a, InputMethodListener b) { 603 return (InputMethodListener)addInternal(a, b); 604 } 605 606 612 public static HierarchyListener add(HierarchyListener a, HierarchyListener b) { 613 return (HierarchyListener)addInternal(a, b); 614 } 615 616 622 public static HierarchyBoundsListener add(HierarchyBoundsListener a, HierarchyBoundsListener b) { 623 return (HierarchyBoundsListener)addInternal(a, b); 624 } 625 626 633 public static MouseWheelListener add(MouseWheelListener a, 634 MouseWheelListener b) { 635 return (MouseWheelListener)addInternal(a, b); 636 } 637 638 644 public static ComponentListener remove(ComponentListener l, ComponentListener oldl) { 645 return (ComponentListener) removeInternal(l, oldl); 646 } 647 648 654 public static ContainerListener remove(ContainerListener l, ContainerListener oldl) { 655 return (ContainerListener) removeInternal(l, oldl); 656 } 657 658 664 public static FocusListener remove(FocusListener l, FocusListener oldl) { 665 return (FocusListener) removeInternal(l, oldl); 666 } 667 668 674 public static KeyListener remove(KeyListener l, KeyListener oldl) { 675 return (KeyListener) removeInternal(l, oldl); 676 } 677 678 684 public static MouseListener remove(MouseListener l, MouseListener oldl) { 685 return (MouseListener) removeInternal(l, oldl); 686 } 687 688 694 public static MouseMotionListener remove(MouseMotionListener l, MouseMotionListener oldl) { 695 return (MouseMotionListener) removeInternal(l, oldl); 696 } 697 698 704 public static WindowListener remove(WindowListener l, WindowListener oldl) { 705 return (WindowListener) removeInternal(l, oldl); 706 } 707 708 714 public static WindowStateListener remove(WindowStateListener l, 715 WindowStateListener oldl) { 716 return (WindowStateListener) removeInternal(l, oldl); 717 } 718 719 725 public static WindowFocusListener remove(WindowFocusListener l, 726 WindowFocusListener oldl) { 727 return (WindowFocusListener) removeInternal(l, oldl); 728 } 729 730 736 public static ActionListener remove(ActionListener l, ActionListener oldl) { 737 return (ActionListener) removeInternal(l, oldl); 738 } 739 740 746 public static ItemListener remove(ItemListener l, ItemListener oldl) { 747 return (ItemListener) removeInternal(l, oldl); 748 } 749 750 756 public static AdjustmentListener remove(AdjustmentListener l, AdjustmentListener oldl) { 757 return (AdjustmentListener) removeInternal(l, oldl); 758 } 759 public static TextListener remove(TextListener l, TextListener oldl) { 760 return (TextListener) removeInternal(l, oldl); 761 } 762 763 769 public static InputMethodListener remove(InputMethodListener l, InputMethodListener oldl) { 770 return (InputMethodListener) removeInternal(l, oldl); 771 } 772 773 779 public static HierarchyListener remove(HierarchyListener l, HierarchyListener oldl) { 780 return (HierarchyListener) removeInternal(l, oldl); 781 } 782 783 790 public static HierarchyBoundsListener remove(HierarchyBoundsListener l, HierarchyBoundsListener oldl) { 791 return (HierarchyBoundsListener) removeInternal(l, oldl); 792 } 793 794 801 public static MouseWheelListener remove(MouseWheelListener l, 802 MouseWheelListener oldl) { 803 return (MouseWheelListener) removeInternal(l, oldl); 804 } 805 806 816 protected static EventListener addInternal(EventListener a, EventListener b) { 817 if (a == null) return b; 818 if (b == null) return a; 819 return new AWTEventMulticaster (a, b); 820 } 821 822 833 protected static EventListener removeInternal(EventListener l, EventListener oldl) { 834 if (l == oldl || l == null) { 835 return null; 836 } else if (l instanceof AWTEventMulticaster ) { 837 return ((AWTEventMulticaster )l).remove(oldl); 838 } else { 839 return l; } 841 } 842 843 844 846 847 protected void saveInternal(ObjectOutputStream s, String k) throws IOException { 848 if (a instanceof AWTEventMulticaster ) { 849 ((AWTEventMulticaster )a).saveInternal(s, k); 850 } 851 else if (a instanceof Serializable ) { 852 s.writeObject(k); 853 s.writeObject(a); 854 } 855 856 if (b instanceof AWTEventMulticaster ) { 857 ((AWTEventMulticaster )b).saveInternal(s, k); 858 } 859 else if (b instanceof Serializable ) { 860 s.writeObject(k); 861 s.writeObject(b); 862 } 863 } 864 865 protected static void save(ObjectOutputStream s, String k, EventListener l) throws IOException { 866 if (l == null) { 867 return; 868 } 869 else if (l instanceof AWTEventMulticaster ) { 870 ((AWTEventMulticaster )l).saveInternal(s, k); 871 } 872 else if (l instanceof Serializable ) { 873 s.writeObject(k); 874 s.writeObject(l); 875 } 876 } 877 878 884 private static int getListenerCount(EventListener l, Class listenerType) { 885 if (l instanceof AWTEventMulticaster ) { 886 AWTEventMulticaster mc = (AWTEventMulticaster )l; 887 return getListenerCount(mc.a, listenerType) + 888 getListenerCount(mc.b, listenerType); 889 } 890 else { 891 return listenerType.isInstance(l) ? 1 : 0; 893 } 894 } 895 896 903 private static int populateListenerArray(EventListener [] a, EventListener l, int index) { 904 if (l instanceof AWTEventMulticaster ) { 905 AWTEventMulticaster mc = (AWTEventMulticaster )l; 906 int lhs = populateListenerArray(a, mc.a, index); 907 return populateListenerArray(a, mc.b, lhs); 908 } 909 else if (a.getClass().getComponentType().isInstance(l)) { 910 a[index] = l; 911 return index + 1; 912 } 913 else { 915 return index; 916 } 917 } 918 919 946 public static <T extends EventListener > T[] 947 getListeners(EventListener l, Class <T> listenerType) 948 { 949 int n = getListenerCount(l, listenerType); 950 T[] result = (T[])Array.newInstance(listenerType, n); 951 populateListenerArray(result, l, 0); 952 return result; 953 } 954 } 955 | Popular Tags |