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 getPlatformScale() {
  50         View view = scene.getPlatformView();
  51         if (view != null) {
  52             Window w = view.getWindow();
  53             if (w != null) {
  54                 return w.getPlatformScale();
  55             }
  56         }
  57         return 1.0;
  58     }
  59 
  60     public TransferMode handleDragEnter(final int x, final int y, final int xAbs, final int yAbs,
  61                                         final TransferMode recommendedTransferMode,
  62                                         final ClipboardAssistance dropTargetAssistant)
  63     {
  64         assert Platform.isFxApplicationThread();
  65         return AccessController.doPrivileged((PrivilegedAction<TransferMode>) () -> {
  66             if (scene.dropTargetListener != null) {
  67                 double pScale = getPlatformScale();
  68                 QuantumClipboard dragboard =
  69                         QuantumClipboard.getDragboardInstance(dropTargetAssistant, false);
  70                 return scene.dropTargetListener.dragEnter(x / pScale, y / pScale, xAbs / pScale, yAbs / pScale,
  71                         recommendedTransferMode, dragboard);
  72             }
  73             return null;
  74         }, scene.getAccessControlContext());
  75     }
  76 
  77     public void handleDragLeave(final ClipboardAssistance dropTargetAssistant) {
  78         assert Platform.isFxApplicationThread();
  79         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
  80             if (scene.dropTargetListener != null) {
  81                 scene.dropTargetListener.dragExit(0, 0, 0, 0);
  82             }
  83             return null;
  84         }, scene.getAccessControlContext());
  85     }
  86 
  87     public TransferMode handleDragDrop(final int x, final int y, final int xAbs, final int yAbs,
  88                                        final TransferMode recommendedTransferMode,
  89                                        final ClipboardAssistance dropTargetAssistant)
  90     {
  91         assert Platform.isFxApplicationThread();
  92         return AccessController.doPrivileged((PrivilegedAction<TransferMode>) () -> {
  93             if (scene.dropTargetListener != null) {
  94                 double pScale = getPlatformScale();
  95                 return scene.dropTargetListener.drop(x / pScale, y / pScale, xAbs / pScale, yAbs / pScale,
  96                         recommendedTransferMode);
  97             }
  98             return null;
  99         }, scene.getAccessControlContext());
 100     }
 101 
 102     public TransferMode handleDragOver(final int x, final int y, final int xAbs, final int yAbs,
 103                                        final TransferMode recommendedTransferMode,
 104                                        final ClipboardAssistance dropTargetAssistant)
 105     {
 106         assert Platform.isFxApplicationThread();
 107         return AccessController.doPrivileged((PrivilegedAction<TransferMode>) () -> {
 108             if (scene.dropTargetListener != null) {
 109                 double pScale = getPlatformScale();
 110                 return scene.dropTargetListener.dragOver(x / pScale, y / pScale, xAbs / pScale, yAbs / pScale,
 111                         recommendedTransferMode);
 112             }
 113             return null;
 114         }, scene.getAccessControlContext());
 115     }
 116 
 117     // Drag source handlers
 118 
 119     // This is a callback from the native platform, when a drag gesture is
 120     // detected. This mechanism is currently not used in FX, as we have
 121     // a custom gesture recognizer in Scene, and DnD is started with
 122     // Toolkit.startDrag().
 123     public void handleDragStart(final int button, final int x, final int y, final int xAbs, final int yAbs,
 124                                 final ClipboardAssistance dragSourceAssistant)
 125     {
 126         assert Platform.isFxApplicationThread();
 127         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 128             if (scene.dragGestureListener != null) {
 129                 double pScale = getPlatformScale();
 130                 QuantumClipboard dragboard =
 131                         QuantumClipboard.getDragboardInstance(dragSourceAssistant, true);
 132                 scene.dragGestureListener.dragGestureRecognized(
 133                         x / pScale, y / pScale, xAbs / pScale, yAbs / pScale, button, dragboard);
 134             }
 135             return null;
 136         }, scene.getAccessControlContext());
 137     }
 138 
 139     // This is a callback from the native platform, when the drag was started
 140     // from handleDragStart() above, or when FX as a drag source is embedded
 141     // to Swing/SWT.
 142     public void handleDragEnd(final TransferMode performedTransferMode,
 143                               final ClipboardAssistance dragSourceAssistant)
 144     {
 145         assert Platform.isFxApplicationThread();
 146         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 147             try {
 148                 if (scene.dragSourceListener != null) {
 149                     scene.dragSourceListener.dragDropEnd(0, 0, 0, 0, performedTransferMode);
 150                 }
 151             } finally {
 152                 QuantumClipboard.releaseCurrentDragboard();
 153             }
 154             return null;
 155         }, scene.getAccessControlContext());
 156     }
 157 
 158 }