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