0N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage java.awt.dnd;
0N/A
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Cursor;
0N/A
0N/Aimport java.awt.Image;
0N/Aimport java.awt.Point;
0N/A
0N/Aimport java.awt.event.InputEvent;
0N/A
0N/Aimport java.awt.datatransfer.Transferable;
0N/A
0N/Aimport java.util.EventObject;
0N/A
0N/Aimport java.util.Collections;
0N/Aimport java.util.List;
0N/Aimport java.util.Iterator;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/A
0N/A
0N/A/**
0N/A * A <code>DragGestureEvent</code> is passed
0N/A * to <code>DragGestureListener</code>'s
0N/A * dragGestureRecognized() method
0N/A * when a particular <code>DragGestureRecognizer</code> detects that a
0N/A * platform dependent drag initiating gesture has occurred
0N/A * on the <code>Component</code> that it is tracking.
0N/A *
217N/A * The {@code action} field of any {@code DragGestureEvent} instance should take one of the following
217N/A * values:
217N/A * <ul>
217N/A * <li> {@code DnDConstants.ACTION_COPY}
217N/A * <li> {@code DnDConstants.ACTION_MOVE}
217N/A * <li> {@code DnDConstants.ACTION_LINK}
217N/A * </ul>
217N/A * Assigning the value different from listed above will cause an unspecified behavior.
217N/A *
0N/A * @see java.awt.dnd.DragGestureRecognizer
0N/A * @see java.awt.dnd.DragGestureListener
0N/A * @see java.awt.dnd.DragSource
217N/A * @see java.awt.dnd.DnDConstants
0N/A */
0N/A
0N/Apublic class DragGestureEvent extends EventObject {
0N/A
0N/A private static final long serialVersionUID = 9080172649166731306L;
0N/A
0N/A /**
217N/A * Constructs a <code>DragGestureEvent</code> object given by the
217N/A * <code>DragGestureRecognizer</code> instance firing this event,
217N/A * an {@code act} parameter representing
217N/A * the user's preferred action, an {@code ori} parameter
217N/A * indicating the origin of the drag, and a {@code List} of
217N/A * events that comprise the gesture({@code evs} parameter).
0N/A * <P>
0N/A * @param dgr The <code>DragGestureRecognizer</code> firing this event
217N/A * @param act The user's preferred action.
217N/A * For information on allowable values, see
217N/A * the class description for {@link DragGestureEvent}
0N/A * @param ori The origin of the drag
0N/A * @param evs The <code>List</code> of events that comprise the gesture
0N/A * <P>
217N/A * @throws IllegalArgumentException if any parameter equals {@code null}
217N/A * @throws IllegalArgumentException if the act parameter does not comply with
217N/A * the values given in the class
217N/A * description for {@link DragGestureEvent}
217N/A * @see java.awt.dnd.DnDConstants
0N/A */
0N/A
0N/A public DragGestureEvent(DragGestureRecognizer dgr, int act, Point ori,
0N/A List<? extends InputEvent> evs)
0N/A {
0N/A super(dgr);
0N/A
0N/A if ((component = dgr.getComponent()) == null)
0N/A throw new IllegalArgumentException("null component");
0N/A if ((dragSource = dgr.getDragSource()) == null)
0N/A throw new IllegalArgumentException("null DragSource");
0N/A
0N/A if (evs == null || evs.isEmpty())
0N/A throw new IllegalArgumentException("null or empty list of events");
0N/A
0N/A if (act != DnDConstants.ACTION_COPY &&
0N/A act != DnDConstants.ACTION_MOVE &&
0N/A act != DnDConstants.ACTION_LINK)
0N/A throw new IllegalArgumentException("bad action");
0N/A
0N/A if (ori == null) throw new IllegalArgumentException("null origin");
0N/A
0N/A events = evs;
0N/A action = act;
0N/A origin = ori;
0N/A }
0N/A
0N/A /**
0N/A * Returns the source as a <code>DragGestureRecognizer</code>.
0N/A * <P>
0N/A * @return the source as a <code>DragGestureRecognizer</code>
0N/A */
0N/A
0N/A public DragGestureRecognizer getSourceAsDragGestureRecognizer() {
0N/A return (DragGestureRecognizer)getSource();
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Component</code> associated
0N/A * with this <code>DragGestureEvent</code>.
0N/A * <P>
0N/A * @return the Component
0N/A */
0N/A
0N/A public Component getComponent() { return component; }
0N/A
0N/A /**
0N/A * Returns the <code>DragSource</code>.
0N/A * <P>
0N/A * @return the <code>DragSource</code>
0N/A */
0N/A
0N/A public DragSource getDragSource() { return dragSource; }
0N/A
0N/A /**
0N/A * Returns a <code>Point</code> in the coordinates
0N/A * of the <code>Component</code> over which the drag originated.
0N/A * <P>
0N/A * @return the Point where the drag originated in Component coords.
0N/A */
0N/A
0N/A public Point getDragOrigin() {
0N/A return origin;
0N/A }
0N/A
0N/A /**
0N/A * Returns an <code>Iterator</code> for the events
0N/A * comprising the gesture.
0N/A * <P>
0N/A * @return an Iterator for the events comprising the gesture
0N/A */
0N/A
0N/A public Iterator<InputEvent> iterator() { return events.iterator(); }
0N/A
0N/A /**
0N/A * Returns an <code>Object</code> array of the
0N/A * events comprising the drag gesture.
0N/A * <P>
0N/A * @return an array of the events comprising the gesture
0N/A */
0N/A
0N/A public Object[] toArray() { return events.toArray(); }
0N/A
0N/A /**
0N/A * Returns an array of the events comprising the drag gesture.
0N/A * <P>
0N/A * @param array the array of <code>EventObject</code> sub(types)
0N/A * <P>
0N/A * @return an array of the events comprising the gesture
0N/A */
0N/A
0N/A public Object[] toArray(Object[] array) { return events.toArray(array); }
0N/A
0N/A /**
0N/A * Returns an <code>int</code> representing the
0N/A * action selected by the user.
0N/A * <P>
0N/A * @return the action selected by the user
0N/A */
0N/A
0N/A public int getDragAction() { return action; }
0N/A
0N/A /**
0N/A * Returns the initial event that triggered the gesture.
0N/A * <P>
0N/A * @return the first "triggering" event in the sequence of the gesture
0N/A */
0N/A
0N/A public InputEvent getTriggerEvent() {
0N/A return getSourceAsDragGestureRecognizer().getTriggerEvent();
0N/A }
0N/A
0N/A /**
0N/A * Starts the drag operation given the <code>Cursor</code> for this drag
0N/A * operation and the <code>Transferable</code> representing the source data
0N/A * for this drag operation.
0N/A * <br>
0N/A * If a <code>null</code> <code>Cursor</code> is specified no exception will
0N/A * be thrown and default drag cursors will be used instead.
0N/A * <br>
0N/A * If a <code>null</code> <code>Transferable</code> is specified
0N/A * <code>NullPointerException</code> will be thrown.
0N/A * @param dragCursor The initial {@code Cursor} for this drag operation
0N/A * or {@code null} for the default cursor handling;
0N/A * see
0N/A * <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a>
0N/A * for more details on the cursor handling mechanism
0N/A * during drag and drop
0N/A * @param transferable The <code>Transferable</code> representing the source
0N/A * data for this drag operation.
0N/A *
0N/A * @throws InvalidDnDOperationException if the Drag and Drop
0N/A * system is unable to initiate a drag operation, or if the user
0N/A * attempts to start a drag while an existing drag operation is
0N/A * still executing.
0N/A * @throws NullPointerException if the {@code Transferable} is {@code null}
0N/A * @since 1.4
0N/A */
0N/A public void startDrag(Cursor dragCursor, Transferable transferable)
0N/A throws InvalidDnDOperationException {
0N/A dragSource.startDrag(this, dragCursor, transferable, null);
0N/A }
0N/A
0N/A /**
0N/A * Starts the drag given the initial <code>Cursor</code> to display,
0N/A * the <code>Transferable</code> object,
0N/A * and the <code>DragSourceListener</code> to use.
0N/A * <P>
0N/A * @param dragCursor The initial {@code Cursor} for this drag operation
0N/A * or {@code null} for the default cursor handling;
0N/A * see
0N/A * <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a>
0N/A * for more details on the cursor handling mechanism
0N/A * during drag and drop
0N/A * @param transferable The source's Transferable
0N/A * @param dsl The source's DragSourceListener
0N/A * <P>
0N/A * @throws InvalidDnDOperationException if
0N/A * the Drag and Drop system is unable to
0N/A * initiate a drag operation, or if the user
0N/A * attempts to start a drag while an existing
0N/A * drag operation is still executing.
0N/A */
0N/A
0N/A public void startDrag(Cursor dragCursor, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException {
0N/A dragSource.startDrag(this, dragCursor, transferable, dsl);
0N/A }
0N/A
0N/A /**
0N/A * Start the drag given the initial <code>Cursor</code> to display,
0N/A * a drag <code>Image</code>, the offset of
0N/A * the <code>Image</code>,
0N/A * the <code>Transferable</code> object, and
0N/A * the <code>DragSourceListener</code> to use.
0N/A * <P>
0N/A * @param dragCursor The initial {@code Cursor} for this drag operation
0N/A * or {@code null} for the default cursor handling;
0N/A * see
0N/A * <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a>
0N/A * for more details on the cursor handling mechanism
0N/A * during drag and drop
0N/A * @param dragImage The source's dragImage
0N/A * @param imageOffset The dragImage's offset
0N/A * @param transferable The source's Transferable
0N/A * @param dsl The source's DragSourceListener
0N/A * <P>
0N/A * @throws InvalidDnDOperationException if
0N/A * the Drag and Drop system is unable to
0N/A * initiate a drag operation, or if the user
0N/A * attempts to start a drag while an existing
0N/A * drag operation is still executing.
0N/A */
0N/A
0N/A public void startDrag(Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException {
0N/A dragSource.startDrag(this, dragCursor, dragImage, imageOffset, transferable, dsl);
0N/A }
0N/A
0N/A /**
0N/A * Serializes this <code>DragGestureEvent</code>. Performs default
0N/A * serialization and then writes out this object's <code>List</code> of
0N/A * gesture events if and only if the <code>List</code> can be serialized.
0N/A * If not, <code>null</code> is written instead. In this case, a
0N/A * <code>DragGestureEvent</code> created from the resulting deserialized
0N/A * stream will contain an empty <code>List</code> of gesture events.
0N/A *
0N/A * @serialData The default serializable fields, in alphabetical order,
0N/A * followed by either a <code>List</code> instance, or
0N/A * <code>null</code>.
0N/A * @since 1.4
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A
0N/A s.writeObject(SerializationTester.test(events) ? events : null);
0N/A }
0N/A
0N/A /**
0N/A * Deserializes this <code>DragGestureEvent</code>. This method first
0N/A * performs default deserialization for all non-<code>transient</code>
0N/A * fields. An attempt is then made to deserialize this object's
0N/A * <code>List</code> of gesture events as well. This is first attempted
0N/A * by deserializing the field <code>events</code>, because, in releases
0N/A * prior to 1.4, a non-<code>transient</code> field of this name stored the
0N/A * <code>List</code> of gesture events. If this fails, the next object in
0N/A * the stream is used instead. If the resulting <code>List</code> is
0N/A * <code>null</code>, this object's <code>List</code> of gesture events
0N/A * is set to an empty <code>List</code>.
0N/A *
0N/A * @since 1.4
0N/A */
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException
0N/A {
0N/A ObjectInputStream.GetField f = s.readFields();
0N/A
0N/A dragSource = (DragSource)f.get("dragSource", null);
0N/A component = (Component)f.get("component", null);
0N/A origin = (Point)f.get("origin", null);
0N/A action = f.get("action", 0);
0N/A
0N/A // Pre-1.4 support. 'events' was previously non-transient
0N/A try {
0N/A events = (List)f.get("events", null);
0N/A } catch (IllegalArgumentException e) {
0N/A // 1.4-compatible byte stream. 'events' was written explicitly
0N/A events = (List)s.readObject();
0N/A }
0N/A
0N/A // Implementation assumes 'events' is never null.
0N/A if (events == null) {
0N/A events = Collections.EMPTY_LIST;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * fields
0N/A */
0N/A
0N/A private transient List events;
0N/A
0N/A /**
0N/A * The DragSource associated with this DragGestureEvent.
0N/A *
0N/A * @serial
0N/A */
0N/A private DragSource dragSource;
0N/A
0N/A /**
0N/A * The Component associated with this DragGestureEvent.
0N/A *
0N/A * @serial
0N/A */
0N/A private Component component;
0N/A
0N/A /**
0N/A * The origin of the drag.
0N/A *
0N/A * @serial
0N/A */
0N/A private Point origin;
0N/A
0N/A /**
0N/A * The user's preferred action.
0N/A *
0N/A * @serial
0N/A */
0N/A private int action;
0N/A}