1 package org.feistymeow.dragdrop;
3 import java.awt.datatransfer.DataFlavor;
4 import java.awt.datatransfer.Transferable;
5 import java.awt.datatransfer.UnsupportedFlavorException;
6 import java.io.BufferedReader;
8 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Vector;
15 import java.util.StringTokenizer;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
29 @SuppressWarnings(
"serial")
40 if (initial !=
null) add(initial);
45 if (initial !=
null) addAll(initial);
52 private static DataFlavor URIListFlavor;
55 URIListFlavor =
new DataFlavor(
"text/uri-list;class=java.lang.String");
56 }
catch (ClassNotFoundException e) {
57 logger.error(
"should never happen", e);
60 private static DataFlavor AltURIListFlavor;
63 AltURIListFlavor =
new DataFlavor(
"text/uri-list;representationclass=java.lang.String");
64 }
catch (ClassNotFoundException e) {
65 logger.error(
"should never happen", e);
79 return AltURIListFlavor;
86 protected ArrayList<DataFlavor> FLAVORS =
new ArrayList<DataFlavor>(Arrays.asList(
87 DataFlavor.javaFileListFlavor, URIListFlavor, AltURIListFlavor));
96 logger.warn(
"base loadDataJustInTime. derived class should have implemented this.");
107 if (flavor ==
null)
return null;
109 logger.debug(
"size was zero, so loading data just in time");
110 boolean worked = loadDataJustInTime(flavor);
111 if (!worked || (size() == 0)) {
112 logger.warn(
"failed to retrieve data just in time for getTransferData.");
117 logger.debug(
"responding to flavor: " + flavor.toString());
118 if (flavor.equals(DataFlavor.javaFileListFlavor)) {
119 logger.debug(
"java file list flavor...");
120 List<Object> data =
new java.util.ArrayList<Object>();
123 }
else if (flavor.equals(URIListFlavor) || flavor.equals(AltURIListFlavor)) {
124 logger.debug(
"uri list flavor...");
125 StringBuilder data =
new StringBuilder();
126 Iterator<Object> iter = iterator();
127 while (iter.hasNext()) {
128 Object x = iter.next();
129 if (x instanceof File) {
130 File elem = (File) x;
131 data.append(elem.toURI() +
"\r\n");
132 }
else if (x instanceof String) {
133 data.append((String) x +
"\r\n");
135 logger.debug(
"did not know how to handle type in transfer: " + x.toString());
138 logger.debug(
"returning URI string: " + data.toString());
139 return data.toString();
141 logger.debug(
"getTransferData: didn't know how to handle the requested flavor.");
142 throw new UnsupportedFlavorException(flavor);
151 return (DataFlavor[]) FLAVORS.toArray(
new DataFlavor[FLAVORS.size()]);
159 if (flavor ==
null)
return false;
160 for (
int i = 0; i < FLAVORS.size(); i++) {
161 if (flavor.equals((DataFlavor) FLAVORS.get(i))) {
165 logger.debug(
"failed to find flavor: " + flavor.toString());
172 @SuppressWarnings(
"unchecked")
173 static public List<Object> extractData(Transferable tran) {
174 if (tran ==
null)
return null;
175 if (tran.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
176 logger.debug(
"extractData seeing java files flavor.");
178 return (List<Object>) tran.getTransferData(DataFlavor.javaFileListFlavor);
179 }
catch (Throwable cause) {
180 logger.error(
"extractData caught exception for java file list.", cause);
185 logger.debug(
"extractData seeing uri list flavor.");
187 return textURIListToFileList((String) tran.getTransferData(getURIListFlavor1()));
188 }
catch (Throwable cause) {
189 logger.error(
"extractData caught exception for URI list.", cause);
193 logger.error(
"extractData: Transferable did not support known data flavor.");
207 if (data ==
null)
return null;
208 List<Object> list =
new ArrayList<Object>(0);
209 for (StringTokenizer st =
new StringTokenizer(data,
"\r\n"); st.hasMoreTokens();) {
210 String s = st.nextToken();
211 if (s.startsWith(
"#")) {
216 java.net.URI uri =
new java.net.URI(s);
217 java.io.File file =
new java.io.File(uri);
219 }
catch (java.net.URISyntaxException e) {
221 logger.error(
"Found a malformed URI of: " + data);
222 }
catch (IllegalArgumentException e) {
224 logger.error(
"Found invalid 'file:' URI of: " + data);
233 @SuppressWarnings(
"unchecked")
234 public static List<Object> processStandardFileList(Transferable tran)
236 if (tran ==
null)
return null;
237 logger.debug(
"trying java file list flavor.");
239 return (List<Object>) tran.getTransferData(DataFlavor.javaFileListFlavor);
240 }
catch (Throwable cause) {
241 logger.debug(
"failed to retrieve transfer data for standard java file list flavor.",
244 return new ArrayList<Object>();
252 if (tran ==
null)
return false;
253 DataFlavor[] flavors = tran.getTransferDataFlavors();
254 for (
int i = 0; i < flavors.length; i++) {
255 if (flavors[i].isRepresentationClassReader())
266 if (tran ==
null)
return null;
267 logger.debug(
"trying URI list flavor.");
268 DataFlavor[] flavors = tran.getTransferDataFlavors();
269 for (
int i = 0; i < flavors.length; i++) {
270 if (flavors[i].isRepresentationClassReader()) {
272 logger.debug(
"found a reader flavor.");
274 Reader reader = flavors[i].getReaderForText(tran);
275 BufferedReader br =
new BufferedReader(reader);
276 return createFileArray(br);
277 }
catch (Throwable cause) {
278 logger.debug(
"failed to scan reader for file list.");
282 return new ArrayList<Object>();
285 private static String ZERO_CHAR_STRING =
"" + (char) 0;
288 if (bReader ==
null)
return null;
290 List<Object> list =
new ArrayList<Object>();
292 while ((line = bReader.readLine()) !=
null) {
295 if (ZERO_CHAR_STRING.equals(line))
297 File file =
new java.io.File(
new java.net.URI(line));
299 }
catch (Exception ex) {
300 logger.error(
"Error with " + line +
": " + ex.getMessage());
305 }
catch (IOException ex) {
306 logger.error(
"IOException while working on file list");
308 return new ArrayList<Object>();
ListTransferable(List< Object > initial)
static DataFlavor getURIListFlavor1()
boolean isDataFlavorSupported(DataFlavor flavor)
static DataFlavor getURIListFlavor2()
static List< Object > textURIListToFileList(String data)
Object getTransferData(DataFlavor flavor)
ListTransferable(Object initial)
static boolean checkReaderFlavor(Transferable tran)
boolean loadDataJustInTime(DataFlavor flavor)
static List< Object > processReaderFlavor(Transferable tran)
static List< Object > createFileArray(BufferedReader bReader)
DataFlavor[] getTransferDataFlavors()