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
24import java.awt.dnd.*;
25import java.awt.*;
26import java.awt.datatransfer.DataFlavor;
27import java.awt.datatransfer.UnsupportedFlavorException;
28import java.io.File;
29import java.io.IOException;
30import java.util.ArrayList;
31import java.util.Collection;
32import java.util.Iterator;
33import java.util.Arrays;
34
35public class TargetPanel extends Panel implements DropTargetListener{
36
37    private java.util.List <File> content = new ArrayList<File>();
38
39    //private final CustomDropTargetListener dropTargetListener = new CustomDropTargetListener();
40
41    private Frame frame;
42
43    public TargetPanel (Frame frame)
44    {
45        this.frame = frame;
46        setBackground(Color.DARK_GRAY);
47        setPreferredSize(new Dimension(200, 200));
48        setDropTarget(new DropTarget(this, this));
49    }
50
51    public void dragEnter(DropTargetDragEvent dtde) {
52        if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
53            dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
54        }
55    }
56
57    public void dragOver(DropTargetDragEvent dtde) {
58        if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
59            dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
60        }
61    }
62
63    public void dropActionChanged(DropTargetDragEvent dtde) {
64        if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
65            dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
66        }
67    }
68
69    public void dragExit(DropTargetEvent dte) {
70
71    }
72
73    public void drop(DropTargetDropEvent dtde) {
74        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
75        if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
76            try {
77                content = (java.util.List)dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
78                repaint();
79            } catch (UnsupportedFlavorException e) {
80                e.printStackTrace();
81            } catch (IOException e) {
82                e.printStackTrace();
83            }
84            dtde.dropComplete(true);
85
86
87
88            boolean listsAreEqual = true;
89
90             for (int i = 0; i < content.size(); i++) {
91                if(!FileListTransferable.files[i].getName().equals(content.get(i).getName())) {
92                    listsAreEqual = false;
93                }
94            }
95
96            if (listsAreEqual) {
97                System.err.println(InterprocessMessages.EXECUTION_IS_SUCCESSFULL);
98                System.exit(0);
99            }
100        }
101        dtde.rejectDrop();
102        System.err.println(InterprocessMessages.FILES_ON_TARGET_ARE_CORRUPTED);
103        System.exit(1);
104    }
105
106    public void paint(Graphics g) {
107        g.setColor(Color.YELLOW);
108        int i = 0;
109        for (Iterator <File> iterator = content.iterator(); iterator.hasNext();i++) {
110            g.drawString(iterator.next().getName(), 5, g.getFontMetrics().getAscent()*i+20);
111        }
112
113    }
114
115}
116