KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > dnd > DnDEventMulticaster


1 /*
2  * @(#)DnDEventMulticaster.java 1.5 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt.dnd;
8
9 import java.awt.AWTEventMulticaster JavaDoc;
10 import java.lang.reflect.Array JavaDoc;
11 import java.util.EventListener JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.EventListener JavaDoc;
16
17
18 /**
19  * A class extends <code>AWTEventMulticaster</code> to implement efficient and
20  * thread-safe multi-cast event dispatching for the drag-and-drop events defined
21  * in the java.awt.dnd package.
22  *
23  * @version 1.5, 12/19/03
24  * @since 1.4
25  * @see AWTEventMulticaster
26  */

27
28 class DnDEventMulticaster extends AWTEventMulticaster JavaDoc
29     implements DragSourceListener JavaDoc, DragSourceMotionListener JavaDoc {
30
31     /**
32      * Creates an event multicaster instance which chains listener-a
33      * with listener-b. Input parameters <code>a</code> and <code>b</code>
34      * should not be <code>null</code>, though implementations may vary in
35      * choosing whether or not to throw <code>NullPointerException</code>
36      * in that case.
37      *
38      * @param a listener-a
39      * @param b listener-b
40      */

41     protected DnDEventMulticaster(EventListener JavaDoc a, EventListener JavaDoc b) {
42         super(a,b);
43     }
44
45     /**
46      * Handles the <code>DragSourceDragEvent</code> by invoking
47      * <code>dragEnter</code> on listener-a and listener-b.
48      *
49      * @param dsde the <code>DragSourceDragEvent</code>
50      */

51     public void dragEnter(DragSourceDragEvent JavaDoc dsde) {
52         ((DragSourceListener JavaDoc)a).dragEnter(dsde);
53         ((DragSourceListener JavaDoc)b).dragEnter(dsde);
54     }
55
56     /**
57      * Handles the <code>DragSourceDragEvent</code> by invoking
58      * <code>dragOver</code> on listener-a and listener-b.
59      *
60      * @param e the <code>DragSourceDragEvent</code>
61      */

62     public void dragOver(DragSourceDragEvent JavaDoc dsde) {
63         ((DragSourceListener JavaDoc)a).dragOver(dsde);
64         ((DragSourceListener JavaDoc)b).dragOver(dsde);
65     }
66
67     /**
68      * Handles the <code>DragSourceDragEvent</code> by invoking
69      * <code>dropActionChanged</code> on listener-a and listener-b.
70      *
71      * @param dsde the <code>DragSourceDragEvent</code>
72      */

73     public void dropActionChanged(DragSourceDragEvent JavaDoc dsde) {
74         ((DragSourceListener JavaDoc)a).dropActionChanged(dsde);
75         ((DragSourceListener JavaDoc)b).dropActionChanged(dsde);
76     }
77
78     /**
79      * Handles the <code>DragSourceEvent</code> by invoking
80      * <code>dragExit</code> on listener-a and listener-b.
81      *
82      * @param dse the <code>DragSourceEvent</code>
83      */

84     public void dragExit(DragSourceEvent JavaDoc dse) {
85         ((DragSourceListener JavaDoc)a).dragExit(dse);
86         ((DragSourceListener JavaDoc)b).dragExit(dse);
87     }
88
89     /**
90      * Handles the <code>DragSourceDropEvent</code> by invoking
91      * <code>dragDropEnd</code> on listener-a and listener-b.
92      *
93      * @param dsde the <code>DragSourceDropEvent</code>
94      */

95     public void dragDropEnd(DragSourceDropEvent JavaDoc dsde) {
96         ((DragSourceListener JavaDoc)a).dragDropEnd(dsde);
97         ((DragSourceListener JavaDoc)b).dragDropEnd(dsde);
98     }
99
100     /**
101      * Handles the <code>DragSourceDragEvent</code> by invoking
102      * <code>dragMouseMoved</code> on listener-a and listener-b.
103      *
104      * @param dsde the <code>DragSourceDragEvent</code>
105      */

106     public void dragMouseMoved(DragSourceDragEvent JavaDoc dsde) {
107         ((DragSourceMotionListener JavaDoc)a).dragMouseMoved(dsde);
108         ((DragSourceMotionListener JavaDoc)b).dragMouseMoved(dsde);
109     }
110
111     /**
112      * Adds drag-source-listener-a with drag-source-listener-b and
113      * returns the resulting multicast listener.
114      *
115      * @param a drag-source-listener-a
116      * @param b drag-source-listener-b
117      */

118     public static DragSourceListener JavaDoc add(DragSourceListener JavaDoc a,
119                                          DragSourceListener JavaDoc b) {
120         return (DragSourceListener JavaDoc)addInternal(a, b);
121     }
122
123     /**
124      * Adds drag-source-motion-listener-a with drag-source-motion-listener-b and
125      * returns the resulting multicast listener.
126      *
127      * @param a drag-source-motion-listener-a
128      * @param b drag-source-motion-listener-b
129      */

130     public static DragSourceMotionListener JavaDoc add(DragSourceMotionListener JavaDoc a,
131                                                DragSourceMotionListener JavaDoc b) {
132         return (DragSourceMotionListener JavaDoc)addInternal(a, b);
133     }
134
135     /**
136      * Removes the old drag-source-listener from drag-source-listener-l
137      * and returns the resulting multicast listener.
138      *
139      * @param l drag-source-listener-l
140      * @param oldl the drag-source-listener being removed
141      */

142     public static DragSourceListener JavaDoc remove(DragSourceListener JavaDoc l,
143                                             DragSourceListener JavaDoc oldl) {
144         return (DragSourceListener JavaDoc)removeInternal(l, oldl);
145     }
146
147     /**
148      * Removes the old drag-source-motion-listener from
149      * drag-source-motion-listener-l and returns the resulting multicast
150      * listener.
151      *
152      * @param l drag-source-motion-listener-l
153      * @param ol the drag-source-motion-listener being removed
154      */

155     public static DragSourceMotionListener JavaDoc remove(DragSourceMotionListener JavaDoc l,
156                                                   DragSourceMotionListener JavaDoc ol) {
157         return (DragSourceMotionListener JavaDoc)removeInternal(l, ol);
158     }
159
160     /**
161      * Returns the resulting multicast listener from adding listener-a
162      * and listener-b together.
163      * If listener-a is null, it returns listener-b;
164      * If listener-b is null, it returns listener-a
165      * If neither are null, then it creates and returns
166      * a new AWTEventMulticaster instance which chains a with b.
167      * @param a event listener-a
168      * @param b event listener-b
169      */

170     protected static EventListener JavaDoc addInternal(EventListener JavaDoc a, EventListener JavaDoc b) {
171     if (a == null) return b;
172     if (b == null) return a;
173     return new DnDEventMulticaster JavaDoc(a, b);
174     }
175
176     /**
177      * Removes a listener from this multicaster and returns the
178      * resulting multicast listener.
179      * @param oldl the listener to be removed
180      */

181     protected EventListener JavaDoc remove(EventListener JavaDoc oldl) {
182         if (oldl == a) return b;
183         if (oldl == b) return a;
184         EventListener JavaDoc a2 = removeInternal(a, oldl);
185         EventListener JavaDoc b2 = removeInternal(b, oldl);
186         if (a2 == a && b2 == b) {
187             return this; // it's not here
188
}
189         return addInternal(a2, b2);
190     }
191
192     /**
193      * Returns the resulting multicast listener after removing the
194      * old listener from listener-l.
195      * If listener-l equals the old listener OR listener-l is null,
196      * returns null.
197      * Else if listener-l is an instance of AWTEventMulticaster,
198      * then it removes the old listener from it.
199      * Else, returns listener l.
200      * @param l the listener being removed from
201      * @param oldl the listener being removed
202      */

203     protected static EventListener JavaDoc removeInternal(EventListener JavaDoc l, EventListener JavaDoc oldl) {
204     if (l == oldl || l == null) {
205         return null;
206     } else if (l instanceof DnDEventMulticaster JavaDoc) {
207         return ((DnDEventMulticaster JavaDoc)l).remove(oldl);
208     } else {
209         return l; // it's not here
210
}
211     }
212       
213     protected static void save(ObjectOutputStream JavaDoc s, String JavaDoc k, EventListener JavaDoc l)
214       throws IOException JavaDoc {
215         AWTEventMulticaster.save(s, k, l);
216     }
217 }
218
Popular Tags