2129N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
2129N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2129N/A *
2129N/A * This code is free software; you can redistribute it and/or modify it
2129N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
2129N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
2129N/A *
2129N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2129N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2129N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2129N/A * version 2 for more details (a copy is included in the LICENSE file that
2129N/A * accompanied this code).
2129N/A *
2129N/A * You should have received a copy of the GNU General Public License version
2129N/A * 2 along with this work; if not, write to the Free Software Foundation,
2129N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2129N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2129N/A */
2129N/A
2129N/A/*
2129N/A* Panel is a DropTarget
2129N/A*
2129N/A*/
2129N/A
2129N/Aimport java.awt.*;
2129N/Aimport java.awt.datatransfer.*;
2129N/Aimport java.awt.dnd.*;
2129N/Aimport java.io.*;
2129N/A
2129N/A
2129N/Aclass DnDTarget extends Panel implements DropTargetListener {
2129N/A //private int dragOperation = DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE;
2129N/A Color bgColor;
2129N/A Color htColor;
2129N/A
2129N/A DnDTarget(Color bgColor, Color htColor) {
2129N/A super();
2129N/A setLayout(new FlowLayout());
2129N/A this.bgColor = bgColor;
2129N/A this.htColor = htColor;
2129N/A setBackground(bgColor);
2129N/A setDropTarget(new DropTarget(this, this));
2129N/A add(new Label("drop here"));
2129N/A }
2129N/A
2129N/A boolean check(DropTargetDragEvent dtde)
2129N/A {
2129N/A if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) {
2129N/A dtde.acceptDrag(DnDConstants.ACTION_COPY);
2129N/A return true;
2129N/A }
2129N/A return false;
2129N/A }
2129N/A
2129N/A public void dragEnter(DropTargetDragEvent dtde) {
2129N/A if(check(dtde)){
2129N/A setBackground(htColor);
2129N/A repaint();
2129N/A }
2129N/A }
2129N/A
2129N/A public void dragOver(DropTargetDragEvent dtde) {
2129N/A check(dtde);
2129N/A }
2129N/A
2129N/A public void dropActionChanged(DropTargetDragEvent dtde) {
2129N/A check(dtde);
2129N/A }
2129N/A
2129N/A public void dragExit(DropTargetEvent e) {
2129N/A setBackground(bgColor);
2129N/A repaint();
2129N/A }
2129N/A
2129N/A public void dragScroll(DropTargetDragEvent e) {
2129N/A System.out.println("[Target] dragScroll");
2129N/A }
2129N/A
2129N/A public void drop(DropTargetDropEvent dtde) {
2129N/A System.out.println("[Target] drop");
2129N/A boolean success = false;
2129N/A dtde.acceptDrop(DnDConstants.ACTION_COPY);
2129N/A if( dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor) ){
2129N/A System.out.println("[Target] DROP OK!");
2129N/A try {
2129N/A Transferable transfer = dtde.getTransferable();
2129N/A java.util.List<File> fl = (java.util.List<File>)transfer.getTransferData(DataFlavor.javaFileListFlavor);
2129N/A for(File f : fl){
2129N/A add(new Button(f.getCanonicalPath()));
2129N/A System.out.println("[Target] drop file:" + f.getCanonicalPath());
2129N/A }
2129N/A validate();
2129N/A } catch(Exception ex) {
2129N/A ex.printStackTrace();
2129N/A }
2129N/A setBackground(bgColor);
2129N/A repaint();
2129N/A success = true;
2129N/A }
2129N/A dtde.dropComplete(success);
2129N/A }
2129N/A}