filebot/source/net/sourceforge/filebot/ui/transfer/FileTransferable.java

154 lines
4.0 KiB
Java
Raw Normal View History

2012-11-23 19:11:07 -05:00
2007-12-26 11:48:28 -05:00
package net.sourceforge.filebot.ui.transfer;
2012-11-23 19:11:07 -05:00
2007-12-26 11:48:28 -05:00
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
2009-07-09 16:04:47 -04:00
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
2009-06-30 08:12:34 -04:00
import java.nio.CharBuffer;
2009-07-09 16:04:47 -04:00
import java.util.ArrayList;
import java.util.Arrays;
2007-12-26 11:48:28 -05:00
import java.util.Collection;
2009-07-09 16:04:47 -04:00
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.filebot.gio.GVFS;
2007-12-26 11:48:28 -05:00
2012-11-23 19:11:07 -05:00
public class FileTransferable implements Transferable {
public static final DataFlavor uriListFlavor = createUriListFlavor();
2012-11-23 19:11:07 -05:00
private static DataFlavor createUriListFlavor() {
try {
2009-06-30 08:12:34 -04:00
return new DataFlavor("text/uri-list;class=java.nio.CharBuffer");
} catch (ClassNotFoundException e) {
// will never happen
2009-06-19 18:24:27 -04:00
throw new RuntimeException(e);
}
}
2012-11-23 19:11:07 -05:00
2009-06-19 18:24:27 -04:00
private final File[] files;
2012-11-23 19:11:07 -05:00
public FileTransferable(File... files) {
2009-06-19 18:24:27 -04:00
this.files = files;
2007-12-26 11:48:28 -05:00
}
2012-11-23 19:11:07 -05:00
public FileTransferable(Collection<File> files) {
2009-06-19 18:24:27 -04:00
this.files = files.toArray(new File[0]);
2007-12-26 11:48:28 -05:00
}
2012-11-23 19:11:07 -05:00
@Override
2012-11-23 19:11:07 -05:00
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (flavor.isFlavorJavaFileListType())
2009-06-19 18:24:27 -04:00
return Arrays.asList(files);
else if (flavor.equals(uriListFlavor))
2009-06-30 08:12:34 -04:00
return CharBuffer.wrap(getUriList());
else
2007-12-26 11:48:28 -05:00
throw new UnsupportedFlavorException(flavor);
}
2012-11-23 19:11:07 -05:00
/**
* @return line separated list of file URIs
*/
private String getUriList() {
2009-06-19 18:24:27 -04:00
StringBuilder sb = new StringBuilder(80 * files.length);
2012-11-23 19:11:07 -05:00
for (File file : files) {
2009-06-19 18:24:27 -04:00
// use URI encoded path
sb.append("file://").append(file.toURI().getRawPath());
2008-02-15 13:57:18 -05:00
sb.append("\r\n");
}
2012-11-23 19:11:07 -05:00
return sb.toString();
2007-12-26 11:48:28 -05:00
}
2012-11-23 19:11:07 -05:00
@Override
2007-12-26 11:48:28 -05:00
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.javaFileListFlavor, uriListFlavor };
2007-12-26 11:48:28 -05:00
}
2012-11-23 19:11:07 -05:00
@Override
2007-12-26 11:48:28 -05:00
public boolean isDataFlavorSupported(DataFlavor flavor) {
return isFileListFlavor(flavor);
}
2012-11-23 19:11:07 -05:00
public static boolean isFileListFlavor(DataFlavor flavor) {
2012-11-23 19:11:07 -05:00
return flavor.isFlavorJavaFileListType() || flavor.equals(uriListFlavor);
2007-12-26 11:48:28 -05:00
}
2012-11-23 19:11:07 -05:00
2009-07-09 16:04:47 -04:00
public static boolean hasFileListFlavor(Transferable tr) {
2012-11-23 19:11:07 -05:00
return tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || tr.isDataFlavorSupported(FileTransferable.uriListFlavor);
2009-07-09 16:04:47 -04:00
}
2012-11-23 19:11:07 -05:00
2009-07-09 16:04:47 -04:00
@SuppressWarnings("unchecked")
2012-11-23 19:11:07 -05:00
public static List<File> getFilesFromTransferable(Transferable tr) throws IOException, UnsupportedFlavorException {
if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) {
// file URI list flavor (Linux)
2012-11-23 19:11:07 -05:00
Readable transferData = (Readable) tr.getTransferData(FileTransferable.uriListFlavor);
2009-07-09 16:04:47 -04:00
Scanner scanner = new Scanner(transferData);
List<File> files = new ArrayList<File>();
2012-11-23 19:11:07 -05:00
2009-07-09 16:04:47 -04:00
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
2012-11-23 19:11:07 -05:00
if (line.startsWith("#")) {
2009-07-09 16:04:47 -04:00
// the line is a comment (as per RFC 2483)
continue;
}
2012-11-23 19:11:07 -05:00
2009-07-09 16:04:47 -04:00
try {
URI uri = new URI(line);
File file = null;
2012-11-23 19:11:07 -05:00
try {
// file URIs
file = new File(uri);
} catch (IllegalArgumentException e) {
// try handle other GVFS URI schemes
try {
if (GVFS.isSupported()) {
file = GVFS.getPathForURI(uri);
}
} catch (LinkageError error) {
Logger.getLogger(FileTransferable.class.getName()).log(Level.WARNING, "Unable to resolve GVFS URI", e);
}
}
2012-11-23 19:11:07 -05:00
if (file == null || !file.exists()) {
throw new FileNotFoundException(line);
}
2012-11-23 19:11:07 -05:00
2009-07-09 16:04:47 -04:00
files.add(file);
2012-11-23 19:11:07 -05:00
} catch (Throwable e) {
// URISyntaxException, IllegalArgumentException, FileNotFoundException, LinkageError, etc
Logger.getLogger(FileTransferable.class.getName()).log(Level.WARNING, "Invalid file URI: " + line);
2009-07-09 16:04:47 -04:00
}
return files;
2009-07-09 16:04:47 -04:00
}
} else if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
// file list flavor
2012-11-23 19:11:07 -05:00
return (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
2009-07-09 16:04:47 -04:00
}
2012-11-23 19:11:07 -05:00
2009-07-09 16:04:47 -04:00
// cannot get files from transferable
throw new UnsupportedFlavorException(null);
2009-07-09 16:04:47 -04:00
}
2007-12-26 11:48:28 -05:00
}