From bc41ca4fb40d773056486e6768b64d4ec75bcf38 Mon Sep 17 00:00:00 2001 From: liach Date: Sat, 3 Jul 2021 08:29:08 -0500 Subject: Makes sure save mappings is completed before next action (#409) * Makes sure save mappings is completed before next action Fixes #407 Signed-off-by: liach * Fix the freeze Signed-off-by: liach Co-authored-by: liach --- enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java | 10 ++++++---- .../src/main/java/cuchaz/enigma/gui/GuiController.java | 11 +++++++++++ .../src/main/java/cuchaz/enigma/gui/elements/MenuBar.java | 3 +-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java index c46bda2e..30bf4d3e 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java @@ -17,6 +17,7 @@ import java.awt.Point; import java.awt.event.*; import java.nio.file.Path; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Function; @@ -752,9 +753,10 @@ public class Gui implements LanguageChangeListener { callback.apply(response); } - public void saveMapping() { + public CompletableFuture saveMapping() { if (this.enigmaMappingsFileChooser.getSelectedFile() != null || this.enigmaMappingsFileChooser.showSaveDialog(this.frame) == JFileChooser.APPROVE_OPTION) - this.controller.saveMappings(this.enigmaMappingsFileChooser.getSelectedFile().toPath()); + return this.controller.saveMappings(this.enigmaMappingsFileChooser.getSelectedFile().toPath()); + return CompletableFuture.completedFuture(null); } public void close() { @@ -765,8 +767,8 @@ public class Gui implements LanguageChangeListener { // ask to save before closing showDiscardDiag((response) -> { if (response == JOptionPane.YES_OPTION) { - this.saveMapping(); - exit(); + this.saveMapping().thenRun(this::exit); + // do not join, as join waits on swing to clear events } else if (response == JOptionPane.NO_OPTION) { exit(); } diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java index e6f7b832..e28271e7 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java @@ -149,6 +149,17 @@ public class GuiController implements ClientPacketHandler { return saveMappings(path, loadedMappingFormat); } + /** + * Saves the mappings, with a dialog popping up, showing the progress. + * + *

Notice the returned completable future has to be completed by + * {@link SwingUtilities#invokeLater(Runnable)}. Hence, do not try to + * join on the future in gui, but rather call {@code thenXxx} methods. + * + * @param path the path of the save + * @param format the format of the save + * @return the future of saving + */ public CompletableFuture saveMappings(Path path, MappingFormat format) { if (project == null) return CompletableFuture.completedFuture(null); diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java index a0b2a52b..61f97803 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java @@ -244,8 +244,7 @@ public class MenuBar { if (this.gui.getController().isDirty()) { this.gui.showDiscardDiag((response -> { if (response == JOptionPane.YES_OPTION) { - this.gui.saveMapping(); - then.run(); + this.gui.saveMapping().thenRun(then); } else if (response == JOptionPane.NO_OPTION) then.run(); return null; -- cgit v1.2.3