* @param transferable a Transferable instance that contains the files to be retrieved
* @return the files contained by the specified Transferable as a FileSet, or <code>null</code> if no file
* was present or if an error occurred
*/
public static FileSet getTransferFiles(Transferable transferable) {
FileSet files;
AbstractFile file;
try {
// FileSet DataFlavor
if(transferable.isDataFlavorSupported(FILE_SET_DATA_FLAVOR)) {
files = (FileSet)transferable.getTransferData(FILE_SET_DATA_FLAVOR);
}
// File list DataFlavor
else if(transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
List<File> fileList = (List<File>)transferable.getTransferData(DataFlavor.javaFileListFlavor);
int nbFiles = fileList.size();
files = new FileSet();
for(int i=0; i<nbFiles; i++) {
file = FileFactory.getFile(fileList.get(i).getAbsolutePath());
if(file!=null)
files.add(file);
}
}
// Text plain DataFlavor: assume that lines designate file paths
else if(transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
BufferedReader br;
br = null;
try {
br = new BufferedReader(DataFlavor.getTextPlainUnicodeFlavor().getReaderForText(transferable));
// Read input line by line and try to create AbstractFile instances
String path;
files = new FileSet();
while((path=br.readLine())!=null) {
// Try to create an AbstractFile instance, returned instance may be null
file = FileFactory.getFile(path);
// Safety precaution: if at least one line doesn't resolve as a file, stop reading
// and return null. This is to avoid any nasty effect that could arise if a random
// piece of text (let's say an email contents) was inadvertently pasted or dropped to muCommander.
if(file==null)
return null;
files.add(file);
}
}
// Documentation is not explicit on whether DataFlavor streams need to be closed, we might as well
// do so just to be sure.
finally {