1 /* 2 * Copyright (c) 2009, 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 /* 25 * Panel is a DropTarget 26 * 27 */ 28 29 import java.awt.*; 30 import java.awt.datatransfer.*; 31 import java.awt.dnd.*; 32 import java.io.*; 33 34 35 class DnDTarget extends Panel implements DropTargetListener { 36 private int dragOperation = DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE; 37 Color bgColor; 38 Color htColor; 39 40 DnDTarget(Color bgColor, Color htColor) { 41 super(); 42 this.bgColor = bgColor; 43 this.htColor = htColor; 44 setBackground(bgColor); 45 setDropTarget(new DropTarget(this, this)); 46 } 47 48 49 public void dragEnter(DropTargetDragEvent e) { 50 System.out.println("[Target] dragEnter"); 51 setBackground(htColor); 52 repaint(); 53 } 54 55 public void dragOver(DropTargetDragEvent e) { 56 System.out.println("[Target] dragOver"); 57 } 58 59 public void dragExit(DropTargetEvent e) { 60 System.out.println("[Target] dragExit"); 61 setBackground(bgColor); 62 repaint(); 63 } 64 65 public void dragScroll(DropTargetDragEvent e) { 66 System.out.println("[Target] dragScroll"); 67 } 68 69 public void dropActionChanged(DropTargetDragEvent e) { 70 System.out.println("[Target] dropActionChanged"); 71 } 72 73 public void drop(DropTargetDropEvent dtde) { 74 System.out.println("[Target] drop"); 75 boolean success = false; 76 if ((dtde.getDropAction() & dragOperation) == 0) { 77 dtde.rejectDrop(); 78 Label label = new Label("[no links here :) ]"); 79 label.setBackground(Color.cyan); 80 add(label); 81 } else { 82 dtde.acceptDrop(dragOperation); 83 DataFlavor[] dfs = dtde.getCurrentDataFlavors(); 84 if (dfs != null && dfs.length >= 1){ 85 Transferable transfer = dtde.getTransferable(); 86 try { 87 Button button = (Button)transfer.getTransferData(dfs[0]); 88 if( button != null ){ 89 add(button); 90 success = true; 91 } 92 } catch (IOException ioe) { 93 System.out.println(ioe.getMessage()); 94 return; 95 } catch (UnsupportedFlavorException ufe) { 96 System.out.println(ufe.getMessage()); 97 return; 98 } catch (Exception e) { 99 System.out.println(e.getMessage()); 100 return; 101 } 102 } 103 } 104 setBackground(bgColor); 105 dtde.dropComplete(success); 106 107 invalidate(); 108 validate(); 109 repaint(); 110 } 111 }