1. /*
  2. * @(#)DropTargetDropEvent.java 1.28 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>DropTargetDropEvent</code> is delivered
  14. * via the <code>DropTargetListener</code> drop() method.
  15. * <p>
  16. * The <code>DropTargetDropEvent</code> reports the <i>source drop actions</i>
  17. * and the <i>user drop action</i> that reflect the current state of the
  18. * drag-and-drop 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-and-drop 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.28, 05/05/04
  47. * @since 1.2
  48. */
  49. public class DropTargetDropEvent extends DropTargetEvent {
  50. private static final long serialVersionUID = -1721911170440459322L;
  51. /**
  52. * Construct a <code>DropTargetDropEvent</code> given
  53. * the <code>DropTargetContext</code> for this operation,
  54. * the location of the drag <code>Cursor</code>'s
  55. * hotspot in the <code>Component</code>'s coordinates,
  56. * the currently
  57. * selected user drop action, and the current set of
  58. * actions supported by the source.
  59. * By default, this constructor
  60. * assumes that the target is not in the same virtual machine as
  61. * the source; that is, {@link #isLocalTransfer()} will
  62. * return <code>false</code>.
  63. * <P>
  64. * @param dtc The <code>DropTargetContext</code> for this operation
  65. * @param cursorLocn The location of the "Drag" Cursor's
  66. * hotspot in <code>Component</code> coordinates
  67. * @param dropAction the user drop action.
  68. * @param srcActions the source drop actions.
  69. *
  70. * @throws <code>NullPointerException</code>
  71. * if cursorLocn is <code>null</code>
  72. * @throws <code>IllegalArgumentException</code> if dropAction is not one of
  73. * <code>DnDConstants</code>.
  74. * @throws <code>IllegalArgumentException</code> if srcActions is not
  75. * a bitwise mask of <code>DnDConstants</code>.
  76. * @throws <code>IllegalArgumentException</code> if dtc is <code>null</code>.
  77. */
  78. public DropTargetDropEvent(DropTargetContext dtc, Point cursorLocn, int dropAction, int srcActions) {
  79. super(dtc);
  80. if (cursorLocn == null) throw new NullPointerException("cursorLocn");
  81. if (dropAction != DnDConstants.ACTION_NONE &&
  82. dropAction != DnDConstants.ACTION_COPY &&
  83. dropAction != DnDConstants.ACTION_MOVE &&
  84. dropAction != DnDConstants.ACTION_LINK
  85. ) throw new IllegalArgumentException("dropAction = " + dropAction);
  86. if ((srcActions & ~(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK)) != 0) throw new IllegalArgumentException("srcActions");
  87. location = cursorLocn;
  88. actions = srcActions;
  89. this.dropAction = dropAction;
  90. }
  91. /**
  92. * Construct a <code>DropTargetEvent</code> given the
  93. * <code>DropTargetContext</code> for this operation,
  94. * the location of the drag <code>Cursor</code>'s hotspot
  95. * in the <code>Component</code>'s
  96. * coordinates, the currently selected user drop action,
  97. * the current set of actions supported by the source,
  98. * and a <code>boolean</code> indicating if the source is in the same JVM
  99. * as the target.
  100. * <P>
  101. * @param dtc The DropTargetContext for this operation
  102. * @param cursorLocn The location of the "Drag" Cursor's
  103. * hotspot in Component's coordinates
  104. * @param dropAction the user drop action.
  105. * @param srcActions the source drop actions.
  106. * @param isLocal True if the source is in the same JVM as the target
  107. *
  108. * @throws <code>NullPointerException</code> if cursorLocn is
  109. * <code>null</code>
  110. * @throws <code>IllegalArgumentException</code> if dropAction is not one of
  111. * <code>DnDConstants</code>.
  112. * @throws <code>IllegalArgumentException</code> if srcActions is not
  113. * a bitwise mask of <code>DnDConstants</code>.
  114. * @throws <code>IllegalArgumentException</code> if dtc is <code>null</code>.
  115. */
  116. public DropTargetDropEvent(DropTargetContext dtc, Point cursorLocn, int dropAction, int srcActions, boolean isLocal) {
  117. this(dtc, cursorLocn, dropAction, srcActions);
  118. isLocalTx = isLocal;
  119. }
  120. /**
  121. * This method returns a <code>Point</code>
  122. * indicating the <code>Cursor</code>'s current
  123. * location in the <code>Component</code>'s coordinates.
  124. * <P>
  125. * @return the current <code>Cursor</code> location in Component's coords.
  126. */
  127. public Point getLocation() {
  128. return location;
  129. }
  130. /**
  131. * This method returns the current DataFlavors.
  132. * <P>
  133. * @return current DataFlavors
  134. */
  135. public DataFlavor[] getCurrentDataFlavors() {
  136. return getDropTargetContext().getCurrentDataFlavors();
  137. }
  138. /**
  139. * This method returns the currently available
  140. * <code>DataFlavor</code>s as a <code>java.util.List</code>.
  141. * <P>
  142. * @return the currently available DataFlavors as a java.util.List
  143. */
  144. public List<DataFlavor> getCurrentDataFlavorsAsList() {
  145. return getDropTargetContext().getCurrentDataFlavorsAsList();
  146. }
  147. /**
  148. * This method returns a <code>boolean</code> indicating if the
  149. * specified <code>DataFlavor</code> is available
  150. * from the source.
  151. * <P>
  152. * @param df the <code>DataFlavor</code> to test
  153. * <P>
  154. * @return if the DataFlavor specified is available from the source
  155. */
  156. public boolean isDataFlavorSupported(DataFlavor df) {
  157. return getDropTargetContext().isDataFlavorSupported(df);
  158. }
  159. /**
  160. * This method returns the source drop actions.
  161. *
  162. * @return the source drop actions.
  163. */
  164. public int getSourceActions() { return actions; }
  165. /**
  166. * This method returns the user drop action.
  167. *
  168. * @return the user drop actions.
  169. */
  170. public int getDropAction() { return dropAction; }
  171. /**
  172. * This method returns the <code>Transferable</code> object
  173. * associated with the drop.
  174. * <P>
  175. * @return the <code>Transferable</code> associated with the drop
  176. */
  177. public Transferable getTransferable() {
  178. return getDropTargetContext().getTransferable();
  179. }
  180. /**
  181. * accept the drop, using the specified action.
  182. * <P>
  183. * @param dropAction the specified action
  184. */
  185. public void acceptDrop(int dropAction) {
  186. getDropTargetContext().acceptDrop(dropAction);
  187. }
  188. /**
  189. * reject the Drop.
  190. */
  191. public void rejectDrop() {
  192. getDropTargetContext().rejectDrop();
  193. }
  194. /**
  195. * This method notifies the <code>DragSource</code>
  196. * that the drop transfer(s) are completed.
  197. * <P>
  198. * @param success a <code>boolean</code> indicating that the drop transfer(s) are completed.
  199. */
  200. public void dropComplete(boolean success) {
  201. getDropTargetContext().dropComplete(success);
  202. }
  203. /**
  204. * This method returns an <code>int</code> indicating if
  205. * the source is in the same JVM as the target.
  206. * <P>
  207. * @return if the Source is in the same JVM
  208. */
  209. public boolean isLocalTransfer() {
  210. return isLocalTx;
  211. }
  212. /*
  213. * fields
  214. */
  215. static final private Point zero = new Point(0,0);
  216. /**
  217. * The location of the drag cursor's hotspot in Component coordinates.
  218. *
  219. * @serial
  220. */
  221. private Point location = zero;
  222. /**
  223. * The source drop actions.
  224. *
  225. * @serial
  226. */
  227. private int actions = DnDConstants.ACTION_NONE;
  228. /**
  229. * The user drop action.
  230. *
  231. * @serial
  232. */
  233. private int dropAction = DnDConstants.ACTION_NONE;
  234. /**
  235. * <code>true</code> if the source is in the same JVM as the target.
  236. *
  237. * @serial
  238. */
  239. private boolean isLocalTx = false;
  240. }