1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
package tim.prune;
import java.awt.datatransfer.DataFlavor;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import javax.swing.TransferHandler;
/**
* Class to listen for drag/drop events and react to dropping files on to the application
*/
public class FileDropHandler extends TransferHandler
{
/** App object for passing results back to */
private App _app = null;
/** Fixed flavour in case the java file list flavour isn't available */
private static DataFlavor _uriListFlavour = null;
/** Static block to initialise the list flavour */
static
{
try {_uriListFlavour = new DataFlavor("text/uri-list;class=java.lang.String");
} catch (ClassNotFoundException nfe) {}
}
/**
* Constructor
* @param inApp App object to pass results of drop back to
*/
public FileDropHandler(App inApp)
{
_app = inApp;
}
/**
* Check if the object being dragged can be accepted
* @param inSupport object to check
*/
public boolean canImport(TransferSupport inSupport)
{
boolean retval = inSupport.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
|| inSupport.isDataFlavorSupported(_uriListFlavour);
// Modify icon to show a copy, not a move (+ icon on cursor)
if (retval) {
inSupport.setDropAction(COPY);
}
return retval;
}
/**
* Accept the incoming data and pass it on to the App
* @param inSupport contents of drop
*/
public boolean importData(TransferSupport inSupport)
{
if (!canImport(inSupport)) {return false;} // not allowed
boolean success = false;
ArrayList<File> dataFiles = new ArrayList<File>();
// Try a java file list flavour first
try
{
@SuppressWarnings("unchecked")
java.util.List<File> fileList = (java.util.List<File>)
inSupport.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
success = true;
for (File f : fileList)
{
if (f != null && f.exists())
{
if (f.isDirectory()) {
addDirectoryToList(f, dataFiles);
}
else if (f.isFile()) {
dataFiles.add(f);
}
}
}
} catch (Exception e) {} // exception caught, probably missing a javafilelist flavour - just continue
// If that didn't work, try a list of strings instead
if (!success)
{
try
{
String pathList = inSupport.getTransferable().getTransferData(_uriListFlavour).toString();
success = true;
for (String s : pathList.split("[\n\r]+"))
{
if (s != null && !s.equals(""))
{
File f = new File(new URI(s));
if (f.exists())
{
if (f.isDirectory()) {
addDirectoryToList(f, dataFiles);
}
else if (f.isFile()) {
dataFiles.add(f);
}
}
}
}
} catch (Exception e) {
System.err.println("exception: " + e.getClass().getName() + " - " + e.getMessage());
return false;
}
}
// Pass files back to app
if (!dataFiles.isEmpty()) {
_app.loadDataFiles(dataFiles);
}
return true;
}
/**
* Recursively-called method to add files from the given directory (and its subdirectories)
* to the given list of files
* @param inDir directory to add
* @param inList file list to append to
*/
private void addDirectoryToList(File inDir, ArrayList<File> inList)
{
if (inDir != null && inDir.exists() && inDir.canRead() && inDir.isDirectory() && inList != null)
{
for (String path : inDir.list())
{
if (path != null)
{
File f = new File(inDir, path);
if (f.exists() && f.canRead())
{
if (f.isFile())
{
// Add the found file to the list (if it's not in there already)
if (!inList.contains(f)) {
inList.add(f);
}
}
else if (f.isDirectory())
{
// Recursively add the files from subdirectories
addDirectoryToList(f, inList);
}
}
}
}
}
}
}
|