1 /*
   2  * Copyright (c) 2004, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.*;
  25 import java.awt.datatransfer.*;
  26 import java.awt.dnd.*;
  27 
  28 /*
  29  * @author Girish R (girish.ramachandran@sun.com)
  30  */
  31 
  32 public class GUIXDnDJava extends Frame implements ClipboardOwner {
  33 
  34     Process process;
  35     volatile boolean framesVisible;
  36     final Clipboard selection = Toolkit.getDefaultToolkit().getSystemSelection();
  37 
  38     Object dropData = new String("");
  39     volatile boolean dragDropEnd, dropSuccess;
  40     int dropAction;
  41     DragSource ds;
  42     DragSourceListener dsl;
  43     DragGestureListener dgl;
  44     DropTargetListener dtl;
  45     static String data = "DragDropEnd TEST";
  46 
  47 
  48     public GUIXDnDJava() throws Exception {
  49         this.setSize(500, 300);
  50         this.setLocation(new Point(6, 200));
  51         this.setVisible(true);
  52         ds = DragSource.getDefaultDragSource();
  53         dsl = new DragSourceAdapter() {
  54             public void dragDropEnd(DragSourceDropEvent dsde) {
  55                 dragDropEnd = true;
  56                 dropSuccess = dsde.getDropSuccess();
  57                 dropAction = dsde.getDropAction();
  58 
  59                 System.out.println("getDropSuccess: " + dsde.getDropSuccess());
  60                 System.out.println("getDropAction: " + dsde.getDropAction());
  61             }
  62         };
  63 
  64         dgl = dge -> {
  65             System.out.println("Drag gesture");
  66             dge.startDrag(null, new StringSelection(data), dsl);
  67         };
  68 
  69         ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, dgl);
  70         dtl = new DropTargetAdapter() {
  71             public void drop(DropTargetDropEvent dtde) {
  72                 System.out.println("Drop occured");
  73                 dtde.acceptDrop(DnDConstants.ACTION_COPY);
  74                 Transferable t = dtde.getTransferable();
  75                 try {
  76                     dropData = t.getTransferData(DataFlavor.stringFlavor);
  77                 } catch (Exception e) {
  78                     throw new RuntimeException(e);
  79                 }
  80                 dtde.dropComplete(true);
  81             }
  82         };
  83         new DropTarget(this, dtl);
  84 
  85         // Only a way to receive notification when the child is ready.
  86         selection.setContents(new StringSelection("Sun"), this);
  87     }
  88 
  89     public void startChild() throws Exception {
  90         String childName = "xdnd-child";
  91 
  92         Runtime runtime = Runtime.getRuntime();
  93         Process proc = runtime.exec("/bin/chmod u+x "+childName);
  94         proc.waitFor();
  95 
  96         //Run the native process and wait for it to get over
  97         process = Runtime.getRuntime().exec("./"+childName);
  98     }
  99 
 100     /**
 101      * This will be notified when the Native window comes up. Test starts here.
 102      */
 103     public void lostOwnership(Clipboard clipboard, Transferable contents) {
 104         this.framesVisible = true;
 105     }
 106 
 107     public void destroyProcess() {
 108         if (process != null)
 109             process.destroy();
 110     }
 111 }