Closing the window now does the same as Quit
ui: * Add a WindowListener catching the event of closing the window to ask the user if the maze must be saved or not (if it's not already saved) (same behavior as the Quit button) (see #19)
This commit is contained in:
1 parent
9b272e5f25
commit
8bef42e2a6
2 files changed
+39
-3
No files matched your search
+38
-2
@@ -6,9 +6,11 @@ import model.MazeAppModel;
|
|||||||
|
|
||||||
import java.util.Observable;
|
import java.util.Observable;
|
||||||
import java.util.Observer;
|
import java.util.Observer;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.awt.event.WindowListener;
|
||||||
|
|
||||||
public class MazeApp extends JFrame
|
public class MazeApp extends JFrame
|
||||||
implements Observer {
|
implements Observer, WindowListener {
|
||||||
|
|
||||||
private final MazeMenuBar mazeMenuBar;
|
private final MazeMenuBar mazeMenuBar;
|
||||||
private final WindowPanel windowPanel;
|
private final WindowPanel windowPanel;
|
||||||
@@ -21,10 +23,11 @@ public class MazeApp extends JFrame
|
|||||||
this.mazeAppModel = new MazeAppModel();
|
this.mazeAppModel = new MazeAppModel();
|
||||||
mazeAppModel.addObserver(this);
|
mazeAppModel.addObserver(this);
|
||||||
|
|
||||||
|
addWindowListener(this);
|
||||||
setJMenuBar(mazeMenuBar = new MazeMenuBar(this));
|
setJMenuBar(mazeMenuBar = new MazeMenuBar(this));
|
||||||
setContentPane(windowPanel = new WindowPanel(this));
|
setContentPane(windowPanel = new WindowPanel(this));
|
||||||
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
||||||
|
|
||||||
pack();
|
pack();
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
@@ -40,4 +43,37 @@ public class MazeApp extends JFrame
|
|||||||
System.out.println("New maze detected");
|
System.out.println("New maze detected");
|
||||||
windowPanel.notifyForUpdates(param);
|
windowPanel.notifyForUpdates(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void windowClosing(WindowEvent e) {
|
||||||
|
if (! mazeAppModel.isSaved()) {
|
||||||
|
if (! SaveBox.promptAndContinue(null, this, "Quit application")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void windowDeactivated(WindowEvent e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void windowActivated(WindowEvent e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void windowDeiconified(WindowEvent e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void windowIconified(WindowEvent e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void windowClosed(WindowEvent e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void windowOpened(WindowEvent e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -12,7 +12,7 @@ public class SaveBox {
|
|||||||
MazeApp mazeApp,
|
MazeApp mazeApp,
|
||||||
String title) {
|
String title) {
|
||||||
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
||||||
int response = JOptionPane.showInternalOptionDialog(parent,
|
int response = JOptionPane.showOptionDialog(parent,
|
||||||
message,
|
message,
|
||||||
title,
|
title,
|
||||||
JOptionPane.YES_NO_CANCEL_OPTION,
|
JOptionPane.YES_NO_CANCEL_OPTION,
|
||||||
|
|||||||
Reference in new issue
Block a user