diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 8346fe58e7f..7b58612c146 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -502,17 +502,8 @@ public boolean quit() { context.getDatabaseFile().map(File::getAbsolutePath).ifPresent(filenames::add); } - // Wait for save operations to finish - for (int i = 0; i < tabbedPane.getTabs().size(); i++) { - if (getBasePanelAt(i).isSaving()) { - // There is a database still being saved, so we need to wait. - WaitForSaveOperation w = new WaitForSaveOperation(this); - w.show(); // This method won't return until canceled or the save operation is done. - if (w.canceled()) { - return false; // The user clicked cancel. - } - } - } + WaitForSaveFinishedDialog waitForSaveFinishedDialog = new WaitForSaveFinishedDialog(dialogService); + waitForSaveFinishedDialog.showAndWait(getBasePanelList()); // Good bye! tearDownJabRef(filenames); diff --git a/src/main/java/org/jabref/gui/WaitForSaveFinishedDialog.java b/src/main/java/org/jabref/gui/WaitForSaveFinishedDialog.java new file mode 100644 index 00000000000..f8758ab8e1e --- /dev/null +++ b/src/main/java/org/jabref/gui/WaitForSaveFinishedDialog.java @@ -0,0 +1,43 @@ +package org.jabref.gui; + +import java.util.List; + +import javafx.concurrent.Task; + +import org.jabref.logic.l10n.Localization; + +/** + * Dialog shown when closing of application needs to wait for a save operation to finish. + */ +public class WaitForSaveFinishedDialog { + + private final DialogService dialogService; + + public WaitForSaveFinishedDialog(DialogService dialogService) { + this.dialogService = dialogService; + } + + public void showAndWait(List basePanels) { + if (basePanels.stream().anyMatch(BasePanel::isSaving)) { + Task waitForSaveFinished = new Task() { + @Override + protected Void call() throws Exception { + while (basePanels.stream().anyMatch(BasePanel::isSaving)) { + if (isCancelled()) { + return null; + } else { + Thread.sleep(100); + } + } + return null; + } + }; + + dialogService.showProgressDialogAndWait( + Localization.lang("Please wait..."), + Localization.lang("Waiting for save operation to finish") + "...", + waitForSaveFinished + ); + } + } +} diff --git a/src/main/java/org/jabref/gui/WaitForSaveOperation.java b/src/main/java/org/jabref/gui/WaitForSaveOperation.java deleted file mode 100644 index 86b2e0a2f8b..00000000000 --- a/src/main/java/org/jabref/gui/WaitForSaveOperation.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.jabref.gui; - -import java.awt.BorderLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import javax.swing.BorderFactory; -import javax.swing.JButton; -import javax.swing.JDialog; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JProgressBar; -import javax.swing.Timer; - -import org.jabref.logic.l10n.Localization; - -import com.jgoodies.forms.builder.ButtonBarBuilder; - -/** - * Dialog shown when closing of application needs to wait for a save operation to finish. - */ -public class WaitForSaveOperation implements ActionListener { - - private final JabRefFrame frame; - private final JDialog diag; - private final Timer t = new Timer(500, this); - private boolean canceled; - - - public WaitForSaveOperation(JabRefFrame frame) { - this.frame = frame; - - JButton cancel = new JButton(Localization.lang("Cancel")); - JProgressBar prog = new JProgressBar(0); - prog.setIndeterminate(true); - prog.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); - diag = new JDialog((JFrame) null, Localization.lang("Please wait..."), true); - - ButtonBarBuilder bb = new ButtonBarBuilder(); - bb.addGlue(); - bb.addButton(cancel); - bb.addGlue(); - cancel.addActionListener(e -> { - canceled = true; - t.stop(); - diag.dispose(); - }); - - JLabel message = new JLabel(Localization.lang("Waiting for save operation to finish") + "..."); - message.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); - - diag.getContentPane().add(message, BorderLayout.NORTH); - diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH); - diag.getContentPane().add(prog, BorderLayout.CENTER); - diag.pack(); - } - - public void show() { - t.start(); - diag.setVisible(true); - - } - - public boolean canceled() { - return canceled; - } - - @Override - public void actionPerformed(ActionEvent actionEvent) { - boolean anySaving = false; - for (BasePanel basePanel : frame.getBasePanelList()) { - if (basePanel.isSaving()) { - anySaving = true; - break; - } - } - if (!anySaving) { - t.stop(); - diag.dispose(); - } - } -}