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
37 lines
786 B
Java
37 lines
786 B
Java
package ui;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
|
|
import javax.swing.JMenuItem;
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.KeyStroke;
|
|
|
|
import model.MazeAppModel;
|
|
|
|
public class QuitMenuItem extends JMenuItem
|
|
implements ActionListener {
|
|
|
|
private final MazeApp mazeApp;
|
|
|
|
public QuitMenuItem(MazeApp mazeApp) {
|
|
|
|
super("Quit");
|
|
this.mazeApp = mazeApp;
|
|
this.setAccelerator(KeyStroke.getKeyStroke("control Q"));
|
|
addActionListener(this);
|
|
}
|
|
|
|
public void actionPerformed(ActionEvent evt) {
|
|
|
|
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
|
|
|
if (! mazeAppModel.isSaved()) {
|
|
if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) {
|
|
return;
|
|
}
|
|
}
|
|
System.exit(0);
|
|
}
|
|
}
|