Skip to content

Commit 0bac953

Browse files
NicoKiaruhinerm
authored andcommitted
The task interface now contains a method to retrieve its cancel callback. Use case: adding a confirmation dialog before cancelling.
1 parent 9af7cb6 commit 0bac953

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/main/java/org/scijava/task/DefaultTask.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* %%
77
* Redistribution and use in source and binary forms, with or without
88
* modification, are permitted provided that the following conditions are met:
9-
*
9+
*
1010
* 1. Redistributions of source code must retain the above copyright notice,
1111
* this list of conditions and the following disclaimer.
1212
* 2. Redistributions in binary form must reproduce the above copyright notice,
1313
* this list of conditions and the following disclaimer in the documentation
1414
* and/or other materials provided with the distribution.
15-
*
15+
*
1616
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1717
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1818
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -84,18 +84,19 @@ public class DefaultTask implements Task {
8484

8585
/**
8686
* Creates a new task.
87-
*
87+
*
8888
* @param threadService Service to use for launching the task in its own
8989
* thread. Required.
9090
* @param eventService Service to use for reporting status updates as
9191
* {@link TaskEvent}s. May be null, in which case no events are
9292
* reported.
9393
*/
9494
public DefaultTask(final ThreadService threadService,
95-
final EventService eventService)
95+
final EventService eventService)
9696
{
9797
this.threadService = threadService;
9898
this.eventService = eventService;
99+
cancelCallBack = this::defaultCancelCallback;
99100
}
100101

101102
// -- Task methods --
@@ -176,17 +177,25 @@ public void cancel(final String reason) {
176177
canceled = true;
177178
cancelReason = reason;
178179
if (cancelCallBack!=null) cancelCallBack.run();
180+
fireTaskEvent();
181+
}
182+
183+
void defaultCancelCallback() {
179184
if (future!=null) {
180185
isDone = future.cancel(true);
181186
}
182-
fireTaskEvent();
183187
}
184188

185189
@Override
186190
public void setCancelCallBack(Runnable r) {
187191
this.cancelCallBack = r;
188192
}
189193

194+
@Override
195+
public Runnable getCancelCallBack() {
196+
return this.cancelCallBack;
197+
}
198+
190199
@Override
191200
public String getCancelReason() {
192201
return cancelReason;

src/main/java/org/scijava/task/Task.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* %%
77
* Redistribution and use in source and binary forms, with or without
88
* modification, are permitted provided that the following conditions are met:
9-
*
9+
*
1010
* 1. Redistributions of source code must retain the above copyright notice,
1111
* this list of conditions and the following disclaimer.
1212
* 2. Redistributions in binary form must reproduce the above copyright notice,
1313
* this list of conditions and the following disclaimer in the documentation
1414
* and/or other materials provided with the distribution.
15-
*
15+
*
1616
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1717
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1818
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -140,4 +140,12 @@ default void finish() {}
140140
* @param runnable : should be executed if this task is cancelled through {@link Task#cancel(String)}
141141
*/
142142
default void setCancelCallBack(Runnable runnable) {}
143+
144+
/**
145+
* Returns the current cancel callback runnable,
146+
* This can be used to concatenate callbacks in order,
147+
* for instance, to ask for a user confirmation before cancelling the task
148+
*/
149+
default Runnable getCancelCallBack() { return () -> {}; }
150+
143151
}

0 commit comments

Comments
 (0)