From d69b1da6a3a11447fa40dffba9d92e33d07363a0 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 18 Jul 2014 22:03:27 +0000 Subject: [PATCH] * enforce good alphabetical order for all dropped data (on Windows files a dropped in selection order which can be confusing) --- .../filebot/ui/transfer/FileTransferable.java | 66 ++++++++----------- 1 file changed, 27 insertions(+), 39 deletions(-) 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); } - + }