Skip to content

Commit

Permalink
before closing app, ask user to save progress
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Aug 10, 2024
1 parent 33b2dc6 commit 4768076
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/main/java/com/github/creme332/controller/FrameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -39,6 +41,8 @@ public SideMenuController(AppState app, SideMenuPanel sidebar) {
this.sidebar = sidebar;
canvasModel = app.getCanvasModel();

app.addPropertyChangeListener(this);

refreshCanvasSettingsUI();

initializeButtonListeners();
Expand Down Expand Up @@ -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();
}
}
}
7 changes: 6 additions & 1 deletion src/main/java/com/github/creme332/model/AppState.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AppState {

private MenuModel[] menuModels;

Map<Mode, Integer> modeToMenuMapper;
private Map<Mode, Integer> modeToMenuMapper;

/**
* Screen currently visible.
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/github/creme332/view/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 4768076

Please sign in to comment.