1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package wjhk.jupload2.gui;
22
23 import java.awt.datatransfer.DataFlavor;
24 import java.awt.datatransfer.UnsupportedFlavorException;
25 import java.awt.dnd.DnDConstants;
26 import java.awt.dnd.DropTargetDragEvent;
27 import java.awt.dnd.DropTargetDropEvent;
28 import java.awt.dnd.DropTargetEvent;
29 import java.awt.dnd.DropTargetListener;
30 import java.io.File;
31 import java.io.IOException;
32 import java.net.URI;
33 import java.net.URISyntaxException;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import wjhk.jupload2.policies.UploadPolicy;
38
39
40
41
42
43
44
45 public class DnDListener implements DropTargetListener {
46
47 private JUploadPanel uploadPanel;
48
49 private UploadPolicy uploadPolicy;
50
51
52
53
54
55
56
57 public DnDListener(JUploadPanel uploadPanel, UploadPolicy uploadPolicy) {
58 this.uploadPanel = uploadPanel;
59 this.uploadPolicy = uploadPolicy;
60 }
61
62
63
64 DataFlavor uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String", null);
65
66
67
68
69 public void dragEnter(DropTargetDragEvent e) {
70
71
72
73 if (!e.isDataFlavorSupported(DataFlavor.javaFileListFlavor) && !e.isDataFlavorSupported(uriListFlavor)) {
74 e.rejectDrag();
75 }
76 }
77
78
79
80
81 public void dragOver(DropTargetDragEvent e) {
82
83 }
84
85
86
87
88 public void dropActionChanged(DropTargetDragEvent e) {
89
90 }
91
92
93
94
95 @SuppressWarnings("unchecked")
96 public void drop(DropTargetDropEvent e) {
97 if (!e.isDataFlavorSupported(DataFlavor.javaFileListFlavor) && !e.isDataFlavorSupported(uriListFlavor)) {
98 e.rejectDrop();
99 } else {
100 e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
101 try {
102 File[] fileArray;
103
104 if (e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
105 List<File> fileList = (List<File>) e.getTransferable().getTransferData(
106 DataFlavor.javaFileListFlavor);
107
108 fileArray = (File[]) (fileList.toArray());
109 } else {
110
111 String strList = (String) e.getTransferable().getTransferData(uriListFlavor);
112
113 List<File> fileList = new ArrayList<File>();
114 for (String s : strList.split("\n"))
115 try {
116 fileList.add(new File(new URI(s.trim())));
117 } catch (URISyntaxException ex) {
118 ex.printStackTrace();
119 e.rejectDrop();
120 return;
121 }
122 fileArray = (File[]) (fileList.toArray(new File[fileList.size()]));
123
124 }
125 this.uploadPanel.getFilePanel().addFiles(fileArray);
126
127 e.getDropTargetContext().dropComplete(true);
128
129
130
131 this.uploadPolicy.afterFileDropped(e);
132
133 } catch (IOException ioe) {
134 this.uploadPolicy.displayErr("DnDListener.drop()", ioe);
135 e.rejectDrop();
136 } catch (UnsupportedFlavorException ufe) {
137 this.uploadPolicy.displayErr("DnDListener.drop()", ufe);
138 e.rejectDrop();
139 }
140
141 }
142 }
143
144
145
146
147 public void dragExit(DropTargetEvent e) {
148
149 }
150 }