* 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());
}
// 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;

View File

@ -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);

View File

@ -751,7 +751,7 @@ public class MediaBindingBean {
if (mediaFile.isDirectory()) {
// 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) {
return videos.iterator().next();
}

View File

@ -1076,7 +1076,7 @@ public class MediaDetection {
Set<Integer> collection = new LinkedHashSet<Integer>();
List<File> nfoFiles = new ArrayList<File>();
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));
}

View File

@ -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<? super String> list;
public FileListTransferablePolicy(FileBotList<? super String> list) {
this.list = list;
}
@Override
protected boolean accept(List<File> files) {
return true;
}
@Override
protected void clear() {
list.getModel().clear();
}
@Override
protected void load(List<File> 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<File> files) throws IOException {
List<Torrent> torrents = new ArrayList<Torrent>(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";
}
}

View File

@ -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);

View File

@ -175,8 +175,7 @@ abstract class SubtitleDropTarget extends JButton {
final Collection<File> videoFiles = new TreeSet<File>();
// 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<File> files = new TreeSet<File>();
// video files only
files.addAll(filter(input, FILES));
files.addAll(listFiles(filter(input, FOLDERS), 5, false));
files.addAll(listFiles(input));
final List<File> videos = filter(files, VIDEO_FILES);
final List<File> subtitles = filter(files, SUBTITLE_FILES);

View File

@ -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<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) {
return listPathTail(file, Integer.MAX_VALUE, false);
}
@ -398,25 +384,39 @@ public final class FileUtilities {
}
public static List<File> listFiles(File... folders) {
return listFiles(Arrays.asList(folders));
return listFiles(asList(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>();
// 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<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)
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);
}
}
}