diff options
| author | 2021-07-03 08:29:08 -0500 | |
|---|---|---|
| committer | 2021-07-03 14:29:08 +0100 | |
| commit | bc41ca4fb40d773056486e6768b64d4ec75bcf38 (patch) | |
| tree | dd559f5276be80e206f32b1f7d1dad17db8e187e | |
| parent | Update proguard (#418) (diff) | |
| download | enigma-bc41ca4fb40d773056486e6768b64d4ec75bcf38.tar.gz enigma-bc41ca4fb40d773056486e6768b64d4ec75bcf38.tar.xz enigma-bc41ca4fb40d773056486e6768b64d4ec75bcf38.zip | |
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 <liach@users.noreply.github.com>
* Fix the freeze
Signed-off-by: liach <liach@users.noreply.github.com>
Co-authored-by: liach <liach@users.noreply.github.com>
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; | |||
| 17 | import java.awt.event.*; | 17 | import java.awt.event.*; |
| 18 | import java.nio.file.Path; | 18 | import java.nio.file.Path; |
| 19 | import java.util.*; | 19 | import java.util.*; |
| 20 | import java.util.concurrent.CompletableFuture; | ||
| 20 | import java.util.function.Consumer; | 21 | import java.util.function.Consumer; |
| 21 | import java.util.function.Function; | 22 | import java.util.function.Function; |
| 22 | 23 | ||
| @@ -752,9 +753,10 @@ public class Gui implements LanguageChangeListener { | |||
| 752 | callback.apply(response); | 753 | callback.apply(response); |
| 753 | } | 754 | } |
| 754 | 755 | ||
| 755 | public void saveMapping() { | 756 | public CompletableFuture<Void> saveMapping() { |
| 756 | if (this.enigmaMappingsFileChooser.getSelectedFile() != null || this.enigmaMappingsFileChooser.showSaveDialog(this.frame) == JFileChooser.APPROVE_OPTION) | 757 | if (this.enigmaMappingsFileChooser.getSelectedFile() != null || this.enigmaMappingsFileChooser.showSaveDialog(this.frame) == JFileChooser.APPROVE_OPTION) |
| 757 | this.controller.saveMappings(this.enigmaMappingsFileChooser.getSelectedFile().toPath()); | 758 | return this.controller.saveMappings(this.enigmaMappingsFileChooser.getSelectedFile().toPath()); |
| 759 | return CompletableFuture.completedFuture(null); | ||
| 758 | } | 760 | } |
| 759 | 761 | ||
| 760 | public void close() { | 762 | public void close() { |
| @@ -765,8 +767,8 @@ public class Gui implements LanguageChangeListener { | |||
| 765 | // ask to save before closing | 767 | // ask to save before closing |
| 766 | showDiscardDiag((response) -> { | 768 | showDiscardDiag((response) -> { |
| 767 | if (response == JOptionPane.YES_OPTION) { | 769 | if (response == JOptionPane.YES_OPTION) { |
| 768 | this.saveMapping(); | 770 | this.saveMapping().thenRun(this::exit); |
| 769 | exit(); | 771 | // do not join, as join waits on swing to clear events |
| 770 | } else if (response == JOptionPane.NO_OPTION) { | 772 | } else if (response == JOptionPane.NO_OPTION) { |
| 771 | exit(); | 773 | exit(); |
| 772 | } | 774 | } |
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 { | |||
| 149 | return saveMappings(path, loadedMappingFormat); | 149 | return saveMappings(path, loadedMappingFormat); |
| 150 | } | 150 | } |
| 151 | 151 | ||
| 152 | /** | ||
| 153 | * Saves the mappings, with a dialog popping up, showing the progress. | ||
| 154 | * | ||
| 155 | * <p>Notice the returned completable future has to be completed by | ||
| 156 | * {@link SwingUtilities#invokeLater(Runnable)}. Hence, do not try to | ||
| 157 | * join on the future in gui, but rather call {@code thenXxx} methods. | ||
| 158 | * | ||
| 159 | * @param path the path of the save | ||
| 160 | * @param format the format of the save | ||
| 161 | * @return the future of saving | ||
| 162 | */ | ||
| 152 | public CompletableFuture<Void> saveMappings(Path path, MappingFormat format) { | 163 | public CompletableFuture<Void> saveMappings(Path path, MappingFormat format) { |
| 153 | if (project == null) return CompletableFuture.completedFuture(null); | 164 | if (project == null) return CompletableFuture.completedFuture(null); |
| 154 | 165 | ||
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 { | |||
| 244 | if (this.gui.getController().isDirty()) { | 244 | if (this.gui.getController().isDirty()) { |
| 245 | this.gui.showDiscardDiag((response -> { | 245 | this.gui.showDiscardDiag((response -> { |
| 246 | if (response == JOptionPane.YES_OPTION) { | 246 | if (response == JOptionPane.YES_OPTION) { |
| 247 | this.gui.saveMapping(); | 247 | this.gui.saveMapping().thenRun(then); |
| 248 | then.run(); | ||
| 249 | } else if (response == JOptionPane.NO_OPTION) | 248 | } else if (response == JOptionPane.NO_OPTION) |
| 250 | then.run(); | 249 | then.run(); |
| 251 | return null; | 250 | return null; |