1. /*
  2. * @(#)DropTargetDragEvent.java 1.24 04/05/05
  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.Point;
  9. import java.awt.datatransfer.DataFlavor;
  10. import java.awt.datatransfer.Transferable;
  11. import java.util.List;
  12. /**
  13. * The <code>DropTargetDragEvent</code> is delivered to a
  14. * <code>DropTargetListener</code> via its
  15. * dragEnter() and dragOver() methods.
  16. * <p>
  17. * The <code>DropTargetDragEvent</code> reports the <i>source drop actions</i>
  18. * and the <i>user drop action</i> that reflect the current state of
  19. * the drag operation.
  20. * <p>
  21. * <i>Source drop actions</i> is a bitwise mask of <code>DnDConstants</code>
  22. * that represents the set of drop actions supported by the drag source for
  23. * this drag operation.
  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.24, 05/05/04
  48. * @since 1.2
  49. */
  50. public class DropTargetDragEvent extends DropTargetEvent {
  51. private static final long serialVersionUID = -8422265619058953682L;
  52. /**
  53. * Construct a <code>DropTargetDragEvent</code> given the
  54. * <code>DropTargetContext</code> for this operation,
  55. * the location of the "Drag" <code>Cursor</code>'s hotspot
  56. * in the <code>Component</code>'s coordinates, the
  57. * user drop action, and the source drop actions.
  58. * <P>
  59. * @param dtc The DropTargetContext for this operation
  60. * @param cursorLocn The location of the "Drag" Cursor's
  61. * hotspot in Component coordinates
  62. * @param dropAction The user drop action
  63. * @param srcActions The source drop actions
  64. *
  65. * @throws NullPointerException if cursorLocn is null
  66. * @throws <code>IllegalArgumentException</code> if dropAction is not one of
  67. * <code>DnDConstants</code>.
  68. * @throws <code>IllegalArgumentException</code> if srcActions is not
  69. * a bitwise mask of <code>DnDConstants</code>.
  70. * @throws <code>IllegalArgumentException</code> if dtc is <code>null</code>.
  71. */
  72. public DropTargetDragEvent(DropTargetContext dtc, Point cursorLocn, int dropAction, int srcActions) {
  73. super(dtc);
  74. if (cursorLocn == null) throw new NullPointerException("cursorLocn");
  75. if (dropAction != DnDConstants.ACTION_NONE &&
  76. dropAction != DnDConstants.ACTION_COPY &&
  77. dropAction != DnDConstants.ACTION_MOVE &&
  78. dropAction != DnDConstants.ACTION_LINK
  79. ) throw new IllegalArgumentException("dropAction" + dropAction);
  80. if ((srcActions & ~(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK)) != 0) throw new IllegalArgumentException("srcActions");
  81. location = cursorLocn;
  82. actions = srcActions;
  83. this.dropAction = dropAction;
  84. }
  85. /**
  86. * This method returns a <code>Point</code>
  87. * indicating the <code>Cursor</code>'s current
  88. * location within the <code>Component'</code>s
  89. * coordinates.
  90. * <P>
  91. * @return the current cursor location in
  92. * <code>Component</code>'s coords.
  93. */
  94. public Point getLocation() {
  95. return location;
  96. }
  97. /**
  98. * This method returns the current <code>DataFlavor</code>s from the
  99. * <code>DropTargetContext</code>.
  100. * <P>
  101. * @return current DataFlavors from the DropTargetContext
  102. */
  103. public DataFlavor[] getCurrentDataFlavors() {
  104. return getDropTargetContext().getCurrentDataFlavors();
  105. }
  106. /**
  107. * This method returns the current <code>DataFlavor</code>s
  108. * as a <code>java.util.List</code>
  109. * <P>
  110. * @return a <code>java.util.List</code> of the Current <code>DataFlavor</code>s
  111. */
  112. public List<DataFlavor> getCurrentDataFlavorsAsList() {
  113. return getDropTargetContext().getCurrentDataFlavorsAsList();
  114. }
  115. /**
  116. * This method returns a <code>boolean</code> indicating
  117. * if the specified <code>DataFlavor</code> is supported.
  118. * <P>
  119. * @param df the <code>DataFlavor</code> to test
  120. * <P>
  121. * @return if a particular DataFlavor is supported
  122. */
  123. public boolean isDataFlavorSupported(DataFlavor df) {
  124. return getDropTargetContext().isDataFlavorSupported(df);
  125. }
  126. /**
  127. * This method returns the source drop actions.
  128. *
  129. * @return the source drop actions
  130. */
  131. public int getSourceActions() { return actions; }
  132. /**
  133. * This method returns the user drop action.
  134. *
  135. * @return the user drop action
  136. */
  137. public int getDropAction() { return dropAction; }
  138. /**
  139. * This method returns the Transferable object that represents
  140. * the data associated with the current drag operation.
  141. *
  142. * @return the Transferable associated with the drag operation
  143. * @throws InvalidDnDOperationException if the data associated with the drag
  144. * operation is not available
  145. *
  146. * @since 1.5
  147. */
  148. public Transferable getTransferable() {
  149. return getDropTargetContext().getTransferable();
  150. }
  151. /**
  152. * Accepts the drag.
  153. *
  154. * This method should be called from a
  155. * <code>DropTargetListeners</code> <code>dragEnter</code>,
  156. * <code>dragOver</code>, and <code>dropActionChanged</code>
  157. * methods if the implementation wishes to accept an operation
  158. * from the srcActions other than the one selected by
  159. * the user as represented by the <code>dropAction</code>.
  160. *
  161. * @param dragOperation the operation accepted by the target
  162. */
  163. public void acceptDrag(int dragOperation) {
  164. getDropTargetContext().acceptDrag(dragOperation);
  165. }
  166. /**
  167. * Rejects the drag as a result of examining either the
  168. * <code>dropAction</code> or the available <code>DataFlavor</code>
  169. * types.
  170. */
  171. public void rejectDrag() {
  172. getDropTargetContext().rejectDrag();
  173. }
  174. /*
  175. * fields
  176. */
  177. /**
  178. * The location of the drag cursor's hotspot in Component coordinates.
  179. *
  180. * @serial
  181. */
  182. private Point location;
  183. /**
  184. * The source drop actions.
  185. *
  186. * @serial
  187. */
  188. private int actions;
  189. /**
  190. * The user drop action.
  191. *
  192. * @serial
  193. */
  194. private int dropAction;
  195. }