1. /*
  2. * @(#)DragGestureEvent.java 1.14 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.Component;
  9. import java.awt.Cursor;
  10. import java.awt.Image;
  11. import java.awt.Point;
  12. import java.awt.event.InputEvent;
  13. import java.awt.datatransfer.Transferable;
  14. import java.util.EventObject;
  15. import java.util.List;
  16. import java.util.Iterator;
  17. /**
  18. * A <code>DragGestureEvent</code> is passed
  19. * to <code>DragGestureListener</code>'s
  20. * dragGestureRecognized() method
  21. * when a particular <code>DragGestureRecognizer</code> detects that a
  22. * platform dependent drag initiating gesture has occurred
  23. * on the <code>Component</code> that it is tracking.
  24. *
  25. * @version 1.14
  26. * @see java.awt.dnd.DragGestureRecognizer
  27. * @see java.awt.dnd.DragGestureListener
  28. * @see java.awt.dnd.DragSource
  29. */
  30. public class DragGestureEvent extends EventObject {
  31. /**
  32. * Construct a <code>DragGestureEvent</code> given the
  33. * <code>DragGestureRecognizer</code> firing this event,
  34. * an <code>int</code> representing
  35. * the user's preferred action, a <code>Point</code>
  36. * indicating the origin of the drag, and a <code>List</code>
  37. * of events that comprise the gesture.
  38. * <P>
  39. * @param dgr The <code>DragGestureRecognizer</code> firing this event
  40. * @param act The the user's preferred action
  41. * @param ori The origin of the drag
  42. * @param evs The <code>List</code> of events that comprise the gesture
  43. * <P>
  44. * @throws <code>IllegalArgumentException</code> if
  45. * input parameters are null
  46. */
  47. public DragGestureEvent(DragGestureRecognizer dgr, int act, Point ori, List evs) {
  48. super(dgr);
  49. if ((component = dgr.getComponent()) == null)
  50. throw new IllegalArgumentException("null component");
  51. if ((dragSource = dgr.getDragSource()) == null)
  52. throw new IllegalArgumentException("null DragSource");
  53. if (evs == null || evs.isEmpty())
  54. throw new IllegalArgumentException("null or empty list of events");
  55. if (act != DnDConstants.ACTION_COPY &&
  56. act != DnDConstants.ACTION_MOVE &&
  57. act != DnDConstants.ACTION_LINK)
  58. throw new IllegalArgumentException("bad action");
  59. if (ori == null) throw new IllegalArgumentException("null origin");
  60. events = evs;
  61. action = act;
  62. origin = ori;
  63. }
  64. /**
  65. * This method returns the source as a <code>DragGestureRecognizer</code>.
  66. * <P>
  67. * @return the source as a <code>DragGestureRecognizer</code>
  68. */
  69. public DragGestureRecognizer getSourceAsDragGestureRecognizer() {
  70. return (DragGestureRecognizer)getSource();
  71. }
  72. /**
  73. * This method returns the <code>Component</code> associated
  74. * with this <code>DragGestureEvent</code>.
  75. * <P>
  76. * @return the Component
  77. */
  78. public Component getComponent() { return component; }
  79. /**
  80. * This method returns the <code>DragSource</code>.
  81. * <P>
  82. * @return the <code>DragSource</code>
  83. */
  84. public DragSource getDragSource() { return dragSource; }
  85. /**
  86. * This method returns a <code>Point</code> in the coordinates
  87. * of the <code>Component</code> over which the drag originated.
  88. * <P>
  89. * @return the Point where the drag originated in Component coords.
  90. */
  91. public Point getDragOrigin() {
  92. return origin;
  93. }
  94. /**
  95. * This method returns an <code>Iterator</code> for the events
  96. * comprising the gesture.
  97. * <P>
  98. * @return an Iterator for the events comprising the gesture
  99. */
  100. public Iterator iterator() { return events.iterator(); }
  101. /**
  102. * This method returns an <code>Object</code> array of the
  103. * events comprising the drag gesture.
  104. * <P>
  105. * @return an array of the events comprising the gesture
  106. */
  107. public Object[] toArray() { return events.toArray(); }
  108. /**
  109. * This method returns an array of the
  110. * events comprising the drag gesture.
  111. * <P>
  112. * @param array the array of <code>EventObject</code> sub(types)
  113. * <P>
  114. * @return an array of the events comprising the gesture
  115. */
  116. public Object[] toArray(Object[] array) { return events.toArray(array); }
  117. /**
  118. * This method returns an <code>int</code> representing the
  119. * action selected by the user.
  120. * <P>
  121. * @return the action selected by the user
  122. */
  123. public int getDragAction() { return action; }
  124. /**
  125. * This method returns the initial event that triggered the gesture.
  126. * <P>
  127. * @returns the first "triggering" event in the sequence of the gesture
  128. */
  129. public InputEvent getTriggerEvent() {
  130. return getSourceAsDragGestureRecognizer().getTriggerEvent();
  131. }
  132. /**
  133. * Start the drag given the initial <code>Cursor</code> to display,
  134. * the <code>Transferable</code> object,
  135. * and the <code>DragSourceListener</code> to use.
  136. * <P>
  137. * @param dragCursor The initial drag Cursor
  138. * @param transferable The source's Transferable
  139. * @param dsl The source's DragSourceListener
  140. * <P>
  141. * @throws <code>InvalidDnDOperationException</code> if
  142. * the Drag and Drop system is unable to
  143. * initiate a drag operation, or if the user
  144. * attempts to start a drag while an existing
  145. * drag operation is still executing.
  146. */
  147. public void startDrag(Cursor dragCursor, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException {
  148. dragSource.startDrag(this, dragCursor, transferable, dsl);
  149. }
  150. /**
  151. * Start the drag given the initial <code>Cursor</code> to display,
  152. * a drag <code>Image</code>, the offset of
  153. * the <code>Image</code>,
  154. * the <code>Transferable</code> object, and
  155. * the <code>DragSourceListener</code> to use.
  156. * <P>
  157. * @param dragCursor The initial drag Cursor
  158. * @param dragImage The source's dragImage
  159. * @param imageOffset The dragImage's offset
  160. * @param transferable The source's Transferable
  161. * @param dsl The source's DragSourceListener
  162. * <P>
  163. * @throws <code>InvalidDnDOperationException</code> if
  164. * the Drag and Drop system is unable to
  165. * initiate a drag operation, or if the user
  166. * attempts to start a drag while an existing
  167. * drag operation is still executing.
  168. */
  169. public void startDrag(Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException {
  170. dragSource.startDrag(this, dragCursor, dragImage, imageOffset, transferable, dsl);
  171. }
  172. /*
  173. * fields
  174. */
  175. private List events;
  176. private DragSource dragSource;
  177. private Component component;
  178. private Point origin;
  179. private int action;
  180. }