diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 7ab7954..4214be0 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -21,7 +21,7 @@ public class MazeAppModel extends Observable { private Previous previous; private ArrayList shortest; private ArrayList boxList; - private Boolean modified = false; + private boolean saved = true; public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); @@ -45,7 +45,7 @@ public class MazeAppModel extends Observable { this.filename = new String(filename); maze = new Maze(this.filename); initModelFromMaze(); - modified = true; + saved = true; setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); } @@ -58,18 +58,19 @@ public class MazeAppModel extends Observable { return filename; } - public boolean isModified() { - return modified; + public boolean isSaved() { + return saved; } public void saveToFile() { maze.saveToTextFile(filename); + saved = true; } public void saveToFile(String filename) { this.filename = new String(filename); maze.saveToTextFile(this.filename); - modified = true; + saved = true; setChanged(); notifyObservers(MazeAppModelMessage.FileChange); } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 40d4a49..05cf17e 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -23,6 +23,11 @@ public class LoadMazeMenuItem extends JMenuItem public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){ + return; + } + } String filename = FileBox.load(mazeApp); if (filename != null) { mazeApp.getMazeAppModel().loadMaze(filename); diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 13250a0..c8632ce 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -26,23 +26,11 @@ public class QuitMenuItem extends JMenuItem MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - if (mazeAppModel.isModified()) { - int response = JOptionPane.showInternalOptionDialog(this, - "Maze not saved. Save it ?", - "Quit application", - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.WARNING_MESSAGE, - null, null, null); - switch (response) { - case JOptionPane.CANCEL_OPTION: - return; - case JOptionPane.OK_OPTION: - mazeAppModel.saveToFile(); - break; - case JOptionPane.NO_OPTION: - break; - } - } + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { + return; + } + } System.exit(0); } } diff --git a/src/ui/SaveBox.java b/src/ui/SaveBox.java new file mode 100644 index 0000000..d333279 --- /dev/null +++ b/src/ui/SaveBox.java @@ -0,0 +1,43 @@ +package ui; + +import javax.swing.JOptionPane; +import java.awt.Component; + +import model.MazeAppModel; + +public class SaveBox { + private static String message = "Maze not saved. Save it ?"; + + public static boolean promptAndContinue(Component parent, + MazeApp mazeApp, + String title) { + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + int response = JOptionPane.showInternalOptionDialog(parent, + message, + title, + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.WARNING_MESSAGE, + null, null, null); + switch (response) { + case JOptionPane.CANCEL_OPTION: + return false; + case JOptionPane.OK_OPTION: + if (mazeAppModel.getFilename() != null) { + mazeAppModel.saveToFile(); + } + else { + String filename = FileBox.save(mazeApp); + if (filename != null) { + mazeApp.getMazeAppModel().saveToFile(filename); + } + else { + return promptAndContinue(null, mazeApp, title); + } + } + break; + case JOptionPane.NO_OPTION: + break; + } + return true; + } +}