summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Thog2016-08-15 13:10:09 +0200
committerGravatar Thog2016-08-15 13:10:09 +0200
commitfa6263bf6e11293783931619461b5e88ef33f9aa (patch)
tree1da5a1403e4fe4e4aceab505c6f0754c71d50455
parentMARK EVERYTHING PUBLIC BECAUSE WE ARE EVIL (diff)
downloadenigma-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.java81
-rw-r--r--src/main/java/cuchaz/enigma/gui/elements/MenuBar.java25
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 @@
11package cuchaz.enigma.gui; 11package cuchaz.enigma.gui;
12 12
13import com.google.common.collect.Lists; 13import com.google.common.collect.Lists;
14
15import java.awt.*;
16import java.awt.event.*;
17import java.io.File;
18import java.io.IOException;
19import java.util.Collection;
20import java.util.Collections;
21import java.util.List;
22import java.util.Vector;
23
24import javax.swing.*;
25import javax.swing.text.BadLocationException;
26import javax.swing.text.Highlighter;
27import javax.swing.tree.DefaultTreeModel;
28import javax.swing.tree.TreeNode;
29import javax.swing.tree.TreePath;
30
31import cuchaz.enigma.Constants; 14import cuchaz.enigma.Constants;
32import cuchaz.enigma.ExceptionIgnorer; 15import cuchaz.enigma.ExceptionIgnorer;
33import cuchaz.enigma.analysis.*; 16import cuchaz.enigma.analysis.*;
@@ -50,6 +33,23 @@ import cuchaz.enigma.throwables.IllegalNameException;
50import cuchaz.enigma.utils.Utils; 33import cuchaz.enigma.utils.Utils;
51import de.sciss.syntaxpane.DefaultSyntaxKit; 34import de.sciss.syntaxpane.DefaultSyntaxKit;
52 35
36import javax.swing.*;
37import javax.swing.text.BadLocationException;
38import javax.swing.text.Highlighter;
39import javax.swing.tree.DefaultTreeModel;
40import javax.swing.tree.TreeNode;
41import javax.swing.tree.TreePath;
42import java.awt.*;
43import java.awt.event.*;
44import java.io.File;
45import java.io.IOException;
46import java.util.Collection;
47import java.util.Collections;
48import java.util.List;
49import java.util.Vector;
50import java.util.function.Function;
51import java.util.function.Supplier;
52
53public class Gui { 53public 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();