src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 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


  31 import java.awt.datatransfer.DataFlavor;
  32 import java.awt.datatransfer.Transferable;
  33 import java.awt.datatransfer.UnsupportedFlavorException;
  34 
  35 import java.awt.dnd.DnDConstants;
  36 
  37 import java.awt.dnd.DropTarget;
  38 import java.awt.dnd.DropTargetContext;
  39 import java.awt.dnd.DropTargetListener;
  40 import java.awt.dnd.DropTargetEvent;
  41 import java.awt.dnd.DropTargetDragEvent;
  42 import java.awt.dnd.DropTargetDropEvent;
  43 import java.awt.dnd.InvalidDnDOperationException;
  44 
  45 import java.awt.dnd.peer.DropTargetContextPeer;
  46 
  47 import java.util.HashSet;
  48 import java.util.Map;
  49 import java.util.Arrays;
  50 


  51 import sun.util.logging.PlatformLogger;
  52 
  53 import java.io.IOException;
  54 import java.io.InputStream;
  55 
  56 import sun.awt.AppContext;
  57 import sun.awt.AWTPermissions;
  58 import sun.awt.SunToolkit;
  59 import sun.awt.datatransfer.DataTransferer;
  60 import sun.awt.datatransfer.ToolkitThreadBlockedHandler;
  61 
  62 /**
  63  * <p>
  64  * The SunDropTargetContextPeer class is the generic class responsible for handling
  65  * the interaction between a windowing systems DnD system and Java.
  66  * </p>
  67  *
  68  * @since 1.3.1
  69  *
  70  */


 296                                    final int x, final int y,
 297                                    final int dropAction,
 298                                    final int actions, final long[] formats,
 299                                    final long nativeCtxt) {
 300         return postDropTargetEvent(component, x, y, dropAction, actions,
 301                                    formats, nativeCtxt,
 302                                    SunDropTargetEvent.MOUSE_ENTERED,
 303                                    SunDropTargetContextPeer.DISPATCH_SYNC);
 304     }
 305 
 306     /**
 307      * actual processing on EventQueue Thread
 308      */
 309 
 310     protected void processEnterMessage(SunDropTargetEvent event) {
 311         Component  c    = (Component)event.getSource();
 312         DropTarget dt   = c.getDropTarget();
 313         Point      hots = event.getPoint();
 314 
 315         local = getJVMLocalSourceTransferable();
 316 

 317         if (currentDTC != null) { // some wreckage from last time
 318             currentDTC.removeNotify();
 319             currentDTC = null;
 320         }
 321 
 322         if (c.isShowing() && dt != null && dt.isActive()) {
 323             currentDT  = dt;
 324             currentDTC = currentDT.getDropTargetContext();
 325 
 326             currentDTC.addNotify(this);
 327 
 328             currentA   = dt.getDefaultActions();
 329 
 330             try {
 331                 ((DropTargetListener)dt).dragEnter(new DropTargetDragEvent(currentDTC,
 332                                                                            hots,
 333                                                                            currentDA,
 334                                                                            currentSA));
 335             } catch (Exception e) {
 336                 e.printStackTrace();
 337                 currentDA = DnDConstants.ACTION_NONE;
 338             }
 339         } else {
 340             currentDT  = null;
 341             currentDTC = null;
 342             currentDA   = DnDConstants.ACTION_NONE;
 343             currentSA   = DnDConstants.ACTION_NONE;
 344             currentA   = DnDConstants.ACTION_NONE;
 345         }
 346 


 353     private void handleExitMessage(final Component component,
 354                                    final long nativeCtxt) {
 355         /*
 356          * Even though the return value is irrelevant for this event, it is
 357          * dispatched synchronously to fix 4393148 properly.
 358          */
 359         postDropTargetEvent(component, 0, 0, DnDConstants.ACTION_NONE,
 360                             DnDConstants.ACTION_NONE, null, nativeCtxt,
 361                             SunDropTargetEvent.MOUSE_EXITED,
 362                             SunDropTargetContextPeer.DISPATCH_SYNC);
 363     }
 364 
 365     /**
 366      *
 367      */
 368 
 369     protected void processExitMessage(SunDropTargetEvent event) {
 370         Component         c   = (Component)event.getSource();
 371         DropTarget        dt  = c.getDropTarget();
 372         DropTargetContext dtc = null;


 373 
 374         if (dt == null) {
 375             currentDT = null;
 376             currentT  = null;
 377 
 378             if (currentDTC != null) {
 379                 currentDTC.removeNotify();
 380             }
 381 
 382             currentDTC = null;
 383 
 384             return;
 385         }
 386 
 387         if (dt != currentDT) {
 388 
 389             if (currentDTC != null) {
 390                 currentDTC.removeNotify();
 391             }
 392 
 393             currentDT  = dt;
 394             currentDTC = dt.getDropTargetContext();
 395 
 396             currentDTC.addNotify(this);
 397         }
 398 
 399         dtc = currentDTC;
 400 
 401         if (dt.isActive()) try {
 402             ((DropTargetListener)dt).dragExit(new DropTargetEvent(dtc));
 403         } catch (Exception e) {
 404             e.printStackTrace();
 405         } finally {
 406             currentA  = DnDConstants.ACTION_NONE;
 407             currentSA = DnDConstants.ACTION_NONE;
 408             currentDA = DnDConstants.ACTION_NONE;
 409             currentDT = null;
 410             currentT  = null;
 411 
 412             currentDTC.removeNotify();
 413             currentDTC = null;
 414 
 415             local = null;
 416 
 417             dragRejected = false;
 418         }
 419     }
 420 
 421     private int handleMotionMessage(final Component component,
 422                                     final int x, final int y,
 423                                     final int dropAction,
 424                                     final int actions, final long[] formats,
 425                                     final long nativeCtxt) {
 426         return postDropTargetEvent(component, x, y, dropAction, actions,
 427                                    formats, nativeCtxt,
 428                                    SunDropTargetEvent.MOUSE_DRAGGED,
 429                                    SunDropTargetContextPeer.DISPATCH_SYNC);
 430     }
 431 
 432     /**
 433      *
 434      */
 435 
 436     protected void processMotionMessage(SunDropTargetEvent event,
 437                                       boolean operationChanged) {
 438         Component         c    = (Component)event.getSource();
 439         Point             hots = event.getPoint();
 440         int               id   = event.getID();
 441         DropTarget        dt   = c.getDropTarget();
 442         DropTargetContext dtc  = null;


 443 
 444         if (c.isShowing() && (dt != null) && dt.isActive()) {
 445             if (currentDT != dt) {
 446                 if (currentDTC != null) {
 447                     currentDTC.removeNotify();
 448                 }
 449 
 450                 currentDT  = dt;
 451                 currentDTC = null;
 452             }
 453 
 454             dtc = currentDT.getDropTargetContext();
 455             if (dtc != currentDTC) {
 456                 if (currentDTC != null) {
 457                     currentDTC.removeNotify();
 458                 }
 459 
 460                 currentDTC = dtc;
 461                 currentDTC.addNotify(this);
 462             }
 463 
 464             currentA = currentDT.getDefaultActions();
 465 
 466             try {
 467                 DropTargetDragEvent dtde = new DropTargetDragEvent(dtc,
 468                                                                    hots,
 469                                                                    currentDA,
 470                                                                    currentSA);
 471                 DropTargetListener dtl = (DropTargetListener)dt;
 472                 if (operationChanged) {
 473                     dtl.dropActionChanged(dtde);
 474                 } else {
 475                     dtl.dragOver(dtde);
 476                 }
 477 
 478                 if (dragRejected) {
 479                     currentDA = DnDConstants.ACTION_NONE;
 480                 }
 481             } catch (Exception e) {


 501                             SunDropTargetEvent.MOUSE_DROPPED,
 502                             !SunDropTargetContextPeer.DISPATCH_SYNC);
 503     }
 504 
 505     /**
 506      *
 507      */
 508 
 509     protected void processDropMessage(SunDropTargetEvent event) {
 510         Component  c    = (Component)event.getSource();
 511         Point      hots = event.getPoint();
 512         DropTarget dt   = c.getDropTarget();
 513 
 514         dropStatus   = STATUS_WAIT; // drop pending ACK
 515         dropComplete = false;
 516 
 517         if (c.isShowing() && dt != null && dt.isActive()) {
 518             DropTargetContext dtc = dt.getDropTargetContext();
 519 
 520             currentDT = dt;


 521 
 522             if (currentDTC != null) {
 523                 currentDTC.removeNotify();
 524             }
 525 
 526             currentDTC = dtc;
 527             currentDTC.addNotify(this);
 528             currentA = dt.getDefaultActions();
 529 
 530             synchronized(_globalLock) {
 531                 if ((local = getJVMLocalSourceTransferable()) != null)
 532                     setCurrentJVMLocalSourceTransferable(null);
 533             }
 534 
 535             dropInProcess = true;
 536 
 537             try {
 538                 ((DropTargetListener)dt).drop(new DropTargetDropEvent(dtc,
 539                                                                       hots,
 540                                                                       currentDA,
 541                                                                       currentSA,
 542                                                                       local != null));
 543             } finally {
 544                 if (dropStatus == STATUS_WAIT) {
 545                     rejectDrop();
 546                 } else if (dropComplete == false) {
 547                     dropComplete(false);


 670 
 671         for (int i = 0; i < operations.length; i++) {
 672             if ((operation & operations[i]) == operations[i]) {
 673                     ret = operations[i];
 674                     break;
 675             }
 676         }
 677 
 678         return ret;
 679     }
 680 
 681     /**
 682      * signal drop complete
 683      */
 684 
 685     public synchronized void dropComplete(boolean success) {
 686         if (dropStatus == STATUS_NONE) {
 687             throw new InvalidDnDOperationException("No Drop pending");
 688         }
 689 
 690         if (currentDTC != null) currentDTC.removeNotify();


 691 
 692         currentDT  = null;
 693         currentDTC = null;
 694         currentT   = null;
 695         currentA   = DnDConstants.ACTION_NONE;
 696 
 697         synchronized(_globalLock) {
 698             currentJVMLocalSourceTransferable = null;
 699         }
 700 
 701         dropStatus   = STATUS_NONE;
 702         dropComplete = true;
 703 
 704         try {
 705             doDropDone(success, currentDA, local != null);
 706         } finally {
 707             currentDA = DnDConstants.ACTION_NONE;
 708             // The native context is invalid after the drop is done.
 709             // Clear the reference to prohibit access.
 710             nativeDragContext = 0;


   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


  31 import java.awt.datatransfer.DataFlavor;
  32 import java.awt.datatransfer.Transferable;
  33 import java.awt.datatransfer.UnsupportedFlavorException;
  34 
  35 import java.awt.dnd.DnDConstants;
  36 
  37 import java.awt.dnd.DropTarget;
  38 import java.awt.dnd.DropTargetContext;
  39 import java.awt.dnd.DropTargetListener;
  40 import java.awt.dnd.DropTargetEvent;
  41 import java.awt.dnd.DropTargetDragEvent;
  42 import java.awt.dnd.DropTargetDropEvent;
  43 import java.awt.dnd.InvalidDnDOperationException;
  44 
  45 import java.awt.dnd.peer.DropTargetContextPeer;
  46 
  47 import java.util.HashSet;
  48 import java.util.Map;
  49 import java.util.Arrays;
  50 
  51 import sun.awt.AWTAccessor;
  52 import sun.awt.AWTAccessor.DropTargetContextAccessor;
  53 import sun.util.logging.PlatformLogger;
  54 
  55 import java.io.IOException;
  56 import java.io.InputStream;
  57 
  58 import sun.awt.AppContext;
  59 import sun.awt.AWTPermissions;
  60 import sun.awt.SunToolkit;
  61 import sun.awt.datatransfer.DataTransferer;
  62 import sun.awt.datatransfer.ToolkitThreadBlockedHandler;
  63 
  64 /**
  65  * <p>
  66  * The SunDropTargetContextPeer class is the generic class responsible for handling
  67  * the interaction between a windowing systems DnD system and Java.
  68  * </p>
  69  *
  70  * @since 1.3.1
  71  *
  72  */


 298                                    final int x, final int y,
 299                                    final int dropAction,
 300                                    final int actions, final long[] formats,
 301                                    final long nativeCtxt) {
 302         return postDropTargetEvent(component, x, y, dropAction, actions,
 303                                    formats, nativeCtxt,
 304                                    SunDropTargetEvent.MOUSE_ENTERED,
 305                                    SunDropTargetContextPeer.DISPATCH_SYNC);
 306     }
 307 
 308     /**
 309      * actual processing on EventQueue Thread
 310      */
 311 
 312     protected void processEnterMessage(SunDropTargetEvent event) {
 313         Component  c    = (Component)event.getSource();
 314         DropTarget dt   = c.getDropTarget();
 315         Point      hots = event.getPoint();
 316 
 317         local = getJVMLocalSourceTransferable();
 318         DropTargetContextAccessor acc =
 319                 AWTAccessor.getDropTargetContextAccessor();
 320         if (currentDTC != null) { // some wreckage from last time
 321             acc.reset(currentDTC);
 322             currentDTC = null;
 323         }
 324 
 325         if (c.isShowing() && dt != null && dt.isActive()) {
 326             currentDT  = dt;
 327             currentDTC = currentDT.getDropTargetContext();
 328 
 329             acc.setDropTargetContextPeer(currentDTC, this);
 330 
 331             currentA   = dt.getDefaultActions();
 332 
 333             try {
 334                 ((DropTargetListener)dt).dragEnter(new DropTargetDragEvent(currentDTC,
 335                                                                            hots,
 336                                                                            currentDA,
 337                                                                            currentSA));
 338             } catch (Exception e) {
 339                 e.printStackTrace();
 340                 currentDA = DnDConstants.ACTION_NONE;
 341             }
 342         } else {
 343             currentDT  = null;
 344             currentDTC = null;
 345             currentDA   = DnDConstants.ACTION_NONE;
 346             currentSA   = DnDConstants.ACTION_NONE;
 347             currentA   = DnDConstants.ACTION_NONE;
 348         }
 349 


 356     private void handleExitMessage(final Component component,
 357                                    final long nativeCtxt) {
 358         /*
 359          * Even though the return value is irrelevant for this event, it is
 360          * dispatched synchronously to fix 4393148 properly.
 361          */
 362         postDropTargetEvent(component, 0, 0, DnDConstants.ACTION_NONE,
 363                             DnDConstants.ACTION_NONE, null, nativeCtxt,
 364                             SunDropTargetEvent.MOUSE_EXITED,
 365                             SunDropTargetContextPeer.DISPATCH_SYNC);
 366     }
 367 
 368     /**
 369      *
 370      */
 371 
 372     protected void processExitMessage(SunDropTargetEvent event) {
 373         Component         c   = (Component)event.getSource();
 374         DropTarget        dt  = c.getDropTarget();
 375         DropTargetContext dtc = null;
 376         DropTargetContextAccessor acc =
 377                 AWTAccessor.getDropTargetContextAccessor();
 378 
 379         if (dt == null) {
 380             currentDT = null;
 381             currentT  = null;
 382 
 383             if (currentDTC != null) {
 384                 acc.reset(currentDTC);
 385             }
 386 
 387             currentDTC = null;
 388 
 389             return;
 390         }
 391 
 392         if (dt != currentDT) {
 393 
 394             if (currentDTC != null) {
 395                 acc.reset(currentDTC);
 396             }
 397 
 398             currentDT  = dt;
 399             currentDTC = dt.getDropTargetContext();
 400 
 401             acc.setDropTargetContextPeer(currentDTC, this);
 402         }
 403 
 404         dtc = currentDTC;
 405 
 406         if (dt.isActive()) try {
 407             ((DropTargetListener)dt).dragExit(new DropTargetEvent(dtc));
 408         } catch (Exception e) {
 409             e.printStackTrace();
 410         } finally {
 411             currentA  = DnDConstants.ACTION_NONE;
 412             currentSA = DnDConstants.ACTION_NONE;
 413             currentDA = DnDConstants.ACTION_NONE;
 414             currentDT = null;
 415             currentT  = null;
 416 
 417             acc.reset(currentDTC);
 418             currentDTC = null;
 419 
 420             local = null;
 421 
 422             dragRejected = false;
 423         }
 424     }
 425 
 426     private int handleMotionMessage(final Component component,
 427                                     final int x, final int y,
 428                                     final int dropAction,
 429                                     final int actions, final long[] formats,
 430                                     final long nativeCtxt) {
 431         return postDropTargetEvent(component, x, y, dropAction, actions,
 432                                    formats, nativeCtxt,
 433                                    SunDropTargetEvent.MOUSE_DRAGGED,
 434                                    SunDropTargetContextPeer.DISPATCH_SYNC);
 435     }
 436 
 437     /**
 438      *
 439      */
 440 
 441     protected void processMotionMessage(SunDropTargetEvent event,
 442                                       boolean operationChanged) {
 443         Component         c    = (Component)event.getSource();
 444         Point             hots = event.getPoint();
 445         int               id   = event.getID();
 446         DropTarget        dt   = c.getDropTarget();
 447         DropTargetContext dtc  = null;
 448         DropTargetContextAccessor acc =
 449                 AWTAccessor.getDropTargetContextAccessor();
 450 
 451         if (c.isShowing() && (dt != null) && dt.isActive()) {
 452             if (currentDT != dt) {
 453                 if (currentDTC != null) {
 454                     acc.reset(currentDTC);
 455                 }
 456 
 457                 currentDT  = dt;
 458                 currentDTC = null;
 459             }
 460 
 461             dtc = currentDT.getDropTargetContext();
 462             if (dtc != currentDTC) {
 463                 if (currentDTC != null) {
 464                     acc.reset(currentDTC);
 465                 }
 466 
 467                 currentDTC = dtc;
 468                 acc.setDropTargetContextPeer(currentDTC, this);
 469             }
 470 
 471             currentA = currentDT.getDefaultActions();
 472 
 473             try {
 474                 DropTargetDragEvent dtde = new DropTargetDragEvent(dtc,
 475                                                                    hots,
 476                                                                    currentDA,
 477                                                                    currentSA);
 478                 DropTargetListener dtl = (DropTargetListener)dt;
 479                 if (operationChanged) {
 480                     dtl.dropActionChanged(dtde);
 481                 } else {
 482                     dtl.dragOver(dtde);
 483                 }
 484 
 485                 if (dragRejected) {
 486                     currentDA = DnDConstants.ACTION_NONE;
 487                 }
 488             } catch (Exception e) {


 508                             SunDropTargetEvent.MOUSE_DROPPED,
 509                             !SunDropTargetContextPeer.DISPATCH_SYNC);
 510     }
 511 
 512     /**
 513      *
 514      */
 515 
 516     protected void processDropMessage(SunDropTargetEvent event) {
 517         Component  c    = (Component)event.getSource();
 518         Point      hots = event.getPoint();
 519         DropTarget dt   = c.getDropTarget();
 520 
 521         dropStatus   = STATUS_WAIT; // drop pending ACK
 522         dropComplete = false;
 523 
 524         if (c.isShowing() && dt != null && dt.isActive()) {
 525             DropTargetContext dtc = dt.getDropTargetContext();
 526 
 527             currentDT = dt;
 528             DropTargetContextAccessor acc =
 529                     AWTAccessor.getDropTargetContextAccessor();
 530 
 531             if (currentDTC != null) {
 532                 acc.reset(currentDTC);
 533             }
 534 
 535             currentDTC = dtc;
 536             acc.setDropTargetContextPeer(currentDTC, this);
 537             currentA = dt.getDefaultActions();
 538 
 539             synchronized(_globalLock) {
 540                 if ((local = getJVMLocalSourceTransferable()) != null)
 541                     setCurrentJVMLocalSourceTransferable(null);
 542             }
 543 
 544             dropInProcess = true;
 545 
 546             try {
 547                 ((DropTargetListener)dt).drop(new DropTargetDropEvent(dtc,
 548                                                                       hots,
 549                                                                       currentDA,
 550                                                                       currentSA,
 551                                                                       local != null));
 552             } finally {
 553                 if (dropStatus == STATUS_WAIT) {
 554                     rejectDrop();
 555                 } else if (dropComplete == false) {
 556                     dropComplete(false);


 679 
 680         for (int i = 0; i < operations.length; i++) {
 681             if ((operation & operations[i]) == operations[i]) {
 682                     ret = operations[i];
 683                     break;
 684             }
 685         }
 686 
 687         return ret;
 688     }
 689 
 690     /**
 691      * signal drop complete
 692      */
 693 
 694     public synchronized void dropComplete(boolean success) {
 695         if (dropStatus == STATUS_NONE) {
 696             throw new InvalidDnDOperationException("No Drop pending");
 697         }
 698 
 699         if (currentDTC != null) {
 700             AWTAccessor.getDropTargetContextAccessor().reset(currentDTC);
 701         }
 702 
 703         currentDT  = null;
 704         currentDTC = null;
 705         currentT   = null;
 706         currentA   = DnDConstants.ACTION_NONE;
 707 
 708         synchronized(_globalLock) {
 709             currentJVMLocalSourceTransferable = null;
 710         }
 711 
 712         dropStatus   = STATUS_NONE;
 713         dropComplete = true;
 714 
 715         try {
 716             doDropDone(success, currentDA, local != null);
 717         } finally {
 718             currentDA = DnDConstants.ACTION_NONE;
 719             // The native context is invalid after the drop is done.
 720             // Clear the reference to prohibit access.
 721             nativeDragContext = 0;