From 396fb3508f3e8cb60e507b5d42d96aa688f3122c Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 17 Apr 2014 15:35:33 +0000 Subject: [PATCH] * refactoring & added utils --- .../sourceforge/filebot/cli/ArgumentBean.java | 11 +++- .../filebot/cli/FolderWatchService.java | 2 +- .../filebot/format/MediaBindingBean.java | 2 +- .../filebot/media/MediaDetection.java | 2 +- .../ui/list/FileListTransferablePolicy.java | 29 ++++----- .../rename/NamesListTransferablePolicy.java | 4 +- .../ui/subtitle/SubtitleDropTarget.java | 6 +- .../filebot/util/FileUtilities.java | 59 ++++++++++--------- 8 files changed, 57 insertions(+), 58 deletions(-) diff --git a/source/net/sourceforge/filebot/cli/ArgumentBean.java b/source/net/sourceforge/filebot/cli/ArgumentBean.java index 36489970..2c5938b8 100644 --- a/source/net/sourceforge/filebot/cli/ArgumentBean.java +++ b/source/net/sourceforge/filebot/cli/ArgumentBean.java @@ -164,8 +164,15 @@ public class ArgumentBean { Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage()); } - // resolve folders - files.addAll(resolveFolders && file.isDirectory() ? listFiles(singleton(file), recursive ? 10 : 0, false) : singleton(file)); + if (resolveFolders && file.isDirectory()) { + if (recursive) { + files.addAll(listFiles(file)); + } else { + files.addAll(asList(file.listFiles())); + } + } else { + files.add(file); + } } return files; diff --git a/source/net/sourceforge/filebot/cli/FolderWatchService.java b/source/net/sourceforge/filebot/cli/FolderWatchService.java index 450efeba..4cd91518 100644 --- a/source/net/sourceforge/filebot/cli/FolderWatchService.java +++ b/source/net/sourceforge/filebot/cli/FolderWatchService.java @@ -148,7 +148,7 @@ public abstract class FolderWatchService implements Closeable { // start watching newly created folder if (watchTree) { try { - commitSet.addAll(listFiles(singleton(file), Integer.MAX_VALUE, false)); + commitSet.addAll(listFiles(file)); watchFolder(file); } catch (IOException e) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.getMessage(), e); diff --git a/source/net/sourceforge/filebot/format/MediaBindingBean.java b/source/net/sourceforge/filebot/format/MediaBindingBean.java index 21e159de..0c44172b 100644 --- a/source/net/sourceforge/filebot/format/MediaBindingBean.java +++ b/source/net/sourceforge/filebot/format/MediaBindingBean.java @@ -751,7 +751,7 @@ public class MediaBindingBean { if (mediaFile.isDirectory()) { // just select the first video file in the folder as media sample - SortedSet videos = new TreeSet(filter(listFiles(singleton(mediaFile), 2, false), VIDEO_FILES)); + SortedSet videos = new TreeSet(filter(listFiles(mediaFile), VIDEO_FILES)); if (videos.size() > 0) { return videos.iterator().next(); } diff --git a/source/net/sourceforge/filebot/media/MediaDetection.java b/source/net/sourceforge/filebot/media/MediaDetection.java index 0e8351c8..8479b2cc 100644 --- a/source/net/sourceforge/filebot/media/MediaDetection.java +++ b/source/net/sourceforge/filebot/media/MediaDetection.java @@ -1076,7 +1076,7 @@ public class MediaDetection { Set collection = new LinkedHashSet(); List nfoFiles = new ArrayList(); if (file.isDirectory()) { - nfoFiles.addAll(filter(listFiles(singleton(file), 10, false), NFO_FILES)); + nfoFiles.addAll(filter(listFiles(file), NFO_FILES)); } else if (file.getParentFile().isDirectory()) { addAll(nfoFiles, file.getParentFile().listFiles(NFO_FILES)); } diff --git a/source/net/sourceforge/filebot/ui/list/FileListTransferablePolicy.java b/source/net/sourceforge/filebot/ui/list/FileListTransferablePolicy.java index 28d42b56..89658dce 100644 --- a/source/net/sourceforge/filebot/ui/list/FileListTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/list/FileListTransferablePolicy.java @@ -1,7 +1,5 @@ - package net.sourceforge.filebot.ui.list; - import static net.sourceforge.filebot.util.FileUtilities.*; import java.io.File; @@ -15,37 +13,32 @@ import net.sourceforge.filebot.ui.FileBotList; import net.sourceforge.filebot.ui.transfer.FileTransferablePolicy; import net.sourceforge.filebot.util.FileUtilities; - class FileListTransferablePolicy extends FileTransferablePolicy { - + private FileBotList list; - public FileListTransferablePolicy(FileBotList list) { this.list = list; } - @Override protected boolean accept(List files) { return true; } - @Override protected void clear() { list.getModel().clear(); } - @Override protected void load(List files) throws IOException { // set title based on parent folder of first file list.setTitle(FileUtilities.getFolderName(files.get(0).getParentFile())); - + // clear selection list.getListComponent().clearSelection(); - + if (containsOnly(files, MediaTypes.getDefaultFilter("application/torrent"))) { loadTorrents(files); } else { @@ -53,37 +46,35 @@ class FileListTransferablePolicy extends FileTransferablePolicy { 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, false)) { + + // load all files from the given folders recursively up do a depth of 32 + for (File file : listFiles(files)) { list.getModel().add(FileUtilities.getName(file)); } } } - private void loadTorrents(List files) throws IOException { List torrents = new ArrayList(files.size()); - + for (File file : files) { torrents.add(new Torrent(file)); } - + if (torrents.size() == 1) { list.setTitle(FileUtilities.getNameWithoutExtension(torrents.get(0).getName())); } - + for (Torrent torrent : torrents) { for (Torrent.Entry entry : torrent.getFiles()) { list.getModel().add(FileUtilities.getNameWithoutExtension(entry.getName())); } } } - @Override public String getFileFilterDescription() { return "files, folders and torrents"; } - + } diff --git a/source/net/sourceforge/filebot/ui/rename/NamesListTransferablePolicy.java b/source/net/sourceforge/filebot/ui/rename/NamesListTransferablePolicy.java index 72b8e8b8..ce266a2b 100644 --- a/source/net/sourceforge/filebot/ui/rename/NamesListTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/rename/NamesListTransferablePolicy.java @@ -95,8 +95,8 @@ class NamesListTransferablePolicy extends FileTransferablePolicy { // torrent files loadTorrentFiles(files, values); } else { - // load all files from the given folders recursively up do a depth of 5 - values.addAll(FastFile.get(flatten(files, 5, false))); + // load all files from the given folders recursively up do a depth of 32 + values.addAll(FastFile.get(listFiles(files))); } model.addAll(values); diff --git a/source/net/sourceforge/filebot/ui/subtitle/SubtitleDropTarget.java b/source/net/sourceforge/filebot/ui/subtitle/SubtitleDropTarget.java index 33c6877d..71f0b44e 100644 --- a/source/net/sourceforge/filebot/ui/subtitle/SubtitleDropTarget.java +++ b/source/net/sourceforge/filebot/ui/subtitle/SubtitleDropTarget.java @@ -175,8 +175,7 @@ abstract class SubtitleDropTarget extends JButton { final Collection videoFiles = new TreeSet(); // video files only - videoFiles.addAll(filter(input, VIDEO_FILES)); - videoFiles.addAll(filter(listFiles(filter(input, FOLDERS), 5, false), VIDEO_FILES)); + videoFiles.addAll(filter(listFiles(input), VIDEO_FILES)); if (videoFiles.size() > 0) { // invoke later so we don't block the DnD operation with the download dialog @@ -256,8 +255,7 @@ abstract class SubtitleDropTarget extends JButton { final Collection files = new TreeSet(); // video files only - files.addAll(filter(input, FILES)); - files.addAll(listFiles(filter(input, FOLDERS), 5, false)); + files.addAll(listFiles(input)); final List videos = filter(files, VIDEO_FILES); final List subtitles = filter(files, SUBTITLE_FILES); diff --git a/source/net/sourceforge/filebot/util/FileUtilities.java b/source/net/sourceforge/filebot/util/FileUtilities.java index 97c9df7c..c89cbd11 100644 --- a/source/net/sourceforge/filebot/util/FileUtilities.java +++ b/source/net/sourceforge/filebot/util/FileUtilities.java @@ -1,5 +1,7 @@ package net.sourceforge.filebot.util; +import static java.util.Arrays.*; + import java.io.BufferedInputStream; import java.io.File; import java.io.FileFilter; @@ -18,7 +20,6 @@ import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.HashMap; @@ -353,21 +354,6 @@ public final class FileUtilities { return new NotFileFilter(filter); } - public static List flatten(Iterable roots, int maxDepth, boolean listHiddenFiles) { - List files = new ArrayList(); - - // unfold/flatten file tree - for (File root : roots) { - if (root.isDirectory()) { - listFiles(root, 0, files, maxDepth, listHiddenFiles); - } else { - files.add(root); - } - } - - return files; - } - public static List listPath(File file) { return listPathTail(file, Integer.MAX_VALUE, false); } @@ -398,25 +384,39 @@ public final class FileUtilities { } public static List listFiles(File... folders) { - return listFiles(Arrays.asList(folders)); + return listFiles(asList(folders)); } public static List listFiles(Iterable folders) { - return listFiles(folders, 255, false); + return listFiles(folders, 32, false, true, false); } - public static List listFiles(Iterable folders, int maxDepth, boolean listHiddenFiles) { + public static List listFolders(Iterable folders) { + return listFiles(folders, 32, false, false, true); + } + + public static List listFiles(Iterable folders, int maxDepth, boolean addHidden, boolean addFiles, boolean addFolders) { List files = new ArrayList(); // collect files from directory tree - for (File folder : folders) { - listFiles(folder, 0, files, maxDepth, listHiddenFiles); + for (File it : folders) { + if (!addHidden && it.isHidden()) // ignore hidden files + continue; + + if (it.isDirectory()) { + if (addFolders) { + files.add(it); + } + listFiles(it, files, 0, maxDepth, addHidden, addFiles, addFolders); + } else if (addFiles) { + files.add(it); + } } return files; } - private static void listFiles(File folder, int depth, List files, int maxDepth, boolean listHiddenFiles) { + private static void listFiles(File folder, List files, int depth, int maxDepth, boolean addHidden, boolean addFiles, boolean addFolders) { if (depth > maxDepth) return; @@ -424,14 +424,17 @@ public final class FileUtilities { if (children == null) return; - for (File file : children) { - if (!listHiddenFiles && file.isHidden()) // ignore hidden files + for (File it : children) { + if (!addHidden && it.isHidden()) // ignore hidden files continue; - if (file.isDirectory()) { - listFiles(file, depth + 1, files, maxDepth, listHiddenFiles); - } else { - files.add(file); + if (it.isDirectory()) { + if (addFolders) { + files.add(it); + } + listFiles(it, files, depth + 1, maxDepth, addHidden, addFiles, addFolders); + } else if (addFiles) { + files.add(it); } } }