KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DragSourceDragEvent.java 1.29 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
8 package java.awt.dnd;
9
10 import java.awt.event.InputEvent JavaDoc;
11
12 /**
13  * The <code>DragSourceDragEvent</code> is
14  * delivered from the <code>DragSourceContextPeer</code>,
15  * via the <code>DragSourceContext</code>, to the <code>DragSourceListener</code>
16  * registered with that <code>DragSourceContext</code> and with its associated
17  * <code>DragSource</code>.
18  * <p>
19  * The <code>DragSourceDragEvent</code> reports the <i>target drop action</i>
20  * and the <i>user drop action</i> that reflect the current state of
21  * the drag operation.
22  * <p>
23  * <i>Target drop action</i> is one of <code>DnDConstants</code> that represents
24  * the drop action selected by the current drop target if this drop action is
25  * supported by the drag source or <code>DnDConstants.ACTION_NONE</code> if this
26  * drop action is not supported by the drag source.
27  * <p>
28  * <i>User drop action</i> depends on the drop actions supported by the drag
29  * source and the drop action selected by the user. The user can select a drop
30  * action by pressing modifier keys during the drag operation:
31  * <pre>
32  * Ctrl + Shift -> ACTION_LINK
33  * Ctrl -> ACTION_COPY
34  * Shift -> ACTION_MOVE
35  * </pre>
36  * If the user selects a drop action, the <i>user drop action</i> is one of
37  * <code>DnDConstants</code> that represents the selected drop action if this
38  * drop action is supported by the drag source or
39  * <code>DnDConstants.ACTION_NONE</code> if this drop action is not supported
40  * by the drag source.
41  * <p>
42  * If the user doesn't select a drop action, the set of
43  * <code>DnDConstants</code> that represents the set of drop actions supported
44  * by the drag source is searched for <code>DnDConstants.ACTION_MOVE</code>,
45  * then for <code>DnDConstants.ACTION_COPY</code>, then for
46  * <code>DnDConstants.ACTION_LINK</code> and the <i>user drop action</i> is the
47  * first constant found. If no constant is found the <i>user drop action</i>
48  * is <code>DnDConstants.ACTION_NONE</code>.
49  *
50  * @version 1.29, 12/19/03
51  * @since 1.2
52  *
53  */

