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 *
687 * @param a text-listener-a
688 * @param b text-listener-b
689 * @return the resulting listener
690 */
691 @SuppressWarnings("overloads")
692 public static TextListener add(TextListener a, TextListener b) {
693 return (TextListener)addInternal(a, b);
694 }
695
696 /**
697 * Adds input-method-listener-a with input-method-listener-b and
698 * returns the resulting multicast listener.
699 * @param a input-method-listener-a
700 * @param b input-method-listener-b
701 * @return the resulting listener
702 */
703 public static InputMethodListener add(InputMethodListener a, InputMethodListener b) {
704 return (InputMethodListener)addInternal(a, b);
705 }
706
707 /**
708 * Adds hierarchy-listener-a with hierarchy-listener-b and
709 * returns the resulting multicast listener.
710 * @param a hierarchy-listener-a
711 * @param b hierarchy-listener-b
712 * @return the resulting listener
713 * @since 1.3
714 */
715 @SuppressWarnings("overloads")
716 public static HierarchyListener add(HierarchyListener a, HierarchyListener b) {
717 return (HierarchyListener)addInternal(a, b);
718 }
719
720 /**
721 * Adds hierarchy-bounds-listener-a with hierarchy-bounds-listener-b and
722 * returns the resulting multicast listener.
723 * @param a hierarchy-bounds-listener-a
724 * @param b hierarchy-bounds-listener-b
725 * @return the resulting listener
726 * @since 1.3
727 */
728 public static HierarchyBoundsListener add(HierarchyBoundsListener a, HierarchyBoundsListener b) {
729 return (HierarchyBoundsListener)addInternal(a, b);
730 }
731
732 /**
733 * Adds mouse-wheel-listener-a with mouse-wheel-listener-b and
734 * returns the resulting multicast listener.
735 * @param a mouse-wheel-listener-a
736 * @param b mouse-wheel-listener-b
737 * @return the resulting listener
738 * @since 1.4
739 */
740 @SuppressWarnings("overloads")
741 public static MouseWheelListener add(MouseWheelListener a,
742 MouseWheelListener b) {
743 return (MouseWheelListener)addInternal(a, b);
744 }
745
746 /**
747 * Removes the old component-listener from component-listener-l and
748 * returns the resulting multicast listener.
749 * @param l component-listener-l
750 * @param oldl the component-listener being removed
751 * @return the resulting listener
752 */
753 public static ComponentListener remove(ComponentListener l, ComponentListener oldl) {
754 return (ComponentListener) removeInternal(l, oldl);
755 }
756
757 /**
758 * Removes the old container-listener from container-listener-l and
759 * returns the resulting multicast listener.
760 * @param l container-listener-l
761 * @param oldl the container-listener being removed
762 * @return the resulting listener
763 */
764 public static ContainerListener remove(ContainerListener l, ContainerListener oldl) {
765 return (ContainerListener) removeInternal(l, oldl);
766 }
767
768 /**
769 * Removes the old focus-listener from focus-listener-l and
770 * returns the resulting multicast listener.
771 * @param l focus-listener-l
772 * @param oldl the focus-listener being removed
773 * @return the resulting listener
774 */
775 public static FocusListener remove(FocusListener l, FocusListener oldl) {
776 return (FocusListener) removeInternal(l, oldl);
777 }
778
779 /**
780 * Removes the old key-listener from key-listener-l and
781 * returns the resulting multicast listener.
782 * @param l key-listener-l
783 * @param oldl the key-listener being removed
784 * @return the resulting listener
785 */
786 public static KeyListener remove(KeyListener l, KeyListener oldl) {
787 return (KeyListener) removeInternal(l, oldl);
788 }
789
790 /**
791 * Removes the old mouse-listener from mouse-listener-l and
792 * returns the resulting multicast listener.
793 * @param l mouse-listener-l
794 * @param oldl the mouse-listener being removed
795 * @return the resulting listener
796 */
797 public static MouseListener remove(MouseListener l, MouseListener oldl) {
798 return (MouseListener) removeInternal(l, oldl);
799 }
800
801 /**
802 * Removes the old mouse-motion-listener from mouse-motion-listener-l
803 * and returns the resulting multicast listener.
804 * @param l mouse-motion-listener-l
805 * @param oldl the mouse-motion-listener being removed
806 * @return the resulting listener
807 */
808 public static MouseMotionListener remove(MouseMotionListener l, MouseMotionListener oldl) {
809 return (MouseMotionListener) removeInternal(l, oldl);
810 }
811
812 /**
813 * Removes the old window-listener from window-listener-l and
814 * returns the resulting multicast listener.
815 * @param l window-listener-l
816 * @param oldl the window-listener being removed
817 * @return the resulting listener
818 */
819 public static WindowListener remove(WindowListener l, WindowListener oldl) {
820 return (WindowListener) removeInternal(l, oldl);
821 }
822
823 /**
824 * Removes the old window-state-listener from window-state-listener-l
825 * and returns the resulting multicast listener.
826 * @param l window-state-listener-l
827 * @param oldl the window-state-listener being removed
828 * @return the resulting listener
829 * @since 1.4
830 */
831 @SuppressWarnings("overloads")
832 public static WindowStateListener remove(WindowStateListener l,
833 WindowStateListener oldl) {
834 return (WindowStateListener) removeInternal(l, oldl);
835 }
836
837 /**
838 * Removes the old window-focus-listener from window-focus-listener-l
839 * and returns the resulting multicast listener.
840 * @param l window-focus-listener-l
841 * @param oldl the window-focus-listener being removed
842 * @return the resulting listener
843 * @since 1.4
844 */
845 public static WindowFocusListener remove(WindowFocusListener l,
846 WindowFocusListener oldl) {
847 return (WindowFocusListener) removeInternal(l, oldl);
848 }
849
850 /**
851 * Removes the old action-listener from action-listener-l and
852 * returns the resulting multicast listener.
853 * @param l action-listener-l
854 * @param oldl the action-listener being removed
855 * @return the resulting listener
856 */
857 @SuppressWarnings("overloads")
858 public static ActionListener remove(ActionListener l, ActionListener oldl) {
859 return (ActionListener) removeInternal(l, oldl);
860 }
861
862 /**
863 * Removes the old item-listener from item-listener-l and
864 * returns the resulting multicast listener.
865 * @param l item-listener-l
866 * @param oldl the item-listener being removed
867 * @return the resulting listener
868 */
869 @SuppressWarnings("overloads")
870 public static ItemListener remove(ItemListener l, ItemListener oldl) {
871 return (ItemListener) removeInternal(l, oldl);
872 }
873
874 /**
875 * Removes the old adjustment-listener from adjustment-listener-l and
876 * returns the resulting multicast listener.
877 * @param l adjustment-listener-l
878 * @param oldl the adjustment-listener being removed
879 * @return the resulting listener
880 */
881 @SuppressWarnings("overloads")
882 public static AdjustmentListener remove(AdjustmentListener l, AdjustmentListener oldl) {
883 return (AdjustmentListener) removeInternal(l, oldl);
884 }
885
886 /**
887 * Removes the old text-listener from text-listener-l and
888 * returns the resulting multicast listener.
889 *
890 * @param l text-listener-l
891 * @param oldl the text-listener being removed
892 * @return the resulting listener
893 */
894 @SuppressWarnings("overloads")
895 public static TextListener remove(TextListener l, TextListener oldl) {
896 return (TextListener) removeInternal(l, oldl);
897 }
898
899 /**
900 * Removes the old input-method-listener from input-method-listener-l and
901 * returns the resulting multicast listener.
902 * @param l input-method-listener-l
903 * @param oldl the input-method-listener being removed
904 * @return the resulting listener
905 */
906 public static InputMethodListener remove(InputMethodListener l, InputMethodListener oldl) {
907 return (InputMethodListener) removeInternal(l, oldl);
908 }
909
910 /**
911 * Removes the old hierarchy-listener from hierarchy-listener-l and
912 * returns the resulting multicast listener.
913 * @param l hierarchy-listener-l
914 * @param oldl the hierarchy-listener being removed
915 * @return the resulting listener
916 * @since 1.3
917 */
918 @SuppressWarnings("overloads")
919 public static HierarchyListener remove(HierarchyListener l, HierarchyListener oldl) {
920 return (HierarchyListener) removeInternal(l, oldl);
921 }
922
923 /**
924 * Removes the old hierarchy-bounds-listener from
925 * hierarchy-bounds-listener-l and returns the resulting multicast
926 * listener.
927 * @param l hierarchy-bounds-listener-l
928 * @param oldl the hierarchy-bounds-listener being removed
929 * @return the resulting listener
930 * @since 1.3
931 */
932 public static HierarchyBoundsListener remove(HierarchyBoundsListener l, HierarchyBoundsListener oldl) {
933 return (HierarchyBoundsListener) removeInternal(l, oldl);
934 }
935
936 /**
937 * Removes the old mouse-wheel-listener from mouse-wheel-listener-l
938 * and returns the resulting multicast listener.
939 * @param l mouse-wheel-listener-l
940 * @param oldl the mouse-wheel-listener being removed
941 * @return the resulting listener
942 * @since 1.4
943 */
944 @SuppressWarnings("overloads")
945 public static MouseWheelListener remove(MouseWheelListener l,
946 MouseWheelListener oldl) {
947 return (MouseWheelListener) removeInternal(l, oldl);
948 }
949
950 /**
951 * Returns the resulting multicast listener from adding listener-a
952 * and listener-b together.
953 * If listener-a is null, it returns listener-b;
954 * If listener-b is null, it returns listener-a
955 * If neither are null, then it creates and returns
956 * a new AWTEventMulticaster instance which chains a with b.
957 * @param a event listener-a
958 * @param b event listener-b
959 * @return the resulting listener
960 */
961 protected static EventListener addInternal(EventListener a, EventListener b) {
962 if (a == null) return b;
963 if (b == null) return a;
964 return new AWTEventMulticaster(a, b);
965 }
966
967 /**
968 * Returns the resulting multicast listener after removing the
969 * old listener from listener-l.
970 * If listener-l equals the old listener OR listener-l is null,
971 * returns null.
972 * Else if listener-l is an instance of AWTEventMulticaster,
973 * then it removes the old listener from it.
974 * Else, returns listener l.
975 * @param l the listener being removed from
976 * @param oldl the listener being removed
977 * @return the resulting listener
978 */
979 protected static EventListener removeInternal(EventListener l, EventListener oldl) {
980 if (l == oldl || l == null) {
981 return null;
982 } else if (l instanceof AWTEventMulticaster) {
983 return ((AWTEventMulticaster)l).remove(oldl);
984 } else {
985 return l; // it's not here
986 }
987 }
988
989
990 /**
991 * Serialization support. Saves all Serializable listeners
992 * to a serialization stream.
993 *
994 * @param s the stream to save to
995 * @param k a prefix stream to put before each serializable listener
996 * @throws IOException if serialization fails
997 */
998 protected void saveInternal(ObjectOutputStream s, String k) throws IOException {
999 if (a instanceof AWTEventMulticaster) {
1000 ((AWTEventMulticaster)a).saveInternal(s, k);
1001 }
1002 else if (a instanceof Serializable) {
1003 s.writeObject(k);
1004 s.writeObject(a);
1005 }
1006
1007 if (b instanceof AWTEventMulticaster) {
1008 ((AWTEventMulticaster)b).saveInternal(s, k);
1009 }
1010 else if (b instanceof Serializable) {
1011 s.writeObject(k);
1012 s.writeObject(b);
1013 }
1014 }
1015
1016 /**
1017 * Saves a Serializable listener chain to a serialization stream.
1018 *
1019 * @param s the stream to save to
1020 * @param k a prefix stream to put before each serializable listener
1021 * @param l the listener chain to save
1022 * @throws IOException if serialization fails
1023 */
1024 protected static void save(ObjectOutputStream s, String k, EventListener l) throws IOException {
1025 if (l == null) {
1026 return;
1027 }
1028 else if (l instanceof AWTEventMulticaster) {
1029 ((AWTEventMulticaster)l).saveInternal(s, k);
1030 }
1031 else if (l instanceof Serializable) {
1032 s.writeObject(k);
1033 s.writeObject(l);
1034 }
1035 }
1036
1037 /*
1038 * Recursive method which returns a count of the number of listeners in
1039 * EventListener, handling the (common) case of l actually being an
1040 * AWTEventMulticaster. Additionally, only listeners of type listenerType
1041 * are counted. Method modified to fix bug 4513402. -bchristi
1042 */
1043 private static int getListenerCount(EventListener l, Class<?> listenerType) {
|