diff options
| author | 2016-08-15 13:10:09 +0200 | |
|---|---|---|
| committer | 2016-08-15 13:10:09 +0200 | |
| commit | fa6263bf6e11293783931619461b5e88ef33f9aa (patch) | |
| tree | 1da5a1403e4fe4e4aceab505c6f0754c71d50455 | |
| parent | MARK EVERYTHING PUBLIC BECAUSE WE ARE EVIL (diff) | |
| download | enigma-fa6263bf6e11293783931619461b5e88ef33f9aa.tar.gz enigma-fa6263bf6e11293783931619461b5e88ef33f9aa.tar.xz enigma-fa6263bf6e11293783931619461b5e88ef33f9aa.zip | |
"Close mappings" option now check if you have change your mappings and if you want to save them
| -rw-r--r-- | src/main/java/cuchaz/enigma/gui/Gui.java | 81 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/gui/elements/MenuBar.java | 25 |
2 files changed, 67 insertions, 39 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/Gui.java b/src/main/java/cuchaz/enigma/gui/Gui.java index 262c8b68..846e35b5 100644 --- a/src/main/java/cuchaz/enigma/gui/Gui.java +++ b/src/main/java/cuchaz/enigma/gui/Gui.java | |||
| @@ -11,23 +11,6 @@ | |||
| 11 | package cuchaz.enigma.gui; | 11 | package cuchaz.enigma.gui; |
| 12 | 12 | ||
| 13 | import com.google.common.collect.Lists; | 13 | import com.google.common.collect.Lists; |
| 14 | |||
| 15 | import java.awt.*; | ||
| 16 | import java.awt.event.*; | ||
| 17 | import java.io.File; | ||
| 18 | import java.io.IOException; | ||
| 19 | import java.util.Collection; | ||
| 20 | import java.util.Collections; | ||
| 21 | import java.util.List; | ||
| 22 | import java.util.Vector; | ||
| 23 | |||
| 24 | import javax.swing.*; | ||
| 25 | import javax.swing.text.BadLocationException; | ||
| 26 | import javax.swing.text.Highlighter; | ||
| 27 | import javax.swing.tree.DefaultTreeModel; | ||
| 28 | import javax.swing.tree.TreeNode; | ||
| 29 | import javax.swing.tree.TreePath; | ||
| 30 | |||
| 31 | import cuchaz.enigma.Constants; | 14 | import cuchaz.enigma.Constants; |
| 32 | import cuchaz.enigma.ExceptionIgnorer; | 15 | import cuchaz.enigma.ExceptionIgnorer; |
| 33 | import cuchaz.enigma.analysis.*; | 16 | import cuchaz.enigma.analysis.*; |
| @@ -50,6 +33,23 @@ import cuchaz.enigma.throwables.IllegalNameException; | |||
| 50 | import cuchaz.enigma.utils.Utils; | 33 | import cuchaz.enigma.utils.Utils; |
| 51 | import de.sciss.syntaxpane.DefaultSyntaxKit; | 34 | import de.sciss.syntaxpane.DefaultSyntaxKit; |
| 52 | 35 | ||
| 36 | import javax.swing.*; | ||
| 37 | import javax.swing.text.BadLocationException; | ||
| 38 | import javax.swing.text.Highlighter; | ||
| 39 | import javax.swing.tree.DefaultTreeModel; | ||
| 40 | import javax.swing.tree.TreeNode; | ||
| 41 | import javax.swing.tree.TreePath; | ||
| 42 | import java.awt.*; | ||
| 43 | import java.awt.event.*; | ||
| 44 | import java.io.File; | ||
| 45 | import java.io.IOException; | ||
| 46 | import java.util.Collection; | ||
| 47 | import java.util.Collections; | ||
| 48 | import java.util.List; | ||
| 49 | import java.util.Vector; | ||
| 50 | import java.util.function.Function; | ||
| 51 | import java.util.function.Supplier; | ||
| 52 | |||
| 53 | public class Gui { | 53 | public class Gui { |
| 54 | 54 | ||
| 55 | private GuiController controller; | 55 | private GuiController controller; |
| @@ -707,6 +707,19 @@ public class Gui { | |||
| 707 | return new TreePath(nodes.toArray()); | 707 | return new TreePath(nodes.toArray()); |
| 708 | } | 708 | } |
| 709 | 709 | ||
| 710 | public void showDiscardDiag(Function<Integer, Void> callback, String... options) | ||
| 711 | { | ||
| 712 | int response = JOptionPane.showOptionDialog(this.frame, "Your mappings have not been saved yet. Do you want to save?", "Save your changes?", JOptionPane.YES_NO_CANCEL_OPTION, | ||
| 713 | JOptionPane.QUESTION_MESSAGE, null, options, options[2]); | ||
| 714 | callback.apply(response); | ||
| 715 | } | ||
| 716 | |||
| 717 | public void saveMapping() throws IOException | ||
| 718 | { | ||
| 719 | if (this.enigmaMappingsFileChooser.getSelectedFile() != null || this.enigmaMappingsFileChooser.showSaveDialog(this.frame) == JFileChooser.APPROVE_OPTION) | ||
| 720 | this.controller.saveMappings(this.enigmaMappingsFileChooser.getCurrentDirectory()); | ||
| 721 | } | ||
| 722 | |||
| 710 | public void close() { | 723 | public void close() { |
| 711 | if (!this.controller.isDirty()) { | 724 | if (!this.controller.isDirty()) { |
| 712 | // everything is saved, we can exit safely | 725 | // everything is saved, we can exit safely |
| @@ -714,30 +727,22 @@ public class Gui { | |||
| 714 | System.exit(0); | 727 | System.exit(0); |
| 715 | } else { | 728 | } else { |
| 716 | // ask to save before closing | 729 | // ask to save before closing |
| 717 | String[] options = {"Save and exit", "Discard changes", "Cancel"}; | 730 | showDiscardDiag((response) -> { |
| 718 | int response = JOptionPane.showOptionDialog(this.frame, "Your mappings have not been saved yet. Do you want to save?", "Save your changes?", JOptionPane.YES_NO_CANCEL_OPTION, | 731 | if (response == JOptionPane.YES_OPTION) |
| 719 | JOptionPane.QUESTION_MESSAGE, null, options, options[2]); | 732 | { |
| 720 | switch (response) { | 733 | try { |
| 721 | case JOptionPane.YES_OPTION: // save and exit | 734 | this.saveMapping(); |
| 722 | if (this.enigmaMappingsFileChooser.getSelectedFile() != null || this.enigmaMappingsFileChooser.showSaveDialog(this.frame) == JFileChooser.APPROVE_OPTION) { | 735 | this.frame.dispose(); |
| 723 | try { | 736 | |
| 724 | this.controller.saveMappings(this.enigmaMappingsFileChooser.getCurrentDirectory()); | 737 | } catch (IOException ex) { |
| 725 | this.frame.dispose(); | 738 | throw new Error(ex); |
| 726 | } catch (IOException ex) { | ||
| 727 | throw new Error(ex); | ||
| 728 | } | ||
| 729 | } | 739 | } |
| 730 | break; | 740 | } |
| 731 | 741 | else | |
| 732 | case JOptionPane.NO_OPTION: | ||
| 733 | // don't save, exit | ||
| 734 | this.frame.dispose(); | 742 | this.frame.dispose(); |
| 735 | break; | ||
| 736 | default: | ||
| 737 | break; | ||
| 738 | 743 | ||
| 739 | // cancel means do nothing | 744 | return null; |
| 740 | } | 745 | }, "Save and exit", "Discard changes", "Cancel"); |
| 741 | } | 746 | } |
| 742 | } | 747 | } |
| 743 | 748 | ||
diff --git a/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java b/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java index 038698bf..befe1291 100644 --- a/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java +++ b/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java | |||
| @@ -146,7 +146,30 @@ public class MenuBar extends JMenuBar { | |||
| 146 | { | 146 | { |
| 147 | JMenuItem item = new JMenuItem("Close Mappings"); | 147 | JMenuItem item = new JMenuItem("Close Mappings"); |
| 148 | menu.add(item); | 148 | menu.add(item); |
| 149 | item.addActionListener(event -> this.gui.getController().closeMappings()); | 149 | item.addActionListener(event -> { |
| 150 | if (this.gui.getController().isDirty()) | ||
| 151 | { | ||
| 152 | this.gui.showDiscardDiag((response -> { | ||
| 153 | if (response == JOptionPane.YES_OPTION) | ||
| 154 | { | ||
| 155 | try | ||
| 156 | { | ||
| 157 | gui.saveMapping(); | ||
| 158 | this.gui.getController().closeMappings(); | ||
| 159 | } catch (IOException e) | ||
| 160 | { | ||
| 161 | throw new Error(e); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | else if (response == JOptionPane.NO_OPTION) | ||
| 165 | this.gui.getController().closeMappings(); | ||
| 166 | return null; | ||
| 167 | }), "Save and close", "Discard changes", "Cancel"); | ||
| 168 | } | ||
| 169 | else | ||
| 170 | this.gui.getController().closeMappings(); | ||
| 171 | |||
| 172 | }); | ||
| 150 | this.closeMappingsMenu = item; | 173 | this.closeMappingsMenu = item; |
| 151 | } | 174 | } |
| 152 | menu.addSeparator(); | 175 | menu.addSeparator(); |