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