diff --git a/src/main/java/com/github/creme332/controller/FrameController.java b/src/main/java/com/github/creme332/controller/FrameController.java index 4623f49..b1d052b 100644 --- a/src/main/java/com/github/creme332/controller/FrameController.java +++ b/src/main/java/com/github/creme332/controller/FrameController.java @@ -2,6 +2,8 @@ import java.awt.Dimension; import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.ComponentAdapter; @@ -37,6 +39,31 @@ public FrameController(AppState model, Frame frame) { this.frame = frame; this.app = model; + // Add a WindowListener to handle the close operation + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + // Show a confirmation dialog + int option = JOptionPane.showConfirmDialog( + frame, + "Do you want to save your progress before exiting?", + "Save Progress", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE); + + if (option == JOptionPane.YES_OPTION) { + app.notifyExportToCanvasHandler(); + + // Exit the application + frame.dispose(); + } else if (option == JOptionPane.NO_OPTION) { + // Exit without saving + frame.dispose(); + } + // If Cancel (or X) is selected, do nothing, keep the window open + } + }); + // create controller for menubar of frame new MenuBarController(app, frame.getMyMenuBar()); diff --git a/src/main/java/com/github/creme332/controller/console/SideMenuController.java b/src/main/java/com/github/creme332/controller/console/SideMenuController.java index 4f44bb9..ba8bf81 100644 --- a/src/main/java/com/github/creme332/controller/console/SideMenuController.java +++ b/src/main/java/com/github/creme332/controller/console/SideMenuController.java @@ -6,6 +6,8 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -28,7 +30,7 @@ /** * Controller responsible for managing sidebar in CanvasConsole. */ -public class SideMenuController { +public class SideMenuController implements PropertyChangeListener { private SideMenuPanel sidebar; private AppState app; @@ -39,6 +41,8 @@ public SideMenuController(AppState app, SideMenuPanel sidebar) { this.sidebar = sidebar; canvasModel = app.getCanvasModel(); + app.addPropertyChangeListener(this); + refreshCanvasSettingsUI(); initializeButtonListeners(); @@ -269,4 +273,12 @@ private void handleCanvasToJSON() { } sidebar.getTopLevelAncestor().requestFocus(); } + + @Override + public void propertyChange(PropertyChangeEvent evt) { + // if printingCanvas property has changed to true, handle export + if ("exportCanvasToJSON".equals(evt.getPropertyName())) { + handleCanvasToJSON(); + } + } } diff --git a/src/main/java/com/github/creme332/model/AppState.java b/src/main/java/com/github/creme332/model/AppState.java index e27ece3..0c52f98 100644 --- a/src/main/java/com/github/creme332/model/AppState.java +++ b/src/main/java/com/github/creme332/model/AppState.java @@ -15,7 +15,7 @@ public class AppState { private MenuModel[] menuModels; - Map modeToMenuMapper; + private Map modeToMenuMapper; /** * Screen currently visible. @@ -125,6 +125,7 @@ public void addPropertyChangeListener(PropertyChangeListener listener) { support.addPropertyChangeListener("maximizeFrame", listener); support.addPropertyChangeListener("activateToast", listener); support.addPropertyChangeListener("printingCanvas", listener); + support.addPropertyChangeListener("exportCanvasToJSON", listener); } public boolean getSideBarVisibility() { @@ -170,4 +171,8 @@ public void setMaximizeFrame(boolean maximizeFrame) { public void startPrintingProcess() { support.firePropertyChange("printingCanvas", null, true); } + + public void notifyExportToCanvasHandler() { + support.firePropertyChange("exportCanvasToJSON", null, true); + } } \ No newline at end of file diff --git a/src/main/java/com/github/creme332/view/Frame.java b/src/main/java/com/github/creme332/view/Frame.java index 9314613..ee489b8 100644 --- a/src/main/java/com/github/creme332/view/Frame.java +++ b/src/main/java/com/github/creme332/view/Frame.java @@ -5,6 +5,7 @@ import javax.swing.JFrame; import javax.swing.JLayeredPane; import javax.swing.JPanel; +import javax.swing.WindowConstants; import static com.github.creme332.utils.IconLoader.loadIcon; @@ -59,8 +60,7 @@ public void initFrameProperties() throws InvalidPathException { // make frame resizable this.setResizable(true); - // add close button to frame - this.setDefaultCloseOperation(EXIT_ON_CLOSE); + this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); // set application icon this.setIconImage(loadIcon("/icons/icosahedron.png").getImage());