3 import java.awt.datatransfer.DataFlavor;
6 import java.util.ArrayList;
8 import javax.swing.TransferHandler;
11 * Class to listen for drag/drop events and react to dropping files on to the application
13 public class FileDropHandler extends TransferHandler
15 /** App object for passing results back to */
16 private App _app = null;
18 /** Fixed flavour in case the java file list flavour isn't available */
19 private static DataFlavor _uriListFlavour = null;
21 /** Static block to initialise the list flavour */
24 try {_uriListFlavour = new DataFlavor("text/uri-list;class=java.lang.String");
25 } catch (ClassNotFoundException nfe) {}
31 * @param inApp App object to pass results of drop back to
33 public FileDropHandler(App inApp)
39 * Check if the object being dragged can be accepted
40 * @param inSupport object to check
42 public boolean canImport(TransferSupport inSupport)
44 boolean retval = inSupport.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
45 || inSupport.isDataFlavorSupported(_uriListFlavour);
46 // Modify icon to show a copy, not a move (+ icon on cursor)
48 inSupport.setDropAction(COPY);
54 * Accept the incoming data and pass it on to the App
55 * @param inSupport contents of drop
57 public boolean importData(TransferSupport inSupport)
59 if (!canImport(inSupport)) {return false;} // not allowed
61 boolean success = false;
62 ArrayList<File> dataFiles = new ArrayList<File>();
64 // Try a java file list flavour first
67 @SuppressWarnings("unchecked")
68 java.util.List<File> fileList = (java.util.List<File>)
69 inSupport.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
72 for (File f : fileList)
74 if (f != null && f.exists())
76 if (f.isDirectory()) {
77 addDirectoryToList(f, dataFiles);
79 else if (f.isFile()) {
84 } catch (Exception e) {} // exception caught, probably missing a javafilelist flavour - just continue
86 // If that didn't work, try a list of strings instead
91 String pathList = inSupport.getTransferable().getTransferData(_uriListFlavour).toString();
94 for (String s : pathList.split("[\n\r]+"))
96 if (s != null && !s.equals(""))
98 File f = new File(new URI(s));
101 if (f.isDirectory()) {
102 addDirectoryToList(f, dataFiles);
104 else if (f.isFile()) {
110 } catch (Exception e) {
111 System.err.println("exception: " + e.getClass().getName() + " - " + e.getMessage());
116 // Pass files back to app
117 if (!dataFiles.isEmpty()) {
118 _app.loadDataFiles(dataFiles);
125 * Recursively-called method to add files from the given directory (and its subdirectories)
126 * to the given list of files
127 * @param inDir directory to add
128 * @param inList file list to append to
130 private void addDirectoryToList(File inDir, ArrayList<File> inList)
132 if (inDir != null && inDir.exists() && inDir.canRead() && inDir.isDirectory() && inList != null)
134 for (String path : inDir.list())
138 File f = new File(inDir, path);
139 if (f.exists() && f.canRead())
143 // Add the found file to the list (if it's not in there already)
144 if (!inList.contains(f)) {
148 else if (f.isDirectory())
150 // Recursively add the files from subdirectories
151 addDirectoryToList(f, inList);