001 /*
002 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package java.awt.dnd;
026
027 import java.awt.AWTEventMulticaster;
028 import java.io.ObjectOutputStream;
029 import java.io.IOException;
030 import java.util.EventListener;
031
032 /**
033 * A class extends <code>AWTEventMulticaster</code> to implement efficient and
034 * thread-safe multi-cast event dispatching for the drag-and-drop events defined
035 * in the java.awt.dnd package.
036 *
037 * @version 1.14, 06/05/07
038 * @since 1.4
039 * @see AWTEventMulticaster
040 */
041
042 class DnDEventMulticaster extends AWTEventMulticaster implements
043 DragSourceListener, DragSourceMotionListener {
044
045 /**
046 * Creates an event multicaster instance which chains listener-a
047 * with listener-b. Input parameters <code>a</code> and <code>b</code>
048 * should not be <code>null</code>, though implementations may vary in
049 * choosing whether or not to throw <code>NullPointerException</code>
050 * in that case.
051 *
052 * @param a listener-a
053 * @param b listener-b
054 */
055 protected DnDEventMulticaster(EventListener a, EventListener b) {
056 super (a, b);
057 }
058
059 /**
060 * Handles the <code>DragSourceDragEvent</code> by invoking
061 * <code>dragEnter</code> on listener-a and listener-b.
062 *
063 * @param dsde the <code>DragSourceDragEvent</code>
064 */
065 public void dragEnter(DragSourceDragEvent dsde) {
066 ((DragSourceListener) a).dragEnter(dsde);
067 ((DragSourceListener) b).dragEnter(dsde);
068 }
069
070 /**
071 * Handles the <code>DragSourceDragEvent</code> by invoking
072 * <code>dragOver</code> on listener-a and listener-b.
073 *
074 * @param e the <code>DragSourceDragEvent</code>
075 */
076 public void dragOver(DragSourceDragEvent dsde) {
077 ((DragSourceListener) a).dragOver(dsde);
078 ((DragSourceListener) b).dragOver(dsde);
079 }
080
081 /**
082 * Handles the <code>DragSourceDragEvent</code> by invoking
083 * <code>dropActionChanged</code> on listener-a and listener-b.
084 *
085 * @param dsde the <code>DragSourceDragEvent</code>
086 */
087 public void dropActionChanged(DragSourceDragEvent dsde) {
088 ((DragSourceListener) a).dropActionChanged(dsde);
089 ((DragSourceListener) b).dropActionChanged(dsde);
090 }
091
092 /**
093 * Handles the <code>DragSourceEvent</code> by invoking
094 * <code>dragExit</code> on listener-a and listener-b.
095 *
096 * @param dse the <code>DragSourceEvent</code>
097 */
098 public void dragExit(DragSourceEvent dse) {
099 ((DragSourceListener) a).dragExit(dse);
100 ((DragSourceListener) b).dragExit(dse);
101 }
102
103 /**
104 * Handles the <code>DragSourceDropEvent</code> by invoking
105 * <code>dragDropEnd</code> on listener-a and listener-b.
106 *
107 * @param dsde the <code>DragSourceDropEvent</code>
108 */
109 public void dragDropEnd(DragSourceDropEvent dsde) {
110 ((DragSourceListener) a).dragDropEnd(dsde);
111 ((DragSourceListener) b).dragDropEnd(dsde);
112 }
113
114 /**
115 * Handles the <code>DragSourceDragEvent</code> by invoking
116 * <code>dragMouseMoved</code> on listener-a and listener-b.
117 *
118 * @param dsde the <code>DragSourceDragEvent</code>
119 */
120 public void dragMouseMoved(DragSourceDragEvent dsde) {
121 ((DragSourceMotionListener) a).dragMouseMoved(dsde);
122 ((DragSourceMotionListener) b).dragMouseMoved(dsde);
123 }
124
125 /**
126 * Adds drag-source-listener-a with drag-source-listener-b and
127 * returns the resulting multicast listener.
128 *
129 * @param a drag-source-listener-a
130 * @param b drag-source-listener-b
131 */
132 public static DragSourceListener add(DragSourceListener a,
133 DragSourceListener b) {
134 return (DragSourceListener) addInternal(a, b);
135 }
136
137 /**
138 * Adds drag-source-motion-listener-a with drag-source-motion-listener-b and
139 * returns the resulting multicast listener.
140 *
141 * @param a drag-source-motion-listener-a
142 * @param b drag-source-motion-listener-b
143 */
144 public static DragSourceMotionListener add(
145 DragSourceMotionListener a, DragSourceMotionListener b) {
146 return (DragSourceMotionListener) addInternal(a, b);
147 }
148
149 /**
150 * Removes the old drag-source-listener from drag-source-listener-l
151 * and returns the resulting multicast listener.
152 *
153 * @param l drag-source-listener-l
154 * @param oldl the drag-source-listener being removed
155 */
156 public static DragSourceListener remove(DragSourceListener l,
157 DragSourceListener oldl) {
158 return (DragSourceListener) removeInternal(l, oldl);
159 }
160
161 /**
162 * Removes the old drag-source-motion-listener from
163 * drag-source-motion-listener-l and returns the resulting multicast
164 * listener.
165 *
166 * @param l drag-source-motion-listener-l
167 * @param ol the drag-source-motion-listener being removed
168 */
169 public static DragSourceMotionListener remove(
170 DragSourceMotionListener l, DragSourceMotionListener ol) {
171 return (DragSourceMotionListener) removeInternal(l, ol);
172 }
173
174 /**
175 * Returns the resulting multicast listener from adding listener-a
176 * and listener-b together.
177 * If listener-a is null, it returns listener-b;
178 * If listener-b is null, it returns listener-a
179 * If neither are null, then it creates and returns
180 * a new AWTEventMulticaster instance which chains a with b.
181 * @param a event listener-a
182 * @param b event listener-b
183 */
184 protected static EventListener addInternal(EventListener a,
185 EventListener b) {
186 if (a == null)
187 return b;
188 if (b == null)
189 return a;
190 return new DnDEventMulticaster(a, b);
191 }
192
193 /**
194 * Removes a listener from this multicaster and returns the
195 * resulting multicast listener.
196 * @param oldl the listener to be removed
197 */
198 protected EventListener remove(EventListener oldl) {
199 if (oldl == a)
200 return b;
201 if (oldl == b)
202 return a;
203 EventListener a2 = removeInternal(a, oldl);
204 EventListener b2 = removeInternal(b, oldl);
205 if (a2 == a && b2 == b) {
206 return this ; // it's not here
207 }
208 return addInternal(a2, b2);
209 }
210
211 /**
212 * Returns the resulting multicast listener after removing the
213 * old listener from listener-l.
214 * If listener-l equals the old listener OR listener-l is null,
215 * returns null.
216 * Else if listener-l is an instance of AWTEventMulticaster,
217 * then it removes the old listener from it.
218 * Else, returns listener l.
219 * @param l the listener being removed from
220 * @param oldl the listener being removed
221 */
222 protected static EventListener removeInternal(EventListener l,
223 EventListener oldl) {
224 if (l == oldl || l == null) {
225 return null;
226 } else if (l instanceof DnDEventMulticaster) {
227 return ((DnDEventMulticaster) l).remove(oldl);
228 } else {
229 return l; // it's not here
230 }
231 }
232
233 protected static void save(ObjectOutputStream s, String k,
234 EventListener l) throws IOException {
235 AWTEventMulticaster.save(s, k, l);
236 }
237 }
|