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 String childName = "xdnd-child"; 86 87 Runtime runtime = Runtime.getRuntime(); 88 Process proc = runtime.exec("/bin/chmod u+x "+childName); 89 proc.waitFor(); 90 91 // Only a way to receive notification when the child is ready. 92 selection.setContents(new StringSelection("Sun"), this); 93 94 //Run the native process and wait for it to get over 95 process = Runtime.getRuntime().exec("./"+childName); 96 } 97 98 /** 99 * This will be notified when the Native window comes up. Test starts here. 100 */ 101 public void lostOwnership(Clipboard clipboard, Transferable contents) { 102 this.framesVisible = true; 103 } 104 105 public void destroyProcess() { 106 if (process != null) 107 process.destroy(); 108 } 109 }