1. /*
  2. * @(#)DragSourceDragEvent.java 1.26 03/01/23
  3. *
  4. * Copyright 2003 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.26, 01/23/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. *
  63. * @param dsc the <code>DragSourceContext</code> that is to manage
  64. * notifications for this event.
  65. * @param dropAction the user drop action.
  66. * @param actions the target drop action.
  67. * @param modifiers the modifier keys down during event (shift, ctrl,
  68. * alt, meta)
  69. * Either extended _DOWN_MASK or old _MASK modifiers
  70. * should be used, but both models should not be mixed
  71. * in one event. Use of the extended modifiers is
  72. * preferred.
  73. * @see java.awt.event.InputEvent
  74. * @see DragSourceEvent#getLocation
  75. */
  76. public DragSourceDragEvent(DragSourceContext dsc, int dropAction,
  77. int actions, int modifiers) {
  78. super(dsc);
  79. targetActions = actions;
  80. gestureModifiers = modifiers;
  81. this.dropAction = dropAction;
  82. }
  83. /**
  84. * Constructs a <code>DragSourceDragEvent</code> given the specified
  85. * <code>DragSourceContext</code>, user drop action, target drop action,
  86. * modifiers and coordinates.
  87. *
  88. * @param dsc the <code>DragSourceContext</code> associated with this
  89. * event.
  90. * @param dropAction the user drop action.
  91. * @param actions the target drop action.
  92. * @param modifiers the modifier keys down during event (shift, ctrl,
  93. * alt, meta)
  94. * Either extended _DOWN_MASK or old _MASK modifiers
  95. * should be used, but both models should not be mixed
  96. * in one event. Use of the extended modifiers is
  97. * preferred.
  98. * @param x the horizontal coordinate for the cursor location
  99. * @param y the vertical coordinate for the cursor location
  100. * @see java.awt.event.InputEvent
  101. * @since 1.4
  102. */
  103. public DragSourceDragEvent(DragSourceContext dsc, int dropAction,
  104. int actions, int modifiers, int x, int y) {
  105. super(dsc, x, y);
  106. targetActions = actions;
  107. gestureModifiers = modifiers;
  108. this.dropAction = dropAction;
  109. if ((getGestureModifiers() != 0) && (getGestureModifiersEx() == 0)) {
  110. setNewModifiers();
  111. } else if ((getGestureModifiers() == 0) && (getGestureModifiersEx() != 0)) {
  112. setOldModifiers();
  113. }
  114. }
  115. /**
  116. * This method returns the target drop action.
  117. *
  118. * @return the target drop action.
  119. */
  120. public int getTargetActions() {
  121. return targetActions;
  122. }
  123. static final int JDK_1_3_MODIFIERS = InputEvent.SHIFT_DOWN_MASK - 1;
  124. /**
  125. * This method returns an <code>int</code> representing
  126. * the current state of the input device modifiers
  127. * associated with the user's gesture. Typically these
  128. * would be mouse buttons or keyboard modifiers.
  129. * <P>
  130. * @return the current state of the input device modifiers
  131. */
  132. public int getGestureModifiers() {
  133. return gestureModifiers & JDK_1_3_MODIFIERS;
  134. }
  135. /**
  136. * This method returns an <code>int</code> representing
  137. * the current state of the input device extended modifiers
  138. * associated with the user's gesture.
  139. * See {@link InputEvent#getModifiersEx}
  140. * <P>
  141. * @return the current state of the input device extended modifiers
  142. * @since 1.4
  143. */
  144. public int getGestureModifiersEx() {
  145. return gestureModifiers & ~JDK_1_3_MODIFIERS;
  146. }
  147. /**
  148. * This method returns the user drop action.
  149. *
  150. * @return the user drop action.
  151. */
  152. public int getUserAction() { return dropAction; }
  153. /**
  154. * This method returns the logical intersection of the user drop action,
  155. * the target drop action and the set of drop actions supported by
  156. * the drag source.
  157. *
  158. * @return the logical intersection of the user drop action, the target drop
  159. * action and the set of drop actions supported by the drag source.
  160. */
  161. public int getDropAction() {
  162. return dropAction & targetActions & getDragSourceContext().getSourceActions();
  163. }
  164. /*
  165. * fields
  166. */
  167. /**
  168. * The target drop action.
  169. *
  170. * @serial
  171. */
  172. private int targetActions = DnDConstants.ACTION_NONE;
  173. /**
  174. * The user drop action.
  175. *
  176. * @serial
  177. */
  178. private int dropAction = DnDConstants.ACTION_NONE;
  179. /**
  180. * The state of the input device modifiers associated with the user
  181. * gesture.
  182. *
  183. * @serial
  184. */
  185. private int gestureModifiers = 0;
  186. /**
  187. * Sets new modifiers by the old ones.
  188. * The mouse modifiers have higher priority than overlaying key
  189. * modifiers.
  190. */
  191. private void setNewModifiers() {
  192. if ((gestureModifiers & InputEvent.BUTTON1_MASK) != 0) {
  193. gestureModifiers |= InputEvent.BUTTON1_DOWN_MASK;
  194. }
  195. if ((gestureModifiers & InputEvent.BUTTON2_MASK) != 0) {
  196. gestureModifiers |= InputEvent.BUTTON2_DOWN_MASK;
  197. }
  198. if ((gestureModifiers & InputEvent.BUTTON3_MASK) != 0) {
  199. gestureModifiers |= InputEvent.BUTTON3_DOWN_MASK;
  200. }
  201. if ((gestureModifiers & InputEvent.SHIFT_MASK) != 0) {
  202. gestureModifiers |= InputEvent.SHIFT_DOWN_MASK;
  203. }
  204. if ((gestureModifiers & InputEvent.CTRL_MASK) != 0) {
  205. gestureModifiers |= InputEvent.CTRL_DOWN_MASK;
  206. }
  207. if ((gestureModifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
  208. gestureModifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
  209. }
  210. }
  211. /**
  212. * Sets old modifiers by the new ones.
  213. */
  214. private void setOldModifiers() {
  215. if ((gestureModifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) {
  216. gestureModifiers |= InputEvent.BUTTON1_MASK;
  217. }
  218. if ((gestureModifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) {
  219. gestureModifiers |= InputEvent.BUTTON2_MASK;
  220. }
  221. if ((gestureModifiers & InputEvent.BUTTON3_DOWN_MASK) != 0) {
  222. gestureModifiers |= InputEvent.BUTTON3_MASK;
  223. }
  224. if ((gestureModifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
  225. gestureModifiers |= InputEvent.SHIFT_MASK;
  226. }
  227. if ((gestureModifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
  228. gestureModifiers |= InputEvent.CTRL_MASK;
  229. }
  230. if ((gestureModifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
  231. gestureModifiers |= InputEvent.ALT_GRAPH_MASK;
  232. }
  233. }
  234. }