1. /*
  2. * @(#)DropTargetDropEvent.java 1.18 01/11/29
  3. *
  4. * Copyright 2002 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.awt.dnd.DropTargetEvent;
  12. import java.util.Arrays;
  13. import java.util.List;
  14. /**
  15. * The <code>DropTargetDropEvent</code> is delivered
  16. * via the <code>DropTargetListener</code> drop() method.
  17. *
  18. * @version 1.18
  19. * @since JDK1.2
  20. */
  21. public class DropTargetDropEvent extends DropTargetEvent {
  22. /**
  23. * Construct a <code>DropTargetDropEvent</code> given
  24. * the <code>DropTargetContext</code> for this operation,
  25. * the location of the drag <code>Cursor</code>'s
  26. * hotspot in the <code>Component</code>'s coordinates,
  27. * the currently
  28. * selected user drop action, and the current set of
  29. * actions supported by the source.
  30. * By default, this constructor
  31. * assumes that the target is not in the same virtual machine as
  32. * the source; that is, {@link #isLocalTransfer()} will
  33. * return <code>false</code>.
  34. * <P>
  35. * @param dtc The <code>DropTargetContext</code> for this operation
  36. * @param cursorLocn The location of the "Drag" Cursor's
  37. * hotspot in <code>Component</code> coordinates
  38. * @param dropAction The currently selected user drop action: COPY, MOVE, or LINK
  39. * constants found in DnDConstants.
  40. * @param srcActions The current set of actions supported by the source: some
  41. * combination of COPY, MOVE, or LINK as exposed by the <code>DragSource</code>.
  42. * <P>
  43. * @throws <code>NullPointerException</code>
  44. * if cursorLocn is <code>null</code>
  45. * @throws <code>IllegalArgumentException</code>
  46. * if the dropAction or srcActions are illegal values,
  47. * or if dtc is <code>null</code>.
  48. */
  49. public DropTargetDropEvent(DropTargetContext dtc, Point cursorLocn, int dropAction, int srcActions) {
  50. super(dtc);
  51. if (cursorLocn == null) throw new NullPointerException("cursorLocn");
  52. if (dropAction != DnDConstants.ACTION_NONE &&
  53. dropAction != DnDConstants.ACTION_COPY &&
  54. dropAction != DnDConstants.ACTION_MOVE &&
  55. dropAction != DnDConstants.ACTION_LINK
  56. ) throw new IllegalArgumentException("dropAction = " + dropAction);
  57. if ((srcActions & ~(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK)) != 0) throw new IllegalArgumentException("srcActions");
  58. location = cursorLocn;
  59. actions = srcActions;
  60. this.dropAction = dropAction;
  61. }
  62. /**
  63. * Construct a <code>DropTargetEvent</code> given the
  64. * <code>DropTargetContext</code> for this operation,
  65. * the location of the drag <code>Cursor</code>'s hotspot
  66. * in the <code>Component</code>'s
  67. * coordinates, the currently selected user drop action,
  68. * the current set of actions supported by the source,
  69. * and a <code>boolean</code> indicating if the source is in the same JVM
  70. * as the target.
  71. * <P>
  72. * @param dtc The DropTargetContext for this operation
  73. * @param cursorLocn The location of the "Drag" Cursor's
  74. * hotspot in Component's coordinates
  75. * @param dropAction The currently selected user drop action: COPY, MOVE, or LINK
  76. * constants found in DnDConstants.
  77. * @param srcActions The current set of actions supported by the source: some
  78. * combination of COPY, MOVE, or LINK as exposed by the <code>DragSource</code>.
  79. * @param isLocalTx True if the source is in the same JVM as the target
  80. */
  81. public DropTargetDropEvent(DropTargetContext dtc, Point cursorLocn, int dropAction, int srcActions, boolean isLocal) {
  82. this(dtc, cursorLocn, dropAction, srcActions);
  83. isLocalTx = isLocal;
  84. }
  85. /**
  86. * This method returns a <code>Point</code>
  87. * indicating the <code>Cursor</code>'s current
  88. * location in the <code>Component</code>'s coordinates.
  89. * <P>
  90. * @return the current <code>Cursor</code> location in Component's coords.
  91. */
  92. public Point getLocation() {
  93. return location;
  94. }
  95. /**
  96. * This method returns the current DataFlavors.
  97. * <P>
  98. * @return current DataFlavors
  99. */
  100. public DataFlavor[] getCurrentDataFlavors() {
  101. return getDropTargetContext().getCurrentDataFlavors();
  102. }
  103. /**
  104. * This method returns the currently available
  105. * <code>DataFlavor</code>s as a <code>java.util.List</code>.
  106. * <P>
  107. * @return the currently available DataFlavors as a java.util.List
  108. */
  109. public List getCurrentDataFlavorsAsList() {
  110. return getDropTargetContext().getCurrentDataFlavorsAsList();
  111. }
  112. /**
  113. * This method returns a <code>boolean</code> indicating if the
  114. * specified <code>DataFlavor</code> is available
  115. * from the source.
  116. * <P>
  117. * @param df the <code>DataFlavor</code> to test
  118. * <P>
  119. * @return if the DataFlavor specified is available from the source
  120. */
  121. public boolean isDataFlavorSupported(DataFlavor df) {
  122. return getDropTargetContext().isDataFlavorSupported(df);
  123. }
  124. /**
  125. * This method returns an <code>int</code> representing the
  126. * action(s) supported by the source.
  127. * <P>
  128. * @return source actions
  129. */
  130. public int getSourceActions() { return actions; }
  131. /**
  132. * This method returns an <code>int</code>
  133. * representing the action(s) supported
  134. * by the source at the time of the drop.
  135. * <P>
  136. * @return source actions
  137. */
  138. public int getDropAction() { return dropAction; }
  139. /**
  140. * This method returns the <code>Transferable</code> object
  141. * associated with the drop.
  142. * <P>
  143. * @return the <code>Transferable</code> associated with the drop
  144. */
  145. public Transferable getTransferable() {
  146. return getDropTargetContext().getTransferable();
  147. }
  148. /**
  149. * accept the drop, using the specified action.
  150. * <P>
  151. * @param dropAction the specified action
  152. */
  153. public void acceptDrop(int dropAction) {
  154. getDropTargetContext().acceptDrop(dropAction);
  155. }
  156. /**
  157. * reject the Drop.
  158. */
  159. public void rejectDrop() {
  160. getDropTargetContext().rejectDrop();
  161. }
  162. /**
  163. * This method notifies the <code>DragSource</code>
  164. * that the drop transfer(s) are completed.
  165. * <P>
  166. * @param success a <code>boolean</code> indicating that the drop transfer(s) are completed.
  167. */
  168. public void dropComplete(boolean success) {
  169. getDropTargetContext().dropComplete(success);
  170. }
  171. /**
  172. * This method returns an <code>int</code> indicating if
  173. * the source is in the same JVM as the target.
  174. * <P>
  175. * @return if the Source is in the same JVM
  176. */
  177. public boolean isLocalTransfer() {
  178. return isLocalTx;
  179. }
  180. /*
  181. * fields
  182. */
  183. static final private Point zero = new Point(0,0);
  184. private Point location = zero;
  185. private int actions = DnDConstants.ACTION_NONE;
  186. private int dropAction = DnDConstants.ACTION_NONE;
  187. private boolean isLocalTx = false;
  188. }