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

152 lines
4.3 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot.ui.transfer;
2007-12-26 11:48:28 -05:00
2016-03-09 15:36:28 -05:00
import static net.filebot.Logging.*;
2014-04-19 02:30:29 -04:00
import static net.filebot.Settings.*;
import static net.filebot.util.FileUtilities.*;
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;
2014-04-19 02:30:29 -04:00
import net.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();
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);
}
}
2009-06-19 18:24:27 -04:00
private final File[] files;
public FileTransferable(File... files) {
2009-06-19 18:24:27 -04:00
this.files = files;
2007-12-26 11:48:28 -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
}
@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);
}
/**
* @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);
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");
}
return sb.toString();
2007-12-26 11:48:28 -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
}
@Override
2007-12-26 11:48:28 -05:00
public boolean isDataFlavorSupported(DataFlavor flavor) {
return isFileListFlavor(flavor);
}
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
}
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
}
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(DataFlavor.javaFileListFlavor) && !useGVFS()) {
// file list flavor
2014-07-22 03:13:40 -04:00
Object transferable = tr.getTransferData(DataFlavor.javaFileListFlavor);
if (transferable instanceof List) {
return sortByUniquePath((List<File>) transferable); // FORCE NATURAL FILE ORDER
} else {
2014-07-28 15:20:42 -04:00
return null; // on some platforms transferable data will not be available until the drop has been accepted
2014-07-22 03:13:40 -04:00
}
}
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>();
2009-07-09 16:04:47 -04:00
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#")) {
2009-07-09 16:04:47 -04:00
// the line is a comment (as per RFC 2483)
continue;
}
2009-07-09 16:04:47 -04:00
try {
URI uri = new URI(line);
File file = null;
try {
// file URIs
file = new File(uri);
2012-11-24 16:56:09 -05:00
} catch (IllegalArgumentException exception) {
// try handle other GVFS URI schemes
try {
if (useGVFS()) {
file = GVFS.getPathForURI(uri);
}
} catch (LinkageError error) {
2016-03-09 15:36:28 -05:00
debug.log(Level.WARNING, "Unable to resolve GVFS URI", error);
}
}
if (file == null || !file.exists()) {
2012-11-25 07:52:08 -05:00
throw new FileNotFoundException(file != null ? file.getPath() : line);
}
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
2016-03-09 15:36:28 -05:00
debug.log(Level.WARNING, "Invalid file URI: " + line);
2009-07-09 16:04:47 -04:00
}
}
return sortByUniquePath(files); // FORCE NATURAL FILE ORDER
2009-07-09 16:04:47 -04: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
}