src/java.desktop/macosx/classes/sun/lwawt/macosx/CDragSourceContextPeer.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 2014, 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 
  27 package sun.lwawt.macosx;
  28 
  29 import java.awt.*;
  30 import java.awt.datatransfer.*;
  31 import java.awt.dnd.*;
  32 import java.awt.event.*;
  33 import java.awt.image.*;
  34 import java.awt.peer.*;
  35 
  36 import javax.swing.*;
  37 import javax.swing.text.*;
  38 import javax.accessibility.*;
  39 
  40 import java.util.Map;
  41 import java.util.concurrent.Callable;
  42 

  43 import sun.awt.dnd.*;
  44 import sun.lwawt.LWComponentPeer;
  45 import sun.lwawt.LWWindowPeer;
  46 import sun.lwawt.PlatformWindow;
  47 
  48 
  49 public final class CDragSourceContextPeer extends SunDragSourceContextPeer {
  50 
  51     private static final CDragSourceContextPeer fInstance = new CDragSourceContextPeer(null);
  52 
  53     private Image  fDragImage;
  54     private CImage fDragCImage;
  55     private Point  fDragImageOffset;
  56 
  57     private static Component hoveringComponent = null;
  58 
  59     private static double fMaxImageSize = 128.0;
  60 
  61     static {
  62         String propValue = java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("apple.awt.dnd.defaultDragImageSize"));


  71     }
  72 
  73     private CDragSourceContextPeer(DragGestureEvent dge) {
  74         super(dge);
  75     }
  76 
  77     public static CDragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException {
  78         fInstance.setTrigger(dge);
  79 
  80         return fInstance;
  81     }
  82 
  83     // We have to overload this method just to be able to grab the drag image and its offset as shared code doesn't store it:
  84     public void startDrag(DragSourceContext dsc, Cursor cursor, Image dragImage, Point dragImageOffset) throws InvalidDnDOperationException {
  85         fDragImage = dragImage;
  86         fDragImageOffset = dragImageOffset;
  87 
  88         super.startDrag(dsc, cursor, dragImage, dragImageOffset);
  89     }
  90 
  91     @SuppressWarnings("deprecation")
  92     protected void startDrag(Transferable transferable, long[] formats, Map<Long, DataFlavor> formatMap) {
  93         DragGestureEvent trigger = getTrigger();
  94         InputEvent         triggerEvent = trigger.getTriggerEvent();
  95 
  96         Point dragOrigin = new Point(trigger.getDragOrigin());
  97         int extModifiers = (triggerEvent.getModifiers() | triggerEvent.getModifiersEx());
  98         long timestamp   = triggerEvent.getWhen();
  99         int clickCount   = ((triggerEvent instanceof MouseEvent) ? (((MouseEvent) triggerEvent).getClickCount()) : 1);
 100 
 101         Component component = trigger.getComponent();
 102         // For a lightweight component traverse up the hierarchy to the root
 103         Point loc = component.getLocation();
 104         Component rootComponent = component;
 105         while (!(rootComponent instanceof Window)) {
 106             dragOrigin.translate(loc.x, loc.y);
 107             rootComponent = rootComponent.getParent();
 108             loc = rootComponent.getLocation();
 109         }
 110 
 111         // If there isn't any drag image make one of default appearance:


 118         if (fDragImage != null) {
 119             try {
 120                 fDragCImage = CImage.getCreator().createFromImageImmediately(fDragImage);
 121             } catch(Exception e) {
 122                 // image creation may fail for any reason
 123                 throw new InvalidDnDOperationException("Drag image can not be created.");
 124             }
 125             if (fDragCImage == null) {
 126                 throw new InvalidDnDOperationException("Drag image is not ready.");
 127             }
 128 
 129             dragImageOffset = fDragImageOffset;
 130         } else {
 131 
 132             fDragCImage = null;
 133             dragImageOffset = new Point(0, 0);
 134         }
 135 
 136         try {
 137             //It sure will be LWComponentPeer instance as rootComponent is a Window
 138             PlatformWindow platformWindow = ((LWComponentPeer)rootComponent.getPeer()).getPlatformWindow();


 139             long nativeViewPtr = CPlatformWindow.getNativeViewPtr(platformWindow);
 140             if (nativeViewPtr == 0L) throw new InvalidDnDOperationException("Unsupported platform window implementation");
 141 
 142             // Create native dragging source:
 143             final long nativeDragSource = createNativeDragSource(component, nativeViewPtr, transferable, triggerEvent,
 144                 (int) (dragOrigin.getX()), (int) (dragOrigin.getY()), extModifiers,
 145                 clickCount, timestamp, fDragCImage != null ? fDragCImage.ptr : 0L, dragImageOffset.x, dragImageOffset.y,
 146                 getDragSourceContext().getSourceActions(), formats, formatMap);
 147 
 148             if (nativeDragSource == 0)
 149                 throw new InvalidDnDOperationException("");
 150 
 151             setNativeContext(nativeDragSource);
 152         }
 153 
 154         catch (Exception e) {
 155             throw new InvalidDnDOperationException("failed to create native peer: " + e);
 156         }
 157 
 158         SunDropTargetContextPeer.setCurrentJVMLocalSourceTransferable(transferable);


   1 /*
   2  * Copyright (c) 2011, 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 
  27 package sun.lwawt.macosx;
  28 
  29 import java.awt.*;
  30 import java.awt.datatransfer.*;
  31 import java.awt.dnd.*;
  32 import java.awt.event.*;
  33 import java.awt.image.*;

  34 
  35 import javax.swing.*;
  36 import javax.swing.text.*;
  37 import javax.accessibility.*;
  38 
  39 import java.util.Map;
  40 import java.util.concurrent.Callable;
  41 
  42 import sun.awt.AWTAccessor;
  43 import sun.awt.dnd.*;
  44 import sun.lwawt.LWComponentPeer;
  45 import sun.lwawt.LWWindowPeer;
  46 import sun.lwawt.PlatformWindow;
  47 
  48 
  49 public final class CDragSourceContextPeer extends SunDragSourceContextPeer {
  50 
  51     private static final CDragSourceContextPeer fInstance = new CDragSourceContextPeer(null);
  52 
  53     private Image  fDragImage;
  54     private CImage fDragCImage;
  55     private Point  fDragImageOffset;
  56 
  57     private static Component hoveringComponent = null;
  58 
  59     private static double fMaxImageSize = 128.0;
  60 
  61     static {
  62         String propValue = java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("apple.awt.dnd.defaultDragImageSize"));


  71     }
  72 
  73     private CDragSourceContextPeer(DragGestureEvent dge) {
  74         super(dge);
  75     }
  76 
  77     public static CDragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException {
  78         fInstance.setTrigger(dge);
  79 
  80         return fInstance;
  81     }
  82 
  83     // We have to overload this method just to be able to grab the drag image and its offset as shared code doesn't store it:
  84     public void startDrag(DragSourceContext dsc, Cursor cursor, Image dragImage, Point dragImageOffset) throws InvalidDnDOperationException {
  85         fDragImage = dragImage;
  86         fDragImageOffset = dragImageOffset;
  87 
  88         super.startDrag(dsc, cursor, dragImage, dragImageOffset);
  89     }
  90 

  91     protected void startDrag(Transferable transferable, long[] formats, Map<Long, DataFlavor> formatMap) {
  92         DragGestureEvent trigger = getTrigger();
  93         InputEvent         triggerEvent = trigger.getTriggerEvent();
  94 
  95         Point dragOrigin = new Point(trigger.getDragOrigin());
  96         int extModifiers = (triggerEvent.getModifiers() | triggerEvent.getModifiersEx());
  97         long timestamp   = triggerEvent.getWhen();
  98         int clickCount   = ((triggerEvent instanceof MouseEvent) ? (((MouseEvent) triggerEvent).getClickCount()) : 1);
  99 
 100         Component component = trigger.getComponent();
 101         // For a lightweight component traverse up the hierarchy to the root
 102         Point loc = component.getLocation();
 103         Component rootComponent = component;
 104         while (!(rootComponent instanceof Window)) {
 105             dragOrigin.translate(loc.x, loc.y);
 106             rootComponent = rootComponent.getParent();
 107             loc = rootComponent.getLocation();
 108         }
 109 
 110         // If there isn't any drag image make one of default appearance:


 117         if (fDragImage != null) {
 118             try {
 119                 fDragCImage = CImage.getCreator().createFromImageImmediately(fDragImage);
 120             } catch(Exception e) {
 121                 // image creation may fail for any reason
 122                 throw new InvalidDnDOperationException("Drag image can not be created.");
 123             }
 124             if (fDragCImage == null) {
 125                 throw new InvalidDnDOperationException("Drag image is not ready.");
 126             }
 127 
 128             dragImageOffset = fDragImageOffset;
 129         } else {
 130 
 131             fDragCImage = null;
 132             dragImageOffset = new Point(0, 0);
 133         }
 134 
 135         try {
 136             //It sure will be LWComponentPeer instance as rootComponent is a Window
 137             LWComponentPeer<?, ?> peer = AWTAccessor.getComponentAccessor()
 138                                                     .getPeer(rootComponent);
 139             PlatformWindow platformWindow = peer.getPlatformWindow();
 140             long nativeViewPtr = CPlatformWindow.getNativeViewPtr(platformWindow);
 141             if (nativeViewPtr == 0L) throw new InvalidDnDOperationException("Unsupported platform window implementation");
 142 
 143             // Create native dragging source:
 144             final long nativeDragSource = createNativeDragSource(component, nativeViewPtr, transferable, triggerEvent,
 145                 (int) (dragOrigin.getX()), (int) (dragOrigin.getY()), extModifiers,
 146                 clickCount, timestamp, fDragCImage != null ? fDragCImage.ptr : 0L, dragImageOffset.x, dragImageOffset.y,
 147                 getDragSourceContext().getSourceActions(), formats, formatMap);
 148 
 149             if (nativeDragSource == 0)
 150                 throw new InvalidDnDOperationException("");
 151 
 152             setNativeContext(nativeDragSource);
 153         }
 154 
 155         catch (Exception e) {
 156             throw new InvalidDnDOperationException("failed to create native peer: " + e);
 157         }
 158 
 159         SunDropTargetContextPeer.setCurrentJVMLocalSourceTransferable(transferable);