Skip to content

Commit 109600b

Browse files
kopporcalixtusLoayGhreebSiedlerchr
authored
Add documentation for BackgroundTask (and improve code) (#11346)
* Add documentation for BackgroundTask (and improve code) Co-authored-by: Carl Christian Snethlage <[email protected]> Co-authored-by: Loay Ghreeb <[email protected]> * Use <pre> tag for code example * another useless wrap --------- Co-authored-by: Carl Christian Snethlage <[email protected]> Co-authored-by: Loay Ghreeb <[email protected]> Co-authored-by: Loay Ghreeb <[email protected]> Co-authored-by: Siedlerchr <[email protected]>
1 parent 67f74a3 commit 109600b

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/main/java/org/jabref/gui/util/BackgroundTask.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,21 @@
3030
* We cannot use {@link Task} directly since it runs certain update notifications on the JavaFX thread,
3131
* and so makes testing harder.
3232
* We take the opportunity and implement a fluid interface.
33-
*
33+
* <p>
34+
* A task created here is to be submitted to {@link org.jabref.gui.Globals#TASK_EXECUTOR}: Use {@link TaskExecutor#execute(BackgroundTask)} to submit.
35+
* <p>
36+
* Example (for using the fluent interface)
37+
* <pre>{@code
38+
* BackgroundTask
39+
* .wrap(() -> ...)
40+
* .showToUser(true)
41+
* .onRunning(() -> ...)
42+
* .onSuccess(() -> ...)
43+
* .onFailure(() -> ...)
44+
* .executeWith(taskExecutor);
45+
* }</pre>
46+
* Background: The task executor one takes care to show it in the UI. See {@link org.jabref.gui.StateManager#addBackgroundTask(BackgroundTask, Task)} for details.
47+
* <p>
3448
* TODO: Think of migrating to <a href="https://github.com/ReactiveX/RxJava#simple-background-computation">RxJava</a>;
3549
* <a href="https://www.baeldung.com/java-completablefuture">CompletableFuture</a> do not seem to support everything.
3650
* If this is not possible, add an @implNote why.
@@ -136,8 +150,9 @@ public boolean showToUser() {
136150
return showToUser.get();
137151
}
138152

139-
public void showToUser(boolean show) {
153+
public BackgroundTask<V> showToUser(boolean show) {
140154
showToUser.set(show);
155+
return this;
141156
}
142157

143158
public boolean willBeRecoveredAutomatically() {

src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ public DefaultTaskExecutor(StateManager stateManager) {
4141
this.stateManager = stateManager;
4242
}
4343

44-
/**
45-
*
46-
*/
4744
public static <V> V runInJavaFXThread(Callable<V> callable) {
4845
if (Platform.isFxApplicationThread()) {
4946
try {
@@ -140,6 +137,13 @@ public DelayTaskThrottler createThrottler(int delay) {
140137
return throttler;
141138
}
142139

140+
/**
141+
* Generates a wrapper JavaFX {@link Task} monitoring the progress based on the data given from the task.
142+
* <code>call</code> is routed to the given task object.
143+
*
144+
* @param task the BackgroundTask to wrap
145+
* @return a new Task object
146+
*/
143147
private <V> Task<V> getJavaFXTask(BackgroundTask<V> task) {
144148
Task<V> javaTask = new Task<>() {
145149
{

src/main/java/org/jabref/logic/pdf/search/IndexingTaskManager.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ public IndexingTaskManager(TaskExecutor taskExecutor) {
3232
this.taskExecutor = taskExecutor;
3333
showToUser(true);
3434
willBeRecoveredAutomatically(true);
35-
DefaultTaskExecutor.runInJavaFXThread(() -> {
36-
this.updateProgress(1, 1);
37-
this.titleProperty().set(Localization.lang("Indexing pdf files"));
38-
});
35+
// runs on fx thread, no need to wrap
36+
this.updateProgress(1, 1);
37+
this.titleProperty().set(Localization.lang("Indexing pdf files"));
3938
}
4039

4140
@Override
@@ -56,10 +55,8 @@ protected Void call() throws Exception {
5655
}
5756

5857
private void updateProgress() {
59-
DefaultTaskExecutor.runInJavaFXThread(() -> {
60-
updateMessage(Localization.lang("%0 of %1 linked files added to the index", numOfIndexedFiles, numOfIndexedFiles + taskQueue.size()));
61-
updateProgress(numOfIndexedFiles, numOfIndexedFiles + taskQueue.size());
62-
});
58+
updateMessage(Localization.lang("%0 of %1 linked files added to the index", numOfIndexedFiles, numOfIndexedFiles + taskQueue.size()));
59+
updateProgress(numOfIndexedFiles, numOfIndexedFiles + taskQueue.size());
6360
}
6461

6562
private void enqueueTask(Runnable indexingTask) {

0 commit comments

Comments
 (0)