1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
/* * @(#)DragSourceDragEvent.java 1.31 06/04/04 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.awt.dnd; import java.awt.event.InputEvent; /** * The <code>DragSourceDragEvent</code> is * delivered from the <code>DragSourceContextPeer</code>, * via the <code>DragSourceContext</code>, to the <code>DragSourceListener</code> * registered with that <code>DragSourceContext</code> and with its associated * <code>DragSource</code>. * <p> * The <code>DragSourceDragEvent</code> reports the <i>target drop action</i> * and the <i>user drop action</i> that reflect the current state of * the drag operation. * <p> * <i>Target drop action</i> is one of <code>DnDConstants</code> that represents * the drop action selected by the current drop target if this drop action is * supported by the drag source or <code>DnDConstants.ACTION_NONE</code> if this * drop action is not supported by the drag source. * <p> * <i>User drop action</i> depends on the drop actions supported by the drag * source and the drop action selected by the user. The user can select a drop * action by pressing modifier keys during the drag operation: * <pre> * Ctrl + Shift -> ACTION_LINK * Ctrl -> ACTION_COPY * Shift -> ACTION_MOVE * </pre> * If the user selects a drop action, the <i>user drop action</i> is one of * <code>DnDConstants</code> that represents the selected drop action if this * drop action is supported by the drag source or * <code>DnDConstants.ACTION_NONE</code> if this drop action is not supported * by the drag source. * <p> * If the user doesn't select a drop action, the set of * <code>DnDConstants</code> that represents the set of drop actions supported * by the drag source is searched for <code>DnDConstants.ACTION_MOVE</code>, * then for <code>DnDConstants.ACTION_COPY</code>, then for * <code>DnDConstants.ACTION_LINK</code> and the <i>user drop action</i> is the * first constant found. If no constant is found the <i>user drop action</i> * is <code>DnDConstants.ACTION_NONE</code>. * * @version 1.31, 04/04/06 * @since 1.2 * */ public class DragSourceDragEvent extends DragSourceEvent { private static final long serialVersionUID = 481346297933902471L; /** * Constructs a <code>DragSourceDragEvent</code>. * This class is typically * instantiated by the <code>DragSourceContextPeer</code> * rather than directly * by client code. * The coordinates for this <code>DragSourceDragEvent</code> * are not specified, so <code>getLocation</code> will return * <code>null</code> for this event. * <p> * The arguments <code>dropAction</code> and <code>action</code> should * be one of <code>DnDConstants</code> that represents a single action. * The argument <code>modifiers</code> should be either a bitwise mask * of old <code>java.awt.event.InputEvent.*_MASK</code> constants or a * bitwise mask of extended <code>java.awt.event.InputEvent.*_DOWN_MASK</code> * constants. * This constructor does not throw any exception for invalid <code>dropAction</code>, * <code>action</code> and <code>modifiers</code>. * * @param dsc the <code>DragSourceContext</code> that is to manage * notifications for this event. * @param dropAction the user drop action. * @param action the target drop action. * @param modifiers the modifier keys down during event (shift, ctrl, * alt, meta) * Either extended _DOWN_MASK or old _MASK modifiers * should be used, but both models should not be mixed * in one event. Use of the extended modifiers is * preferred. * * @throws <code>IllegalArgumentException</code> if <code>dsc</code> is <code>null</code>. * * @see java.awt.event.InputEvent * @see DragSourceEvent#getLocation */ public DragSourceDragEvent(DragSourceContext dsc, int dropAction, int action, int modifiers) { super(dsc); targetActions = action; gestureModifiers = modifiers; this.dropAction = dropAction; if ((modifiers & ~(JDK_1_3_MODIFIERS | JDK_1_4_MODIFIERS)) != 0) { invalidModifiers = true; } else if ((getGestureModifiers() != 0) && (getGestureModifiersEx() == 0)) { setNewModifiers(); } else if ((getGestureModifiers() == 0) && (getGestureModifiersEx() != 0)) { setOldModifiers(); } else { invalidModifiers = true; } } /** * Constructs a <code>DragSourceDragEvent</code> given the specified * <code>DragSourceContext</code>, user drop action, target drop action, * modifiers and coordinates. * <p> * The arguments <code>dropAction</code> and <code>action</code> should * be one of <code>DnDConstants</code> that represents a single action. * The argument <code>modifiers</code> should be either a bitwise mask * of old <code>java.awt.event.InputEvent.*_MASK</code> constants or a * bitwise mask of extended <code>java.awt.event.InputEvent.*_DOWN_MASK</code> * constants. * This constructor does not throw any exception for invalid <code>dropAction</code>, * <code>action</code> and <code>modifiers</code>. * * @param dsc the <code>DragSourceContext</code> associated with this * event. * @param dropAction the user drop action. * @param action the target drop action. * @param modifiers the modifier keys down during event (shift, ctrl, * alt, meta) * Either extended _DOWN_MASK or old _MASK modifiers * should be used, but both models should not be mixed * in one event. Use of the extended modifiers is * preferred. * @param x the horizontal coordinate for the cursor location * @param y the vertical coordinate for the cursor location * * @throws <code>IllegalArgumentException</code> if <code>dsc</code> is <code>null</code>. * * @see java.awt.event.InputEvent * @since 1.4 */ public DragSourceDragEvent(DragSourceContext dsc, int dropAction, int action, int modifiers, int x, int y) { super(dsc, x, y); targetActions = action; gestureModifiers = modifiers; this.dropAction = dropAction; if ((modifiers & ~(JDK_1_3_MODIFIERS | JDK_1_4_MODIFIERS)) != 0) { invalidModifiers = true; } else if ((getGestureModifiers() != 0) && (getGestureModifiersEx() == 0)) { setNewModifiers(); } else if ((getGestureModifiers() == 0) && (getGestureModifiersEx() != 0)) { setOldModifiers(); } else { invalidModifiers = true; } } /** * This method returns the target drop action. * * @return the target drop action. */ public int getTargetActions() { return targetActions; } private static final int JDK_1_3_MODIFIERS = InputEvent.SHIFT_DOWN_MASK - 1; private static final int JDK_1_4_MODIFIERS = ((InputEvent.ALT_GRAPH_DOWN_MASK << 1) - 1) & ~JDK_1_3_MODIFIERS; /** * This method returns an <code>int</code> representing * the current state of the input device modifiers * associated with the user's gesture. Typically these * would be mouse buttons or keyboard modifiers. * <P> * If the <code>modifiers</code> passed to the constructor * are invalid, this method returns them unchanged. * * @return the current state of the input device modifiers */ public int getGestureModifiers() { return invalidModifiers ? gestureModifiers : gestureModifiers & JDK_1_3_MODIFIERS; } /** * This method returns an <code>int</code> representing * the current state of the input device extended modifiers * associated with the user's gesture. * See {@link InputEvent#getModifiersEx} * <P> * If the <code>modifiers</code> passed to the constructor * are invalid, this method returns them unchanged. * * @return the current state of the input device extended modifiers * @since 1.4 */ public int getGestureModifiersEx() { return invalidModifiers ? gestureModifiers : gestureModifiers & JDK_1_4_MODIFIERS; } /** * This method returns the user drop action. * * @return the user drop action. */ public int getUserAction() { return dropAction; } /** * This method returns the logical intersection of * the target drop action and the set of drop actions supported by * the drag source. * * @return the logical intersection of the target drop action and * the set of drop actions supported by the drag source. */ public int getDropAction() { return targetActions & getDragSourceContext().getSourceActions(); } /* * fields */ /** * The target drop action. * * @serial */ private int targetActions = DnDConstants.ACTION_NONE; /** * The user drop action. * * @serial */ private int dropAction = DnDConstants.ACTION_NONE; /** * The state of the input device modifiers associated with the user * gesture. * * @serial */ private int gestureModifiers = 0; /** * Indicates whether the <code>gestureModifiers</code> are invalid. * * @serial */ private boolean invalidModifiers; /** * Sets new modifiers by the old ones. * The mouse modifiers have higher priority than overlaying key * modifiers. */ private void setNewModifiers() { if ((gestureModifiers & InputEvent.BUTTON1_MASK) != 0) { gestureModifiers |= InputEvent.BUTTON1_DOWN_MASK; } if ((gestureModifiers & InputEvent.BUTTON2_MASK) != 0) { gestureModifiers |= InputEvent.BUTTON2_DOWN_MASK; } if ((gestureModifiers & InputEvent.BUTTON3_MASK) != 0) { gestureModifiers |= InputEvent.BUTTON3_DOWN_MASK; } if ((gestureModifiers & InputEvent.SHIFT_MASK) != 0) { gestureModifiers |= InputEvent.SHIFT_DOWN_MASK; } if ((gestureModifiers & InputEvent.CTRL_MASK) != 0) { gestureModifiers |= InputEvent.CTRL_DOWN_MASK; } if ((gestureModifiers & InputEvent.ALT_GRAPH_MASK) != 0) { gestureModifiers |= InputEvent.ALT_GRAPH_DOWN_MASK; } } /** * Sets old modifiers by the new ones. */ private void setOldModifiers() { if ((gestureModifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) { gestureModifiers |= InputEvent.BUTTON1_MASK; } if ((gestureModifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) { gestureModifiers |= InputEvent.BUTTON2_MASK; } if ((gestureModifiers & InputEvent.BUTTON3_DOWN_MASK) != 0) { gestureModifiers |= InputEvent.BUTTON3_MASK; } if ((gestureModifiers & InputEvent.SHIFT_DOWN_MASK) != 0) { gestureModifiers |= InputEvent.SHIFT_MASK; } if ((gestureModifiers & InputEvent.CTRL_DOWN_MASK) != 0) { gestureModifiers |= InputEvent.CTRL_MASK; } if ((gestureModifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) { gestureModifiers |= InputEvent.ALT_GRAPH_MASK; } } }