* AnalyzePanel performance improvement, at least felt performance ;)

* some refactoring
This commit is contained in:
Reinhard Pointner 2008-03-16 14:00:10 +00:00
parent 1541066280
commit 0f102d4eb6
21 changed files with 228 additions and 200 deletions

View File

@ -2,6 +2,8 @@
package net.sourceforge.filebot;
import java.util.Iterator;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
@ -46,4 +48,19 @@ public class FileBotUtil {
return t;
}
public static String join(Iterable<?> list, String delim) {
StringBuilder sb = new StringBuilder();
Iterator<?> it = list.iterator();
while (it.hasNext()) {
sb.append(it.next().toString());
if (it.hasNext())
sb.append(delim);
}
return sb.toString();
}
}

View File

@ -28,9 +28,9 @@ import net.sourceforge.filebot.ui.transfer.ImportHandler;
import net.sourceforge.filebot.ui.transfer.Saveable;
import net.sourceforge.filebot.ui.transfer.SaveableExportHandler;
import net.sourceforge.filebot.ui.transfer.TransferablePolicyImportHandler;
import net.sourceforge.filebot.ui.transfer.TransferablePolicySupport;
import net.sourceforge.filebot.ui.transferablepolicies.NullTransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicySupport;
import net.sourceforge.tuned.ui.FancyListCellRenderer;
import net.sourceforge.tuned.ui.SimpleListModel;

View File

@ -26,9 +26,9 @@ import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import net.sourceforge.filebot.resources.ResourceManager;
import net.sourceforge.filebot.ui.transfer.TransferablePolicySupport;
import net.sourceforge.filebot.ui.transferablepolicies.NullTransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicySupport;
public class FileBotTree extends JTree implements TransferablePolicySupport {
@ -49,7 +49,8 @@ public class FileBotTree extends JTree implements TransferablePolicySupport {
public void clear() {
setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));
DefaultMutableTreeNode root = (DefaultMutableTreeNode) getModel().getRoot();
root.removeAllChildren();
}

View File

@ -6,7 +6,10 @@ import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
@ -14,7 +17,6 @@ import net.sourceforge.filebot.ui.FileBotTree;
import net.sourceforge.filebot.ui.transfer.DefaultTransferHandler;
import net.sourceforge.filebot.ui.transfer.FileTransferable;
import net.sourceforge.filebot.ui.transfer.TransferablePolicyImportHandler;
import net.sourceforge.filebot.ui.transferablepolicies.BackgroundFileTransferablePolicy;
public class FileTree extends FileBotTree {
@ -22,9 +24,14 @@ public class FileTree extends FileBotTree {
public static final String LOADING_PROPERTY = "loading";
public static final String CONTENT_PROPERTY = "content";
private PostProcessor postProcessor;
public FileTree() {
setTransferablePolicy(new FileTreeTransferPolicy());
FileTreeTransferPolicy transferPolicy = new FileTreeTransferPolicy((DefaultMutableTreeNode) getModel().getRoot());
transferPolicy.addPropertyChangeListener(LOADING_PROPERTY, new LoadingPropertyChangeListener());
setTransferablePolicy(transferPolicy);
setTransferHandler(new DefaultTransferHandler(new TransferablePolicyImportHandler(this), null));
}
@ -50,94 +57,73 @@ public class FileTree extends FileBotTree {
}
private void contentChanged() {
List<File> files = convertToList();
firePropertyChange(CONTENT_PROPERTY, null, files);
}
@Override
public void clear() {
((BackgroundFileTransferablePolicy<?>) getTransferablePolicy()).cancelAll();
FileTreeTransferPolicy transferPolicy = ((FileTreeTransferPolicy) getTransferablePolicy());
boolean loading = transferPolicy.isActive();
if (loading) {
transferPolicy.cancelAll();
}
super.clear();
contentChanged();
if (!loading) {
contentChanged();
}
// else, contentChanged() will be called after when loading is finished
}
private class FileTreeTransferPolicy extends BackgroundFileTransferablePolicy<DefaultMutableTreeNode> implements PropertyChangeListener {
public FileTreeTransferPolicy() {
addPropertyChangeListener(LOADING_PROPERTY, this);
}
@Override
protected boolean accept(File file) {
return file.isFile() || file.isDirectory();
}
@Override
protected void clear() {
FileTree.this.clear();
}
@Override
protected void process(List<DefaultMutableTreeNode> chunks) {
DefaultMutableTreeNode root = (DefaultMutableTreeNode) getModel().getRoot();
private void contentChanged() {
synchronized (this) {
if (postProcessor != null)
postProcessor.cancel(false);
for (DefaultMutableTreeNode node : chunks) {
root.add(node);
postProcessor = new PostProcessor();
postProcessor.execute();
}
};
private class LoadingPropertyChangeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Boolean loading = (Boolean) evt.getNewValue();
if (loading) {
firePropertyChange(FileTree.LOADING_PROPERTY, null, true);
} else {
contentChanged();
}
}
}
private class PostProcessor extends SwingWorker<List<File>, Object> {
@Override
protected List<File> doInBackground() throws Exception {
return convertToList();
}
@Override
protected void done() {
if (isCancelled())
return;
try {
List<File> files = get();
FileTree.this.firePropertyChange(CONTENT_PROPERTY, null, files);
} catch (Exception e) {
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e);
}
FileTree.this.firePropertyChange(FileTree.LOADING_PROPERTY, null, false);
updateUI();
}
@Override
protected void load(List<File> files) {
for (File file : files) {
publish(getTree(file));
}
}
private DefaultMutableTreeNode getTree(File file) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
if (file.isDirectory() && !Thread.currentThread().isInterrupted()) {
// run through file tree
for (File f : file.listFiles()) {
node.add(getTree(f));
}
}
return node;
}
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName() == BackgroundFileTransferablePolicy.LOADING_PROPERTY) {
Boolean loading = (Boolean) evt.getNewValue();
if (loading) {
FileTree.this.firePropertyChange(FileTree.LOADING_PROPERTY, null, true);
} else {
FileTree.this.firePropertyChange(FileTree.LOADING_PROPERTY, null, false);
contentChanged();
}
}
}
@Override
public String getDescription() {
return "files and folders";
}
}
}

