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