1. /*
  2. * @(#)DragGestureEvent.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.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.Collections;
  16. import java.util.List;
  17. import java.util.Iterator;
  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. /**
  23. * A <code>DragGestureEvent</code> is passed
  24. * to <code>DragGestureListener</code>'s
  25. * dragGestureRecognized() method
  26. * when a particular <code>DragGestureRecognizer</code> detects that a
  27. * platform dependent drag initiating gesture has occurred
  28. * on the <code>Component</code> that it is tracking.
  29. *
  30. * @version 1.21
  31. * @see java.awt.dnd.DragGestureRecognizer
  32. * @see java.awt.dnd.DragGestureListener
  33. * @see java.awt.dnd.DragSource
  34. */
  35. public class DragGestureEvent extends EventObject {
  36. private static final long serialVersionUID = 9080172649166731306L;
  37. /**
  38. * Construct a <code>DragGestureEvent</code> given the
  39. * <code>DragGestureRecognizer</code> firing this event,
  40. * an <code>int</code> representing
  41. * the user's preferred action, a <code>Point</code>
  42. * indicating the origin of the drag, and a <code>List</code>
  43. * of events that comprise the gesture.
  44. * <P>
  45. * @param dgr The <code>DragGestureRecognizer</code> firing this event
  46. * @param act The the user's preferred action
  47. * @param ori The origin of the drag
  48. * @param evs The <code>List</code> of events that comprise the gesture
  49. * <P>
  50. * @throws <code>IllegalArgumentException</code> if
  51. * input parameters are null
  52. */
  53. public DragGestureEvent(DragGestureRecognizer dgr, int act, Point ori, List evs) {
  54. super(dgr);
  55. if ((component = dgr.getComponent()) == null)
  56. throw new IllegalArgumentException("null component");
  57. if ((dragSource = dgr.getDragSource()) == null)
  58. throw new IllegalArgumentException("null DragSource");
  59. if (evs == null || evs.isEmpty())
  60. throw new IllegalArgumentException("null or empty list of events");
  61. if (act != DnDConstants.ACTION_COPY &&
  62. act != DnDConstants.ACTION_MOVE &&
  63. act != DnDConstants.ACTION_LINK)
  64. throw new IllegalArgumentException("bad action");
  65. if (ori == null) throw new IllegalArgumentException("null origin");
  66. events = evs;
  67. action = act;
  68. origin = ori;
  69. }
  70. /**
  71. * Returns the source as a <code>DragGestureRecognizer</code>.
  72. * <P>
  73. * @return the source as a <code>DragGestureRecognizer</code>
  74. */
  75. public DragGestureRecognizer getSourceAsDragGestureRecognizer() {
  76. return (DragGestureRecognizer)getSource();
  77. }
  78. /**
  79. * Returns the <code>Component</code> associated
  80. * with this <code>DragGestureEvent</code>.
  81. * <P>
  82. * @return the Component
  83. */
  84. public Component getComponent() { return component; }
  85. /**
  86. * Returns the <code>DragSource</code>.
  87. * <P>
  88. * @return the <code>DragSource</code>
  89. */
  90. public DragSource getDragSource() { return dragSource; }
  91. /**
  92. * Returns a <code>Point</code> in the coordinates
  93. * of the <code>Component</code> over which the drag originated.
  94. * <P>
  95. * @return the Point where the drag originated in Component coords.
  96. */
  97. public Point getDragOrigin() {
  98. return origin;
  99. }
  100. /**
  101. * Returns an <code>Iterator</code> for the events
  102. * comprising the gesture.
  103. * <P>
  104. * @return an Iterator for the events comprising the gesture
  105. */
  106. public Iterator iterator() { return events.iterator(); }
  107. /**
  108. * Returns an <code>Object</code> array of the
  109. * events comprising the drag gesture.
  110. * <P>
  111. * @return an array of the events comprising the gesture
  112. */
  113. public Object[] toArray() { return events.toArray(); }
  114. /**
  115. * Returns an array of the events comprising the drag gesture.
  116. * <P>
  117. * @param array the array of <code>EventObject</code> sub(types)
  118. * <P>
  119. * @return an array of the events comprising the gesture
  120. */
  121. public Object[] toArray(Object[] array) { return events.toArray(array); }
  122. /**
  123. * Returns an <code>int</code> representing the
  124. * action selected by the user.
  125. * <P>
  126. * @return the action selected by the user
  127. */
  128. public int getDragAction() { return action; }
  129. /**
  130. * Returns the initial event that triggered the gesture.
  131. * <P>
  132. * @return the first "triggering" event in the sequence of the gesture
  133. */
  134. public InputEvent getTriggerEvent() {
  135. return getSourceAsDragGestureRecognizer().getTriggerEvent();
  136. }
  137. /**
  138. * Starts the drag operation given the <code>Cursor</code> for this drag
  139. * operation and the <code>Transferable</code> representing the source data
  140. * for this drag operation.
  141. * <br>
  142. * If a <code>null</code> <code>Cursor</code> is specified no exception will
  143. * be thrown and default drag cursors will be used instead.
  144. * <br>
  145. * If a <code>null</code> <code>Transferable</code> is specified
  146. * <code>NullPointerException</code> will be thrown.
  147. *
  148. * @param dragCursor The <code>Cursor</code> for this drag operation
  149. * @param transferable The <code>Transferable</code> representing the source
  150. * data for this drag operation.
  151. *
  152. * @throws <code>InvalidDnDOperationException</code> if the Drag and Drop
  153. * system is unable to initiate a drag operation, or if the user
  154. * attempts to start a drag while an existing drag operation is
  155. * still executing.
  156. * @throws <code>NullPointerException</code> if the
  157. * <code>Transferable</code> is <code>null</code>.
  158. * @since 1.4
  159. */
  160. public void startDrag(Cursor dragCursor, Transferable transferable)
  161. throws InvalidDnDOperationException {
  162. dragSource.startDrag(this, dragCursor, transferable, null);
  163. }
  164. /**
  165. * Starts the drag given the initial <code>Cursor</code> to display,
  166. * the <code>Transferable</code> object,
  167. * and the <code>DragSourceListener</code> to use.
  168. * <P>
  169. * @param dragCursor The initial drag Cursor
  170. * @param transferable The source's Transferable
  171. * @param dsl The source's DragSourceListener
  172. * <P>
  173. * @throws <code>InvalidDnDOperationException</code> if
  174. * the Drag and Drop system is unable to
  175. * initiate a drag operation, or if the user
  176. * attempts to start a drag while an existing
  177. * drag operation is still executing.
  178. */
  179. public void startDrag(Cursor dragCursor, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException {
  180. dragSource.startDrag(this, dragCursor, transferable, dsl);
  181. }
  182. /**
  183. * Start the drag given the initial <code>Cursor</code> to display,
  184. * a drag <code>Image</code>, the offset of
  185. * the <code>Image</code>,
  186. * the <code>Transferable</code> object, and
  187. * the <code>DragSourceListener</code> to use.
  188. * <P>
  189. * @param dragCursor The initial drag Cursor
  190. * @param dragImage The source's dragImage
  191. * @param imageOffset The dragImage's offset
  192. * @param transferable The source's Transferable
  193. * @param dsl The source's DragSourceListener
  194. * <P>
  195. * @throws <code>InvalidDnDOperationException</code> if
  196. * the Drag and Drop system is unable to
  197. * initiate a drag operation, or if the user
  198. * attempts to start a drag while an existing
  199. * drag operation is still executing.
  200. */
  201. public void startDrag(Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException {
  202. dragSource.startDrag(this, dragCursor, dragImage, imageOffset, transferable, dsl);
  203. }
  204. /**
  205. * Serializes this <code>DragGestureEvent</code>. Performs default
  206. * serialization and then writes out this object's <code>List</code> of
  207. * gesture events if and only if the <code>List</code> can be serialized.
  208. * If not, <code>null</code> is written instead. In this case, a
  209. * <code>DragGestureEvent</code> created from the resulting deserialized
  210. * stream will contain an empty <code>List</code> of gesture events.
  211. *
  212. * @serialData The default serializable fields, in alphabetical order,
  213. * followed by either a <code>List</code> instance, or
  214. * <code>null</code>.
  215. * @since 1.4
  216. */
  217. private void writeObject(ObjectOutputStream s) throws IOException {
  218. s.defaultWriteObject();
  219. s.writeObject(SerializationTester.test(events) ? events : null);
  220. }
  221. /**
  222. * Deserializes this <code>DragGestureEvent</code>. This method first
  223. * performs default deserialization for all non-<code>transient</code>
  224. * fields. An attempt is then made to deserialize this object's
  225. * <code>List</code> of gesture events as well. This is first attempted
  226. * by deserializing the field <code>events</code>, because, in releases
  227. * prior to 1.4, a non-<code>transient</code> field of this name stored the
  228. * <code>List</code> of gesture events. If this fails, the next object in
  229. * the stream is used instead. If the resulting <code>List</code> is
  230. * <code>null</code>, this object's <code>List</code> of gesture events
  231. * is set to an empty <code>List</code>.
  232. *
  233. * @since 1.4
  234. */
  235. private void readObject(ObjectInputStream s)
  236. throws ClassNotFoundException, IOException
  237. {
  238. ObjectInputStream.GetField f = s.readFields();
  239. dragSource = (DragSource)f.get("dragSource", null);
  240. component = (Component)f.get("component", null);
  241. origin = (Point)f.get("origin", null);
  242. action = f.get("action", 0);
  243. // Pre-1.4 support. 'events' was previously non-transient
  244. try {
  245. events = (List)f.get("events", null);
  246. } catch (IllegalArgumentException e) {
  247. // 1.4-compatible byte stream. 'events' was written explicitly
  248. events = (List)s.readObject();
  249. }
  250. // Implementation assumes 'events' is never null.
  251. if (events == null) {
  252. events = Collections.EMPTY_LIST;
  253. }
  254. }
  255. /*
  256. * fields
  257. */
  258. private transient List events;
  259. /**
  260. * The DragSource associated with this DragGestureEvent.
  261. *
  262. * @serial
  263. */
  264. private DragSource dragSource;
  265. /**
  266. * The Component associated with this DragGestureEvent.
  267. *
  268. * @serial
  269. */
  270. private Component component;
  271. /**
  272. * The origin of the drag.
  273. *
  274. * @serial
  275. */
  276. private Point origin;
  277. /**
  278. * The user's preferred action.
  279. *
  280. * @serial
  281. */
  282. private int action;
  283. }