diff --git a/source/net/filebot/ui/transfer/FileTransferable.java b/source/net/filebot/ui/transfer/FileTransferable.java index 92952cde..3b20592c 100644 --- a/source/net/filebot/ui/transfer/FileTransferable.java +++ b/source/net/filebot/ui/transfer/FileTransferable.java @@ -1,8 +1,7 @@ - package net.filebot.ui.transfer; - import static net.filebot.Settings.*; +import static net.filebot.util.FileUtilities.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; @@ -22,12 +21,10 @@ import java.util.logging.Logger; import net.filebot.gio.GVFS; - public class FileTransferable implements Transferable { - + public static final DataFlavor uriListFlavor = createUriListFlavor(); - - + private static DataFlavor createUriListFlavor() { try { return new DataFlavor("text/uri-list;class=java.nio.CharBuffer"); @@ -36,20 +33,17 @@ public class FileTransferable implements Transferable { throw new RuntimeException(e); } } - + private final File[] files; - - + public FileTransferable(File... files) { this.files = files; } - - + public FileTransferable(Collection files) { this.files = files.toArray(new File[0]); } - - + @Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { if (flavor.isFlavorJavaFileListType()) @@ -59,71 +53,65 @@ public class FileTransferable implements Transferable { else throw new UnsupportedFlavorException(flavor); } - - + /** * @return line separated list of file URIs */ private String getUriList() { StringBuilder sb = new StringBuilder(80 * files.length); - + for (File file : files) { // use URI encoded path sb.append("file://").append(file.toURI().getRawPath()); sb.append("\r\n"); } - + return sb.toString(); } - - + @Override public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[] { DataFlavor.javaFileListFlavor, uriListFlavor }; } - - + @Override public boolean isDataFlavorSupported(DataFlavor flavor) { return isFileListFlavor(flavor); } - - + public static boolean isFileListFlavor(DataFlavor flavor) { return flavor.isFlavorJavaFileListType() || flavor.equals(uriListFlavor); } - - + public static boolean hasFileListFlavor(Transferable tr) { return tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || tr.isDataFlavorSupported(FileTransferable.uriListFlavor); } - - + @SuppressWarnings("unchecked") public static List getFilesFromTransferable(Transferable tr) throws IOException, UnsupportedFlavorException { if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor) && !useGVFS()) { // file list flavor - return (List) tr.getTransferData(DataFlavor.javaFileListFlavor); + return sortByUniquePath((List) tr.getTransferData(DataFlavor.javaFileListFlavor)); // FORCE NATURAL FILE ORDER } - + if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) { // file URI list flavor (Linux) Readable transferData = (Readable) tr.getTransferData(FileTransferable.uriListFlavor); Scanner scanner = new Scanner(transferData); List files = new ArrayList(); - + while (scanner.hasNextLine()) { String line = scanner.nextLine(); - + if (line.startsWith("#")) { // the line is a comment (as per RFC 2483) continue; } - + try { URI uri = new URI(line); File file = null; - + try { // file URIs file = new File(uri); @@ -137,23 +125,23 @@ public class FileTransferable implements Transferable { Logger.getLogger(FileTransferable.class.getName()).log(Level.WARNING, "Unable to resolve GVFS URI", error); } } - + if (file == null || !file.exists()) { throw new FileNotFoundException(file != null ? file.getPath() : line); } - + files.add(file); } catch (Throwable e) { // URISyntaxException, IllegalArgumentException, FileNotFoundException, LinkageError, etc Logger.getLogger(FileTransferable.class.getName()).log(Level.WARNING, "Invalid file URI: " + line); } } - - return files; + + return sortByUniquePath(files); // FORCE NATURAL FILE ORDER } - + // cannot get files from transferable throw new UnsupportedFlavorException(null); } - + }