View File

@ -0,0 +1,70 @@
package net.sourceforge.filebot.ui.panel.analyze;
import java.io.File;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
import net.sourceforge.filebot.ui.transferablepolicies.BackgroundFileTransferablePolicy;
class FileTreeTransferPolicy extends BackgroundFileTransferablePolicy<DefaultMutableTreeNode> {
DefaultMutableTreeNode root;
public FileTreeTransferPolicy(DefaultMutableTreeNode root) {
this.root = root;
}
@Override
protected boolean accept(File file) {
return file.isFile() || file.isDirectory();
}
@Override
protected void clear() {
root.removeAllChildren();
}
@Override
protected void process(List<DefaultMutableTreeNode> chunks) {
for (DefaultMutableTreeNode node : chunks) {
root.add(node);
}
}
@Override
protected void load(List<File> files) {
for (File file : files) {
publish(getTree(file));
}
}
private DefaultMutableTreeNode getTree(File file) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
if (file.isDirectory() && !Thread.currentThread().isInterrupted()) {
// run through file tree
for (File f : file.listFiles()) {
node.add(getTree(f));
}
}
return node;
}
@Override
public String getDescription() {
return "files and folders";
}
}

View File

@ -13,7 +13,7 @@ import net.sourceforge.filebot.ui.FileBotList;
import net.sourceforge.filebot.ui.transferablepolicies.FileTransferablePolicy;
public class FileListTransferablePolicy extends FileTransferablePolicy {
class FileListTransferablePolicy extends FileTransferablePolicy {
private FileBotList list;

View File

@ -10,12 +10,12 @@ import net.sourceforge.filebot.ui.transferablepolicies.FileTransferablePolicy;
import net.sourceforge.tuned.ui.SimpleListModel;
public class FilesRenameListTransferablePolicy extends FileTransferablePolicy {
class FilesListTransferablePolicy extends FileTransferablePolicy {
private SimpleListModel listModel;
public FilesRenameListTransferablePolicy(SimpleListModel listModel) {
public FilesListTransferablePolicy(SimpleListModel listModel) {
this.listModel = listModel;
}

View File

@ -11,7 +11,7 @@ public class FilesRenameList extends RenameList {
public FilesRenameList() {
setTitle("Files");
setTransferablePolicy(new FilesRenameListTransferablePolicy(getModel()));
setTransferablePolicy(new FilesListTransferablePolicy(getModel()));
}

View File

@ -16,23 +16,23 @@ import net.sourceforge.filebot.ui.panel.rename.entry.TorrentEntry;
import net.sourceforge.filebot.ui.transferablepolicies.FileTransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.MultiTransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TextTransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicy;
import net.sourceforge.tuned.ui.SimpleListModel;
public class NamesRenameListTransferablePolicy extends MultiTransferablePolicy {
class NamesListTransferablePolicy extends MultiTransferablePolicy {
private SimpleListModel listModel;
public NamesRenameListTransferablePolicy(SimpleListModel listModel) {
public NamesListTransferablePolicy(SimpleListModel listModel) {
this.listModel = listModel;
addPolicy(filePolicy);
addPolicy(textPolicy);
addPolicy(new FilePolicy());
addPolicy(new TextPolicy());
}
private TransferablePolicy filePolicy = new FileTransferablePolicy() {
private class FilePolicy extends FileTransferablePolicy {
private long MAX_FILESIZE = 10 * FileFormat.MEGA;
@ -83,7 +83,8 @@ public class NamesRenameListTransferablePolicy extends MultiTransferablePolicy {
};
private TransferablePolicy textPolicy = new TextTransferablePolicy() {
private class TextPolicy extends TextTransferablePolicy {
@Override
protected boolean load(String text) {
@ -107,7 +108,7 @@ public class NamesRenameListTransferablePolicy extends MultiTransferablePolicy {
@Override
public String getDescription() {
return "Lines of text";
return "lines of text";
}
};

View File

@ -11,7 +11,7 @@ public class NamesRenameList extends RenameList {
public NamesRenameList() {
setTitle("Names");
setTransferablePolicy(new NamesRenameListTransferablePolicy(this.getModel()));
setTransferablePolicy(new NamesListTransferablePolicy(this.getModel()));
}

View File

@ -28,10 +28,10 @@ import net.sourceforge.filebot.ui.transfer.ImportHandler;
import net.sourceforge.filebot.ui.transfer.Saveable;
import net.sourceforge.filebot.ui.transfer.SaveableExportHandler;
import net.sourceforge.filebot.ui.transfer.TransferablePolicyImportHandler;
import net.sourceforge.filebot.ui.transfer.TransferablePolicySupport;
import net.sourceforge.filebot.ui.transferablepolicies.BackgroundFileTransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.NullTransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicySupport;
public class SfvTable extends JTable implements TransferablePolicySupport, Saveable {

View File

@ -17,7 +17,7 @@ import net.sourceforge.filebot.FileFormat;
import net.sourceforge.filebot.ui.transferablepolicies.BackgroundFileTransferablePolicy;
public class SfvTransferablePolicy extends BackgroundFileTransferablePolicy<SfvTableModel.Entry> {
class SfvTransferablePolicy extends BackgroundFileTransferablePolicy<SfvTableModel.Entry> {
private SfvTableModel tableModel;

View File

@ -9,7 +9,6 @@ import javax.swing.JFileChooser;
import net.sourceforge.filebot.resources.ResourceManager;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicySupport;
public class LoadAction extends AbstractAction {

View File

@ -10,7 +10,6 @@ import java.util.logging.Logger;
import javax.swing.TransferHandler;
import javax.swing.TransferHandler.TransferSupport;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicySupport;
public class TransferablePolicyImportHandler implements ImportHandler {

View File

@ -1,5 +1,8 @@
package net.sourceforge.filebot.ui.transferablepolicies;
package net.sourceforge.filebot.ui.transfer;
import net.sourceforge.filebot.ui.transferablepolicies.TransferablePolicy;
public interface TransferablePolicySupport {

View File

@ -3,6 +3,8 @@ package net.sourceforge.filebot.ui.transferablepolicies;
import java.awt.datatransfer.Transferable;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.util.Arrays;
import java.util.List;
@ -47,7 +49,12 @@ public abstract class BackgroundFileTransferablePolicy<V> extends FileTransferab
public boolean isActive() {
return executor.isActive();
synchronized (this) {
if (executor == null)
return false;
return executor.isActive();
}
}
@ -169,4 +176,25 @@ public abstract class BackgroundFileTransferablePolicy<V> extends FileTransferab
}
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
}
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
}

View File

@ -6,6 +6,7 @@ import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
@ -17,13 +18,10 @@ import java.util.logging.Logger;
import net.sourceforge.filebot.ui.transfer.FileTransferable;
public abstract class FileTransferablePolicy extends TransferablePolicy {
public abstract class FileTransferablePolicy implements TransferablePolicy {
@Override
public boolean accept(Transferable tr) {
if (!isEnabled())
return false;
List<File> files = getFilesFromTransferable(tr);
if ((files == null) || files.isEmpty())
@ -37,8 +35,10 @@ public abstract class FileTransferablePolicy extends TransferablePolicy {
protected List<File> getFilesFromTransferable(Transferable tr) {
try {
if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
// file list flavor
return (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
} else if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) {
// file uri list flavor
String transferString = (String) tr.getTransferData(FileTransferable.uriListFlavor);
String lines[] = transferString.split("\r?\n");
@ -53,10 +53,12 @@ public abstract class FileTransferablePolicy extends TransferablePolicy {
try {
File file = new File(new URI(line));
if (file.exists())
files.add(file);
if (!file.exists())
throw new FileNotFoundException(file.toString());
files.add(file);
} catch (Exception e) {
// URISyntaxException, IllegalArgumentException
// URISyntaxException, IllegalArgumentException, FileNotFoundException
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.WARNING, "Invalid file url: " + line);
}
}

View File

@ -3,15 +3,16 @@ package net.sourceforge.filebot.ui.transferablepolicies;
import java.awt.datatransfer.Transferable;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import java.util.List;
import net.sourceforge.filebot.FileBotUtil;
public class MultiTransferablePolicy extends TransferablePolicy {
public class MultiTransferablePolicy implements TransferablePolicy {
private ArrayList<TransferablePolicy> policies = new ArrayList<TransferablePolicy>();
private List<TransferablePolicy> policies = Collections.synchronizedList(new ArrayList<TransferablePolicy>());
public MultiTransferablePolicy() {
@ -21,21 +22,16 @@ public class MultiTransferablePolicy extends TransferablePolicy {
public void addPolicy(TransferablePolicy policy) {
policies.add(policy);
policy.addPropertyChangeListener(relayListener);
}
public void removePolicy(TransferablePolicy policy) {
policy.removePropertyChangeListener(relayListener);
policies.remove(policy);
}
@Override
public boolean accept(Transferable tr) {
if (!isEnabled())
return false;
for (TransferablePolicy policy : policies) {
if (policy.accept(tr))
return true;
@ -55,14 +51,7 @@ public class MultiTransferablePolicy extends TransferablePolicy {
}
}
private final PropertyChangeListener relayListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
firePropertyChange(evt);
}
};
@Override
public String getDescription() {
return getDescription(TransferablePolicy.class);
@ -70,9 +59,7 @@ public class MultiTransferablePolicy extends TransferablePolicy {
public String getDescription(Class<? extends TransferablePolicy> filter) {
StringBuffer sb = new StringBuffer();
ArrayList<String> descriptions = new ArrayList<String>();
List<String> descriptions = new ArrayList<String>();
for (TransferablePolicy policy : policies) {
String desc = policy.getDescription();
@ -81,17 +68,7 @@ public class MultiTransferablePolicy extends TransferablePolicy {
descriptions.add(desc);
}
Iterator<String> it = descriptions.iterator();
while (it.hasNext()) {
String desc = it.next();
sb.append(desc);
if (it.hasNext())
sb.append(", ");
}
return sb.toString();
return FileBotUtil.join(descriptions, ", ");
}
}

View File

@ -5,7 +5,7 @@ package net.sourceforge.filebot.ui.transferablepolicies;
import java.awt.datatransfer.Transferable;
public class NullTransferablePolicy extends TransferablePolicy {
public class NullTransferablePolicy implements TransferablePolicy {
@Override
public boolean accept(Transferable tr) {

View File

@ -8,13 +8,10 @@ import java.util.logging.Level;
import java.util.logging.Logger;
public abstract class TextTransferablePolicy extends TransferablePolicy {
public abstract class TextTransferablePolicy implements TransferablePolicy {
@Override
public boolean accept(Transferable tr) {
if (!isEnabled())
return false;
return tr.isDataFlavorSupported(DataFlavor.stringFlavor);
}

View File

@ -3,68 +3,16 @@ package net.sourceforge.filebot.ui.transferablepolicies;
import java.awt.datatransfer.Transferable;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public abstract class TransferablePolicy {
public interface TransferablePolicy {
public abstract boolean accept(Transferable tr);
public boolean accept(Transferable tr);
public abstract void handleTransferable(Transferable tr, boolean add);
private boolean enabled = true;
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
public static final String ENABLED_PROPERTY = "enabled";
public abstract String getDescription();
public void handleTransferable(Transferable tr, boolean add);
//TODO remove enabled stuff
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
firePropertyChange(ENABLED_PROPERTY, null, enabled);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
}
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
protected void firePropertyChange(PropertyChangeEvent evt) {
propertyChangeSupport.firePropertyChange(evt);
}
public String getDescription();
}