54
55 public class DragSourceDragEvent extends DragSourceEvent JavaDoc {
56
57     private static final long serialVersionUID = 481346297933902471L;
58
59     /**
60      * Constructs a <code>DragSourceDragEvent</code>.
61      * This class is typically
62      * instantiated by the <code>DragSourceContextPeer</code>
63      * rather than directly
64      * by client code.
65      * The coordinates for this <code>DragSourceDragEvent</code>
66      * are not specified, so <code>getLocation</code> will return
67      * <code>null</code> for this event.
68      * <p>
69      * The arguments <code>dropAction</code> and <code>action</code> should
70      * be one of <code>DnDConstants</code> that represents a single action.
71      * The argument <code>modifiers</code> should be either a bitwise mask
72      * of old <code>java.awt.event.InputEvent.*_MASK</code> constants or a
73      * bitwise mask of extended <code>java.awt.event.InputEvent.*_DOWN_MASK</code>
74      * constants.
75      * This constructor does not throw any exception for invalid <code>dropAction</code>,
76      * <code>action</code> and <code>modifiers</code>.
77      *
78      * @param dsc the <code>DragSourceContext</code> that is to manage
79      * notifications for this event.
80      * @param dropAction the user drop action.
81      * @param action the target drop action.
82      * @param modifiers the modifier keys down during event (shift, ctrl,
83      * alt, meta)
84      * Either extended _DOWN_MASK or old _MASK modifiers
85      * should be used, but both models should not be mixed
86      * in one event. Use of the extended modifiers is
87      * preferred.
88      *
89      * @throws <code>IllegalArgumentException</code> if <code>dsc</code> is <code>null</code>.
90      *
91      * @see java.awt.event.InputEvent
92      * @see DragSourceEvent#getLocation
93      */

94
95     public DragSourceDragEvent(DragSourceContext JavaDoc dsc, int dropAction,
96                                int action, int modifiers) {
97     super(dsc);
98     
99     targetActions = action;
100     gestureModifiers = modifiers;
101     this.dropAction = dropAction;
102         if ((modifiers & ~(JDK_1_3_MODIFIERS | JDK_1_4_MODIFIERS)) != 0) {
103             invalidModifiers = true;
104         } else if ((getGestureModifiers() != 0) && (getGestureModifiersEx() == 0)) {
105         setNewModifiers();
106     } else if ((getGestureModifiers() == 0) && (getGestureModifiersEx() != 0)) {
107         setOldModifiers();
108     } else {
109             invalidModifiers = true;
110         }
111     }
112
113     /**
114      * Constructs a <code>DragSourceDragEvent</code> given the specified
115      * <code>DragSourceContext</code>, user drop action, target drop action,
116      * modifiers and coordinates.
117      * <p>
118      * The arguments <code>dropAction</code> and <code>action</code> should
119      * be one of <code>DnDConstants</code> that represents a single action.
120      * The argument <code>modifiers</code> should be either a bitwise mask
121      * of old <code>java.awt.event.InputEvent.*_MASK</code> constants or a
122      * bitwise mask of extended <code>java.awt.event.InputEvent.*_DOWN_MASK</code>
123      * constants.
124      * This constructor does not throw any exception for invalid <code>dropAction</code>,
125      * <code>action</code> and <code>modifiers</code>.
126      *
127      * @param dsc the <code>DragSourceContext</code> associated with this
128      * event.
129      * @param dropAction the user drop action.
130      * @param action the target drop action.
131      * @param modifiers the modifier keys down during event (shift, ctrl,
132      * alt, meta)
133      * Either extended _DOWN_MASK or old _MASK modifiers
134      * should be used, but both models should not be mixed
135      * in one event. Use of the extended modifiers is
136      * preferred.
137      * @param x the horizontal coordinate for the cursor location
138      * @param y the vertical coordinate for the cursor location
139      *
140      * @throws <code>IllegalArgumentException</code> if <code>dsc</code> is <code>null</code>.
141      *
142      * @see java.awt.event.InputEvent
143      * @since 1.4
144      */

145     public DragSourceDragEvent(DragSourceContext JavaDoc dsc, int dropAction,
146                                int action, int modifiers, int x, int y) {
147         super(dsc, x, y);
148
149         targetActions = action;
150         gestureModifiers = modifiers;
151         this.dropAction = dropAction;
152         if ((modifiers & ~(JDK_1_3_MODIFIERS | JDK_1_4_MODIFIERS)) != 0) {
153             invalidModifiers = true;
154         } else if ((getGestureModifiers() != 0) && (getGestureModifiersEx() == 0)) {
155         setNewModifiers();
156     } else if ((getGestureModifiers() == 0) && (getGestureModifiersEx() != 0)) {
157         setOldModifiers();
158     } else {
159             invalidModifiers = true;
160         }
161     }
162
163     /**
164      * This method returns the target drop action.
165      *
166      * @return the target drop action.
167      */

168     public int getTargetActions() {
169     return targetActions;
170     }
171
172
173     private static final int JDK_1_3_MODIFIERS = InputEvent.SHIFT_DOWN_MASK - 1;
174     private static final int JDK_1_4_MODIFIERS =
175             ((InputEvent.ALT_GRAPH_DOWN_MASK << 1) - 1) & ~JDK_1_3_MODIFIERS;
176
177     /**
178      * This method returns an <code>int</code> representing
179      * the current state of the input device modifiers
180      * associated with the user's gesture. Typically these
181      * would be mouse buttons or keyboard modifiers.
182      * <P>
183      * If the <code>modifiers</code> passed to the constructor
184      * are invalid, this method returns them unchanged.
185      *
186      * @return the current state of the input device modifiers
187      */

188
189     public int getGestureModifiers() {
190     return invalidModifiers ? gestureModifiers : gestureModifiers & JDK_1_3_MODIFIERS;
191     }
192
193     /**
194      * This method returns an <code>int</code> representing
195      * the current state of the input device extended modifiers
196      * associated with the user's gesture.
197      * See {@link InputEvent#getModifiersEx}
198      * <P>
199      * If the <code>modifiers</code> passed to the constructor
200      * are invalid, this method returns them unchanged.
201      *
202      * @return the current state of the input device extended modifiers
203      * @since 1.4
204      */

205
206     public int getGestureModifiersEx() {
207     return invalidModifiers ? gestureModifiers : gestureModifiers & JDK_1_4_MODIFIERS;
208     }
209
210     /**
211      * This method returns the user drop action.
212      *
213      * @return the user drop action.
214      */

215     public int getUserAction() { return dropAction; }
216
217     /**
218      * This method returns the logical intersection of the user drop action,
219      * the target drop action and the set of drop actions supported by
220      * the drag source.
221      *
222      * @return the logical intersection of the user drop action, the target drop
223      * action and the set of drop actions supported by the drag source.
224      */

225     public int getDropAction() {
226     return dropAction & targetActions & getDragSourceContext().getSourceActions();
227     }
228
229     /*
230      * fields
231      */

232
233     /**
234      * The target drop action.
235      *
236      * @serial
237      */

238     private int targetActions = DnDConstants.ACTION_NONE;
239
240     /**
241      * The user drop action.
242      *
243      * @serial
244      */

245     private int dropAction = DnDConstants.ACTION_NONE;
246
247     /**
248      * The state of the input device modifiers associated with the user
249      * gesture.
250      *
251      * @serial
252      */

253     private int gestureModifiers = 0;
254
255     /**
256      * Indicates whether the <code>gestureModifiers</code> are invalid.
257      *
258      * @serial
259      */

260     private boolean invalidModifiers;
261
262     /**
263      * Sets new modifiers by the old ones.
264      * The mouse modifiers have higher priority than overlaying key
265      * modifiers.
266      */

267     private void setNewModifiers() {
268         if ((gestureModifiers & InputEvent.BUTTON1_MASK) != 0) {
269         gestureModifiers |= InputEvent.BUTTON1_DOWN_MASK;
270     }
271     if ((gestureModifiers & InputEvent.BUTTON2_MASK) != 0) {
272         gestureModifiers |= InputEvent.BUTTON2_DOWN_MASK;
273     }
274     if ((gestureModifiers & InputEvent.BUTTON3_MASK) != 0) {
275         gestureModifiers |= InputEvent.BUTTON3_DOWN_MASK;
276     }
277     if ((gestureModifiers & InputEvent.SHIFT_MASK) != 0) {
278         gestureModifiers |= InputEvent.SHIFT_DOWN_MASK;
279     }
280     if ((gestureModifiers & InputEvent.CTRL_MASK) != 0) {
281         gestureModifiers |= InputEvent.CTRL_DOWN_MASK;
282     }
283     if ((gestureModifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
284         gestureModifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
285     }
286     }
287
288     /**
289      * Sets old modifiers by the new ones.
290      */

291     private void setOldModifiers() {
292     if ((gestureModifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) {
293         gestureModifiers |= InputEvent.BUTTON1_MASK;
294         }
295         if ((gestureModifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) {
296             gestureModifiers |= InputEvent.BUTTON2_MASK;
297         }
298     if ((gestureModifiers & InputEvent.BUTTON3_DOWN_MASK) != 0) {
299         gestureModifiers |= InputEvent.BUTTON3_MASK;
300         }
301     if ((gestureModifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
302         gestureModifiers |= InputEvent.SHIFT_MASK;
303     }
304     if ((gestureModifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
305         gestureModifiers |= InputEvent.CTRL_MASK;
306     }
307     if ((gestureModifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
308         gestureModifiers |= InputEvent.ALT_GRAPH_MASK;
309     }
310     }
311 }
312
Popular Tags