From 5a2087c2b427d1c81cade7c341cda293e992f8b6 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 29 Sep 2016 13:45:42 +0800 Subject: [PATCH] Use human sort order for all paths loaded into the UI --- .../rename/FilesListTransferablePolicy.java | 7 +- .../filebot/ui/transfer/FileTransferable.java | 72 ++++++++++--------- source/net/filebot/util/FileUtilities.java | 4 +- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/source/net/filebot/ui/rename/FilesListTransferablePolicy.java b/source/net/filebot/ui/rename/FilesListTransferablePolicy.java index c0d5abd6..5633a2c0 100644 --- a/source/net/filebot/ui/rename/FilesListTransferablePolicy.java +++ b/source/net/filebot/ui/rename/FilesListTransferablePolicy.java @@ -51,13 +51,14 @@ class FilesListTransferablePolicy extends BackgroundFileTransferablePolicy @Override protected void load(List files, TransferAction action) { - Set fileset = new LinkedHashSet(); + // collect files recursively and eliminate duplicates + Set sink = new LinkedHashSet(64, 4); // load files recursively by default - load(files, action != TransferAction.LINK, fileset); + load(files, action != TransferAction.LINK, sink); // use fast file to minimize system calls like length(), isDirectory(), isFile(), ... and list files in human order - publish(fileset.stream().map(FastFile::new).toArray(File[]::new)); + publish(sink.stream().map(FastFile::new).toArray(File[]::new)); } private void load(List files, boolean recursive, Collection sink) { diff --git a/source/net/filebot/ui/transfer/FileTransferable.java b/source/net/filebot/ui/transfer/FileTransferable.java index 4facbb5e..ad0ce386 100644 --- a/source/net/filebot/ui/transfer/FileTransferable.java +++ b/source/net/filebot/ui/transfer/FileTransferable.java @@ -1,5 +1,6 @@ package net.filebot.ui.transfer; +import static java.util.stream.Collectors.*; import static net.filebot.Logging.*; import static net.filebot.Settings.*; import static net.filebot.util.FileUtilities.*; @@ -33,6 +34,14 @@ public class FileTransferable implements Transferable { } } + 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); + } + private final File[] files; public FileTransferable(File... files) { @@ -78,14 +87,6 @@ public class FileTransferable implements Transferable { 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 { // On Linux, if a file is dragged from a smb share to into a java application (e.g. Ubuntu Files to FileBot) @@ -96,45 +97,50 @@ public class FileTransferable implements Transferable { 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(); + try (Scanner scanner = new Scanner(transferData)) { + List files = new ArrayList(); - if (line.startsWith("#")) { - // the line is a comment (as per RFC 2483) - continue; - } + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); - try { - URI uri = new URI(line); - File file = GVFS.getDefaultVFS().getPathForURI(uri); - - if (file == null || !file.exists()) { - throw new FileNotFoundException(line); + if (line.startsWith("#")) { + // the line is a comment (as per RFC 2483) + continue; } - files.add(file); - } catch (Throwable e) { - // URISyntaxException, IllegalArgumentException, FileNotFoundException, LinkageError, etc - debug.warning("Invalid file URI: " + line); - } - } + try { + URI uri = new URI(line); + File file = GVFS.getDefaultVFS().getPathForURI(uri); - return sortByUniquePath(files); // FORCE NATURAL FILE ORDER + if (file == null || !file.exists()) { + throw new FileNotFoundException(line); + } + + files.add(file); + } catch (Throwable e) { + // URISyntaxException, IllegalArgumentException, FileNotFoundException, LinkageError, etc + debug.warning("Invalid file URI: " + line); + } + } + + return files.stream().distinct().sorted(HUMAN_NAME_ORDER).collect(toList()); + } } } - // Windows, Mac and default handling + // Windows / Mac and default handling if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { // file list flavor Object transferable = tr.getTransferData(DataFlavor.javaFileListFlavor); + if (transferable instanceof List) { - return sortByUniquePath((List) transferable); // FORCE NATURAL FILE ORDER - } else { - return null; // on some platforms transferable data will not be available until the drop has been accepted + List files = (List) transferable; + return files.stream().distinct().sorted(HUMAN_NAME_ORDER).collect(toList()); } + + // on some platforms transferable data will not be available until the drop has been accepted + return null; } // cannot get files from transferable diff --git a/source/net/filebot/util/FileUtilities.java b/source/net/filebot/util/FileUtilities.java index c7070c58..0990f4ec 100644 --- a/source/net/filebot/util/FileUtilities.java +++ b/source/net/filebot/util/FileUtilities.java @@ -364,10 +364,8 @@ public final class FileUtilities { } public static List sortByUniquePath(Collection files) { - // sort by unique lower-case paths - TreeSet sortedSet = new TreeSet(CASE_INSENSITIVE_PATH_ORDER); + TreeSet sortedSet = new TreeSet(CASE_INSENSITIVE_PATH_ORDER); // sort by unique lower-case paths sortedSet.addAll(files); - return new ArrayList(sortedSet); }