|
1 /* |
|
2 * Copyright (c) 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 import java.awt.*; |
|
25 import java.awt.datatransfer.DataFlavor; |
|
26 import java.awt.datatransfer.UnsupportedFlavorException; |
|
27 import java.awt.dnd.*; |
|
28 import java.awt.event.WindowAdapter; |
|
29 import java.awt.event.WindowEvent; |
|
30 import java.io.File; |
|
31 import java.io.IOException; |
|
32 |
|
33 class TargetFileListFrame extends Frame implements DropTargetListener { |
|
34 |
|
35 private List list = new List(URIListToFileListBetweenJVMsTest.VISIBLE_RAWS_IN_LIST); |
|
36 private int expectationTransferredFilesNumber; |
|
37 |
|
38 TargetFileListFrame(Point location, int expectationTransferredFilesNumber) { |
|
39 this.expectationTransferredFilesNumber = expectationTransferredFilesNumber; |
|
40 initGUI(location); |
|
41 setDropTarget(new DropTarget(list, DnDConstants.ACTION_COPY, this)); |
|
42 } |
|
43 |
|
44 private void initGUI(Point location) { |
|
45 this.setLocation(location); |
|
46 this.addWindowListener(new WindowAdapter() { |
|
47 public void windowClosing(WindowEvent e) { |
|
48 TargetFileListFrame.this.dispose(); |
|
49 } |
|
50 }); |
|
51 this.add(new Panel().add(list)); |
|
52 this.pack(); |
|
53 this.setVisible(true); |
|
54 } |
|
55 |
|
56 public void dragEnter(DropTargetDragEvent dtde) { |
|
57 if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) { |
|
58 dtde.acceptDrag(DnDConstants.ACTION_COPY); |
|
59 } |
|
60 } |
|
61 |
|
62 public void dragOver(DropTargetDragEvent dtde) { |
|
63 if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) { |
|
64 dtde.acceptDrag(DnDConstants.ACTION_COPY); |
|
65 } |
|
66 } |
|
67 |
|
68 public void dropActionChanged(DropTargetDragEvent dtde) { |
|
69 if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) { |
|
70 dtde.acceptDrag(DnDConstants.ACTION_COPY); |
|
71 } |
|
72 } |
|
73 |
|
74 public void dragExit(DropTargetEvent dte) {} |
|
75 |
|
76 public void drop(DropTargetDropEvent dtde) { |
|
77 list.removeAll(); |
|
78 dtde.acceptDrop(DnDConstants.ACTION_COPY); |
|
79 java.util.List<File> fileList = extractListOfFiles(dtde); |
|
80 for (File file:fileList) { |
|
81 list.add(file.getName()); |
|
82 } |
|
83 |
|
84 if (fileList.size() != expectationTransferredFilesNumber) |
|
85 { |
|
86 System.err.println("ERROR: Expected file number:" |
|
87 + expectationTransferredFilesNumber |
|
88 + "; Received file number: " |
|
89 + fileList.size()); |
|
90 TargetFileListFrame.this.dispose(); |
|
91 System.exit(InterprocessMessages.WRONG_FILES_NUMBER_ON_TARGET); |
|
92 } |
|
93 |
|
94 TargetFileListFrame.this.dispose(); |
|
95 |
|
96 } |
|
97 |
|
98 private java.util.List<File> extractListOfFiles(DropTargetDropEvent dtde) { |
|
99 java.util.List<File> fileList = null; |
|
100 try { |
|
101 fileList = (java.util.List<File>)dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); |
|
102 } catch (UnsupportedFlavorException | IOException e) { |
|
103 e.printStackTrace(); |
|
104 } |
|
105 return fileList; |
|
106 } |
|
107 |
|
108 Point getDropTargetPoint() { |
|
109 return new Point((int)list.getLocationOnScreen().getX()+(list.getWidth()/2), |
|
110 (int)list.getLocationOnScreen().getY()+(list.getHeight()/2)); |
|
111 } |
|
112 } |