1/*
2 * Copyright (c) 2009, 2013, 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
29import java.awt.*;
30import java.awt.datatransfer.*;
31import java.awt.dnd.*;
32import java.io.*;
33
34
35class 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        setLayout(new FlowLayout());
43        this.bgColor = bgColor;
44        this.htColor = htColor;
45        setBackground(bgColor);
46        setDropTarget(new DropTarget(this, this));
47        add(new Label("drop here"));
48    }
49
50    boolean check(DropTargetDragEvent dtde)
51    {
52        if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) {
53            dtde.acceptDrag(DnDConstants.ACTION_COPY);
54            return true;
55        }
56        return false;
57    }
58
59    public void dragEnter(DropTargetDragEvent dtde) {
60        if(check(dtde)){
61            setBackground(htColor);
62            repaint();
63        }
64    }
65
66    public void dragOver(DropTargetDragEvent dtde) {
67        check(dtde);
68    }
69
70    public void dropActionChanged(DropTargetDragEvent dtde) {
71        check(dtde);
72    }
73
74    public void dragExit(DropTargetEvent e) {
75        setBackground(bgColor);
76        repaint();
77    }
78
79    public void dragScroll(DropTargetDragEvent e) {
80        System.out.println("[Target] dragScroll");
81    }
82
83    public void drop(DropTargetDropEvent dtde) {
84        System.out.println("[Target] drop");
85        boolean success = false;
86        dtde.acceptDrop(DnDConstants.ACTION_COPY);
87        if( dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor) ){
88            System.out.println("[Target] DROP OK!");
89            try {
90                Transferable transfer = dtde.getTransferable();
91                java.util.List<File> fl = (java.util.List<File>)transfer.getTransferData(DataFlavor.javaFileListFlavor);
92                for(File f : fl){
93                    add(new Button(f.getCanonicalPath()));
94                    System.out.println("[Target] drop file:" + f.getCanonicalPath());
95                }
96                validate();
97            } catch(Exception ex) {
98                ex.printStackTrace();
99            }
100            setBackground(bgColor);
101            repaint();
102            success = true;
103        }
104        dtde.dropComplete(success);
105    }
106}
107