From 8364015504ab48a56af107e2d7f8fecb83e5311b Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 23 Oct 2010 03:51:19 +0000 Subject: [PATCH] * unfold/flatten folders when dropped in Rename panel and List panel --- .../list/FileListTransferablePolicy.java | 32 ++++++------------- .../rename/FilesListTransferablePolicy.java | 10 ++---- .../rename/NamesListTransferablePolicy.java | 9 ++---- .../net/sourceforge/tuned/FileUtilities.java | 16 ++++++++++ 4 files changed, 29 insertions(+), 38 deletions(-) diff --git a/source/net/sourceforge/filebot/ui/panel/list/FileListTransferablePolicy.java b/source/net/sourceforge/filebot/ui/panel/list/FileListTransferablePolicy.java index fe2277ec..e5e06786 100644 --- a/source/net/sourceforge/filebot/ui/panel/list/FileListTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/panel/list/FileListTransferablePolicy.java @@ -7,7 +7,6 @@ import static net.sourceforge.tuned.FileUtilities.*; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import net.sourceforge.filebot.MediaTypes; @@ -47,24 +46,18 @@ class FileListTransferablePolicy extends FileTransferablePolicy { // clear selection list.getListComponent().clearSelection(); - if (containsOnly(files, FOLDERS)) { - loadFolders(files); - } else if (containsOnly(files, MediaTypes.getDefaultFilter("application/torrent"))) { + if (containsOnly(files, MediaTypes.getDefaultFilter("application/torrent"))) { loadTorrents(files); } else { - loadFiles(files); - } - } - - - private void loadFolders(List folders) { - if (folders.size() == 1) { // if only one folder was dropped, use its name as title - list.setTitle(FileUtilities.getFolderName(folders.get(0))); - } - - for (File folder : folders) { - loadFiles(Arrays.asList(folder.listFiles())); + if (files.size() == 1 && files.get(0).isDirectory()) { + list.setTitle(FileUtilities.getFolderName(files.get(0))); + } + + // load all files from the given folders recursively up do a depth of 5 + for (File file : flatten(files, 5)) { + list.getModel().add(FileUtilities.getName(file)); + } } } @@ -88,13 +81,6 @@ class FileListTransferablePolicy extends FileTransferablePolicy { } - private void loadFiles(List files) { - for (File file : files) { - list.getModel().add(FileUtilities.getName(file)); - } - } - - @Override public String getFileFilterDescription() { return "files, folders and torrents"; diff --git a/source/net/sourceforge/filebot/ui/panel/rename/FilesListTransferablePolicy.java b/source/net/sourceforge/filebot/ui/panel/rename/FilesListTransferablePolicy.java index c5e35643..d5289fd5 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/FilesListTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/FilesListTransferablePolicy.java @@ -15,7 +15,7 @@ class FilesListTransferablePolicy extends FileTransferablePolicy { private final List model; - + public FilesListTransferablePolicy(List model) { this.model = model; } @@ -35,13 +35,7 @@ class FilesListTransferablePolicy extends FileTransferablePolicy { @Override protected void load(List files) { - if (containsOnly(files, FOLDERS)) { - for (File folder : files) { - model.addAll(FastFile.foreach(folder.listFiles())); - } - } else { - model.addAll(FastFile.foreach(files)); - } + model.addAll(FastFile.foreach(flatten(files, 5))); } diff --git a/source/net/sourceforge/filebot/ui/panel/rename/NamesListTransferablePolicy.java b/source/net/sourceforge/filebot/ui/panel/rename/NamesListTransferablePolicy.java index db9fcec2..1412b2b4 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/NamesListTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/NamesListTransferablePolicy.java @@ -107,14 +107,9 @@ class NamesListTransferablePolicy extends FileTransferablePolicy { } else if (containsOnly(files, MediaTypes.getDefaultFilter("application/torrent"))) { // torrent files loadTorrentFiles(files, values); - } else if (containsOnly(files, FOLDERS)) { - // load files from each folder - for (File folder : files) { - values.addAll(FastFile.foreach(folder.listFiles())); - } } else { - // just add all given files - values.addAll(FastFile.foreach(files)); + // load all files from the given folders recursively up do a depth of 5 + values.addAll(FastFile.foreach(flatten(files, 5))); } model.addAll(values); diff --git a/source/net/sourceforge/tuned/FileUtilities.java b/source/net/sourceforge/tuned/FileUtilities.java index a1d20caa..44a8e811 100644 --- a/source/net/sourceforge/tuned/FileUtilities.java +++ b/source/net/sourceforge/tuned/FileUtilities.java @@ -142,6 +142,22 @@ public final class FileUtilities { } + public static List flatten(Iterable roots, int maxDepth) { + List files = new ArrayList(); + + // unfold/flatten file tree + for (File root : roots) { + if (root.isDirectory()) { + listFiles(root, 0, files, maxDepth); + } else { + files.add(root); + } + } + + return files; + } + + public static List listPath(File file) { LinkedList nodes = new LinkedList();