001 /*
002 * Copyright 1997-2006 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
026 package java.awt.dnd;
027
028 import java.awt.event.InputEvent;
029
030 /**
031 * The <code>DragSourceDragEvent</code> is
032 * delivered from the <code>DragSourceContextPeer</code>,
033 * via the <code>DragSourceContext</code>, to the <code>DragSourceListener</code>
034 * registered with that <code>DragSourceContext</code> and with its associated
035 * <code>DragSource</code>.
036 * <p>
037 * The <code>DragSourceDragEvent</code> reports the <i>target drop action</i>
038 * and the <i>user drop action</i> that reflect the current state of
039 * the drag operation.
040 * <p>
041 * <i>Target drop action</i> is one of <code>DnDConstants</code> that represents
042 * the drop action selected by the current drop target if this drop action is
043 * supported by the drag source or <code>DnDConstants.ACTION_NONE</code> if this
044 * drop action is not supported by the drag source.
045 * <p>
046 * <i>User drop action</i> depends on the drop actions supported by the drag
047 * source and the drop action selected by the user. The user can select a drop
048 * action by pressing modifier keys during the drag operation:
049 * <pre>
050 * Ctrl + Shift -> ACTION_LINK
051 * Ctrl -> ACTION_COPY
052 * Shift -> ACTION_MOVE
053 * </pre>
054 * If the user selects a drop action, the <i>user drop action</i> is one of
055 * <code>DnDConstants</code> that represents the selected drop action if this
056 * drop action is supported by the drag source or
057 * <code>DnDConstants.ACTION_NONE</code> if this drop action is not supported
058 * by the drag source.
059 * <p>
060 * If the user doesn't select a drop action, the set of
061 * <code>DnDConstants</code> that represents the set of drop actions supported
062 * by the drag source is searched for <code>DnDConstants.ACTION_MOVE</code>,
063 * then for <code>DnDConstants.ACTION_COPY</code>, then for
064 * <code>DnDConstants.ACTION_LINK</code> and the <i>user drop action</i> is the
065 * first constant found. If no constant is found the <i>user drop action</i>
066 * is <code>DnDConstants.ACTION_NONE</code>.
067 *
068 * @version 1.37, 05/05/07
069 * @since 1.2
070 *
071 */
072
073 public class DragSourceDragEvent extends DragSourceEvent {
074
075 private static final long serialVersionUID = 481346297933902471L;
076
077 /**
078 * Constructs a <code>DragSourceDragEvent</code>.
079 * This class is typically
080 * instantiated by the <code>DragSourceContextPeer</code>
081 * rather than directly
082 * by client code.
083 * The coordinates for this <code>DragSourceDragEvent</code>
084 * are not specified, so <code>getLocation</code> will return
085 * <code>null</code> for this event.
086 * <p>
087 * The arguments <code>dropAction</code> and <code>action</code> should
088 * be one of <code>DnDConstants</code> that represents a single action.
089 * The argument <code>modifiers</code> should be either a bitwise mask
090 * of old <code>java.awt.event.InputEvent.*_MASK</code> constants or a
091 * bitwise mask of extended <code>java.awt.event.InputEvent.*_DOWN_MASK</code>
092 * constants.
093 * This constructor does not throw any exception for invalid <code>dropAction</code>,
094 * <code>action</code> and <code>modifiers</code>.
095 *
096 * @param dsc the <code>DragSourceContext</code> that is to manage
097 * notifications for this event.
098 * @param dropAction the user drop action.
099 * @param action the target drop action.
100 * @param modifiers the modifier keys down during event (shift, ctrl,
101 * alt, meta)
102 * Either extended _DOWN_MASK or old _MASK modifiers
103 * should be used, but both models should not be mixed
104 * in one event. Use of the extended modifiers is
105 * preferred.
106 *
107 * @throws <code>IllegalArgumentException</code> if <code>dsc</code> is <code>null</code>.
108 *
109 * @see java.awt.event.InputEvent
110 * @see DragSourceEvent#getLocation
111 */
112
113 public DragSourceDragEvent(DragSourceContext dsc, int dropAction,
114 int action, int modifiers) {
115 super (dsc);
116
117 targetActions = action;
118 gestureModifiers = modifiers;
119 this .dropAction = dropAction;
120 if ((modifiers & ~(JDK_1_3_MODIFIERS | JDK_1_4_MODIFIERS)) != 0) {
121 invalidModifiers = true;
122 } else if ((getGestureModifiers() != 0)
123 && (getGestureModifiersEx() == 0)) {
124 setNewModifiers();
125 } else if ((getGestureModifiers() == 0)
126 && (getGestureModifiersEx() != 0)) {
127 setOldModifiers();
128 } else {
129 invalidModifiers = true;
130 }
131 }
132
133 /**
134 * Constructs a <code>DragSourceDragEvent</code> given the specified
135 * <code>DragSourceContext</code>, user drop action, target drop action,
136 * modifiers and coordinates.
137 * <p>
138 * The arguments <code>dropAction</code> and <code>action</code> should
139 * be one of <code>DnDConstants</code> that represents a single action.
140 * The argument <code>modifiers</code> should be either a bitwise mask
141 * of old <code>java.awt.event.InputEvent.*_MASK</code> constants or a
142 * bitwise mask of extended <code>java.awt.event.InputEvent.*_DOWN_MASK</code>
143 * constants.
144 * This constructor does not throw any exception for invalid <code>dropAction</code>,
145 * <code>action</code> and <code>modifiers</code>.
146 *
147 * @param dsc the <code>DragSourceContext</code> associated with this
148 * event.
149 * @param dropAction the user drop action.
150 * @param action the target drop action.
151 * @param modifiers the modifier keys down during event (shift, ctrl,
152 * alt, meta)
153 * Either extended _DOWN_MASK or old _MASK modifiers
154 * should be used, but both models should not be mixed
155 * in one event. Use of the extended modifiers is
156 * preferred.
157 * @param x the horizontal coordinate for the cursor location
158 * @param y the vertical coordinate for the cursor location
159 *
160 * @throws <code>IllegalArgumentException</code> if <code>dsc</code> is <code>null</code>.
161 *
162 * @see java.awt.event.InputEvent
163 * @since 1.4
164 */
165 public DragSourceDragEvent(DragSourceContext dsc, int dropAction,
166 int action, int modifiers, int x, int y) {
167 super (dsc, x, y);
168
169 targetActions = action;
170 gestureModifiers = modifiers;
171 this .dropAction = dropAction;
172 if ((modifiers & ~(JDK_1_3_MODIFIERS | JDK_1_4_MODIFIERS)) != 0) {
173 invalidModifiers = true;
174 } else if ((getGestureModifiers() != 0)
175 && (getGestureModifiersEx() == 0)) {
176 setNewModifiers();
177 } else if ((getGestureModifiers() == 0)
178 && (getGestureModifiersEx() != 0)) {
179 setOldModifiers();
180 } else {
181 invalidModifiers = true;
182 }
183 }
184
185 /**
186 * This method returns the target drop action.
187 *
188 * @return the target drop action.
189 */
190 public int getTargetActions() {
191 return targetActions;
192 }
193
194 private static final int JDK_1_3_MODIFIERS = InputEvent.SHIFT_DOWN_MASK - 1;
195 private static final int JDK_1_4_MODIFIERS = ((InputEvent.ALT_GRAPH_DOWN_MASK << 1) - 1)
196 & ~JDK_1_3_MODIFIERS;
197
198 /**
199 * This method returns an <code>int</code> representing
200 * the current state of the input device modifiers
201 * associated with the user's gesture. Typically these
202 * would be mouse buttons or keyboard modifiers.
203 * <P>
204 * If the <code>modifiers</code> passed to the constructor
205 * are invalid, this method returns them unchanged.
206 *
207 * @return the current state of the input device modifiers
208 */
209
210 public int getGestureModifiers() {
211 return invalidModifiers ? gestureModifiers : gestureModifiers
212 & JDK_1_3_MODIFIERS;
213 }
214
215 /**
216 * This method returns an <code>int</code> representing
217 * the current state of the input device extended modifiers
218 * associated with the user's gesture.
219 * See {@link InputEvent#getModifiersEx}
220 * <P>
221 * If the <code>modifiers</code> passed to the constructor
222 * are invalid, this method returns them unchanged.
223 *
224 * @return the current state of the input device extended modifiers
225 * @since 1.4
226 */
227
228 public int getGestureModifiersEx() {
229 return invalidModifiers ? gestureModifiers : gestureModifiers
230 & JDK_1_4_MODIFIERS;
231 }
232
233 /**
234 * This method returns the user drop action.
235 *
236 * @return the user drop action.
237 */
238 public int getUserAction() {
239 return dropAction;
240 }
241
242 /**
243 * This method returns the logical intersection of
244 * the target drop action and the set of drop actions supported by
245 * the drag source.
246 *
247 * @return the logical intersection of the target drop action and
248 * the set of drop actions supported by the drag source.
249 */
250 public int getDropAction() {
251 return targetActions
252 & getDragSourceContext().getSourceActions();
253 }
254
255 /*
256 * fields
257 */
258
259 /**
260 * The target drop action.
261 *
262 * @serial
263 */
264 private int targetActions = DnDConstants.ACTION_NONE;
265
266 /**
267 * The user drop action.
268 *
269 * @serial
270 */
271 private int dropAction = DnDConstants.ACTION_NONE;
272
273 /**
274 * The state of the input device modifiers associated with the user
275 * gesture.
276 *
277 * @serial
278 */
279 private int gestureModifiers = 0;
280
281 /**
282 * Indicates whether the <code>gestureModifiers</code> are invalid.
283 *
284 * @serial
285 */
286 private boolean invalidModifiers;
287
288 /**
289 * Sets new modifiers by the old ones.
290 * The mouse modifiers have higher priority than overlaying key
291 * modifiers.
292 */
293 private void setNewModifiers() {
294 if ((gestureModifiers & InputEvent.BUTTON1_MASK) != 0) {
295 gestureModifiers |= InputEvent.BUTTON1_DOWN_MASK;
296 }
297 if ((gestureModifiers & InputEvent.BUTTON2_MASK) != 0) {
298 gestureModifiers |= InputEvent.BUTTON2_DOWN_MASK;
299 }
300 if ((gestureModifiers & InputEvent.BUTTON3_MASK) != 0) {
301 gestureModifiers |= InputEvent.BUTTON3_DOWN_MASK;
302 }
303 if ((gestureModifiers & InputEvent.SHIFT_MASK) != 0) {
304 gestureModifiers |= InputEvent.SHIFT_DOWN_MASK;
305 }
306 if ((gestureModifiers & InputEvent.CTRL_MASK) != 0) {
307 gestureModifiers |= InputEvent.CTRL_DOWN_MASK;
308 }
309 if ((gestureModifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
310 gestureModifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
311 }
312 }
313
314 /**
315 * Sets old modifiers by the new ones.
316 */
317 private void setOldModifiers() {
318 if ((gestureModifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) {
319 gestureModifiers |= InputEvent.BUTTON1_MASK;
320 }
321 if ((gestureModifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) {
322 gestureModifiers |= InputEvent.BUTTON2_MASK;
323 }
324 if ((gestureModifiers & InputEvent.BUTTON3_DOWN_MASK) != 0) {
325 gestureModifiers |= InputEvent.BUTTON3_MASK;
326 }
327 if ((gestureModifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
328 gestureModifiers |= InputEvent.SHIFT_MASK;
329 }
330 if ((gestureModifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
331 gestureModifiers |= InputEvent.CTRL_MASK;
332 }
333 if ((gestureModifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
334 gestureModifiers |= InputEvent.ALT_GRAPH_MASK;
335 }
336 }
337 }
|