* improved tool panel

This commit is contained in:
Reinhard Pointner 2008-03-24 18:08:41 +00:00
parent a1b118d0f6
commit 683c5e4cee
6 changed files with 151 additions and 112 deletions

View File

@ -31,14 +31,6 @@ public class FileFormat {
}
public static String formatNumberOfFiles(int n) {
if (n == 1)
return n + " file";
else
return n + " files";
}
public static boolean hasExtension(File file, String... extensions) {
if (file.isDirectory())
return false;

View File

@ -57,8 +57,8 @@ public class AnalyzePanel extends FileBotPanel {
fileTreePanel.setMinimumSize(min);
toolsPanel.setMinimumSize(min);
addTool(new SplitPanel());
addTool(new TypePanel());
addTool(new SplitPanel());
fileTreePanel.getFileTree().addPropertyChangeListener(FileTree.CONTENT_PROPERTY, fileTreeChangeListener);

View File

@ -3,6 +3,7 @@ package net.sourceforge.filebot.ui.panel.analyze;
import java.io.File;
import java.io.FileFilter;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
@ -54,8 +55,13 @@ class FileTreeTransferPolicy extends BackgroundFileTransferablePolicy<DefaultMut
DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
if (file.isDirectory() && !Thread.currentThread().isInterrupted()) {
// run through file tree
for (File f : file.listFiles()) {
// run through folders first
for (File f : file.listFiles(FOLDER_FILTER)) {
node.add(getTree(f));
}
// then files
for (File f : file.listFiles(FILE_FILTER)) {
node.add(getTree(f));
}
}
@ -69,4 +75,22 @@ class FileTreeTransferPolicy extends BackgroundFileTransferablePolicy<DefaultMut
return "files and folders";
}
private static final FileFilter FOLDER_FILTER = new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
};
private static final FileFilter FILE_FILTER = new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile();
}
};
}

View File

