src/macosx/classes/sun/lwawt/macosx/CDropTargetContextPeer.java

Print this page




   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 sun.lwawt.macosx;
  27 
  28 import java.awt.*;

  29 
  30 import sun.awt.dnd.SunDropTargetContextPeer;
  31 import sun.awt.dnd.SunDropTargetEvent;
  32 
  33 import javax.swing.*;
  34 
  35 
  36 final class CDropTargetContextPeer extends SunDropTargetContextPeer {
  37 
  38     private long    fNativeDropTransfer = 0;
  39     private long    fNativeDataAvailable = 0;
  40     private Object  fNativeData    = null;
  41     private boolean insideTarget = true;
  42 
  43     Object awtLockAccess = new Object();
  44 
  45     static CDropTargetContextPeer getDropTargetContextPeer() {
  46         return new CDropTargetContextPeer();
  47     }
  48 
  49     private CDropTargetContextPeer() {
  50         super();
  51     }
  52 
  53     // We block, waiting for an empty event to get posted (CToolkit.invokeAndWait)
  54     // This is so we finish dispatching DropTarget events before we dispatch the dragDropFinished event (which is a higher priority).
  55     private void flushEvents(Component c) {
  56         try {
  57             LWCToolkit.invokeAndWait(new Runnable() {
  58                 public synchronized void run() {
  59                 }
  60             }, c);
  61         }


  71             fNativeDataAvailable = 0;
  72 
  73             if (fNativeDropTransfer == 0) {
  74                 fNativeDropTransfer = startTransfer(nativeDropTarget, format);
  75             } else {
  76                 addTransfer(nativeDropTarget, fNativeDropTransfer, format);
  77             }
  78 
  79             while (format != fNativeDataAvailable) {
  80                 try {
  81                     awtLockAccess.wait();
  82                 } catch (Throwable e) {
  83                     e.printStackTrace();
  84                 }
  85             }
  86         }
  87 
  88         return fNativeData;
  89     }
  90 
  91     // We need to take care of dragExit message because for some reason it is not being
  92     // generated for lightweight components
  93     @Override
  94     protected void processMotionMessage(SunDropTargetEvent event, boolean operationChanged) {
  95         Component eventSource = (Component)event.getComponent();
  96         Point screenPoint = event.getPoint();
  97         SwingUtilities.convertPointToScreen(screenPoint, eventSource);
  98         Rectangle screenBounds = new Rectangle(eventSource.getLocationOnScreen().x,
  99                 eventSource.getLocationOnScreen().y,
 100                 eventSource.getWidth(), eventSource.getHeight());
 101         if(insideTarget) {
 102             if(!screenBounds.contains(screenPoint)) {
 103                 processExitMessage(event);
 104                 insideTarget = false;
 105                 return;
 106             }
 107         } else {
 108             if(screenBounds.contains(screenPoint)) {
 109                 processEnterMessage(event);
 110                 insideTarget = true;
 111             } else {
 112                 return;
 113             }
 114         }
 115         super.processMotionMessage(event, operationChanged);
 116     }
 117 




























 118     @Override
 119     protected void processDropMessage(SunDropTargetEvent event) {
 120         Component eventSource = (Component)event.getComponent();







 121         Point screenPoint = event.getPoint();
 122         SwingUtilities.convertPointToScreen(screenPoint, eventSource);
 123         Rectangle screenBounds = new Rectangle(eventSource.getLocationOnScreen().x,
 124                 eventSource.getLocationOnScreen().y,
 125                 eventSource.getWidth(), eventSource.getHeight());
 126         if(screenBounds.contains(screenPoint)) {
 127             super.processDropMessage(event);
 128         }
 129     }
 130 
 131     @Override
 132     protected int postDropTargetEvent(Component component, int x, int y, int dropAction,
 133                                       int actions, long[] formats, long nativeCtxt, int eventID,
 134                                       boolean dispatchType) {
 135         // On MacOS X all the DnD events should be synchronous
 136         return super.postDropTargetEvent(component, x, y, dropAction, actions, formats, nativeCtxt,
 137                 eventID, SunDropTargetContextPeer.DISPATCH_SYNC);
 138     }
 139 
 140     // Signal drop complete:
 141     protected void doDropDone(boolean success, int dropAction, boolean isLocal) {
 142         long nativeDropTarget = this.getNativeDragContext();
 143 
 144         dropDone(nativeDropTarget, fNativeDropTransfer, isLocal, success, dropAction);
 145     }
 146 
 147     // Notify transfer complete - this is an upcall from getNativeData's native calls:
 148     private void newData(long format, byte[] data) {




   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 sun.lwawt.macosx;
  27 
  28 import java.awt.*;
  29 import java.awt.dnd.DropTarget;
  30 
  31 import sun.awt.dnd.SunDropTargetContextPeer;
  32 import sun.awt.dnd.SunDropTargetEvent;
  33 
  34 import javax.swing.*;
  35 
  36 
  37 final class CDropTargetContextPeer extends SunDropTargetContextPeer {
  38 
  39     private long    fNativeDropTransfer = 0;
  40     private long    fNativeDataAvailable = 0;
  41     private Object  fNativeData    = null;
  42     private DropTarget insideTarget = null;
  43 
  44     Object awtLockAccess = new Object();
  45 
  46     static CDropTargetContextPeer getDropTargetContextPeer() {
  47         return new CDropTargetContextPeer();
  48     }
  49 
  50     private CDropTargetContextPeer() {
  51         super();
  52     }
  53 
  54     // We block, waiting for an empty event to get posted (CToolkit.invokeAndWait)
  55     // This is so we finish dispatching DropTarget events before we dispatch the dragDropFinished event (which is a higher priority).
  56     private void flushEvents(Component c) {
  57         try {
  58             LWCToolkit.invokeAndWait(new Runnable() {
  59                 public synchronized void run() {
  60                 }
  61             }, c);
  62         }


  72             fNativeDataAvailable = 0;
  73 
  74             if (fNativeDropTransfer == 0) {
  75                 fNativeDropTransfer = startTransfer(nativeDropTarget, format);
  76             } else {
  77                 addTransfer(nativeDropTarget, fNativeDropTransfer, format);
  78             }
  79 
  80             while (format != fNativeDataAvailable) {
  81                 try {
  82                     awtLockAccess.wait();
  83                 } catch (Throwable e) {
  84                     e.printStackTrace();
  85                 }
  86             }
  87         }
  88 
  89         return fNativeData;
  90     }
  91 
  92     // We need to take care of dragEnter and dragExit messages because
  93     // native system generates them only for heavyweights
  94     @Override
  95     protected void processMotionMessage(SunDropTargetEvent event, boolean operationChanged) {
  96         boolean eventInsideTarget = isEventInsideTarget(event);
  97         if(event.getComponent().getDropTarget() == insideTarget) {
  98             if(!eventInsideTarget) {





  99                 processExitMessage(event);

 100                 return;
 101             }
 102         } else {
 103             if(eventInsideTarget) {
 104                 processEnterMessage(event);

 105             } else {
 106                 return;
 107             }
 108         }
 109         super.processMotionMessage(event, operationChanged);
 110     }
 111 
 112     /**
 113      * Could be called when DnD enters a heavyweight or synthesized in processMotionMessage
 114      */
 115     @Override
 116     protected void processEnterMessage(SunDropTargetEvent event) {
 117         Component c = event.getComponent();
 118         DropTarget dt = event.getComponent().getDropTarget();
 119         if (isEventInsideTarget(event)
 120                 && dt != insideTarget
 121                 && c.isShowing()
 122                 && dt != null
 123                 && dt.isActive()) {
 124             insideTarget = dt;
 125             super.processEnterMessage(event);
 126         }
 127     }
 128 
 129     /**
 130      * Could be called when DnD exits a heavyweight or synthesized in processMotionMessage
 131      */
 132     @Override
 133     protected void processExitMessage(SunDropTargetEvent event) {
 134         if(event.getComponent().getDropTarget() == insideTarget) {
 135             insideTarget = null;
 136             super.processExitMessage(event);
 137         }
 138     }
 139 
 140     @Override
 141     protected void processDropMessage(SunDropTargetEvent event) {
 142         if(isEventInsideTarget(event)) {
 143             super.processDropMessage(event);
 144             insideTarget = null;
 145         }
 146     }
 147 
 148     private boolean isEventInsideTarget(SunDropTargetEvent event) {
 149         Component eventSource = event.getComponent();
 150         Point screenPoint = event.getPoint();
 151         SwingUtilities.convertPointToScreen(screenPoint, eventSource);
 152         Rectangle screenBounds = new Rectangle(eventSource.getLocationOnScreen().x,
 153                 eventSource.getLocationOnScreen().y,
 154                 eventSource.getWidth(), eventSource.getHeight());
 155         return screenBounds.contains(screenPoint);


 156     }
 157 
 158     @Override
 159     protected int postDropTargetEvent(Component component, int x, int y, int dropAction,
 160                                       int actions, long[] formats, long nativeCtxt, int eventID,
 161                                       boolean dispatchType) {
 162         // On MacOS X all the DnD events should be synchronous
 163         return super.postDropTargetEvent(component, x, y, dropAction, actions, formats, nativeCtxt,
 164                 eventID, SunDropTargetContextPeer.DISPATCH_SYNC);
 165     }
 166 
 167     // Signal drop complete:
 168     protected void doDropDone(boolean success, int dropAction, boolean isLocal) {
 169         long nativeDropTarget = this.getNativeDragContext();
 170 
 171         dropDone(nativeDropTarget, fNativeDropTransfer, isLocal, success, dropAction);
 172     }
 173 
 174     // Notify transfer complete - this is an upcall from getNativeData's native calls:
 175     private void newData(long format, byte[] data) {