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)
44 lines
989 B
Java
44 lines
989 B
Java
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.showOptionDialog(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;
|
|
}
|
|
}
|