1 package wjhk.jupload2.gui;
2
3
4
5
6
7 import java.awt.datatransfer.DataFlavor;
8 import java.awt.datatransfer.Transferable;
9 import java.awt.datatransfer.UnsupportedFlavorException;
10 import java.io.BufferedReader;
11 import java.io.File;
12 import java.io.IOException;
13 import java.io.Reader;
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import javax.swing.JComponent;
20 import javax.swing.TransferHandler;
21
22 import wjhk.jupload2.policies.UploadPolicy;
23
24 class JUploadTransferHandler extends TransferHandler {
25
26
27 private static final long serialVersionUID = -1241261479500810699L;
28
29 DataFlavor fileListFlavor = DataFlavor.javaFileListFlavor;
30
31
32 DataFlavor uriListFlavor;
33
34
35
36
37 JUploadPanel uploadPanel = null;
38
39
40
41
42 UploadPolicy uploadPolicy = null;
43
44
45
46
47
48
49
50
51 public JUploadTransferHandler(UploadPolicy uploadPolicy, JUploadPanel uploadPanel) {
52 this.uploadPolicy = uploadPolicy;
53 this.uploadPanel = uploadPanel;
54 try {
55 this.uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
56 } catch (ClassNotFoundException ex) {
57 this.uriListFlavor = DataFlavor.javaFileListFlavor;
58 }
59 }
60
61
62
63
64 @Override
65 @SuppressWarnings("unchecked")
66 public boolean importData(JComponent c, Transferable t) {
67 DataFlavor[] flavors = t.getTransferDataFlavors();
68 boolean importAccepted = false;
69 if (canImport(c, flavors)) {
70 try {
71 List<File> fileList = new ArrayList<File>();
72 if (isFile(flavors)) {
73 fileList = (List<File>) t.getTransferData(this.fileListFlavor);
74 importAccepted = true;
75 } else if (isUrl(flavors)) {
76 Reader in = uriListFlavor.getReaderForText(t);
77 BufferedReader br = new BufferedReader(in);
78 String uriStr;
79 while ((uriStr = br.readLine()) != null) {
80 try {
81 fileList.add(new File(new URI(uriStr)));
82 } catch (URISyntaxException use) {
83 this.uploadPolicy.displayErr(this.getClass().getName() + ".importData()", use);
84 }
85 }
86 importAccepted = true;
87 }
88 File[] fileArray = fileList.toArray(new File[fileList.size()]);
89 this.uploadPanel.getFilePanel().addFiles(fileArray);
90 return importAccepted;
91 } catch (UnsupportedFlavorException ufe) {
92 this.uploadPolicy.displayErr(this.getClass().getName() + ".importData()", ufe);
93 } catch (IOException ioe) {
94 this.uploadPolicy.displayErr(this.getClass().getName() + ".importData()", ioe);
95 }
96 }
97
98 return importAccepted;
99 }
100
101
102
103
104 @Override
105 public int getSourceActions(JComponent c) {
106 return MOVE;
107 }
108
109
110
111
112 @Override
113 public boolean canImport(JComponent c, DataFlavor[] flavors) {
114 return isFile(flavors) || isUrl(flavors);
115 }
116
117
118
119
120
121
122
123 protected boolean isFile(DataFlavor[] flavors) {
124 for (DataFlavor flavor : flavors) {
125 if (this.fileListFlavor.equals(flavor)) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132
133
134
135
136
137
138 protected boolean isUrl(DataFlavor[] flavors) {
139 for (DataFlavor flavor : flavors) {
140 if (this.uriListFlavor.equals(flavor)) {
141 return true;
142 }
143 }
144 return false;
145 }
146 }