@ -6,7 +6,9 @@ import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -74,12 +76,10 @@ public class SplitPanel extends ToolPanel implements ChangeListener {
}
/**
* callback when splitsize has been changed
*/
public void stateChanged(ChangeEvent e) {
if (files != null)
if (fileChache != null) {
update();
}
}
@ -87,83 +87,94 @@ public class SplitPanel extends ToolPanel implements ChangeListener {
return spinnerModel.getNumber().intValue() * FileFormat.MEGA;
}
private UpdateTask latestUpdateTask;
private UpdateTask updateTask;
private Collection<File> files;
private Collection<File> fileChache;
@Override
public void update(Collection<File> files) {
this.files = files;
this.fileChache = files;
update();
}
private void update() {
latestUpdateTask = new UpdateTask();
private synchronized void update() {
if (updateTask != null) {
updateTask.cancel(false);
}
updateTask = new UpdateTask(fileChache);
tree.firePropertyChange(LoadingOverlayPane.LOADING_PROPERTY, false, true);
latestUpdateTask.execute();
updateTask.execute();
}
private class UpdateTask extends SwingWorker<DefaultTreeModel, Object> {
private boolean isLatest() {
if (this == latestUpdateTask)
return true;
else
return false;
}
private final Collection<File> files;
private void setLastChildUserObject(DefaultMutableTreeNode root, int part, long size) {
DefaultMutableTreeNode node = ((DefaultMutableTreeNode) root.getLastChild());
node.setUserObject(String.format("Part %d (%s)", part, FileFormat.formatSize(size)));
public UpdateTask(Collection<File> files) {
this.files = files;
}
@Override
protected DefaultTreeModel doInBackground() throws Exception {
long currentSize = 0;
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode first = new DefaultMutableTreeNode();
root.add(first);
DefaultMutableTreeNode remainder = new DefaultMutableTreeNode("Remainder");
List<List<File>> parts = new ArrayList<List<File>>();
List<File> remainder = new ArrayList<File>();
int p = 1;
long splitSize = getSplitSize();
for (File f : files) {
long fileSize = f.length();
DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode(f);
long currentSize = 0;
List<File> currentPart = null;
for (File file : files) {
long fileSize = file.length();
if (fileSize > splitSize)
remainder.add(fileNode);
else if (currentSize + fileSize <= splitSize) {
currentSize += fileSize;
((DefaultMutableTreeNode) root.getLastChild()).add(fileNode);
} else {
setLastChildUserObject(root, p, currentSize);
currentSize = fileSize;
p++;
DefaultMutableTreeNode node = new DefaultMutableTreeNode();
node.add(fileNode);
root.add(node);
if (fileSize > splitSize) {
remainder.add(file);
continue;
}
if (!isLatest())
if (currentSize + fileSize > splitSize) {
currentSize = 0;
currentPart = null;
}
if (currentPart == null) {
currentPart = new ArrayList<File>();
parts.add(currentPart);
}
currentSize += fileSize;
currentPart.add(file);
if (isCancelled()) {
return null;
}
}
setLastChildUserObject(root, p, currentSize);
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
if (!remainder.isLeaf())
root.add(remainder);
int count = 1;
if (first.isLeaf())
first.removeFromParent();
for (List<File> part : parts) {
root.add(createTreeNode(String.format("Part %d", count), part));
count++;
if (isCancelled()) {
return null;
}
}
if (!remainder.isEmpty()) {
root.add(createTreeNode("Remainder", remainder));
}
return new DefaultTreeModel(root);
}
@ -171,13 +182,12 @@ public class SplitPanel extends ToolPanel implements ChangeListener {
@Override
protected void done() {
if (isCancelled()) {
return;
}
try {
DefaultTreeModel model = get();
if (model == null)
return;
tree.setModel(model);
tree.setModel(get());
} catch (Exception e) {
// should not happen
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e);

View File

@ -6,6 +6,9 @@ import java.io.File;
import java.util.Collection;
import javax.swing.JComponent;
import javax.swing.tree.DefaultMutableTreeNode;
import net.sourceforge.filebot.FileFormat;
public abstract class ToolPanel extends JComponent {
@ -24,4 +27,28 @@ public abstract class ToolPanel extends JComponent {
public abstract void update(Collection<File> list);
protected static DefaultMutableTreeNode createTreeNode(String name, Collection<File> files) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode();
long totalSize = 0;
for (File file : files) {
node.add(new DefaultMutableTreeNode(file));
totalSize += file.length();
}
String count = null;
if (files.size() == 1) {
count = String.format("%d file", files.size());
} else {
count = String.format("%d files", files.size());
}
node.setUserObject(String.format("%s (%s, %s)", name, count, FileFormat.formatSize(totalSize)));
return node;
}
}

View File

@ -4,11 +4,12 @@ package net.sourceforge.filebot.ui.panel.analyze.tools;
import java.awt.BorderLayout;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -42,21 +43,25 @@ public class TypePanel extends ToolPanel {
tree.setDragEnabled(true);
}
private UpdateTask latestUpdateTask;
private UpdateTask updateTask = null;
@Override
public void update(Collection<File> files) {
latestUpdateTask = new UpdateTask(files);
public synchronized void update(Collection<File> files) {
if (updateTask != null) {
updateTask.cancel(false);
}
updateTask = new UpdateTask(files);
tree.firePropertyChange(LoadingOverlayPane.LOADING_PROPERTY, false, true);
latestUpdateTask.execute();
updateTask.execute();
}
private class UpdateTask extends SwingWorker<DefaultTreeModel, Object> {
private Collection<File> files;
private final Collection<File> files;
public UpdateTask(Collection<File> files) {
@ -64,54 +69,36 @@ public class TypePanel extends ToolPanel {
}
private boolean isLatest() {
if (this == latestUpdateTask)
return true;
else
return false;
}
@Override
protected DefaultTreeModel doInBackground() throws Exception {
Map<String, Collection<File>> map = new HashMap<String, Collection<File>>();
SortedMap<String, SortedSet<File>> map = new TreeMap<String, SortedSet<File>>();
for (File f : files) {
String extension = FileFormat.getExtension(f);
for (File file : files) {
String extension = FileFormat.getExtension(file);
Collection<File> list = map.get(extension);
SortedSet<File> set = map.get(extension);
if (list != null)
list.add(f);
else {
list = new ArrayList<File>();
list.add(f);
map.put(extension, list);
if (set == null) {
set = new TreeSet<File>();
map.put(extension, set);
}
if (!isLatest())
set.add(file);
if (isCancelled()) {
return null;
}
}
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
Iterator<String> i = map.keySet().iterator();
while (i.hasNext()) {
String key = i.next();
Collection<File> list = map.get(key);
DefaultMutableTreeNode node = new DefaultMutableTreeNode();
long size = 0;
for (Map.Entry<String, SortedSet<File>> entry : map.entrySet()) {
for (File f : list) {
node.add(new DefaultMutableTreeNode(f));
size += f.length();
}
root.add(createTreeNode(entry.getKey(), entry.getValue()));
node.setUserObject(key + " (" + FileFormat.formatNumberOfFiles(list.size()) + ", " + FileFormat.formatSize(size) + ")");
root.add(node);
if (!isLatest())
if (isCancelled()) {
return null;
}
}
return new DefaultTreeModel(root);
@ -120,13 +107,12 @@ public class TypePanel extends ToolPanel {
@Override
protected void done() {
if (isCancelled()) {
return;
}
try {
DefaultTreeModel model = get();
if (model == null)
return;
tree.setModel(model);
tree.setModel(get());
} catch (Exception e) {
// should not happen
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e);