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