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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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 
  30 import java.awt.datatransfer.DataFlavor;
  31 import java.awt.datatransfer.Transferable;
  32 import java.awt.datatransfer.UnsupportedFlavorException;
  33 
  34 import java.awt.dnd.peer.DropTargetContextPeer;
  35 
  36 import java.io.IOException;
  37 import java.io.Serializable;
  38 
  39 import java.util.Arrays;
  40 import java.util.List;
  41 


  42 
  43 /**
  44  * A <code>DropTargetContext</code> is created
  45  * whenever the logical cursor associated
  46  * with a Drag and Drop operation coincides with the visible geometry of
  47  * a <code>Component</code> associated with a <code>DropTarget</code>.
  48  * The <code>DropTargetContext</code> provides
  49  * the mechanism for a potential receiver
  50  * of a drop operation to both provide the end user with the appropriate
  51  * drag under feedback, but also to effect the subsequent data transfer
  52  * if appropriate.
  53  *
  54  * @since 1.2
  55  */
  56 
  57 public class DropTargetContext implements Serializable {
  58 
  59     private static final long serialVersionUID = -634158968993743371L;
  60 













  61     /**
  62      * Construct a <code>DropTargetContext</code>
  63      * given a specified <code>DropTarget</code>.
  64      *
  65      * @param dt the DropTarget to associate with
  66      */
  67 
  68     DropTargetContext(DropTarget dt) {
  69         super();
  70 
  71         dropTarget = dt;
  72     }
  73 
  74     /**
  75      * This method returns the <code>DropTarget</code> associated with this
  76      * <code>DropTargetContext</code>.
  77      *
  78      * @return the <code>DropTarget</code> associated with this <code>DropTargetContext</code>
  79      */
  80 
  81     public DropTarget getDropTarget() { return dropTarget; }
  82 
  83     /**
  84      * This method returns the <code>Component</code> associated with
  85      * this <code>DropTargetContext</code>.
  86      *
  87      * @return the Component associated with this Context
  88      */
  89 
  90     public Component getComponent() { return dropTarget.getComponent(); }
  91 
  92     /**
  93      * Called when associated with the <code>DropTargetContextPeer</code>.
  94      *
  95      * @param dtcp the <code>DropTargetContextPeer</code>
  96      */
  97 
  98     public void addNotify(DropTargetContextPeer dtcp) {
  99         dropTargetContextPeer = dtcp;
 100     }
 101 
 102     /**
 103      * Called when disassociated with the <code>DropTargetContextPeer</code>.
 104      */
 105 
 106     public void removeNotify() {
 107         dropTargetContextPeer = null;
 108         transferable          = null;
 109     }
 110 
 111     /**
 112      * This method sets the current actions acceptable to
 113      * this <code>DropTarget</code>.
 114      *
 115      * @param actions an <code>int</code> representing the supported action(s)
 116      */
 117 
 118     protected void setTargetActions(int actions) {
 119         DropTargetContextPeer peer = getDropTargetContextPeer();
 120         if (peer != null) {
 121             synchronized (peer) {
 122                 peer.setTargetActions(actions);
 123                 getDropTarget().doSetDefaultActions(actions);
 124             }
 125         } else {
 126             getDropTarget().doSetDefaultActions(actions);


 265         } else {
 266             if (transferable == null) {
 267                 Transferable t = peer.getTransferable();
 268                 boolean isLocal = peer.isTransferableJVMLocal();
 269                 synchronized (this) {
 270                     if (transferable == null) {
 271                         transferable = createTransferableProxy(t, isLocal);
 272                     }
 273                 }
 274             }
 275 
 276             return transferable;
 277         }
 278     }
 279 
 280     /**
 281      * Get the <code>DropTargetContextPeer</code>
 282      *
 283      * @return the platform peer
 284      */
 285 
 286     DropTargetContextPeer getDropTargetContextPeer() {
 287         return dropTargetContextPeer;
 288     }
 289 
 290     /**







 291      * Creates a TransferableProxy to proxy for the specified
 292      * Transferable.
 293      *
 294      * @param t the <tt>Transferable</tt> to be proxied
 295      * @param local <tt>true</tt> if <tt>t</tt> represents
 296      *        the result of a local drag-n-drop operation.
 297      * @return the new <tt>TransferableProxy</tt> instance.
 298      */
 299     protected Transferable createTransferableProxy(Transferable t, boolean local) {
 300         return new TransferableProxy(t, local);
 301     }
 302 
 303 /****************************************************************************/
 304 
 305 
 306     /**
 307      * <code>TransferableProxy</code> is a helper inner class that implements
 308      * <code>Transferable</code> interface and serves as a proxy for another
 309      * <code>Transferable</code> object which represents data transfer for
 310      * a particular drag-n-drop operation.


 395          * A <code>boolean</code> indicating if the encapsulated
 396          * <code>Transferable</code> object represents the result
 397          * of local drag-n-drop operation (within the same JVM).
 398          */
 399         protected boolean       isLocal;
 400 
 401         private sun.awt.datatransfer.TransferableProxy proxy;
 402     }
 403 
 404 /****************************************************************************/
 405 
 406     /*
 407      * fields
 408      */
 409 
 410     /**
 411      * The DropTarget associated with this DropTargetContext.
 412      *
 413      * @serial
 414      */
 415     private DropTarget dropTarget;
 416 
 417     private transient DropTargetContextPeer dropTargetContextPeer;
 418 
 419     private transient Transferable transferable;
 420 }
   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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 
  30 import java.awt.datatransfer.DataFlavor;
  31 import java.awt.datatransfer.Transferable;
  32 import java.awt.datatransfer.UnsupportedFlavorException;
  33 
  34 import java.awt.dnd.peer.DropTargetContextPeer;
  35 
  36 import java.io.IOException;
  37 import java.io.Serializable;
  38 
  39 import java.util.Arrays;
  40 import java.util.List;
  41 
  42 import sun.awt.AWTAccessor;
  43 import sun.awt.AWTAccessor.DropTargetContextAccessor;
  44 
  45 /**
  46  * A <code>DropTargetContext</code> is created
  47  * whenever the logical cursor associated
  48  * with a Drag and Drop operation coincides with the visible geometry of
  49  * a <code>Component</code> associated with a <code>DropTarget</code>.
  50  * The <code>DropTargetContext</code> provides
  51  * the mechanism for a potential receiver
  52  * of a drop operation to both provide the end user with the appropriate
  53  * drag under feedback, but also to effect the subsequent data transfer
  54  * if appropriate.
  55  *
  56  * @since 1.2
  57  */
  58 
  59 public class DropTargetContext implements Serializable {
  60 
  61     private static final long serialVersionUID = -634158968993743371L;
  62 
  63     static {
  64         AWTAccessor.setDropTargetContextAccessor(new DropTargetContextAccessor() {
  65             @Override
  66             public void reset(DropTargetContext dtc) {
  67                 dtc.reset();
  68             }
  69             @Override
  70             public void setDropTargetContextPeer(DropTargetContext dtc,
  71                                                  DropTargetContextPeer dtcp) {
  72                 dtc.setDropTargetContextPeer(dtcp);
  73             }
  74         });
  75     }
  76     /**
  77      * Construct a <code>DropTargetContext</code>
  78      * given a specified <code>DropTarget</code>.
  79      *
  80      * @param dt the DropTarget to associate with
  81      */
  82 
  83     DropTargetContext(DropTarget dt) {
  84         super();
  85 
  86         dropTarget = dt;
  87     }
  88 
  89     /**
  90      * This method returns the <code>DropTarget</code> associated with this
  91      * <code>DropTargetContext</code>.
  92      *
  93      * @return the <code>DropTarget</code> associated with this <code>DropTargetContext</code>
  94      */
  95 
  96     public DropTarget getDropTarget() { return dropTarget; }
  97 
  98     /**
  99      * This method returns the <code>Component</code> associated with
 100      * this <code>DropTargetContext</code>.
 101      *
 102      * @return the Component associated with this Context
 103      */
 104 
 105     public Component getComponent() { return dropTarget.getComponent(); }
 106 
 107     /**










 108      * Called when disassociated with the <code>DropTargetContextPeer</code>.
 109      */
 110     void reset() {

 111         dropTargetContextPeer = null;
 112         transferable          = null;
 113     }
 114 
 115     /**
 116      * This method sets the current actions acceptable to
 117      * this <code>DropTarget</code>.
 118      *
 119      * @param actions an <code>int</code> representing the supported action(s)
 120      */
 121 
 122     protected void setTargetActions(int actions) {
 123         DropTargetContextPeer peer = getDropTargetContextPeer();
 124         if (peer != null) {
 125             synchronized (peer) {
 126                 peer.setTargetActions(actions);
 127                 getDropTarget().doSetDefaultActions(actions);
 128             }
 129         } else {
 130             getDropTarget().doSetDefaultActions(actions);


 269         } else {
 270             if (transferable == null) {
 271                 Transferable t = peer.getTransferable();
 272                 boolean isLocal = peer.isTransferableJVMLocal();
 273                 synchronized (this) {
 274                     if (transferable == null) {
 275                         transferable = createTransferableProxy(t, isLocal);
 276                     }
 277                 }
 278             }
 279 
 280             return transferable;
 281         }
 282     }
 283 
 284     /**
 285      * Get the <code>DropTargetContextPeer</code>
 286      *
 287      * @return the platform peer
 288      */

 289     DropTargetContextPeer getDropTargetContextPeer() {
 290         return dropTargetContextPeer;
 291     }
 292 
 293     /**
 294      * Sets the {@code DropTargetContextPeer}
 295      */
 296     void setDropTargetContextPeer(final DropTargetContextPeer dtcp) {
 297         dropTargetContextPeer = dtcp;
 298     }
 299 
 300     /**
 301      * Creates a TransferableProxy to proxy for the specified
 302      * Transferable.
 303      *
 304      * @param t the <tt>Transferable</tt> to be proxied
 305      * @param local <tt>true</tt> if <tt>t</tt> represents
 306      *        the result of a local drag-n-drop operation.
 307      * @return the new <tt>TransferableProxy</tt> instance.
 308      */
 309     protected Transferable createTransferableProxy(Transferable t, boolean local) {
 310         return new TransferableProxy(t, local);
 311     }
 312 
 313 /****************************************************************************/
 314 
 315 
 316     /**
 317      * <code>TransferableProxy</code> is a helper inner class that implements
 318      * <code>Transferable</code> interface and serves as a proxy for another
 319      * <code>Transferable</code> object which represents data transfer for
 320      * a particular drag-n-drop operation.


 405          * A <code>boolean</code> indicating if the encapsulated
 406          * <code>Transferable</code> object represents the result
 407          * of local drag-n-drop operation (within the same JVM).
 408          */
 409         protected boolean       isLocal;
 410 
 411         private sun.awt.datatransfer.TransferableProxy proxy;
 412     }
 413 
 414 /****************************************************************************/
 415 
 416     /*
 417      * fields
 418      */
 419 
 420     /**
 421      * The DropTarget associated with this DropTargetContext.
 422      *
 423      * @serial
 424      */
 425     private final DropTarget dropTarget;
 426 
 427     private transient DropTargetContextPeer dropTargetContextPeer;
 428 
 429     private transient Transferable transferable;
 430 }