diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 1694450..dcb09b9 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -5,6 +5,7 @@ import maze.Maze; public class MazeAppModel { private Maze maze = new Maze("data/labyrinthe.txt"); + private boolean modified = false; public void setMaze(Maze maze) { this.maze = maze; @@ -13,4 +14,12 @@ public class MazeAppModel { public Maze getMaze() { return maze; } + + public boolean isModified() { + return modified; + } + + public void saveToFile() { + maze.saveToTextFile("data/labyOutput.txt"); + } } \ No newline at end of file diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 30b3196..caead12 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -1,8 +1,15 @@ package ui; -import javax.swing.JMenuItem; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; -public class QuitMenuItem extends JMenuItem { +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; + +import model.MazeAppModel; + +public class QuitMenuItem extends JMenuItem + implements ActionListener { private final MazeApp mazeApp; @@ -10,6 +17,31 @@ public class QuitMenuItem extends JMenuItem { super("Quit"); this.mazeApp = mazeApp; + addActionListener(this); } + public void actionPerformed(ActionEvent evt) { + + 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; + } + } + System.exit(0) ; + } + }