Skip to content

Commit

Permalink
Registered the save task as background task
Browse files Browse the repository at this point in the history
This makes the dialog that pops up if background tasks are running wait
for pending saves.
  • Loading branch information
btut committed May 10, 2020
1 parent e9a7176 commit d7442cc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 55 deletions.
41 changes: 30 additions & 11 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Node;
Expand Down Expand Up @@ -402,15 +403,6 @@ private void tearDownJabRef(List<String> filenames) {
* @return true if the user chose to quit; false otherwise
*/
public boolean quit() {
// First ask if the user really wants to close, if there are still background tasks running
if (stateManager.anyTaskRunningBinding.getValue()) {
WaitForBackgroundtasksFinishedDialog waitForBackgroundtasksFinishedDialog = new WaitForBackgroundtasksFinishedDialog(dialogService);
if (!waitForBackgroundtasksFinishedDialog.showAndWait(stateManager)) {
return false;
}
}

// Then ask if the user really wants to close, if the library has not been saved since last save.
List<String> filenames = new ArrayList<>();
for (int i = 0; i < tabbedPane.getTabs().size(); i++) {
BasePanel panel = getBasePanelAt(i);
Expand All @@ -431,8 +423,35 @@ public boolean quit() {
context.getDatabasePath().map(Path::toAbsolutePath).map(Path::toString).ifPresent(filenames::add);
}

WaitForSaveFinishedDialog waitForSaveFinishedDialog = new WaitForSaveFinishedDialog(dialogService);
waitForSaveFinishedDialog.showAndWait(getBasePanelList());
// Check if any tabs are saving. If so, create a task waiting for them
if (getBasePanelList().stream().anyMatch(BasePanel::isSaving)) {
Task<Void> waitForSaveFinished = new Task<Void>() {
@Override
protected Void call() throws Exception {
updateTitle("Saving");
while (getBasePanelList().stream().anyMatch(BasePanel::isSaving)) {
if (isCancelled()) {
return null;
} else {
Thread.sleep(100);
}
}
return null;
}
};

stateManager.getBackgroundTasks().add(waitForSaveFinished);
Globals.TASK_EXECUTOR.execute(waitForSaveFinished);

}

// Ask if the user really wants to close, if there are background tasks running.
if (stateManager.anyTaskRunningBinding.getValue()) {
WaitForBackgroundtasksFinishedDialog waitForBackgroundtasksFinishedDialog = new WaitForBackgroundtasksFinishedDialog(dialogService);
if (!waitForBackgroundtasksFinishedDialog.showAndWait(stateManager)) {
return false;
}
}

// Good bye!
tearDownJabRef(filenames);
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/org/jabref/gui/WaitForSaveFinishedDialog.java

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/util/BackgroundTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
public abstract class BackgroundTask<V> {

public static ImmutableMap<String, Node> iconMap = ImmutableMap.of(
Localization.lang("Downloading"), IconTheme.JabRefIcons.DOWNLOAD.getGraphicNode()
Localization.lang("Downloading"), IconTheme.JabRefIcons.DOWNLOAD.getGraphicNode(),
Localization.lang("Saving"), IconTheme.JabRefIcons.SAVE_ALL.getGraphicNode()
);

public static Callback<Task<?>, Node> iconCallback = task -> {
Expand Down

0 comments on commit d7442cc

Please sign in to comment.