From 98633f73647707a279ff08033f2627fcc4aa630a Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 27 Jul 2014 18:23:45 +0000 Subject: [PATCH] * support dropping of large / slow folders into Rename panel (Files list) --- .../ui/episodelist/SeasonSpinnerEditor.java | 1 - .../rename/FilesListTransferablePolicy.java | 18 +++++-- .../ui/rename/RenameListCellRenderer.java | 4 ++ source/net/filebot/ui/rename/RenamePanel.java | 14 ++++- .../BackgroundFileTransferablePolicy.java | 51 ++++++------------- .../ui/transfer/FileTransferablePolicy.java | 32 +++++------- 6 files changed, 59 insertions(+), 61 deletions(-) diff --git a/source/net/filebot/ui/episodelist/SeasonSpinnerEditor.java b/source/net/filebot/ui/episodelist/SeasonSpinnerEditor.java index a3313b8d..2da38a35 100644 --- a/source/net/filebot/ui/episodelist/SeasonSpinnerEditor.java +++ b/source/net/filebot/ui/episodelist/SeasonSpinnerEditor.java @@ -7,7 +7,6 @@ import java.text.ParseException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.swing.BorderFactory; import javax.swing.JSpinner; import javax.swing.JSpinner.DefaultEditor; import javax.swing.SwingConstants; diff --git a/source/net/filebot/ui/rename/FilesListTransferablePolicy.java b/source/net/filebot/ui/rename/FilesListTransferablePolicy.java index 1a0493f6..58375d86 100644 --- a/source/net/filebot/ui/rename/FilesListTransferablePolicy.java +++ b/source/net/filebot/ui/rename/FilesListTransferablePolicy.java @@ -1,6 +1,7 @@ package net.filebot.ui.rename; import static net.filebot.MediaTypes.*; +import static net.filebot.ui.NotificationLogging.*; import static net.filebot.ui.transfer.FileTransferable.*; import static net.filebot.util.FileUtilities.*; @@ -14,10 +15,11 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.filebot.media.MediaDetection; -import net.filebot.ui.transfer.FileTransferablePolicy; +import net.filebot.ui.transfer.BackgroundFileTransferablePolicy; +import net.filebot.util.ExceptionUtilities; import net.filebot.util.FastFile; -class FilesListTransferablePolicy extends FileTransferablePolicy { +class FilesListTransferablePolicy extends BackgroundFileTransferablePolicy { private final List model; @@ -93,7 +95,7 @@ class FilesListTransferablePolicy extends FileTransferablePolicy { } } - model.addAll(FastFile.create(entries)); + publish(FastFile.create(entries).toArray(new File[0])); } @Override @@ -101,4 +103,14 @@ class FilesListTransferablePolicy extends FileTransferablePolicy { return "files and folders"; } + @Override + protected void process(List chunks) { + model.addAll(FastFile.create(chunks)); + } + + @Override + protected void process(Exception e) { + UILogger.log(Level.WARNING, ExceptionUtilities.getRootCauseMessage(e), e); + } + } diff --git a/source/net/filebot/ui/rename/RenameListCellRenderer.java b/source/net/filebot/ui/rename/RenameListCellRenderer.java index ca56bafd..c8b12a6d 100644 --- a/source/net/filebot/ui/rename/RenameListCellRenderer.java +++ b/source/net/filebot/ui/rename/RenameListCellRenderer.java @@ -179,6 +179,10 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer { } protected float getMatchProbablity(Match match) { + if (match.getValue() == null || match.getCandidate() == null) { + return 1; // assume match is ok + } + if (match.getValue() instanceof Episode) { float f = verificationMetric().getSimilarity(match.getValue(), match.getCandidate()); return (f + 1) / 2; // normalize -1..1 to 0..1 diff --git a/source/net/filebot/ui/rename/RenamePanel.java b/source/net/filebot/ui/rename/RenamePanel.java index 8c51cf18..4b504ddd 100644 --- a/source/net/filebot/ui/rename/RenamePanel.java +++ b/source/net/filebot/ui/rename/RenamePanel.java @@ -58,6 +58,7 @@ import net.filebot.media.MediaDetection; import net.filebot.similarity.Match; import net.filebot.ui.rename.FormatDialog.Mode; import net.filebot.ui.rename.RenameModel.FormattedFuture; +import net.filebot.ui.transfer.BackgroundFileTransferablePolicy; import net.filebot.util.PreferencesMap.PreferencesEntry; import net.filebot.util.ui.ActionPopup; import net.filebot.util.ui.LoadingOverlayPane; @@ -287,7 +288,18 @@ public class RenamePanel extends JComponent { }); setLayout(new MigLayout("fill, insets dialog, gapx 10px", "[fill][align center, pref!][fill]", "align 33%")); - add(filesList, "grow, sizegroupx list"); + add(new LoadingOverlayPane(filesList, filesList, "32px", "30px"), "grow, sizegroupx list"); + + BackgroundFileTransferablePolicy transferablePolicy = (BackgroundFileTransferablePolicy) filesList.getTransferablePolicy(); + transferablePolicy.addPropertyChangeListener(new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (BackgroundFileTransferablePolicy.LOADING_PROPERTY.equals(evt.getPropertyName())) { + filesList.firePropertyChange(LoadingOverlayPane.LOADING_PROPERTY, (boolean) evt.getOldValue(), (boolean) evt.getNewValue()); + } + } + }); // make buttons larger matchButton.setMargin(new Insets(3, 14, 2, 14)); diff --git a/source/net/filebot/ui/transfer/BackgroundFileTransferablePolicy.java b/source/net/filebot/ui/transfer/BackgroundFileTransferablePolicy.java index 4038b605..4c55e788 100644 --- a/source/net/filebot/ui/transfer/BackgroundFileTransferablePolicy.java +++ b/source/net/filebot/ui/transfer/BackgroundFileTransferablePolicy.java @@ -1,7 +1,5 @@ - package net.filebot.ui.transfer; - import static net.filebot.ui.transfer.FileTransferable.*; import java.awt.datatransfer.Transferable; @@ -15,42 +13,37 @@ import javax.swing.SwingUtilities; import javax.swing.SwingWorker; import javax.swing.event.SwingPropertyChangeSupport; - public abstract class BackgroundFileTransferablePolicy extends FileTransferablePolicy { - + public static final String LOADING_PROPERTY = "loading"; - + private final ThreadLocal threadLocalWorker = new ThreadLocal(); - + private final List workers = new ArrayList(2); - @Override public void handleTransferable(Transferable tr, TransferAction action) throws Exception { List files = getFilesFromTransferable(tr); - + if (action != TransferAction.ADD) { clear(); } - + prepare(files); - + // create and start worker new BackgroundWorker(files).execute(); } - protected void prepare(List files) { - + } - @Override protected void clear() { // stop other workers on clear (before starting new worker) reset(); } - public void reset() { synchronized (workers) { @@ -63,52 +56,45 @@ public abstract class BackgroundFileTransferablePolicy extends FileTransferab } } } - public boolean isLoading() { synchronized (workers) { return !workers.isEmpty(); } } - protected abstract void process(List chunks); - protected abstract void process(Exception exception); - protected final void publish(V... chunks) { BackgroundWorker worker = threadLocalWorker.get(); - + if (worker == null) { // fail if a non-background-worker thread is trying to access the thread-local worker object throw new IllegalThreadStateException("Illegal access thread"); } - + worker.offer(chunks); } - protected final void publish(final Exception exception) { SwingUtilities.invokeLater(new Runnable() { - + @Override public void run() { process(exception); } }); } - protected class BackgroundWorker extends SwingWorker { - + private final List files; - public BackgroundWorker(List files) { this.files = files; - + // register this worker synchronized (workers) { if (workers.add(this) && workers.size() == 1) { @@ -116,29 +102,26 @@ public abstract class BackgroundFileTransferablePolicy extends FileTransferab } } } - @Override protected Object doInBackground() throws Exception { // associate this worker with the current (background) thread threadLocalWorker.set(this); - + try { load(files); } finally { threadLocalWorker.remove(); } - + return null; } - public void offer(V... chunks) { if (!isCancelled()) { publish(chunks); } } - @Override protected void process(List chunks) { @@ -146,7 +129,6 @@ public abstract class BackgroundFileTransferablePolicy extends FileTransferab BackgroundFileTransferablePolicy.this.process(chunks); } } - @Override protected void done() { @@ -158,7 +140,7 @@ public abstract class BackgroundFileTransferablePolicy extends FileTransferab BackgroundFileTransferablePolicy.this.process(e); } } - + // unregister worker synchronized (workers) { if (workers.remove(this) && workers.isEmpty()) { @@ -167,15 +149,12 @@ public abstract class BackgroundFileTransferablePolicy extends FileTransferab } } } - protected final PropertyChangeSupport swingPropertyChangeSupport = new SwingPropertyChangeSupport(this, true); - public void addPropertyChangeListener(PropertyChangeListener listener) { swingPropertyChangeSupport.addPropertyChangeListener(listener); } - public void removePropertyChangeListener(PropertyChangeListener listener) { swingPropertyChangeSupport.removePropertyChangeListener(listener); diff --git a/source/net/filebot/ui/transfer/FileTransferablePolicy.java b/source/net/filebot/ui/transfer/FileTransferablePolicy.java index 2a3376c5..44c7e4ea 100644 --- a/source/net/filebot/ui/transfer/FileTransferablePolicy.java +++ b/source/net/filebot/ui/transfer/FileTransferablePolicy.java @@ -1,7 +1,5 @@ - package net.filebot.ui.transfer; - import static net.filebot.ui.transfer.FileTransferable.*; import static net.filebot.util.FileUtilities.*; @@ -11,51 +9,45 @@ import java.io.File; import java.io.IOException; import java.util.List; - public abstract class FileTransferablePolicy extends TransferablePolicy { - + @Override public boolean accept(Transferable tr) throws Exception { try { List files = getFilesFromTransferable(tr); - + // ignore temporary files (may not work on all platforms since the DnD data may not be accessible during the drag) if (files != null && files.size() > 0 && containsOnly(files, TEMPORARY)) { return false; } - + return accept(files); } catch (UnsupportedFlavorException e) { // no file list flavor } - + return false; } - - + @Override public void handleTransferable(Transferable tr, TransferAction action) throws Exception { List files = getFilesFromTransferable(tr); - + if (action == TransferAction.PUT) { clear(); } - + load(files); } - - + protected abstract boolean accept(List files); - - + protected abstract void load(List files) throws IOException; - - + protected abstract void clear(); - - + public String getFileFilterDescription() { return null; } - + }