-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added auto-key-generation task to task-progress #7797
Conversation
Auto-key-generation tasks are now shown in the list of background tasks, together with a progress representing entries done / entries. Fixes koppor/jabref#7267
src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java
Outdated
Show resolved
Hide resolved
src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java
Outdated
Show resolved
Hide resolved
To simplify thread synchronization, the background task is now wrapped in an anonymous class, just as in https://github.com/JabRef/jabref/blob/fa8cc829727ffddd97a9056fd466e7098577f4d7/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more remarks concerning the admittedly strange JavaFX conventions around concurrency.
if (isCanceled) { | ||
return null; | ||
} | ||
showToUser(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is this golden rule of concurrency in JavaFX, that you shouldn't update any UI-facing properties in background tasks (or only using updateProgress
and updateMessage
), see for example https://docs.oracle.com/javafx/2/api/javafx/concurrent/Task.html.
Thus, setting title and message should happen in the constructor and not in the call method.
titleProperty().set(Localization.lang("Autogenerate citation keys")); | ||
messageProperty().set(entries.size() + " " + Localization.lang("entries")); | ||
|
||
DefaultTaskExecutor.runInJavaFXThread(() -> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The runInJavaFXThread shouldn't be necessary as this is handled by updateProgress.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondered about that, but it is done in ImportHandler as well. Shall I remove it there as well (in an additional PR)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason it is not working in this context, see https://github.com/JabRef/jabref/pull/7209/files#r551271470. Maybe @Siedlerchr still remembers where the problem was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The relevant code is
jabref/src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java
Lines 146 to 155 in fa8cc82
this.updateMessage(task.messageProperty().get()); | |
this.updateTitle(task.titleProperty().get()); | |
BindingsHelper.subscribeFuture(task.progressProperty(), progress -> updateProgress(progress.getWorkDone(), progress.getMax())); | |
BindingsHelper.subscribeFuture(task.messageProperty(), this::updateMessage); | |
BindingsHelper.subscribeFuture(task.titleProperty(), this::updateTitle); | |
BindingsHelper.subscribeFuture(task.isCanceledProperty(), cancelled -> { | |
if (cancelled) { | |
cancel(); | |
} | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The progress updating is not happening in the fx thread by the BackgroundTask. This will leead to an exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So shall we explicitly make updating the progress variable run on the fx thread in updateProgress?
`
protected void updateProgress(BackgroundProgress newProgress) {
DefaultTaskExecutor.runInJavaFXThread(() -> {
progress.setValue(newProgress);
});
}
`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally yes it would happend in the BackgroundTask itself, but I am unsure if this has any side effects on existing tasks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would nesting DefaultTaskExecutor.runInJavaFXThread() be problematic (in addition to being ugly)?
I don't think there are many tasks that use updateProgress right now, let me investigate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I (and Intellij) can tell, updateProgress is only used by GenerateCitationKeyAction, FileDownloadTask and ImportHandler.
FileDownloadTask uses EasyBind to update the progress. Does EasyBind already take care of running on the JavaFX thread?
@Override | |
protected Path call() throws Exception { | |
URLDownload download = new URLDownload(source); | |
try (ProgressInputStream inputStream = download.asInputStream()) { | |
EasyBind.subscribe( | |
inputStream.totalNumBytesReadProperty(), | |
bytesRead -> updateProgress(bytesRead.longValue(), inputStream.getMaxNumBytes())); | |
// Make sure directory exists since otherwise copy fails | |
Files.createDirectories(destination.getParent()); | |
Files.copy(inputStream, destination, StandardCopyOption.REPLACE_EXISTING); | |
} | |
return destination; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure... then using setValue
on progress
still leads to the same problems (i.e. you cannot bind something to the progress property). Personally, I would leave the whole what happens on which thread hidden as long as possible, i.e. only the DefaultTaskExecutor cares about these things when converting a background task to a JavaFX task.
I guess these problems we are discussing can be easily fixed by not using the progress/message properties of the background task anywhere outside of the task itself. So here for the citation generation there shouldn't be any problem.
src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java
Outdated
Show resolved
Hide resolved
UI-facing properties are done on correct task now, and onSuccess is used.
From my point of view this looks now good |
src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java
Outdated
Show resolved
Hide resolved
Some languages place the number after the word 'counter', this would result in an ugly message for those languages.
src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java
Outdated
Show resolved
Hide resolved
Don't forge the l10n (see failing unit test) %0\ entries=%0 entries |
src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java
Outdated
Show resolved
Hide resolved
* upstream/main: Added auto-key-generation task to task-progress (JabRef#7797) cleanup temporary files, use prefix "jabref-" (JabRef#7811) Add easier how-to for checklist (JabRef#7813) Fix annotation + package of ACMPortalParserTest (JabRef#7812) Implemented a select all button for the library import function (issue JabRef#7786) (JabRef#7808) Fix for issue 4682 : Restructuring the side pane structure having additional functionality and merging the remove group menus (JabRef#7714)
Auto-key-generation tasks are now shown in the list of background tasks,
together with a progress representing entries done / entries.
Fixes #7267
CHANGELOG.md
described in a way that is understandable for the average user (if applicable)