From 54da732674288789b9a53c3cc91a603d42bc11c9 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 14 Nov 2014 14:30:38 +0000 Subject: [PATCH] * allow cancellation of all background tasks via clicking [X] on the window --- source/net/filebot/ui/SelectDialog.java | 16 ++++++++++------ .../filebot/ui/rename/EpisodeListMatcher.java | 5 +++++ .../net/filebot/ui/rename/MovieHashMatcher.java | 7 ++++++- source/net/filebot/ui/rename/RenamePanel.java | 7 ++++++- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/source/net/filebot/ui/SelectDialog.java b/source/net/filebot/ui/SelectDialog.java index d0abb2b1..179cdf19 100644 --- a/source/net/filebot/ui/SelectDialog.java +++ b/source/net/filebot/ui/SelectDialog.java @@ -29,11 +29,11 @@ import net.miginfocom.swing.MigLayout; public class SelectDialog extends JDialog { - private final JLabel headerLabel = new JLabel(); + private JLabel headerLabel = new JLabel(); - private final JList list; + private JList list; - private boolean valueSelected = false; + private Action selectedAction = null; public SelectDialog(Component parent, Collection options) { super(getWindow(parent), "Select", ModalityType.DOCUMENT_MODAL); @@ -86,9 +86,13 @@ public class SelectDialog extends JDialog { return headerLabel; } + public Action getSelectedAction() { + return selectedAction; + } + @SuppressWarnings("unchecked") public T getSelectedValue() { - if (!valueSelected) + if (selectedAction != selectAction) return null; return (T) list.getSelectedValue(); @@ -111,7 +115,7 @@ public class SelectDialog extends JDialog { @Override public void actionPerformed(ActionEvent e) { - valueSelected = true; + selectedAction = this; close(); } }; @@ -120,7 +124,7 @@ public class SelectDialog extends JDialog { @Override public void actionPerformed(ActionEvent e) { - valueSelected = false; + selectedAction = this; close(); } }; diff --git a/source/net/filebot/ui/rename/EpisodeListMatcher.java b/source/net/filebot/ui/rename/EpisodeListMatcher.java index 276061af..6049de40 100644 --- a/source/net/filebot/ui/rename/EpisodeListMatcher.java +++ b/source/net/filebot/ui/rename/EpisodeListMatcher.java @@ -27,6 +27,7 @@ import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; import java.util.concurrent.Callable; +import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -101,6 +102,10 @@ class EpisodeListMatcher implements AutoCompleteMatcher { prefs.put("dialog.select.w", Integer.toString(selectDialog.getWidth())); prefs.put("dialog.select.h", Integer.toString(selectDialog.getHeight())); + if (selectDialog.getSelectedAction() == null) { + throw new CancellationException("Cancelled by user"); + } + // selected value or null if the dialog was canceled by the user return selectDialog.getSelectedValue(); } diff --git a/source/net/filebot/ui/rename/MovieHashMatcher.java b/source/net/filebot/ui/rename/MovieHashMatcher.java index 591809f2..7a91cbad 100644 --- a/source/net/filebot/ui/rename/MovieHashMatcher.java +++ b/source/net/filebot/ui/rename/MovieHashMatcher.java @@ -29,6 +29,7 @@ import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; import java.util.concurrent.Callable; +import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -433,10 +434,14 @@ class MovieHashMatcher implements AutoCompleteMatcher { prefs.put("dialog.select.h", Integer.toString(selectDialog.getHeight())); // remember if we should auto-repeat the chosen action in the future - if (checkBox.isSelected()) { + if (checkBox.isSelected() || selectDialog.getSelectedAction() == null) { memory.put("repeat", selectDialog.getSelectedValue() != null ? "select" : "ignore"); } + if (selectDialog.getSelectedAction() == null) { + throw new CancellationException("Cancelled by user"); + } + // selected value or null if the dialog was canceled by the user return selectDialog.getSelectedValue(); } diff --git a/source/net/filebot/ui/rename/RenamePanel.java b/source/net/filebot/ui/rename/RenamePanel.java index f37750d6..781cd408 100644 --- a/source/net/filebot/ui/rename/RenamePanel.java +++ b/source/net/filebot/ui/rename/RenamePanel.java @@ -26,6 +26,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.concurrent.CancellationException; import java.util.logging.Level; import java.util.logging.Logger; @@ -699,7 +700,11 @@ public class RenamePanel extends JComponent { // add remaining file entries renameModel.files().addAll(remainingFiles); } catch (Exception e) { - UILogger.log(Level.WARNING, String.format("%s: %s", getRootCause(e).getClass().getSimpleName(), getRootCauseMessage(e)), e); + if (findCause(e, CancellationException.class) != null) { + Logger.getLogger(RenamePanel.class.getName()).log(Level.WARNING, getRootCause(e).toString()); + } else { + UILogger.log(Level.WARNING, String.format("%s: %s", getRootCause(e).getClass().getSimpleName(), getRootCauseMessage(e)), e); + } } finally { // auto-match finished namesList.firePropertyChange(LOADING_PROPERTY, true, false);