DnDTarget.java revision 2129:fd5bf5955e37
1272343Sngie/* 2272343Sngie * Copyright 2009 Sun Microsystems, Inc. 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. Sun designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 22 * CA 95054 USA or visit www.sun.com if you need additional information or 23 * have any questions. 24 */ 25 26/* 27* Panel is a DropTarget 28* 29*/ 30 31import java.awt.*; 32import java.awt.datatransfer.*; 33import java.awt.dnd.*; 34import java.io.*; 35 36 37class DnDTarget extends Panel implements DropTargetListener { 38 //private int dragOperation = DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE; 39 Color bgColor; 40 Color htColor; 41 42 DnDTarget(Color bgColor, Color htColor) { 43 super(); 44 setLayout(new FlowLayout()); 45 this.bgColor = bgColor; 46 this.htColor = htColor; 47 setBackground(bgColor); 48 setDropTarget(new DropTarget(this, this)); 49 add(new Label("drop here")); 50 } 51 52 boolean check(DropTargetDragEvent dtde) 53 { 54 if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) { 55 dtde.acceptDrag(DnDConstants.ACTION_COPY); 56 return true; 57 } 58 return false; 59 } 60 61 public void dragEnter(DropTargetDragEvent dtde) { 62 if(check(dtde)){ 63 setBackground(htColor); 64 repaint(); 65 } 66 } 67 68 public void dragOver(DropTargetDragEvent dtde) { 69 check(dtde); 70 } 71 72 public void dropActionChanged(DropTargetDragEvent dtde) { 73 check(dtde); 74 } 75 76 public void dragExit(DropTargetEvent e) { 77 setBackground(bgColor); 78 repaint(); 79 } 80 81 public void dragScroll(DropTargetDragEvent e) { 82 System.out.println("[Target] dragScroll"); 83 } 84 85 public void drop(DropTargetDropEvent dtde) { 86 System.out.println("[Target] drop"); 87 boolean success = false; 88 dtde.acceptDrop(DnDConstants.ACTION_COPY); 89 if( dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor) ){ 90 System.out.println("[Target] DROP OK!"); 91 try { 92 Transferable transfer = dtde.getTransferable(); 93 java.util.List<File> fl = (java.util.List<File>)transfer.getTransferData(DataFlavor.javaFileListFlavor); 94 for(File f : fl){ 95 add(new Button(f.getCanonicalPath())); 96 System.out.println("[Target] drop file:" + f.getCanonicalPath()); 97 } 98 validate(); 99 } catch(Exception ex) { 100 ex.printStackTrace(); 101 } 102 setBackground(bgColor); 103 repaint(); 104 success = true; 105 } 106 dtde.dropComplete(success); 107 } 108} 109