* refactoring & added utils

This commit is contained in:
Reinhard Pointner 2014-04-17 15:35:33 +00:00
parent 1f055065d6
commit 396fb3508f
8 changed files with 57 additions and 58 deletions

View File

@ -164,8 +164,15 @@ public class ArgumentBean {
Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage()); Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage());
} }
// resolve folders if (resolveFolders && file.isDirectory()) {
files.addAll(resolveFolders && file.isDirectory() ? listFiles(singleton(file), recursive ? 10 : 0, false) : singleton(file)); if (recursive) {
files.addAll(listFiles(file));
} else {
files.addAll(asList(file.listFiles()));
}
} else {
files.add(file);
}
} }
return files; return files;

View File

@ -148,7 +148,7 @@ public abstract class FolderWatchService implements Closeable {
// start watching newly created folder // start watching newly created folder
if (watchTree) { if (watchTree) {
try { try {
commitSet.addAll(listFiles(singleton(file), Integer.MAX_VALUE, false)); commitSet.addAll(listFiles(file));
watchFolder(file); watchFolder(file);
} catch (IOException e) { } catch (IOException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.getMessage(), e); Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.getMessage(), e);

View File

@ -751,7 +751,7 @@ public class MediaBindingBean {
if (mediaFile.isDirectory()) { if (mediaFile.isDirectory()) {
// just select the first video file in the folder as media sample // just select the first video file in the folder as media sample
SortedSet<File> videos = new TreeSet<File>(filter(listFiles(singleton(mediaFile), 2, false), VIDEO_FILES)); SortedSet<File> videos = new TreeSet<File>(filter(listFiles(mediaFile), VIDEO_FILES));
if (videos.size() > 0) { if (videos.size() > 0) {
return videos.iterator().next(); return videos.iterator().next();
} }

View File

@ -1076,7 +1076,7 @@ public class MediaDetection {
Set<Integer> collection = new LinkedHashSet<Integer>(); Set<Integer> collection = new LinkedHashSet<Integer>();
List<File> nfoFiles = new ArrayList<File>(); List<File> nfoFiles = new ArrayList<File>();
if (file.isDirectory()) { 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()) { } else if (file.getParentFile().isDirectory()) {
addAll(nfoFiles, file.getParentFile().listFiles(NFO_FILES)); addAll(nfoFiles, file.getParentFile().listFiles(NFO_FILES));
} }

View File

@ -1,7 +1,5 @@
package net.sourceforge.filebot.ui.list; package net.sourceforge.filebot.ui.list;
import static net.sourceforge.filebot.util.FileUtilities.*; import static net.sourceforge.filebot.util.FileUtilities.*;
import java.io.File; 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.ui.transfer.FileTransferablePolicy;
import net.sourceforge.filebot.util.FileUtilities; import net.sourceforge.filebot.util.FileUtilities;
class FileListTransferablePolicy extends FileTransferablePolicy { class FileListTransferablePolicy extends FileTransferablePolicy {
private FileBotList<? super String> list; private FileBotList<? super String> list;
public FileListTransferablePolicy(FileBotList<? super String> list) { public FileListTransferablePolicy(FileBotList<? super String> list) {
this.list = list; this.list = list;
} }
@Override @Override
protected boolean accept(List<File> files) { protected boolean accept(List<File> files) {
return true; return true;
} }
@Override @Override
protected void clear() { protected void clear() {
list.getModel().clear(); list.getModel().clear();
} }
@Override @Override
protected void load(List<File> files) throws IOException { protected void load(List<File> files) throws IOException {
// set title based on parent folder of first file // set title based on parent folder of first file
list.setTitle(FileUtilities.getFolderName(files.get(0).getParentFile())); list.setTitle(FileUtilities.getFolderName(files.get(0).getParentFile()));
// clear selection // clear selection
list.getListComponent().clearSelection(); list.getListComponent().clearSelection();
if (containsOnly(files, MediaTypes.getDefaultFilter("application/torrent"))) { if (containsOnly(files, MediaTypes.getDefaultFilter("application/torrent"))) {
loadTorrents(files); loadTorrents(files);
} else { } else {
@ -53,37 +46,35 @@ class FileListTransferablePolicy extends FileTransferablePolicy {
if (files.size() == 1 && files.get(0).isDirectory()) { if (files.size() == 1 && files.get(0).isDirectory()) {
list.setTitle(FileUtilities.getFolderName(files.get(0))); list.setTitle(FileUtilities.getFolderName(files.get(0)));
} }
// load all files from the given folders recursively up do a depth of 5 // load all files from the given folders recursively up do a depth of 32
for (File file : flatten(files, 5, false)) { for (File file : listFiles(files)) {
list.getModel().add(FileUtilities.getName(file)); list.getModel().add(FileUtilities.getName(file));
} }
} }
} }
private void loadTorrents(List<File> files) throws IOException { private void loadTorrents(List<File> files) throws IOException {
List<Torrent> torrents = new ArrayList<Torrent>(files.size()); List<Torrent> torrents = new ArrayList<Torrent>(files.size());
for (File file : files) { for (File file : files) {
torrents.add(new Torrent(file)); torrents.add(new Torrent(file));
} }
if (torrents.size() == 1) { if (torrents.size() == 1) {
list.setTitle(FileUtilities.getNameWithoutExtension(torrents.get(0).getName())); list.setTitle(FileUtilities.getNameWithoutExtension(torrents.get(0).getName()));
} }
for (Torrent torrent : torrents) { for (Torrent torrent : torrents) {
for (Torrent.Entry entry : torrent.getFiles()) { for (Torrent.Entry entry : torrent.getFiles()) {
list.getModel().add(FileUtilities.getNameWithoutExtension(entry.getName())); list.getModel().add(FileUtilities.getNameWithoutExtension(entry.getName()));
} }
} }
} }
@Override @Override
public String getFileFilterDescription() { public String getFileFilterDescription() {
return "files, folders and torrents"; return "files, folders and torrents";
} }
} }

View File

@ -95,8 +95,8 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
// torrent files // torrent files
loadTorrentFiles(files, values); loadTorrentFiles(files, values);
} else { } else {
// load all files from the given folders recursively up do a depth of 5 // load all files from the given folders recursively up do a depth of 32
values.addAll(FastFile.get(flatten(files, 5, false))); values.addAll(FastFile.get(listFiles(files)));
} }
model.addAll(values); model.addAll(values);

View File

@ -175,8 +175,7 @@ abstract class SubtitleDropTarget extends JButton {
final Collection<File> videoFiles = new TreeSet<File>(); final Collection<File> videoFiles = new TreeSet<File>();
// video files only // video files only
videoFiles.addAll(filter(input, VIDEO_FILES)); videoFiles.addAll(filter(listFiles(input), VIDEO_FILES));
videoFiles.addAll(filter(listFiles(filter(input, FOLDERS), 5, false), VIDEO_FILES));
if (videoFiles.size() > 0) { if (videoFiles.size() > 0) {
// invoke later so we don't block the DnD operation with the download dialog // 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<File> files = new TreeSet<File>(); final Collection<File> files = new TreeSet<File>();
// video files only // video files only
files.addAll(filter(input, FILES)); files.addAll(listFiles(input));
files.addAll(listFiles(filter(input, FOLDERS), 5, false));
final List<File> videos = filter(files, VIDEO_FILES); final List<File> videos = filter(files, VIDEO_FILES);
final List<File> subtitles = filter(files, SUBTITLE_FILES); final List<File> subtitles = filter(files, SUBTITLE_FILES);

View File

@ -1,5 +1,7 @@
package net.sourceforge.filebot.util; package net.sourceforge.filebot.util;
import static java.util.Arrays.*;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
@ -18,7 +20,6 @@ import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
@ -353,21 +354,6 @@ public final class FileUtilities {
return new NotFileFilter(filter); return new NotFileFilter(filter);
} }
public static List<File> flatten(Iterable<File> roots, int maxDepth, boolean listHiddenFiles) {
List<File> files = new ArrayList<File>();
// 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<File> listPath(File file) { public static List<File> listPath(File file) {
return listPathTail(file, Integer.MAX_VALUE, false); return listPathTail(file, Integer.MAX_VALUE, false);
} }
@ -398,25 +384,39 @@ public final class FileUtilities {
} }
public static List<File> listFiles(File... folders) { public static List<File> listFiles(File... folders) {
return listFiles(Arrays.asList(folders)); return listFiles(asList(folders));
} }
public static List<File> listFiles(Iterable<File> folders) { public static List<File> listFiles(Iterable<File> folders) {
return listFiles(folders, 255, false); return listFiles(folders, 32, false, true, false);
} }
public static List<File> listFiles(Iterable<File> folders, int maxDepth, boolean listHiddenFiles) { public static List<File> listFolders(Iterable<File> folders) {
return listFiles(folders, 32, false, false, true);
}
public static List<File> listFiles(Iterable<File> folders, int maxDepth, boolean addHidden, boolean addFiles, boolean addFolders) {
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
// collect files from directory tree // collect files from directory tree
for (File folder : folders) { for (File it : folders) {
listFiles(folder, 0, files, maxDepth, listHiddenFiles); 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; return files;
} }
private static void listFiles(File folder, int depth, List<File> files, int maxDepth, boolean listHiddenFiles) { private static void listFiles(File folder, List<File> files, int depth, int maxDepth, boolean addHidden, boolean addFiles, boolean addFolders) {
if (depth > maxDepth) if (depth > maxDepth)
return; return;
@ -424,14 +424,17 @@ public final class FileUtilities {
if (children == null) if (children == null)
return; return;
for (File file : children) { for (File it : children) {
if (!listHiddenFiles && file.isHidden()) // ignore hidden files if (!addHidden && it.isHidden()) // ignore hidden files
continue; continue;
if (file.isDirectory()) { if (it.isDirectory()) {
listFiles(file, depth + 1, files, maxDepth, listHiddenFiles); if (addFolders) {
} else { files.add(it);
files.add(file); }
listFiles(it, files, depth + 1, maxDepth, addHidden, addFiles, addFolders);
} else if (addFiles) {
files.add(it);
} }
} }
} }