Ask to save before loading a new maze
model: * Deprecate modified from MazeAppModel, which was misused and misunderstood * Add saved to MazeAppModel to keep track if a maze (or some modification) was saved into a file or not ui: * Use the new saved attribute to know if the maze was saved or not * Move the box asking whether to save the maze or not from QuitMenuItem to SaveBox and call SaveBox from QuitMenuItem * SaveBox now checks if the maze has a source file or not, if not it prompts a box to ask where to save the maze * If the maze was modified, the Load Maze button will pop SaveBox before erasing the current maze
This commit is contained in:
1 parent
80ffabdd69
commit
9b272e5f25
4 files changed
+59
-22
No files matched your search
@@ -21,7 +21,7 @@ public class MazeAppModel extends Observable {
|
|||||||
private Previous previous;
|
private Previous previous;
|
||||||
private ArrayList<VertexInterface> shortest;
|
private ArrayList<VertexInterface> shortest;
|
||||||
private ArrayList<VertexInterface> boxList;
|
private ArrayList<VertexInterface> boxList;
|
||||||
private Boolean modified = false;
|
private boolean saved = true;
|
||||||
|
|
||||||
public MazeAppModel() {
|
public MazeAppModel() {
|
||||||
maze = new Maze(Maze.emptyMaze(10));
|
maze = new Maze(Maze.emptyMaze(10));
|
||||||
@@ -45,7 +45,7 @@ public class MazeAppModel extends Observable {
|
|||||||
this.filename = new String(filename);
|
this.filename = new String(filename);
|
||||||
maze = new Maze(this.filename);
|
maze = new Maze(this.filename);
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
modified = true;
|
saved = true;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
@@ -58,18 +58,19 @@ public class MazeAppModel extends Observable {
|
|||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isModified() {
|
public boolean isSaved() {
|
||||||
return modified;
|
return saved;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveToFile() {
|
public void saveToFile() {
|
||||||
maze.saveToTextFile(filename);
|
maze.saveToTextFile(filename);
|
||||||
|
saved = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveToFile(String filename) {
|
public void saveToFile(String filename) {
|
||||||
this.filename = new String(filename);
|
this.filename = new String(filename);
|
||||||
maze.saveToTextFile(this.filename);
|
maze.saveToTextFile(this.filename);
|
||||||
modified = true;
|
saved = true;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(MazeAppModelMessage.FileChange);
|
notifyObservers(MazeAppModelMessage.FileChange);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,11 @@ public class LoadMazeMenuItem extends JMenuItem
|
|||||||
public void actionPerformed(ActionEvent evt) {
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
|
||||||
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
||||||
|
if (! mazeAppModel.isSaved()) {
|
||||||
|
if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
String filename = FileBox.load(mazeApp);
|
String filename = FileBox.load(mazeApp);
|
||||||
if (filename != null) {
|
if (filename != null) {
|
||||||
mazeApp.getMazeAppModel().loadMaze(filename);
|
mazeApp.getMazeAppModel().loadMaze(filename);
|
||||||
|
|||||||
@@ -26,23 +26,11 @@ public class QuitMenuItem extends JMenuItem
|
|||||||
|
|
||||||
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
||||||
|
|
||||||
if (mazeAppModel.isModified()) {
|
if (! mazeAppModel.isSaved()) {
|
||||||
int response = JOptionPane.showInternalOptionDialog(this,
|
if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) {
|
||||||
"Maze not saved. Save it ?",
|
return;
|
||||||
"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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user