93 * {@code remove(EventListener)} method.
94 * </ul>
95 * <p>Swing makes use of
96 * {@link javax.swing.event.EventListenerList EventListenerList} for
97 * similar logic. Refer to it for details.
98 *
99 * @see javax.swing.event.EventListenerList
100 *
101 * @author John Rose
102 * @author Amy Fowler
103 * @since 1.1
104 */
105
106 public class AWTEventMulticaster implements
107 ComponentListener, ContainerListener, FocusListener, KeyListener,
108 MouseListener, MouseMotionListener, WindowListener, WindowFocusListener,
109 WindowStateListener, ActionListener, ItemListener, AdjustmentListener,
110 TextListener, InputMethodListener, HierarchyListener,
111 HierarchyBoundsListener, MouseWheelListener {
112
113 protected final EventListener a, b;
114
115 /**
116 * Creates an event multicaster instance which chains listener-a
117 * with listener-b. Input parameters <code>a</code> and <code>b</code>
118 * should not be <code>null</code>, though implementations may vary in
119 * choosing whether or not to throw <code>NullPointerException</code>
120 * in that case.
121 * @param a listener-a
122 * @param b listener-b
123 */
124 protected AWTEventMulticaster(EventListener a, EventListener b) {
125 this.a = a; this.b = b;
126 }
127
128 /**
129 * Removes a listener from this multicaster.
130 * <p>
131 * The returned multicaster contains all the listeners in this
132 * multicaster with the exception of all occurrences of {@code oldl}.
133 * If the resulting multicaster contains only one regular listener
520 ((HierarchyBoundsListener)a).ancestorResized(e);
521 ((HierarchyBoundsListener)b).ancestorResized(e);
522 }
523
524 /**
525 * Handles the mouseWheelMoved event by invoking the
526 * mouseWheelMoved methods on listener-a and listener-b.
527 * @param e the mouse event
528 * @since 1.4
529 */
530 public void mouseWheelMoved(MouseWheelEvent e) {
531 ((MouseWheelListener)a).mouseWheelMoved(e);
532 ((MouseWheelListener)b).mouseWheelMoved(e);
533 }
534
535 /**
536 * Adds component-listener-a with component-listener-b and
537 * returns the resulting multicast listener.
538 * @param a component-listener-a
539 * @param b component-listener-b
540 */
541 public static ComponentListener add(ComponentListener a, ComponentListener b) {
542 return (ComponentListener)addInternal(a, b);
543 }
544
545 /**
546 * Adds container-listener-a with container-listener-b and
547 * returns the resulting multicast listener.
548 * @param a container-listener-a
549 * @param b container-listener-b
550 */
551 public static ContainerListener add(ContainerListener a, ContainerListener b) {
552 return (ContainerListener)addInternal(a, b);
553 }
554
555 /**
556 * Adds focus-listener-a with focus-listener-b and
557 * returns the resulting multicast listener.
558 * @param a focus-listener-a
559 * @param b focus-listener-b
560 */
561 public static FocusListener add(FocusListener a, FocusListener b) {
562 return (FocusListener)addInternal(a, b);
563 }
564
565 /**
566 * Adds key-listener-a with key-listener-b and
567 * returns the resulting multicast listener.
568 * @param a key-listener-a
569 * @param b key-listener-b
570 */
571 public static KeyListener add(KeyListener a, KeyListener b) {
572 return (KeyListener)addInternal(a, b);
573 }
574
575 /**
576 * Adds mouse-listener-a with mouse-listener-b and
577 * returns the resulting multicast listener.
578 * @param a mouse-listener-a
579 * @param b mouse-listener-b
580 */
581 public static MouseListener add(MouseListener a, MouseListener b) {
582 return (MouseListener)addInternal(a, b);
583 }
584
585 /**
586 * Adds mouse-motion-listener-a with mouse-motion-listener-b and
587 * returns the resulting multicast listener.
588 * @param a mouse-motion-listener-a
589 * @param b mouse-motion-listener-b
590 */
591 public static MouseMotionListener add(MouseMotionListener a, MouseMotionListener b) {
592 return (MouseMotionListener)addInternal(a, b);
593 }
594
595 /**
596 * Adds window-listener-a with window-listener-b and
597 * returns the resulting multicast listener.
598 * @param a window-listener-a
599 * @param b window-listener-b
600 */
601 public static WindowListener add(WindowListener a, WindowListener b) {
602 return (WindowListener)addInternal(a, b);
603 }
604
605 /**
606 * Adds window-state-listener-a with window-state-listener-b
607 * and returns the resulting multicast listener.
608 * @param a window-state-listener-a
609 * @param b window-state-listener-b
610 * @since 1.4
611 */
612 @SuppressWarnings("overloads")
613 public static WindowStateListener add(WindowStateListener a,
614 WindowStateListener b) {
615 return (WindowStateListener)addInternal(a, b);
616 }
617
618 /**
619 * Adds window-focus-listener-a with window-focus-listener-b
620 * and returns the resulting multicast listener.
621 * @param a window-focus-listener-a
622 * @param b window-focus-listener-b
623 * @since 1.4
624 */
625 public static WindowFocusListener add(WindowFocusListener a,
626 WindowFocusListener b) {
627 return (WindowFocusListener)addInternal(a, b);
628 }
629
630 /**
631 * Adds action-listener-a with action-listener-b and
632 * returns the resulting multicast listener.
633 * @param a action-listener-a
634 * @param b action-listener-b
635 */
636 @SuppressWarnings("overloads")
637 public static ActionListener add(ActionListener a, ActionListener b) {
638 return (ActionListener)addInternal(a, b);
639 }
640
641 /**
642 * Adds item-listener-a with item-listener-b and
643 * returns the resulting multicast listener.
644 * @param a item-listener-a
645 * @param b item-listener-b
646 */
647 @SuppressWarnings("overloads")
648 public static ItemListener add(ItemListener a, ItemListener b) {
649 return (ItemListener)addInternal(a, b);
650 }
651
652 /**
653 * Adds adjustment-listener-a with adjustment-listener-b and
654 * returns the resulting multicast listener.
655 * @param a adjustment-listener-a
656 * @param b adjustment-listener-b
657 */
658 @SuppressWarnings("overloads")
659 public static AdjustmentListener add(AdjustmentListener a, AdjustmentListener b) {
660 return (AdjustmentListener)addInternal(a, b);
661 }
662 @SuppressWarnings("overloads")
663 public static TextListener add(TextListener a, TextListener b) {
664 return (TextListener)addInternal(a, b);
665 }
666
667 /**
668 * Adds input-method-listener-a with input-method-listener-b and
669 * returns the resulting multicast listener.
670 * @param a input-method-listener-a
671 * @param b input-method-listener-b
672 */
673 public static InputMethodListener add(InputMethodListener a, InputMethodListener b) {
674 return (InputMethodListener)addInternal(a, b);
675 }
676
677 /**
678 * Adds hierarchy-listener-a with hierarchy-listener-b and
679 * returns the resulting multicast listener.
680 * @param a hierarchy-listener-a
681 * @param b hierarchy-listener-b
682 * @since 1.3
683 */
684 @SuppressWarnings("overloads")
685 public static HierarchyListener add(HierarchyListener a, HierarchyListener b) {
686 return (HierarchyListener)addInternal(a, b);
687 }
688
689 /**
690 * Adds hierarchy-bounds-listener-a with hierarchy-bounds-listener-b and
691 * returns the resulting multicast listener.
692 * @param a hierarchy-bounds-listener-a
693 * @param b hierarchy-bounds-listener-b
694 * @since 1.3
695 */
696 public static HierarchyBoundsListener add(HierarchyBoundsListener a, HierarchyBoundsListener b) {
697 return (HierarchyBoundsListener)addInternal(a, b);
698 }
699
700 /**
701 * Adds mouse-wheel-listener-a with mouse-wheel-listener-b and
702 * returns the resulting multicast listener.
703 * @param a mouse-wheel-listener-a
704 * @param b mouse-wheel-listener-b
705 * @since 1.4
706 */
707 @SuppressWarnings("overloads")
708 public static MouseWheelListener add(MouseWheelListener a,
709 MouseWheelListener b) {
710 return (MouseWheelListener)addInternal(a, b);
711 }
712
713 /**
714 * Removes the old component-listener from component-listener-l and
715 * returns the resulting multicast listener.
716 * @param l component-listener-l
717 * @param oldl the component-listener being removed
718 */
719 public static ComponentListener remove(ComponentListener l, ComponentListener oldl) {
720 return (ComponentListener) removeInternal(l, oldl);
721 }
722
723 /**
724 * Removes the old container-listener from container-listener-l and
725 * returns the resulting multicast listener.
726 * @param l container-listener-l
727 * @param oldl the container-listener being removed
728 */
729 public static ContainerListener remove(ContainerListener l, ContainerListener oldl) {
730 return (ContainerListener) removeInternal(l, oldl);
731 }
732
733 /**
734 * Removes the old focus-listener from focus-listener-l and
735 * returns the resulting multicast listener.
736 * @param l focus-listener-l
737 * @param oldl the focus-listener being removed
738 */
739 public static FocusListener remove(FocusListener l, FocusListener oldl) {
740 return (FocusListener) removeInternal(l, oldl);
741 }
742
743 /**
744 * Removes the old key-listener from key-listener-l and
745 * returns the resulting multicast listener.
746 * @param l key-listener-l
747 * @param oldl the key-listener being removed
748 */
749 public static KeyListener remove(KeyListener l, KeyListener oldl) {
750 return (KeyListener) removeInternal(l, oldl);
751 }
752
753 /**
754 * Removes the old mouse-listener from mouse-listener-l and
755 * returns the resulting multicast listener.
756 * @param l mouse-listener-l
757 * @param oldl the mouse-listener being removed
758 */
759 public static MouseListener remove(MouseListener l, MouseListener oldl) {
760 return (MouseListener) removeInternal(l, oldl);
761 }
762
763 /**
764 * Removes the old mouse-motion-listener from mouse-motion-listener-l
765 * and returns the resulting multicast listener.
766 * @param l mouse-motion-listener-l
767 * @param oldl the mouse-motion-listener being removed
768 */
769 public static MouseMotionListener remove(MouseMotionListener l, MouseMotionListener oldl) {
770 return (MouseMotionListener) removeInternal(l, oldl);
771 }
772
773 /**
774 * Removes the old window-listener from window-listener-l and
775 * returns the resulting multicast listener.
776 * @param l window-listener-l
777 * @param oldl the window-listener being removed
778 */
779 public static WindowListener remove(WindowListener l, WindowListener oldl) {
780 return (WindowListener) removeInternal(l, oldl);
781 }
782
783 /**
784 * Removes the old window-state-listener from window-state-listener-l
785 * and returns the resulting multicast listener.
786 * @param l window-state-listener-l
787 * @param oldl the window-state-listener being removed
788 * @since 1.4
789 */
790 @SuppressWarnings("overloads")
791 public static WindowStateListener remove(WindowStateListener l,
792 WindowStateListener oldl) {
793 return (WindowStateListener) removeInternal(l, oldl);
794 }
795
796 /**
797 * Removes the old window-focus-listener from window-focus-listener-l
798 * and returns the resulting multicast listener.
799 * @param l window-focus-listener-l
800 * @param oldl the window-focus-listener being removed
801 * @since 1.4
802 */
803 public static WindowFocusListener remove(WindowFocusListener l,
804 WindowFocusListener oldl) {
805 return (WindowFocusListener) removeInternal(l, oldl);
806 }
807
808 /**
809 * Removes the old action-listener from action-listener-l and
810 * returns the resulting multicast listener.
811 * @param l action-listener-l
812 * @param oldl the action-listener being removed
813 */
814 @SuppressWarnings("overloads")
815 public static ActionListener remove(ActionListener l, ActionListener oldl) {
816 return (ActionListener) removeInternal(l, oldl);
817 }
818
819 /**
820 * Removes the old item-listener from item-listener-l and
821 * returns the resulting multicast listener.
822 * @param l item-listener-l
823 * @param oldl the item-listener being removed
824 */
825 @SuppressWarnings("overloads")
826 public static ItemListener remove(ItemListener l, ItemListener oldl) {
827 return (ItemListener) removeInternal(l, oldl);
828 }
829
830 /**
831 * Removes the old adjustment-listener from adjustment-listener-l and
832 * returns the resulting multicast listener.
833 * @param l adjustment-listener-l
834 * @param oldl the adjustment-listener being removed
835 */
836 @SuppressWarnings("overloads")
837 public static AdjustmentListener remove(AdjustmentListener l, AdjustmentListener oldl) {
838 return (AdjustmentListener) removeInternal(l, oldl);
839 }
840 @SuppressWarnings("overloads")
841 public static TextListener remove(TextListener l, TextListener oldl) {
842 return (TextListener) removeInternal(l, oldl);
843 }
844
845 /**
846 * Removes the old input-method-listener from input-method-listener-l and
847 * returns the resulting multicast listener.
848 * @param l input-method-listener-l
849 * @param oldl the input-method-listener being removed
850 */
851 public static InputMethodListener remove(InputMethodListener l, InputMethodListener oldl) {
852 return (InputMethodListener) removeInternal(l, oldl);
853 }
854
855 /**
856 * Removes the old hierarchy-listener from hierarchy-listener-l and
857 * returns the resulting multicast listener.
858 * @param l hierarchy-listener-l
859 * @param oldl the hierarchy-listener being removed
860 * @since 1.3
861 */
862 @SuppressWarnings("overloads")
863 public static HierarchyListener remove(HierarchyListener l, HierarchyListener oldl) {
864 return (HierarchyListener) removeInternal(l, oldl);
865 }
866
867 /**
868 * Removes the old hierarchy-bounds-listener from
869 * hierarchy-bounds-listener-l and returns the resulting multicast
870 * listener.
871 * @param l hierarchy-bounds-listener-l
872 * @param oldl the hierarchy-bounds-listener being removed
873 * @since 1.3
874 */
875 public static HierarchyBoundsListener remove(HierarchyBoundsListener l, HierarchyBoundsListener oldl) {
876 return (HierarchyBoundsListener) removeInternal(l, oldl);
877 }
878
879 /**
880 * Removes the old mouse-wheel-listener from mouse-wheel-listener-l
881 * and returns the resulting multicast listener.
882 * @param l mouse-wheel-listener-l
883 * @param oldl the mouse-wheel-listener being removed
884 * @since 1.4
885 */
886 @SuppressWarnings("overloads")
887 public static MouseWheelListener remove(MouseWheelListener l,
888 MouseWheelListener oldl) {
889 return (MouseWheelListener) removeInternal(l, oldl);
890 }
891
892 /**
893 * Returns the resulting multicast listener from adding listener-a
894 * and listener-b together.
895 * If listener-a is null, it returns listener-b;
896 * If listener-b is null, it returns listener-a
897 * If neither are null, then it creates and returns
898 * a new AWTEventMulticaster instance which chains a with b.
899 * @param a event listener-a
900 * @param b event listener-b
901 */
902 protected static EventListener addInternal(EventListener a, EventListener b) {
903 if (a == null) return b;
904 if (b == null) return a;
905 return new AWTEventMulticaster(a, b);
906 }
907
908 /**
909 * Returns the resulting multicast listener after removing the
910 * old listener from listener-l.
911 * If listener-l equals the old listener OR listener-l is null,
912 * returns null.
913 * Else if listener-l is an instance of AWTEventMulticaster,
914 * then it removes the old listener from it.
915 * Else, returns listener l.
916 * @param l the listener being removed from
917 * @param oldl the listener being removed
918 */
919 protected static EventListener removeInternal(EventListener l, EventListener oldl) {
920 if (l == oldl || l == null) {
921 return null;
922 } else if (l instanceof AWTEventMulticaster) {
923 return ((AWTEventMulticaster)l).remove(oldl);
924 } else {
925 return l; // it's not here
926 }
927 }
928
929
930 /* Serialization support.
931 */
932
933 protected void saveInternal(ObjectOutputStream s, String k) throws IOException {
934 if (a instanceof AWTEventMulticaster) {
935 ((AWTEventMulticaster)a).saveInternal(s, k);
936 }
937 else if (a instanceof Serializable) {
938 s.writeObject(k);
939 s.writeObject(a);
940 }
941
942 if (b instanceof AWTEventMulticaster) {
943 ((AWTEventMulticaster)b).saveInternal(s, k);
944 }
945 else if (b instanceof Serializable) {
946 s.writeObject(k);
947 s.writeObject(b);
948 }
949 }
950
951 protected static void save(ObjectOutputStream s, String k, EventListener l) throws IOException {
952 if (l == null) {
953 return;
954 }
955 else if (l instanceof AWTEventMulticaster) {
956 ((AWTEventMulticaster)l).saveInternal(s, k);
957 }
958 else if (l instanceof Serializable) {
959 s.writeObject(k);
960 s.writeObject(l);
961 }
962 }
963
964 /*
965 * Recursive method which returns a count of the number of listeners in
966 * EventListener, handling the (common) case of l actually being an
967 * AWTEventMulticaster. Additionally, only listeners of type listenerType
968 * are counted. Method modified to fix bug 4513402. -bchristi
969 */
970 private static int getListenerCount(EventListener l, Class<?> listenerType) {
|
93 * {@code remove(EventListener)} method.
94 * </ul>
95 * <p>Swing makes use of
96 * {@link javax.swing.event.EventListenerList EventListenerList} for
97 * similar logic. Refer to it for details.
98 *
99 * @see javax.swing.event.EventListenerList
100 *
101 * @author John Rose
102 * @author Amy Fowler
103 * @since 1.1
104 */
105
106 public class AWTEventMulticaster implements
107 ComponentListener, ContainerListener, FocusListener, KeyListener,
108 MouseListener, MouseMotionListener, WindowListener, WindowFocusListener,
109 WindowStateListener, ActionListener, ItemListener, AdjustmentListener,
110 TextListener, InputMethodListener, HierarchyListener,
111 HierarchyBoundsListener, MouseWheelListener {
112
113 /**
114 * A variable in the event chain (listener-a)
115 */
116 protected final EventListener a;
117
118 /**
119 * A variable in the event chain (listener-b)
120 */
121 protected final EventListener b;
122
123 /**
124 * Creates an event multicaster instance which chains listener-a
125 * with listener-b. Input parameters <code>a</code> and <code>b</code>
126 * should not be <code>null</code>, though implementations may vary in
127 * choosing whether or not to throw <code>NullPointerException</code>
128 * in that case.
129 * @param a listener-a
130 * @param b listener-b
131 */
132 protected AWTEventMulticaster(EventListener a, EventListener b) {
133 this.a = a; this.b = b;
134 }
135
136 /**
137 * Removes a listener from this multicaster.
138 * <p>
139 * The returned multicaster contains all the listeners in this
140 * multicaster with the exception of all occurrences of {@code oldl}.
141 * If the resulting multicaster contains only one regular listener
528 ((HierarchyBoundsListener)a).ancestorResized(e);
529 ((HierarchyBoundsListener)b).ancestorResized(e);
530 }
531
532 /**
533 * Handles the mouseWheelMoved event by invoking the
534 * mouseWheelMoved methods on listener-a and listener-b.
535 * @param e the mouse event
536 * @since 1.4
537 */
538 public void mouseWheelMoved(MouseWheelEvent e) {
539 ((MouseWheelListener)a).mouseWheelMoved(e);
540 ((MouseWheelListener)b).mouseWheelMoved(e);
541 }
542
543 /**
544 * Adds component-listener-a with component-listener-b and
545 * returns the resulting multicast listener.
546 * @param a component-listener-a
547 * @param b component-listener-b
548 * @return the resulting listener
549 */
550 public static ComponentListener add(ComponentListener a, ComponentListener b) {
551 return (ComponentListener)addInternal(a, b);
552 }
553
554 /**
555 * Adds container-listener-a with container-listener-b and
556 * returns the resulting multicast listener.
557 * @param a container-listener-a
558 * @param b container-listener-b
559 * @return the resulting listener
560 */
561 public static ContainerListener add(ContainerListener a, ContainerListener b) {
562 return (ContainerListener)addInternal(a, b);
563 }
564
565 /**
566 * Adds focus-listener-a with focus-listener-b and
567 * returns the resulting multicast listener.
568 * @param a focus-listener-a
569 * @param b focus-listener-b
570 * @return the resulting listener
571 */
572 public static FocusListener add(FocusListener a, FocusListener b) {
573 return (FocusListener)addInternal(a, b);
574 }
575
576 /**
577 * Adds key-listener-a with key-listener-b and
578 * returns the resulting multicast listener.
579 * @param a key-listener-a
580 * @param b key-listener-b
581 * @return the resulting listener
582 */
583 public static KeyListener add(KeyListener a, KeyListener b) {
584 return (KeyListener)addInternal(a, b);
585 }
586
587 /**
588 * Adds mouse-listener-a with mouse-listener-b and
589 * returns the resulting multicast listener.
590 * @param a mouse-listener-a
591 * @param b mouse-listener-b
592 * @return the resulting listener
593 */
594 public static MouseListener add(MouseListener a, MouseListener b) {
595 return (MouseListener)addInternal(a, b);
596 }
597
598 /**
599 * Adds mouse-motion-listener-a with mouse-motion-listener-b and
600 * returns the resulting multicast listener.
601 * @param a mouse-motion-listener-a
602 * @param b mouse-motion-listener-b
603 * @return the resulting listener
604 */
605 public static MouseMotionListener add(MouseMotionListener a, MouseMotionListener b) {
606 return (MouseMotionListener)addInternal(a, b);
607 }
608
609 /**
610 * Adds window-listener-a with window-listener-b and
611 * returns the resulting multicast listener.
612 * @param a window-listener-a
613 * @param b window-listener-b
614 * @return the resulting listener
615 */
616 public static WindowListener add(WindowListener a, WindowListener b) {
617 return (WindowListener)addInternal(a, b);
618 }
619
620 /**
621 * Adds window-state-listener-a with window-state-listener-b
622 * and returns the resulting multicast listener.
623 * @param a window-state-listener-a
624 * @param b window-state-listener-b
625 * @return the resulting listener
626 * @since 1.4
627 */
628 @SuppressWarnings("overloads")
629 public static WindowStateListener add(WindowStateListener a,
630 WindowStateListener b) {
631 return (WindowStateListener)addInternal(a, b);
632 }
633
634 /**
635 * Adds window-focus-listener-a with window-focus-listener-b
636 * and returns the resulting multicast listener.
637 * @param a window-focus-listener-a
638 * @param b window-focus-listener-b
639 * @return the resulting listener
640 * @since 1.4
641 */
642 public static WindowFocusListener add(WindowFocusListener a,
643 WindowFocusListener b) {
644 return (WindowFocusListener)addInternal(a, b);
645 }
646
647 /**
648 * Adds action-listener-a with action-listener-b and
649 * returns the resulting multicast listener.
650 * @param a action-listener-a
651 * @param b action-listener-b
652 * @return the resulting listener
653 */
654 @SuppressWarnings("overloads")
655 public static ActionListener add(ActionListener a, ActionListener b) {
656 return (ActionListener)addInternal(a, b);
657 }
658
659 /**
660 * Adds item-listener-a with item-listener-b and
661 * returns the resulting multicast listener.
662 * @param a item-listener-a
663 * @param b item-listener-b
664 * @return the resulting listener
665 */
666 @SuppressWarnings("overloads")
667 public static ItemListener add(ItemListener a, ItemListener b) {
668 return (ItemListener)addInternal(a, b);
669 }
670
671 /**
672 * Adds adjustment-listener-a with adjustment-listener-b and
673 * returns the resulting multicast listener.
674 * @param a adjustment-listener-a
675 * @param b adjustment-listener-b
676 * @return the resulting listener
677 */
678 @SuppressWarnings("overloads")
679 public static AdjustmentListener add(AdjustmentListener a, AdjustmentListener b) {
680 return (AdjustmentListener)addInternal(a, b);
681 }
682
683 /**
684 * Adds text-listener-a with text-listener-b and
685 * returns the resulting multicast listener.
686 * @param a text-listener-a
687 * @param b text-listener-b
688 * @return the resulting listener
689 */
690 @SuppressWarnings("overloads")
691 public static TextListener add(TextListener a, TextListener b) {
692 return (TextListener)addInternal(a, b);
693 }
694
695 /**
696 * Adds input-method-listener-a with input-method-listener-b and
697 * returns the resulting multicast listener.
698 * @param a input-method-listener-a
699 * @param b input-method-listener-b
700 * @return the resulting listener
701 */
702 public static InputMethodListener add(InputMethodListener a, InputMethodListener b) {
703 return (InputMethodListener)addInternal(a, b);
704 }
705
706 /**
707 * Adds hierarchy-listener-a with hierarchy-listener-b and
708 * returns the resulting multicast listener.
709 * @param a hierarchy-listener-a
710 * @param b hierarchy-listener-b
711 * @return the resulting listener
712 * @since 1.3
713 */
714 @SuppressWarnings("overloads")
715 public static HierarchyListener add(HierarchyListener a, HierarchyListener b) {
716 return (HierarchyListener)addInternal(a, b);
717 }
718
719 /**
720 * Adds hierarchy-bounds-listener-a with hierarchy-bounds-listener-b and
721 * returns the resulting multicast listener.
722 * @param a hierarchy-bounds-listener-a
723 * @param b hierarchy-bounds-listener-b
724 * @return the resulting listener
725 * @since 1.3
726 */
727 public static HierarchyBoundsListener add(HierarchyBoundsListener a, HierarchyBoundsListener b) {
728 return (HierarchyBoundsListener)addInternal(a, b);
729 }
730
731 /**
732 * Adds mouse-wheel-listener-a with mouse-wheel-listener-b and
733 * returns the resulting multicast listener.
734 * @param a mouse-wheel-listener-a
735 * @param b mouse-wheel-listener-b
736 * @return the resulting listener
737 * @since 1.4
738 */
739 @SuppressWarnings("overloads")
740 public static MouseWheelListener add(MouseWheelListener a,
741 MouseWheelListener b) {
742 return (MouseWheelListener)addInternal(a, b);
743 }
744
745 /**
746 * Removes the old component-listener from component-listener-l and
747 * returns the resulting multicast listener.
748 * @param l component-listener-l
749 * @param oldl the component-listener being removed
750 * @return the resulting listener
751 */
752 public static ComponentListener remove(ComponentListener l, ComponentListener oldl) {
753 return (ComponentListener) removeInternal(l, oldl);
754 }
755
756 /**
757 * Removes the old container-listener from container-listener-l and
758 * returns the resulting multicast listener.
759 * @param l container-listener-l
760 * @param oldl the container-listener being removed
761 * @return the resulting listener
762 */
763 public static ContainerListener remove(ContainerListener l, ContainerListener oldl) {
764 return (ContainerListener) removeInternal(l, oldl);
765 }
766
767 /**
768 * Removes the old focus-listener from focus-listener-l and
769 * returns the resulting multicast listener.
770 * @param l focus-listener-l
771 * @param oldl the focus-listener being removed
772 * @return the resulting listener
773 */
774 public static FocusListener remove(FocusListener l, FocusListener oldl) {
775 return (FocusListener) removeInternal(l, oldl);
776 }
777
778 /**
779 * Removes the old key-listener from key-listener-l and
780 * returns the resulting multicast listener.
781 * @param l key-listener-l
782 * @param oldl the key-listener being removed
783 * @return the resulting listener
784 */
785 public static KeyListener remove(KeyListener l, KeyListener oldl) {
786 return (KeyListener) removeInternal(l, oldl);
787 }
788
789 /**
790 * Removes the old mouse-listener from mouse-listener-l and
791 * returns the resulting multicast listener.
792 * @param l mouse-listener-l
793 * @param oldl the mouse-listener being removed
794 * @return the resulting listener
795 */
796 public static MouseListener remove(MouseListener l, MouseListener oldl) {
797 return (MouseListener) removeInternal(l, oldl);
798 }
799
800 /**
801 * Removes the old mouse-motion-listener from mouse-motion-listener-l
802 * and returns the resulting multicast listener.
803 * @param l mouse-motion-listener-l
804 * @param oldl the mouse-motion-listener being removed
805 * @return the resulting listener
806 */
807 public static MouseMotionListener remove(MouseMotionListener l, MouseMotionListener oldl) {
808 return (MouseMotionListener) removeInternal(l, oldl);
809 }
810
811 /**
812 * Removes the old window-listener from window-listener-l and
813 * returns the resulting multicast listener.
814 * @param l window-listener-l
815 * @param oldl the window-listener being removed
816 * @return the resulting listener
817 */
818 public static WindowListener remove(WindowListener l, WindowListener oldl) {
819 return (WindowListener) removeInternal(l, oldl);
820 }
821
822 /**
823 * Removes the old window-state-listener from window-state-listener-l
824 * and returns the resulting multicast listener.
825 * @param l window-state-listener-l
826 * @param oldl the window-state-listener being removed
827 * @return the resulting listener
828 * @since 1.4
829 */
830 @SuppressWarnings("overloads")
831 public static WindowStateListener remove(WindowStateListener l,
832 WindowStateListener oldl) {
833 return (WindowStateListener) removeInternal(l, oldl);
834 }
835
836 /**
837 * Removes the old window-focus-listener from window-focus-listener-l
838 * and returns the resulting multicast listener.
839 * @param l window-focus-listener-l
840 * @param oldl the window-focus-listener being removed
841 * @return the resulting listener
842 * @since 1.4
843 */
844 public static WindowFocusListener remove(WindowFocusListener l,
845 WindowFocusListener oldl) {
846 return (WindowFocusListener) removeInternal(l, oldl);
847 }
848
849 /**
850 * Removes the old action-listener from action-listener-l and
851 * returns the resulting multicast listener.
852 * @param l action-listener-l
853 * @param oldl the action-listener being removed
854 * @return the resulting listener
855 */
856 @SuppressWarnings("overloads")
857 public static ActionListener remove(ActionListener l, ActionListener oldl) {
858 return (ActionListener) removeInternal(l, oldl);
859 }
860
861 /**
862 * Removes the old item-listener from item-listener-l and
863 * returns the resulting multicast listener.
864 * @param l item-listener-l
865 * @param oldl the item-listener being removed
866 * @return the resulting listener
867 */
868 @SuppressWarnings("overloads")
869 public static ItemListener remove(ItemListener l, ItemListener oldl) {
870 return (ItemListener) removeInternal(l, oldl);
871 }
872
873 /**
874 * Removes the old adjustment-listener from adjustment-listener-l and
875 * returns the resulting multicast listener.
876 * @param l adjustment-listener-l
877 * @param oldl the adjustment-listener being removed
878 * @return the resulting listener
879 */
880 @SuppressWarnings("overloads")
881 public static AdjustmentListener remove(AdjustmentListener l, AdjustmentListener oldl) {
882 return (AdjustmentListener) removeInternal(l, oldl);
883 }
884
885 /**
886 * Removes the old text-listener from text-listener-l and
887 * returns the resulting multicast listener.
888 * @param l text-listener-l
889 * @param oldl the text-listener being removed
890 * @return the resulting listener
891 */
892 @SuppressWarnings("overloads")
893 public static TextListener remove(TextListener l, TextListener oldl) {
894 return (TextListener) removeInternal(l, oldl);
895 }
896
897 /**
898 * Removes the old input-method-listener from input-method-listener-l and
899 * returns the resulting multicast listener.
900 * @param l input-method-listener-l
901 * @param oldl the input-method-listener being removed
902 * @return the resulting listener
903 */
904 public static InputMethodListener remove(InputMethodListener l, InputMethodListener oldl) {
905 return (InputMethodListener) removeInternal(l, oldl);
906 }
907
908 /**
909 * Removes the old hierarchy-listener from hierarchy-listener-l and
910 * returns the resulting multicast listener.
911 * @param l hierarchy-listener-l
912 * @param oldl the hierarchy-listener being removed
913 * @return the resulting listener
914 * @since 1.3
915 */
916 @SuppressWarnings("overloads")
917 public static HierarchyListener remove(HierarchyListener l, HierarchyListener oldl) {
918 return (HierarchyListener) removeInternal(l, oldl);
919 }
920
921 /**
922 * Removes the old hierarchy-bounds-listener from
923 * hierarchy-bounds-listener-l and returns the resulting multicast
924 * listener.
925 * @param l hierarchy-bounds-listener-l
926 * @param oldl the hierarchy-bounds-listener being removed
927 * @return the resulting listener
928 * @since 1.3
929 */
930 public static HierarchyBoundsListener remove(HierarchyBoundsListener l, HierarchyBoundsListener oldl) {
931 return (HierarchyBoundsListener) removeInternal(l, oldl);
932 }
933
934 /**
935 * Removes the old mouse-wheel-listener from mouse-wheel-listener-l
936 * and returns the resulting multicast listener.
937 * @param l mouse-wheel-listener-l
938 * @param oldl the mouse-wheel-listener being removed
939 * @return the resulting listener
940 * @since 1.4
941 */
942 @SuppressWarnings("overloads")
943 public static MouseWheelListener remove(MouseWheelListener l,
944 MouseWheelListener oldl) {
945 return (MouseWheelListener) removeInternal(l, oldl);
946 }
947
948 /**
949 * Returns the resulting multicast listener from adding listener-a
950 * and listener-b together.
951 * If listener-a is null, it returns listener-b;
952 * If listener-b is null, it returns listener-a
953 * If neither are null, then it creates and returns
954 * a new AWTEventMulticaster instance which chains a with b.
955 * @param a event listener-a
956 * @param b event listener-b
957 * @return the resulting listener
958 */
959 protected static EventListener addInternal(EventListener a, EventListener b) {
960 if (a == null) return b;
961 if (b == null) return a;
962 return new AWTEventMulticaster(a, b);
963 }
964
965 /**
966 * Returns the resulting multicast listener after removing the
967 * old listener from listener-l.
968 * If listener-l equals the old listener OR listener-l is null,
969 * returns null.
970 * Else if listener-l is an instance of AWTEventMulticaster,
971 * then it removes the old listener from it.
972 * Else, returns listener l.
973 * @param l the listener being removed from
974 * @param oldl the listener being removed
975 * @return the resulting listener
976 */
977 protected static EventListener removeInternal(EventListener l, EventListener oldl) {
978 if (l == oldl || l == null) {
979 return null;
980 } else if (l instanceof AWTEventMulticaster) {
981 return ((AWTEventMulticaster)l).remove(oldl);
982 } else {
983 return l; // it's not here
984 }
985 }
986
987
988 /**
989 * Serialization support. Saves all Serializable listeners to a serialization stream.
990 *
991 * @param s the stream to save to
992 * @param k a prefix stream to put before each serializable listener
993 * @throws IOException if serialization fails
994 */
995 protected void saveInternal(ObjectOutputStream s, String k) throws IOException {
996 if (a instanceof AWTEventMulticaster) {
997 ((AWTEventMulticaster)a).saveInternal(s, k);
998 }
999 else if (a instanceof Serializable) {
1000 s.writeObject(k);
1001 s.writeObject(a);
1002 }
1003
1004 if (b instanceof AWTEventMulticaster) {
1005 ((AWTEventMulticaster)b).saveInternal(s, k);
1006 }
1007 else if (b instanceof Serializable) {
1008 s.writeObject(k);
1009 s.writeObject(b);
1010 }
1011 }
1012
1013 /**
1014 * Saves a Serializable listener chain to a serialization stream.
1015 *
1016 * @param s the stream to save to
1017 * @param k a prefix stream to put before each serializable listener
1018 * @param l the listener chain to save
1019 * @throws IOException if serialization fails
1020 */
1021 protected static void save(ObjectOutputStream s, String k, EventListener l) throws IOException {
1022 if (l == null) {
1023 return;
1024 }
1025 else if (l instanceof AWTEventMulticaster) {
1026 ((AWTEventMulticaster)l).saveInternal(s, k);
1027 }
1028 else if (l instanceof Serializable) {
1029 s.writeObject(k);
1030 s.writeObject(l);
1031 }
1032 }
1033
1034 /*
1035 * Recursive method which returns a count of the number of listeners in
1036 * EventListener, handling the (common) case of l actually being an
1037 * AWTEventMulticaster. Additionally, only listeners of type listenerType
1038 * are counted. Method modified to fix bug 4513402. -bchristi
1039 */
1040 private static int getListenerCount(EventListener l, Class<?> listenerType) {
|