src/java.desktop/share/classes/java/awt/dnd/DragSourceContext.java

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.dnd;
  27 
  28 import java.awt.Component;
  29 import java.awt.Cursor;
  30 import java.awt.Image;
  31 import java.awt.Point;
  32 
  33 import java.awt.datatransfer.DataFlavor;
  34 import java.awt.datatransfer.Transferable;
  35 import java.awt.datatransfer.UnsupportedFlavorException;
  36 
  37 import java.awt.dnd.peer.DragSourceContextPeer;
  38 
  39 import java.io.IOException;
  40 import java.io.InvalidObjectException;
  41 import java.io.ObjectOutputStream;
  42 import java.io.ObjectInputStream;

  43 import java.io.Serializable;
  44 
  45 import java.util.TooManyListenersException;
  46 


  47 /**
  48  * The <code>DragSourceContext</code> class is responsible for managing the
  49  * initiator side of the Drag and Drop protocol. In particular, it is responsible
  50  * for managing drag event notifications to the
  51  * {@linkplain DragSourceListener DragSourceListeners}
  52  * and {@linkplain DragSourceMotionListener DragSourceMotionListeners}, and providing the
  53  * {@link Transferable} representing the source data for the drag operation.
  54  * <p>
  55  * Note that the <code>DragSourceContext</code> itself
  56  * implements the <code>DragSourceListener</code> and
  57  * <code>DragSourceMotionListener</code> interfaces.
  58  * This is to allow the platform peer
  59  * (the {@link DragSourceContextPeer} instance)
  60  * created by the {@link DragSource} to notify
  61  * the <code>DragSourceContext</code> of
  62  * state changes in the ongoing operation. This allows the
  63  * <code>DragSourceContext</code> object to interpose
  64  * itself between the platform and the
  65  * listeners provided by the initiator of the drag operation.
  66  * <p>


 106      * An <code>int</code> used by updateCurrentCursor()
 107      * indicating that the <code>Cursor</code>
 108      * has entered a <code>DropTarget</code>.
 109      */
 110     protected static final int ENTER   = 1;
 111 
 112     /**
 113      * An <code>int</code> used by updateCurrentCursor()
 114      * indicating that the <code>Cursor</code> is
 115      * over a <code>DropTarget</code>.
 116      */
 117     protected static final int OVER    = 2;
 118 
 119     /**
 120      * An <code>int</code> used by updateCurrentCursor()
 121      * indicating that the user operation has changed.
 122      */
 123 
 124     protected static final int CHANGED = 3;
 125 




 126     /**
 127      * Called from <code>DragSource</code>, this constructor creates a new
 128      * <code>DragSourceContext</code> given the
 129      * <code>DragSourceContextPeer</code> for this Drag, the
 130      * <code>DragGestureEvent</code> that triggered the Drag, the initial
 131      * <code>Cursor</code> to use for the Drag, an (optional)
 132      * <code>Image</code> to display while the Drag is taking place, the offset
 133      * of the <code>Image</code> origin from the hotspot at the instant of the
 134      * triggering event, the <code>Transferable</code> subject data, and the
 135      * <code>DragSourceListener</code> to use during the Drag and Drop
 136      * operation.
 137      * <br>
 138      * If <code>DragSourceContextPeer</code> is <code>null</code>
 139      * <code>NullPointerException</code> is thrown.
 140      * <br>
 141      * If <code>DragGestureEvent</code> is <code>null</code>
 142      * <code>NullPointerException</code> is thrown.
 143      * <br>
 144      * If <code>Cursor</code> is <code>null</code> no exception is thrown and
 145      * the default drag cursor behavior is activated for this drag operation.
 146      * <br>
 147      * If <code>Image</code> is <code>null</code> no exception is thrown.
 148      * <br>
 149      * If <code>Image</code> is not <code>null</code> and the offset is
 150      * <code>null</code> <code>NullPointerException</code> is thrown.
 151      * <br>
 152      * If <code>Transferable</code> is <code>null</code>
 153      * <code>NullPointerException</code> is thrown.
 154      * <br>
 155      * If <code>DragSourceListener</code> is <code>null</code> no exception
 156      * is thrown.
 157      *
 158      * @param dscp       the <code>DragSourceContextPeer</code> for this drag
 159      * @param trigger    the triggering event
 160      * @param dragCursor     the initial {@code Cursor} for this drag operation
 161      *                       or {@code null} for the default cursor handling;
 162      *                       see <a href="DragSourceContext.html#defaultCursor">class level documentation</a>
 163      *                       for more details on the cursor handling mechanism during drag and drop
 164      * @param dragImage  the <code>Image</code> to drag (or <code>null</code>)
 165      * @param offset     the offset of the image origin from the hotspot at the
 166      *                   instant of the triggering event
 167      * @param t          the <code>Transferable</code>
 168      * @param dsl        the <code>DragSourceListener</code>
 169      *
 170      * @throws IllegalArgumentException if the <code>Component</code> associated
 171      *         with the trigger event is <code>null</code>.
 172      * @throws IllegalArgumentException if the <code>DragSource</code> for the
 173      *         trigger event is <code>null</code>.
 174      * @throws IllegalArgumentException if the drag action for the
 175      *         trigger event is <code>DnDConstants.ACTION_NONE</code>.
 176      * @throws IllegalArgumentException if the source actions for the
 177      *         <code>DragGestureRecognizer</code> associated with the trigger
 178      *         event are equal to <code>DnDConstants.ACTION_NONE</code>.
 179      * @throws NullPointerException if dscp, trigger, or t are null, or
 180      *         if dragImage is non-null and offset is null
 181      */
 182     public DragSourceContext(DragSourceContextPeer dscp,
 183                              DragGestureEvent trigger, Cursor dragCursor,
 184                              Image dragImage, Point offset, Transferable t,
 185                              DragSourceListener dsl) {



 186         if (dscp == null) {
 187             throw new NullPointerException("DragSourceContextPeer");
 188         }
 189 
 190         if (trigger == null) {
 191             throw new NullPointerException("Trigger");
 192         }
 193 
 194         if (trigger.getDragSource() == null) {
 195             throw new IllegalArgumentException("DragSource");
 196         }
 197 
 198         if (trigger.getComponent() == null) {
 199             throw new IllegalArgumentException("Component");
 200         }
 201 
 202         if (trigger.getSourceAsDragGestureRecognizer().getSourceActions() ==
 203                  DnDConstants.ACTION_NONE) {
 204             throw new IllegalArgumentException("source actions");
 205         }


 606                         public boolean isDataFlavorSupported(DataFlavor flavor)
 607                         {
 608                             return false;
 609                         }
 610                         public Object getTransferData(DataFlavor flavor)
 611                             throws UnsupportedFlavorException
 612                         {
 613                             throw new UnsupportedFlavorException(flavor);
 614                         }
 615                     };
 616             }
 617             transferable = emptyTransferable;
 618         }
 619     }
 620 
 621     private static Transferable emptyTransferable;
 622 
 623     /*
 624      * fields
 625      */
 626 
 627     private transient DragSourceContextPeer peer;
 628 
 629     /**
 630      * The event which triggered the start of the drag.
 631      *
 632      * @serial
 633      */
 634     private DragGestureEvent    trigger;
 635 
 636     /**
 637      * The current drag cursor.
 638      *
 639      * @serial
 640      */
 641     private Cursor              cursor;
 642 
 643     private transient Transferable      transferable;
 644 
 645     private transient DragSourceListener    listener;
 646 
 647     /**


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.dnd;
  27 
  28 import java.awt.Component;
  29 import java.awt.Cursor;
  30 import java.awt.Image;
  31 import java.awt.Point;
  32 import java.awt.Toolkit;
  33 import java.awt.datatransfer.DataFlavor;
  34 import java.awt.datatransfer.Transferable;
  35 import java.awt.datatransfer.UnsupportedFlavorException;

  36 import java.awt.dnd.peer.DragSourceContextPeer;

  37 import java.io.IOException;
  38 import java.io.InvalidObjectException;

  39 import java.io.ObjectInputStream;
  40 import java.io.ObjectOutputStream;
  41 import java.io.Serializable;

  42 import java.util.TooManyListenersException;
  43 
  44 import sun.awt.AWTAccessor;
  45 
  46 /**
  47  * The <code>DragSourceContext</code> class is responsible for managing the
  48  * initiator side of the Drag and Drop protocol. In particular, it is responsible
  49  * for managing drag event notifications to the
  50  * {@linkplain DragSourceListener DragSourceListeners}
  51  * and {@linkplain DragSourceMotionListener DragSourceMotionListeners}, and providing the
  52  * {@link Transferable} representing the source data for the drag operation.
  53  * <p>
  54  * Note that the <code>DragSourceContext</code> itself
  55  * implements the <code>DragSourceListener</code> and
  56  * <code>DragSourceMotionListener</code> interfaces.
  57  * This is to allow the platform peer
  58  * (the {@link DragSourceContextPeer} instance)
  59  * created by the {@link DragSource} to notify
  60  * the <code>DragSourceContext</code> of
  61  * state changes in the ongoing operation. This allows the
  62  * <code>DragSourceContext</code> object to interpose
  63  * itself between the platform and the
  64  * listeners provided by the initiator of the drag operation.
  65  * <p>


 105      * An <code>int</code> used by updateCurrentCursor()
 106      * indicating that the <code>Cursor</code>
 107      * has entered a <code>DropTarget</code>.
 108      */
 109     protected static final int ENTER   = 1;
 110 
 111     /**
 112      * An <code>int</code> used by updateCurrentCursor()
 113      * indicating that the <code>Cursor</code> is
 114      * over a <code>DropTarget</code>.
 115      */
 116     protected static final int OVER    = 2;
 117 
 118     /**
 119      * An <code>int</code> used by updateCurrentCursor()
 120      * indicating that the user operation has changed.
 121      */
 122 
 123     protected static final int CHANGED = 3;
 124 
 125     static {
 126         AWTAccessor.setDragSourceContextAccessor(dsc -> dsc.peer);
 127     }
 128 
 129     /**
 130      * Called from <code>DragSource</code>, this constructor creates a new
 131      * <code>DragSourceContext</code> given the
 132      * <code>DragSourceContextPeer</code> for this Drag, the
 133      * <code>DragGestureEvent</code> that triggered the Drag, the initial
 134      * <code>Cursor</code> to use for the Drag, an (optional)
 135      * <code>Image</code> to display while the Drag is taking place, the offset
 136      * of the <code>Image</code> origin from the hotspot at the instant of the
 137      * triggering event, the <code>Transferable</code> subject data, and the
 138      * <code>DragSourceListener</code> to use during the Drag and Drop
 139      * operation.
 140      * <br>
 141      * If <code>DragSourceContextPeer</code> is <code>null</code>
 142      * <code>NullPointerException</code> is thrown.
 143      * <br>
 144      * If <code>DragGestureEvent</code> is <code>null</code>
 145      * <code>NullPointerException</code> is thrown.
 146      * <br>
 147      * If <code>Cursor</code> is <code>null</code> no exception is thrown and
 148      * the default drag cursor behavior is activated for this drag operation.
 149      * <br>
 150      * If <code>Image</code> is <code>null</code> no exception is thrown.
 151      * <br>
 152      * If <code>Image</code> is not <code>null</code> and the offset is
 153      * <code>null</code> <code>NullPointerException</code> is thrown.
 154      * <br>
 155      * If <code>Transferable</code> is <code>null</code>
 156      * <code>NullPointerException</code> is thrown.
 157      * <br>
 158      * If <code>DragSourceListener</code> is <code>null</code> no exception
 159      * is thrown.
 160      *

 161      * @param trigger    the triggering event
 162      * @param dragCursor     the initial {@code Cursor} for this drag operation
 163      *                       or {@code null} for the default cursor handling;
 164      *                       see <a href="DragSourceContext.html#defaultCursor">class level documentation</a>
 165      *                       for more details on the cursor handling mechanism during drag and drop
 166      * @param dragImage  the <code>Image</code> to drag (or <code>null</code>)
 167      * @param offset     the offset of the image origin from the hotspot at the
 168      *                   instant of the triggering event
 169      * @param t          the <code>Transferable</code>
 170      * @param dsl        the <code>DragSourceListener</code>
 171      *
 172      * @throws IllegalArgumentException if the <code>Component</code> associated
 173      *         with the trigger event is <code>null</code>.
 174      * @throws IllegalArgumentException if the <code>DragSource</code> for the
 175      *         trigger event is <code>null</code>.
 176      * @throws IllegalArgumentException if the drag action for the
 177      *         trigger event is <code>DnDConstants.ACTION_NONE</code>.
 178      * @throws IllegalArgumentException if the source actions for the
 179      *         <code>DragGestureRecognizer</code> associated with the trigger
 180      *         event are equal to <code>DnDConstants.ACTION_NONE</code>.
 181      * @throws NullPointerException if dscp, trigger, or t are null, or
 182      *         if dragImage is non-null and offset is null
 183      */
 184     public DragSourceContext(DragGestureEvent trigger, Cursor dragCursor,

 185                              Image dragImage, Point offset, Transferable t,
 186                              DragSourceListener dsl) {
 187         DragSourceContextPeer dscp = Toolkit.getDefaultToolkit()
 188                 .createDragSourceContextPeer(trigger);
 189 
 190         if (dscp == null) {
 191             throw new NullPointerException("DragSourceContextPeer");
 192         }
 193 
 194         if (trigger == null) {
 195             throw new NullPointerException("Trigger");
 196         }
 197 
 198         if (trigger.getDragSource() == null) {
 199             throw new IllegalArgumentException("DragSource");
 200         }
 201 
 202         if (trigger.getComponent() == null) {
 203             throw new IllegalArgumentException("Component");
 204         }
 205 
 206         if (trigger.getSourceAsDragGestureRecognizer().getSourceActions() ==
 207                  DnDConstants.ACTION_NONE) {
 208             throw new IllegalArgumentException("source actions");
 209         }


 610                         public boolean isDataFlavorSupported(DataFlavor flavor)
 611                         {
 612                             return false;
 613                         }
 614                         public Object getTransferData(DataFlavor flavor)
 615                             throws UnsupportedFlavorException
 616                         {
 617                             throw new UnsupportedFlavorException(flavor);
 618                         }
 619                     };
 620             }
 621             transferable = emptyTransferable;
 622         }
 623     }
 624 
 625     private static Transferable emptyTransferable;
 626 
 627     /*
 628      * fields
 629      */
 630     private final transient DragSourceContextPeer peer;

 631 
 632     /**
 633      * The event which triggered the start of the drag.
 634      *
 635      * @serial
 636      */
 637     private DragGestureEvent    trigger;
 638 
 639     /**
 640      * The current drag cursor.
 641      *
 642      * @serial
 643      */
 644     private Cursor              cursor;
 645 
 646     private transient Transferable      transferable;
 647 
 648     private transient DragSourceListener    listener;
 649 
 650     /**