1 /*
   2  * Copyright (c) 2012, 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 com.sun.javafx.tk.quantum;
  27 
  28 import javafx.application.Platform;
  29 
  30 import javafx.scene.input.TransferMode;
  31 
  32 import com.sun.glass.ui.ClipboardAssistance;
  33 import com.sun.glass.ui.View;
  34 import com.sun.glass.ui.Window;
  35 
  36 import java.security.AccessController;
  37 import java.security.PrivilegedAction;
  38 
  39 class GlassSceneDnDEventHandler {
  40 
  41     private final GlassScene scene;
  42 
  43     public GlassSceneDnDEventHandler(final GlassScene scene) {
  44         this.scene = scene;
  45     }
  46 
  47     // Drop target handlers
  48 
  49     private double getPlatformScaleX() {
  50         View view = scene.getPlatformView();
  51         if (view != null) {
  52             Window w = view.getWindow();
  53             if (w != null) {
  54                 return w.getPlatformScaleX();
  55             }
  56         }
  57         return 1.0;
  58     }
  59 
  60     private double getPlatformScaleY() {
  61         View view = scene.getPlatformView();
  62         if (view != null) {
  63             Window w = view.getWindow();
  64             if (w != null) {
  65                 return w.getPlatformScaleY();
  66             }
  67         }
  68         return 1.0;
  69     }
  70 
  71     public TransferMode handleDragEnter(final int x, final int y, final int xAbs, final int yAbs,
  72                                         final TransferMode recommendedTransferMode,
  73                                         final ClipboardAssistance dropTargetAssistant)
  74     {
  75         assert Platform.isFxApplicationThread();
  76         return AccessController.doPrivileged((PrivilegedAction<TransferMode>) () -> {
  77             if (scene.dropTargetListener != null) {
  78                 double pScaleX = getPlatformScaleX();
  79                 double pScaleY = getPlatformScaleY();
  80                 QuantumClipboard dragboard =
  81                         QuantumClipboard.getDragboardInstance(dropTargetAssistant, false);
  82                 return scene.dropTargetListener.dragEnter(x / pScaleX, y / pScaleY, xAbs / pScaleX, yAbs / pScaleY,
  83                         recommendedTransferMode, dragboard);
  84             }
  85             return null;
  86         }, scene.getAccessControlContext());
  87     }
  88 
  89     public void handleDragLeave(final ClipboardAssistance dropTargetAssistant) {
  90         assert Platform.isFxApplicationThread();
  91         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
  92             if (scene.dropTargetListener != null) {
  93                 scene.dropTargetListener.dragExit(0, 0, 0, 0);
  94             }
  95             return null;
  96         }, scene.getAccessControlContext());
  97     }
  98 
  99     public TransferMode handleDragDrop(final int x, final int y, final int xAbs, final int yAbs,
 100                                        final TransferMode recommendedTransferMode,
 101                                        final ClipboardAssistance dropTargetAssistant)
 102     {
 103         assert Platform.isFxApplicationThread();
 104         return AccessController.doPrivileged((PrivilegedAction<TransferMode>) () -> {
 105             if (scene.dropTargetListener != null) {
 106                 double pScaleX = getPlatformScaleX();
 107                 double pScaleY = getPlatformScaleY();
 108                 return scene.dropTargetListener.drop(x / pScaleX, y / pScaleY, xAbs / pScaleX, yAbs / pScaleY,
 109                         recommendedTransferMode);
 110             }
 111             return null;
 112         }, scene.getAccessControlContext());
 113     }
 114 
 115     public TransferMode handleDragOver(final int x, final int y, final int xAbs, final int yAbs,
 116                                        final TransferMode recommendedTransferMode,
 117                                        final ClipboardAssistance dropTargetAssistant)
 118     {
 119         assert Platform.isFxApplicationThread();
 120         return AccessController.doPrivileged((PrivilegedAction<TransferMode>) () -> {
 121             if (scene.dropTargetListener != null) {
 122                 double pScaleX = getPlatformScaleX();
 123                 double pScaleY = getPlatformScaleY();
 124                 return scene.dropTargetListener.dragOver(x / pScaleX, y / pScaleY, xAbs / pScaleX, yAbs / pScaleY,
 125                         recommendedTransferMode);
 126             }
 127             return null;
 128         }, scene.getAccessControlContext());
 129     }
 130 
 131     // Drag source handlers
 132 
 133     // This is a callback from the native platform, when a drag gesture is
 134     // detected. This mechanism is currently not used in FX, as we have
 135     // a custom gesture recognizer in Scene, and DnD is started with
 136     // Toolkit.startDrag().
 137     public void handleDragStart(final int button, final int x, final int y, final int xAbs, final int yAbs,
 138                                 final ClipboardAssistance dragSourceAssistant)
 139     {
 140         assert Platform.isFxApplicationThread();
 141         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 142             if (scene.dragGestureListener != null) {
 143                 double pScaleX = getPlatformScaleX();
 144                 double pScaleY = getPlatformScaleY();
 145                 QuantumClipboard dragboard =
 146                         QuantumClipboard.getDragboardInstance(dragSourceAssistant, true);
 147                 scene.dragGestureListener.dragGestureRecognized(
 148                         x / pScaleX, y / pScaleY, xAbs / pScaleX, yAbs / pScaleY, button, dragboard);
 149             }
 150             return null;
 151         }, scene.getAccessControlContext());
 152     }
 153 
 154     // This is a callback from the native platform, when the drag was started
 155     // from handleDragStart() above, or when FX as a drag source is embedded
 156     // to Swing/SWT.
 157     public void handleDragEnd(final TransferMode performedTransferMode,
 158                               final ClipboardAssistance dragSourceAssistant)
 159     {
 160         assert Platform.isFxApplicationThread();
 161         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 162             try {
 163                 if (scene.dragSourceListener != null) {
 164                     scene.dragSourceListener.dragDropEnd(0, 0, 0, 0, performedTransferMode);
 165                 }
 166             } finally {
 167                 QuantumClipboard.releaseCurrentDragboard();
 168             }
 169             return null;
 170         }, scene.getAccessControlContext());
 171     }
 172 
 173 }