1 /*
   2  * Copyright (c) 2000, 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 javafx.scene.input;
  27 
  28 import java.security.Permission;
  29 import java.util.Set;
  30 
  31 import com.sun.javafx.scene.input.DragboardHelper;
  32 import com.sun.javafx.tk.PermissionHelper;
  33 import com.sun.javafx.tk.TKClipboard;
  34 import com.sun.javafx.tk.TKScene;
  35 import javafx.scene.image.Image;
  36 
  37 /**
  38  * A drag and drop specific {@link Clipboard}.
  39  * @since JavaFX 2.0
  40  */
  41 public final class Dragboard extends Clipboard {
  42 
  43     /**
  44      * Whether access to the data requires a permission.
  45      */
  46     private boolean dataAccessRestricted = true;
  47 
  48     Dragboard(TKClipboard peer) {
  49         super(peer);
  50     }
  51 
  52     @Override
  53     Object getContentImpl(DataFormat dataFormat) {
  54         if (dataAccessRestricted) {
  55             final SecurityManager securityManager = System.getSecurityManager();
  56             if (securityManager != null) {
  57                 final Permission clipboardPerm =
  58                         PermissionHelper.getAccessClipboardPermission();
  59                 securityManager.checkPermission(clipboardPerm);
  60             }
  61         }
  62         return super.getContentImpl(dataFormat);
  63     }
  64 
  65     /**
  66      * Gets set of transport modes supported by source of this drag opeation.
  67      * @return set of supported transfer modes
  68      */
  69     public final Set<TransferMode> getTransferModes() {
  70         return peer.getTransferModes();
  71     }
  72 
  73     /**
  74      * @treatAsPrivate implementation detail
  75      * @deprecated This is an internal API that is not intended for use and will be removed in the next version
  76      */
  77     @Deprecated
  78     public TKClipboard impl_getPeer() {
  79         return peer;
  80     }
  81 
  82     /**
  83      * @treatAsPrivate implementation detail
  84      * @deprecated This is an internal API that is not intended for use and will be removed in the next version
  85      */
  86     @Deprecated
  87     public static Dragboard impl_createDragboard(TKClipboard peer) {
  88         return new Dragboard(peer);
  89     }
  90 
  91     // PENDING_DOC_REVIEW
  92     /**
  93      * Sets the visual representation of data being transfered
  94      * in a drag and drop gesture.
  95      * Uses the given image for the drag view with the offsetX and offsetY
  96      * specifying cursor position over the image.
  97      * This method should be called only when starting drag and drop operation
  98      * in the DRAG_DETECTED handler, calling it at other times
  99      * doesn't have any effect.
 100      * @param image image to use for the drag view
 101      * @param offsetX x position of the cursor over the image
 102      * @param offsetY y position of the cursor over the image
 103      * @since JavaFX 8.0
 104      */
 105     public void setDragView(Image image, double offsetX, double offsetY) {
 106         peer.setDragView(image);
 107         peer.setDragViewOffsetX(offsetX);
 108         peer.setDragViewOffsetY(offsetY);
 109     }
 110 
 111     /**
 112      * Sets the visual representation of data being transfered
 113      * in a drag and drop gesture.
 114      * This method should be called only when starting drag and drop operation
 115      * in the DRAG_DETECTED handler, calling it at other times
 116      * doesn't have any effect.
 117      * @param image image to use for the drag view
 118      * @since JavaFX 8.0
 119      */
 120     public void setDragView(Image image) {
 121         peer.setDragView(image);
 122     }
 123 
 124     /**
 125      * Sets the x position of the cursor of the drag view image.
 126      * This method should be called only when starting drag and drop operation
 127      * in the DRAG_DETECTED handler, calling it at other times
 128      * doesn't have any effect.
 129      * @param offsetX x position of the cursor over the image
 130      * @since JavaFX 8.0
 131      */
 132     public void setDragViewOffsetX(double offsetX) {
 133         peer.setDragViewOffsetX(offsetX);
 134     }
 135 
 136     /**
 137      * Sets the y position of the cursor of the drag view image.
 138      * This method should be called only when starting drag and drop operation
 139      * in the DRAG_DETECTED handler, calling it at other times
 140      * doesn't have any effect.
 141      * @param offsetY y position of the cursor over the image
 142      * @since JavaFX 8.0
 143      */
 144     public void setDragViewOffsetY(double offsetY) {
 145         peer.setDragViewOffsetY(offsetY);
 146     }
 147 
 148     /**
 149      * Gets the image used as a drag view.
 150      * This method returns meaningful value only when starting drag and drop
 151      * operation in the DRAG_DETECTED handler, it returns null at other times.
 152      * @return the image used as a drag view
 153      * @since JavaFX 8.0
 154      */
 155     public Image getDragView() {
 156         return peer.getDragView();
 157     }
 158 
 159     /**
 160      * Gets the x position of the cursor of the drag view image.
 161      * This method returns meaningful value only when starting drag and drop
 162      * operation in the DRAG_DETECTED handler, it returns 0 at other times.
 163      * @return x position of the cursor over the image
 164      * @since JavaFX 8.0
 165      */
 166     public double getDragViewOffsetX() {
 167         return peer.getDragViewOffsetX();
 168     }
 169 
 170     /**
 171      * Gets the y position of the cursor of the drag view image.
 172      * This method returns meaningful value only when starting drag and drop
 173      * operation in the DRAG_DETECTED handler, it returns 0 at other times.
 174      * @return y position of the cursor over the image
 175      * @since JavaFX 8.0
 176      */
 177     public double getDragViewOffsetY() {
 178         return peer.getDragViewOffsetY();
 179     }
 180 
 181     static {
 182         // This is used by classes in different packages to get access to
 183         // private and package private methods.
 184         DragboardHelper.setDragboardAccessor((dragboard, restricted) -> {
 185             dragboard.dataAccessRestricted = restricted;
 186         });
 187     }
 188 }