Skip to content

Commit c4de809

Browse files
committed
Cancel modules when preprocessors are canceled
If the preprocessor of a module was canceled, it was not causing the module itself to be canceled. For example, if the file chooser dialog was canceled during OpenImage execution, the OpenImage module was not actually marked as canceled. Now, if a preprocessor is canceled, we cancel the whole module. This should allow appropriate response to preprocessor cancelation.
1 parent c842420 commit c4de809

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/main/java/org/scijava/module/ModuleRunner.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,19 @@ public void run() {
156156
final ModulePreprocessor canceler = preProcess();
157157
if (canceler != null) {
158158
// module execution was canceled by preprocessor
159-
cancel(title, canceler.getCancelReason());
159+
final String reason = canceler.getCancelReason();
160+
cancel(reason);
161+
cleanupAndBroadcastCancelation(title, reason);
160162
return;
161163
}
162164

163165
// execute module
164166
if (es != null) es.publish(new ModuleExecutingEvent(module));
165167
module.run();
166-
if (module instanceof Cancelable) {
167-
final Cancelable cancelable = (Cancelable) module;
168-
if (cancelable.isCanceled()) {
169-
// module execution was canceled by the module itself
170-
cancel(title, cancelable.getCancelReason());
171-
return;
172-
}
168+
if (isCanceled()) {
169+
// module execution was canceled by the module itself
170+
cleanupAndBroadcastCancelation(title, getCancelReason());
171+
return;
173172
}
174173
if (es != null) es.publish(new ModuleExecutedEvent(module));
175174

@@ -183,7 +182,9 @@ public void run() {
183182

184183
// -- Helper methods --
185184

186-
private void cancel(final String title, final String reason) {
185+
private void cleanupAndBroadcastCancelation(final String title,
186+
final String reason)
187+
{
187188
if (ss != null) ss.showStatus("Canceling command: " + title);
188189
module.cancel();
189190
if (es != null) es.publish(new ModuleCanceledEvent(module, reason));
@@ -193,4 +194,18 @@ private void cancel(final String title, final String reason) {
193194
}
194195
}
195196

197+
private boolean isCanceled() {
198+
return module instanceof Cancelable && ((Cancelable) module).isCanceled();
199+
}
200+
201+
private String getCancelReason() {
202+
return module instanceof Cancelable ?
203+
((Cancelable) module).getCancelReason() : null;
204+
}
205+
206+
private void cancel(final String reason) {
207+
if (!(module instanceof Cancelable)) return;
208+
((Cancelable) module).cancel(reason);
209+
}
210+
196211
}

0 commit comments

Comments
 (0)