From ff9ce0068d6b80e131826a9138aab3b1f981b00e Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Mon, 11 May 2020 14:51:08 +0200 Subject: [PATCH] Removed WaitForBackgroundtasksFinishedDialog More in line with the other JabRef dialogs Addresses https://github.com/JabRef/jabref/pull/6443#discussion_r422977336 --- .../java/org/jabref/gui/DialogService.java | 12 ++++ .../org/jabref/gui/JabRefDialogService.java | 40 ++++++++++++ src/main/java/org/jabref/gui/JabRefFrame.java | 7 +- .../WaitForBackgroundtasksFinishedDialog.java | 64 ------------------- 4 files changed, 57 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/org/jabref/gui/WaitForBackgroundtasksFinishedDialog.java diff --git a/src/main/java/org/jabref/gui/DialogService.java b/src/main/java/org/jabref/gui/DialogService.java index 4c8d0a0130b..87da6a3b517 100644 --- a/src/main/java/org/jabref/gui/DialogService.java +++ b/src/main/java/org/jabref/gui/DialogService.java @@ -196,6 +196,18 @@ Optional showCustomButtonDialogAndWait(Alert.AlertType type, String */ Optional showProgressDialogAndWait(String title, String content, Task task); + /** + * Constructs and shows a dialog showing the progress of running background tasks. + * Clicking cancel will cancel the underlying service and close the dialog. + * The dialog will exit as soon as none of the background tasks are running + * + * @param title title of the dialog + * @param content message to show below the list of background tasks + * @param stateManager The {@link StateManager} which contains the background tasks + * @return + */ + Optional showBackgroundProgressDialogAndWait(String title, String content, StateManager stateManager); + /** * Notify the user in an non-blocking way (i.e., in form of toast in a snackbar). * diff --git a/src/main/java/org/jabref/gui/JabRefDialogService.java b/src/main/java/org/jabref/gui/JabRefDialogService.java index 59809903bd6..2048ab8cfb1 100644 --- a/src/main/java/org/jabref/gui/JabRefDialogService.java +++ b/src/main/java/org/jabref/gui/JabRefDialogService.java @@ -12,6 +12,8 @@ import java.util.function.Consumer; import java.util.stream.Collectors; +import javafx.beans.binding.Bindings; +import javafx.collections.ObservableList; import javafx.concurrent.Task; import javafx.print.PrinterJob; import javafx.scene.Group; @@ -23,9 +25,11 @@ import javafx.scene.control.CheckBox; import javafx.scene.control.ChoiceDialog; import javafx.scene.control.DialogPane; +import javafx.scene.control.Label; import javafx.scene.control.TextInputDialog; import javafx.scene.layout.Pane; import javafx.scene.layout.Region; +import javafx.scene.layout.VBox; import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import javafx.stage.Stage; @@ -33,6 +37,7 @@ import javafx.util.Duration; import org.jabref.gui.icon.IconTheme; +import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.DirectoryDialogConfiguration; import org.jabref.gui.util.FileDialogConfiguration; import org.jabref.gui.util.ThemeLoader; @@ -43,8 +48,10 @@ import com.jfoenix.controls.JFXSnackbar; import com.jfoenix.controls.JFXSnackbar.SnackbarEvent; import com.jfoenix.controls.JFXSnackbarLayout; +import org.controlsfx.control.TaskProgressView; import org.controlsfx.dialog.ExceptionDialog; import org.controlsfx.dialog.ProgressDialog; +import org.fxmisc.easybind.EasyBind; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -286,6 +293,39 @@ public Optional showProgressDialogAndWait(String title, String content return progressDialog.showAndWait(); } + @Override + public Optional showBackgroundProgressDialogAndWait(String title, String content, StateManager stateManager) { + TaskProgressView taskProgressView = new TaskProgressView(); + EasyBind.listBind(taskProgressView.getTasks(), stateManager.getBackgroundTasks()); + taskProgressView.setRetainTasks(false); + taskProgressView.setGraphicFactory(BackgroundTask.iconCallback); + + Label message = new Label(content); + + VBox box = new VBox(taskProgressView, message); + + DialogPane contentPane = new DialogPane(); + contentPane.setContent(box); + + FXDialog alert = new FXDialog(AlertType.WARNING, title); + alert.setDialogPane(contentPane); + alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.CANCEL); + alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE); + alert.setResizable(true); + themeLoader.installCss(alert.getDialogPane().getScene(), preferences); + + stateManager.anyTaskRunningBinding.addListener((observable, oldValue, newValue) -> { + if (!newValue) { + alert.setResult(ButtonType.YES); + alert.close(); + } + }); + + Dialog dialog = () -> alert.showAndWait(); + + return showCustomDialogAndWait(dialog); + } + @Override public void notify(String message) { LOGGER.info(message); diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 339168eb2ba..ef824d75c0f 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -408,8 +408,11 @@ public boolean quit() { the background tasks may make changes themselves that need saving. */ if (stateManager.anyTaskRunningBinding.getValue()) { - WaitForBackgroundtasksFinishedDialog waitForBackgroundtasksFinishedDialog = new WaitForBackgroundtasksFinishedDialog(dialogService); - if (!waitForBackgroundtasksFinishedDialog.showAndWait(stateManager, themeLoader, prefs)) { + if (!(dialogService.showBackgroundProgressDialogAndWait( + Localization.lang("Please wait..."), + Localization.lang("Waiting for background tasks to finish. Quit anyway?"), + stateManager + ).orElse(ButtonType.CANCEL) == ButtonType.YES)) { return false; } } diff --git a/src/main/java/org/jabref/gui/WaitForBackgroundtasksFinishedDialog.java b/src/main/java/org/jabref/gui/WaitForBackgroundtasksFinishedDialog.java deleted file mode 100644 index 06360803eed..00000000000 --- a/src/main/java/org/jabref/gui/WaitForBackgroundtasksFinishedDialog.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.jabref.gui; - -import java.util.Optional; - -import javafx.scene.control.Alert; -import javafx.scene.control.ButtonType; -import javafx.scene.control.DialogPane; -import javafx.scene.control.Label; -import javafx.scene.layout.Region; -import javafx.scene.layout.VBox; - -import org.jabref.gui.util.BackgroundTask; -import org.jabref.gui.util.ThemeLoader; -import org.jabref.logic.l10n.Localization; -import org.jabref.preferences.JabRefPreferences; - -import org.controlsfx.control.TaskProgressView; -import org.fxmisc.easybind.EasyBind; - -/** - * Dialog shown when closing of application needs to wait for some background tasks. - */ -public class WaitForBackgroundtasksFinishedDialog { - - private final DialogService dialogService; - - public WaitForBackgroundtasksFinishedDialog(DialogService dialogService) { - this.dialogService = dialogService; - } - - public boolean showAndWait(StateManager stateManager, ThemeLoader themeLoader, JabRefPreferences preferences) { - TaskProgressView taskProgressView = new TaskProgressView(); - EasyBind.listBind(taskProgressView.getTasks(), stateManager.getBackgroundTasks()); - taskProgressView.setRetainTasks(false); - taskProgressView.setGraphicFactory(BackgroundTask.iconCallback); - - Label message = new Label(Localization.lang("Waiting for background tasks to finish. Quit anyway?")); - - VBox box = new VBox(taskProgressView, message); - - DialogPane contentPane = new DialogPane(); - contentPane.setContent(box); - - FXDialog alert = new FXDialog(Alert.AlertType.NONE, Localization.lang("Please wait...")); - alert.setDialogPane(contentPane); - alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.CANCEL); - alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE); - alert.setResizable(true); - themeLoader.installCss(alert.getDialogPane().getScene(), preferences); - - stateManager.anyTaskRunningBinding.addListener((observable, oldValue, newValue) -> { - if (!newValue) { - alert.setResult(ButtonType.YES); - alert.close(); - } - }); - - Dialog dialog = () -> alert.showAndWait(); - - Optional pressedButton = dialogService.showCustomDialogAndWait(dialog); - - return pressedButton.isPresent() && pressedButton.get() == ButtonType.YES; - } -}