1 /* 2 * Copyright (c) 2007, 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.DataFlavor; 26 import java.awt.datatransfer.UnsupportedFlavorException; 27 import java.awt.event.WindowAdapter; 28 import java.awt.event.WindowEvent; 29 import java.awt.dnd.*; 30 import java.io.IOException; 31 import java.security.Permission; 32 import java.security.AccessControlException; 33 34 class DragInterceptorFrame extends Frame implements DropTargetListener { 35 36 private static int exitMessage = InterprocessMessages.TEST_PASSED; 37 private static boolean dataIsAccessible = false; 38 private static boolean exceptionHasBeenThrown = false; 39 40 DragInterceptorFrame(Point location) { 41 System.setSecurityManager(new ClipboardDefender()); 42 initGUI(location); 43 setDropTarget(new DropTarget(this, DnDConstants.ACTION_COPY, 44 this)); 45 } 46 47 private void initGUI(Point location) { 48 this.setLocation(location); 49 this.addWindowListener(new WindowAdapter() { 50 public void windowClosing(WindowEvent e) { 51 DragInterceptorFrame.this.dispose(); 52 } 53 }); 54 setSize (200, 200); 55 this.setVisible(true); 56 } 57 58 public void dragEnter(DropTargetDragEvent dtde) { 59 // We want to set the exception handler on EDT 60 Thread.currentThread().setUncaughtExceptionHandler ( 61 new Thread.UncaughtExceptionHandler() { 62 public void uncaughtException(Thread t, Throwable e) { 63 exceptionHasBeenThrown = true; 64 } 65 } 66 ); 67 examineTransferable(dtde); 68 } 69 70 public void dragOver(DropTargetDragEvent dtde) { 71 examineTransferable(dtde); 72 } 73 74 public void dropActionChanged(DropTargetDragEvent dtde) { 75 examineTransferable(dtde); 76 } 77 78 public void dragExit(DropTargetEvent dte) {} 79 80 public void drop(DropTargetDropEvent dtde) { 81 82 if (dataIsAccessible && !exceptionHasBeenThrown) { 83 exitMessage = InterprocessMessages.DATA_WAS_INTERCEPTED_AND_EXCEPTION_HANDLER_WAS_NOT_TRIGGERED; 84 } else if (dataIsAccessible) { 85 exitMessage = InterprocessMessages.DATA_WAS_INTERCEPTED; 86 } else if (!exceptionHasBeenThrown) { 87 exitMessage = InterprocessMessages.EXCEPTION_HANDLER_WAS_NOT_TRIGGERED; 88 } 89 90 // This returns the diagnostic code from the child VM 91 System.exit(exitMessage); 92 } 93 94 Point getDropTargetPoint() { 95 return new Point((int)getLocationOnScreen().getX()+(getWidth()/2), 96 (int)getLocationOnScreen().getY()+(getHeight()/2)); 97 } 98 99 private void examineTransferable(DropTargetDragEvent dtde) { 100 if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.stringFlavor)) { 101 dtde.acceptDrag(DnDConstants.ACTION_COPY); 102 try{ 103 if (null != dtde.getTransferable().getTransferData(DataFlavor.stringFlavor)) { 104 dataIsAccessible = true; 105 } 106 } catch (IOException e) { 107 e.printStackTrace(); 108 exitMessage = InterprocessMessages.UNEXPECTED_IO_EXCEPTION; 109 } catch (UnsupportedFlavorException e) { 110 e.printStackTrace(); 111 exitMessage = InterprocessMessages.UNEXPECTED_UNSUPPORTED_FLAVOR_EXCEPTION; 112 } 113 } 114 } 115 116 static class ClipboardDefender extends SecurityManager { 117 public void checkPermission(Permission p) { 118 if (p instanceof java.awt.AWTPermission && 119 p.getName().equals("accessClipboard")) { 120 throw new AccessControlException("access denied "); 121 } 122 } 123 } 124 125 public static void main(String[] args) { 126 new DragInterceptorFrame(new Point(200,200)); 127 } 128 }