From 683c5e4ceec75c7fae308e3e45cafd764e89985b Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 24 Mar 2008 18:08:41 +0000 Subject: [PATCH] * improved tool panel --- .../net/sourceforge/filebot/FileFormat.java | 8 -- .../ui/panel/analyze/AnalyzePanel.java | 2 +- .../panel/analyze/FileTreeTransferPolicy.java | 28 ++++- .../ui/panel/analyze/tools/SplitPanel.java | 118 ++++++++++-------- .../ui/panel/analyze/tools/ToolPanel.java | 27 ++++ .../ui/panel/analyze/tools/TypePanel.java | 80 +++++------- 6 files changed, 151 insertions(+), 112 deletions(-) diff --git a/source/net/sourceforge/filebot/FileFormat.java b/source/net/sourceforge/filebot/FileFormat.java index d931d10a..163abc25 100644 --- a/source/net/sourceforge/filebot/FileFormat.java +++ b/source/net/sourceforge/filebot/FileFormat.java @@ -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; diff --git a/source/net/sourceforge/filebot/ui/panel/analyze/AnalyzePanel.java b/source/net/sourceforge/filebot/ui/panel/analyze/AnalyzePanel.java index 9603a24c..75405086 100644 --- a/source/net/sourceforge/filebot/ui/panel/analyze/AnalyzePanel.java +++ b/source/net/sourceforge/filebot/ui/panel/analyze/AnalyzePanel.java @@ -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); diff --git a/source/net/sourceforge/filebot/ui/panel/analyze/FileTreeTransferPolicy.java b/source/net/sourceforge/filebot/ui/panel/analyze/FileTreeTransferPolicy.java index 401160c7..5765c281 100644 --- a/source/net/sourceforge/filebot/ui/panel/analyze/FileTreeTransferPolicy.java +++ b/source/net/sourceforge/filebot/ui/panel/analyze/FileTreeTransferPolicy.java @@ -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 files; + private Collection fileChache; @Override public void update(Collection 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 { - private boolean isLatest() { - if (this == latestUpdateTask) - return true; - else - return false; - } + private final Collection 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 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> parts = new ArrayList>(); + List remainder = new ArrayList(); - int p = 1; long splitSize = getSplitSize(); - for (File f : files) { - long fileSize = f.length(); - DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode(f); + long currentSize = 0; + List 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(); + 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 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); diff --git a/source/net/sourceforge/filebot/ui/panel/analyze/tools/ToolPanel.java b/source/net/sourceforge/filebot/ui/panel/analyze/tools/ToolPanel.java index 8e6ea42b..1124330f 100644 --- a/source/net/sourceforge/filebot/ui/panel/analyze/tools/ToolPanel.java +++ b/source/net/sourceforge/filebot/ui/panel/analyze/tools/ToolPanel.java @@ -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 list); + + + protected static DefaultMutableTreeNode createTreeNode(String name, Collection 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; + } } diff --git a/source/net/sourceforge/filebot/ui/panel/analyze/tools/TypePanel.java b/source/net/sourceforge/filebot/ui/panel/analyze/tools/TypePanel.java index 3f943849..ce19072f 100644 --- a/source/net/sourceforge/filebot/ui/panel/analyze/tools/TypePanel.java +++ b/source/net/sourceforge/filebot/ui/panel/analyze/tools/TypePanel.java @@ -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 files) { - latestUpdateTask = new UpdateTask(files); + public synchronized void update(Collection 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 { - private Collection files; + private final Collection files; public UpdateTask(Collection 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> map = new HashMap>(); + SortedMap> map = new TreeMap>(); - for (File f : files) { - String extension = FileFormat.getExtension(f); + for (File file : files) { + String extension = FileFormat.getExtension(file); - Collection list = map.get(extension); + SortedSet set = map.get(extension); - if (list != null) - list.add(f); - else { - list = new ArrayList(); - list.add(f); - map.put(extension, list); + if (set == null) { + set = new TreeSet(); + map.put(extension, set); } - if (!isLatest()) + set.add(file); + + if (isCancelled()) { return null; + } } DefaultMutableTreeNode root = new DefaultMutableTreeNode(); - Iterator i = map.keySet().iterator(); - while (i.hasNext()) { - String key = i.next(); - Collection list = map.get(key); - DefaultMutableTreeNode node = new DefaultMutableTreeNode(); - long size = 0; + for (Map.Entry> 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);