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 142 * the regular listener may be returned. If the resulting multicaster 143 * is empty, then {@code null} may be returned instead. 144 * <p> 145 * No exception is thrown if {@code oldl} is {@code null}. 146 * 147 * @param oldl the listener to be removed 1061 */ 1062 private static int populateListenerArray(EventListener[] a, EventListener l, int index) { 1063 if (l instanceof AWTEventMulticaster) { 1064 AWTEventMulticaster mc = (AWTEventMulticaster)l; 1065 int lhs = populateListenerArray(a, mc.a, index); 1066 return populateListenerArray(a, mc.b, lhs); 1067 } 1068 else if (a.getClass().getComponentType().isInstance(l)) { 1069 a[index] = l; 1070 return index + 1; 1071 } 1072 // Skip nulls, instances of wrong class 1073 else { 1074 return index; 1075 } 1076 } 1077 1078 /** 1079 * Returns an array of all the objects chained as 1080 * <code><em>Foo</em>Listener</code>s by the specified 1081 * <code>java.util.EventListener</code>. 1082 * <code><em>Foo</em>Listener</code>s are chained by the 1083 * <code>AWTEventMulticaster</code> using the 1084 * <code>add<em>Foo</em>Listener</code> method. 1085 * If a <code>null</code> listener is specified, this method returns an 1086 * empty array. If the specified listener is not an instance of 1087 * <code>AWTEventMulticaster</code>, this method returns an array which 1088 * contains only the specified listener. If no such listeners are chained, 1089 * this method returns an empty array. 1090 * 1091 * @param <T> the listener type 1092 * @param l the specified <code>java.util.EventListener</code> 1093 * @param listenerType the type of listeners requested; this parameter 1094 * should specify an interface that descends from 1095 * <code>java.util.EventListener</code> 1096 * @return an array of all objects chained as 1097 * <code><em>Foo</em>Listener</code>s by the specified multicast 1098 * listener, or an empty array if no such listeners have been 1099 * chained by the specified multicast listener 1100 * @exception NullPointerException if the specified 1101 * {@code listenertype} parameter is {@code null} 1102 * @exception ClassCastException if <code>listenerType</code> 1103 * doesn't specify a class or interface that implements 1104 * <code>java.util.EventListener</code> 1105 * 1106 * @since 1.4 1107 */ 1108 @SuppressWarnings("unchecked") 1109 public static <T extends EventListener> T[] 1110 getListeners(EventListener l, Class<T> listenerType) 1111 { 1112 if (listenerType == null) { 1113 throw new NullPointerException ("Listener type should not be null"); 1114 } 1115 1116 int n = getListenerCount(l, listenerType); 1117 T[] result = (T[])Array.newInstance(listenerType, n); 1118 populateListenerArray(result, l, 0); 1119 return result; 1120 } 1121 } | 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} and {@code b} 126 * should not be {@code null}, though implementations may vary in 127 * choosing whether or not to throw {@code NullPointerException} 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 142 * the regular listener may be returned. If the resulting multicaster 143 * is empty, then {@code null} may be returned instead. 144 * <p> 145 * No exception is thrown if {@code oldl} is {@code null}. 146 * 147 * @param oldl the listener to be removed 1061 */ 1062 private static int populateListenerArray(EventListener[] a, EventListener l, int index) { 1063 if (l instanceof AWTEventMulticaster) { 1064 AWTEventMulticaster mc = (AWTEventMulticaster)l; 1065 int lhs = populateListenerArray(a, mc.a, index); 1066 return populateListenerArray(a, mc.b, lhs); 1067 } 1068 else if (a.getClass().getComponentType().isInstance(l)) { 1069 a[index] = l; 1070 return index + 1; 1071 } 1072 // Skip nulls, instances of wrong class 1073 else { 1074 return index; 1075 } 1076 } 1077 1078 /** 1079 * Returns an array of all the objects chained as 1080 * <code><em>Foo</em>Listener</code>s by the specified 1081 * {@code java.util.EventListener}. 1082 * <code><em>Foo</em>Listener</code>s are chained by the 1083 * {@code AWTEventMulticaster} using the 1084 * <code>add<em>Foo</em>Listener</code> method. 1085 * If a {@code null} listener is specified, this method returns an 1086 * empty array. If the specified listener is not an instance of 1087 * {@code AWTEventMulticaster}, this method returns an array which 1088 * contains only the specified listener. If no such listeners are chained, 1089 * this method returns an empty array. 1090 * 1091 * @param <T> the listener type 1092 * @param l the specified {@code java.util.EventListener} 1093 * @param listenerType the type of listeners requested; this parameter 1094 * should specify an interface that descends from 1095 * {@code java.util.EventListener} 1096 * @return an array of all objects chained as 1097 * <code><em>Foo</em>Listener</code>s by the specified multicast 1098 * listener, or an empty array if no such listeners have been 1099 * chained by the specified multicast listener 1100 * @exception NullPointerException if the specified 1101 * {@code listenertype} parameter is {@code null} 1102 * @exception ClassCastException if {@code listenerType} 1103 * doesn't specify a class or interface that implements 1104 * {@code java.util.EventListener} 1105 * 1106 * @since 1.4 1107 */ 1108 @SuppressWarnings("unchecked") 1109 public static <T extends EventListener> T[] 1110 getListeners(EventListener l, Class<T> listenerType) 1111 { 1112 if (listenerType == null) { 1113 throw new NullPointerException ("Listener type should not be null"); 1114 } 1115 1116 int n = getListenerCount(l, listenerType); 1117 T[] result = (T[])Array.newInstance(listenerType, n); 1118 populateListenerArray(result, l, 0); 1119 return result; 1120 } 1121 } |