From 24025d9cb62cb189fe57e20feb76c9d5b0a2a623 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 24 Sep 2009 16:44:56 +0000 Subject: [PATCH] * cancel/interrupt obsolete background formatters in EpisodeFormatDialog --- .../filebot/format/ExpressionFormat.lib.groovy | 6 ++++++ .../filebot/ui/panel/rename/EpisodeFormatDialog.java | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy b/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy index 008e4789..85b91be1 100644 --- a/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy +++ b/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy @@ -24,6 +24,12 @@ Number.metaClass.compareTo = { String other -> delegate.toString().compareTo(oth String.metaClass.pad = Number.metaClass.pad = { length = 2, padding = "0" -> delegate.toString().padLeft(length, padding) } +/** + * Use empty string as default replacement. + */ +String.metaClass.replaceAll = { String pattern -> replaceAll(pattern, "") } + + /** * Replace space characters with a given characters. * diff --git a/source/net/sourceforge/filebot/ui/panel/rename/EpisodeFormatDialog.java b/source/net/sourceforge/filebot/ui/panel/rename/EpisodeFormatDialog.java index a2ac1a04..50f170c2 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/EpisodeFormatDialog.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/EpisodeFormatDialog.java @@ -21,7 +21,9 @@ import java.util.List; import java.util.ResourceBundle; import java.util.Set; import java.util.TreeMap; +import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.RunnableFuture; import java.util.concurrent.ThreadPoolExecutor; @@ -350,6 +352,9 @@ class EpisodeFormatDialog extends JDialog { } }); + // cancel old worker later + Future obsoletePreviewFuture = currentPreviewFuture; + // create new worker currentPreviewFuture = new SwingWorker() { @@ -376,6 +381,8 @@ class EpisodeFormatDialog extends JDialog { // no warning or error status.setVisible(false); + } catch (CancellationException e) { + // ignore, cancelled tasks are obsolete anyway } catch (Exception e) { status.setText(ExceptionUtilities.getMessage(e)); status.setIcon(ResourceManager.getIcon("status.warning")); @@ -395,6 +402,11 @@ class EpisodeFormatDialog extends JDialog { } }; + // cancel old worker, after new worker has been created, because done() might be called from within cancel() + if (obsoletePreviewFuture != null) { + obsoletePreviewFuture.cancel(true); + } + // submit new worker executor.execute(currentPreviewFuture); } catch (ScriptException e) {