From 78dfe72e01273f140989f379f2a2988bbbcc685d Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Thu, 21 Dec 2017 10:13:26 +0100 Subject: [PATCH 1/5] Open app on empty maze and improve load/save model: * Add a filename attribute to MazeAppModel to keep track of the file currently being used by the app * Add saveToFile(filename) to save the maze into a specific file * Rewrite saveToFile() to use the stored filename attribute to know into which file is to be used to save the maze, instead of the hardcoded path * Remove unused models * MazeAppModel is now initialized with an empty maze, instead of a maze read from a hardcoded path (see #13) * Add a MazeAppModelMessage class to pass proper messages to observers * Replace setMaze(maze) by loadMaze(filename) for now * Move paintMaze into ui/MazeApp ui: * Add a Save As button (just a new name for the previous Save button) and implement an actual Save button (which saves into the file currently being used), disabled when no file is currently in use. (see #16) * Create the FileBox class to handle file choices for the load, save, save as buttons * Add keystrokes to trigger Load, Save or Save As actions from the menu maze: * Add a method to Maze to perform a deep copy of a matrix of boxes * Add a static method to generate an empty maze of a fixed size (10*10) This behavior might change in the future * Add a constructor to make a Maze from a matrix of boxes --- src/maze/Maze.java | 58 +++++++++++++++++++---- src/model/Box.java | 10 ---- src/model/Maze.java | 12 ----- src/model/MazeAppModel.java | 75 ++++++++++++++---------------- src/model/MazeAppModelMessage.java | 6 +++ src/ui/FileBox.java | 36 ++++++++++++++ src/ui/FileMenu.java | 6 ++- src/ui/LoadMazeMenuItem.java | 27 +++-------- src/ui/MazeDrawing.java | 39 ++++++++++++++-- src/ui/QuitMenuItem.java | 12 +++-- src/ui/SaveMazeAsMenuItem.java | 32 +++++++++++++ src/ui/SaveMazeMenuItem.java | 38 ++++++++------- 12 files changed, 232 insertions(+), 119 deletions(-) delete mode 100644 src/model/Box.java delete mode 100644 src/model/Maze.java create mode 100644 src/model/MazeAppModelMessage.java create mode 100644 src/ui/FileBox.java create mode 100644 src/ui/SaveMazeAsMenuItem.java diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 8e6edad..e5a5ece 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -29,31 +29,71 @@ public class Maze } - public Maze(Maze maze){ + public Maze(Maze maze) { this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); - this.maze = new Box[dimensionY][dimensionX]; + this.maze = matrixDeepCopy(maze.getMaze()); + } - for (int line = 0; line < dimensionY; line++) { - for (int row = 0; row < dimensionX; row++) { - Box box = maze.getMaze()[line][row]; + public Maze(int numberOfLines, int numberOfRows, Box[][] boxes) { + this.dimensionY = numberOfLines; + this.dimensionX = numberOfRows; + this.maze = matrixDeepCopy(boxes); + } + + private Box[][] matrixDeepCopy (Box[][] original) { + int numberOfLines = 0; + int numberOfRows = 0; + Box[][] copy; + /* + * We first get the dimensions of the matrix + */ + for (Box[] lineBoxes: original) { + numberOfLines++; + } + if (numberOfLines > 0) { + for (Box box: original[0]) { + numberOfRows++; + } + } + copy = new Box[numberOfLines][numberOfRows]; + + /* + * Now we proceed to the deep copy of each box + */ + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + Box box = original[line][row]; if (box.isWalkable()) { if (box.isDeparture()) { - this.maze[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); + copy[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); } else if (box.isArrival()) { - this.maze[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); + copy[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); } else { - this.maze[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); + copy[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); } } else { - this.maze[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); + copy[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); } } } + return copy; + } + + public static Maze emptySquareMaze() { + int dimensionX = 10; + int dimensionY = 10; + Box[][] maze = new Box[dimensionY][dimensionX]; + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + maze[line][row] = new EBox("E", line, row, null); + } + } + return new Maze(dimensionY, dimensionX, maze); } public int getCost(VertexInterface start, VertexInterface end) { diff --git a/src/model/Box.java b/src/model/Box.java deleted file mode 100644 index ff96031..0000000 --- a/src/model/Box.java +++ /dev/null @@ -1,10 +0,0 @@ -package model; - -import maze.Maze; - -public class Box extends maze.Box { - - public Box(String label, int x, int y, Maze maze) { - super(label, x, y, maze); - } -} diff --git a/src/model/Maze.java b/src/model/Maze.java deleted file mode 100644 index 9afdc55..0000000 --- a/src/model/Maze.java +++ /dev/null @@ -1,12 +0,0 @@ -package model; - -public class Maze extends maze.Maze { - - public Maze(String filename) { - super(filename); - } - - public Maze(Maze maze) { - super(maze); - } -} diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index f46b461..f43f38d 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -10,67 +10,60 @@ import java.util.Observer; import dijkstra.VertexInterface; import dijkstra.Dijkstra; import dijkstra.Previous; +import maze.Maze; public class MazeAppModel extends Observable { - private Maze maze = new Maze("data/labyrinthe.txt"); - private VertexInterface departure = maze.getDeparture(); - private VertexInterface arrival = maze.getArrival(); - private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure); - private ArrayList shortest = previous.getShortestPathTo(arrival); - private ArrayList boxList = maze.getVertexes(); + private String filename; + private Maze maze; + private VertexInterface departure; + private VertexInterface arrival; + private Previous previous; + private ArrayList shortest; + private ArrayList boxList; private Boolean modified = false; - public void setMaze(Maze maze) { - this.maze = new Maze(maze); + public MazeAppModel() { + maze = new Maze(Maze.emptySquareMaze()); + departure = maze.getDeparture(); + arrival = maze.getArrival(); + boxList = maze.getVertexes(); + if (departure == null || arrival == null) { + return; + } + previous = (Previous) Dijkstra.dijkstra(maze, departure); + shortest = previous.getShortestPathTo(arrival); + } + + public void loadMaze(String filename) { + this.filename = new String(filename); + maze = new Maze(this.filename); modified = true; setChanged(); - notifyObservers(maze); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public Maze getMaze() { return maze; } + public String getFilename() { + return filename; + } + public boolean isModified() { return modified; } public void saveToFile() { - maze.saveToTextFile("data/labyOutput.txt"); - } - - public void saveToFile(String filename) { maze.saveToTextFile(filename); } - public void paintMaze(Graphics2D g, int width, int height) { - - int dimensionX = maze.getDimensionX(); - int dimensionY = maze.getDimensionY(); - int boxWidth = width/dimensionX; - int boxHeight = height/dimensionY; - - for (int line = 0; line < dimensionY; line++) { - for (int row = 0; row < dimensionX; row++) { - if (maze.getMaze()[line][row].isWalkable()) { - if (maze.getMaze()[line][row].isDeparture()) { - g.setColor(Color.GREEN); - } - else if (maze.getMaze()[line][row].isArrival()) { - g.setColor(Color.RED); - } - else { - g.setColor(Color.BLACK); - } - } - else { - g.setColor(Color.WHITE); - } - g.fill3DRect(row*boxWidth, line*boxHeight, - boxWidth, boxHeight, - true); - } - } + public void saveToFile(String filename) { + this.filename = new String(filename); + maze.saveToTextFile(this.filename); + modified = true; + setChanged(); + notifyObservers(MazeAppModelMessage.FileChange); } } diff --git a/src/model/MazeAppModelMessage.java b/src/model/MazeAppModelMessage.java new file mode 100644 index 0000000..af9cf63 --- /dev/null +++ b/src/model/MazeAppModelMessage.java @@ -0,0 +1,6 @@ +package model; + +public class MazeAppModelMessage { + public final static String MazeRenewal = "MazeRenewal"; + public final static String FileChange = "FileChange"; +} diff --git a/src/ui/FileBox.java b/src/ui/FileBox.java new file mode 100644 index 0000000..fc651c2 --- /dev/null +++ b/src/ui/FileBox.java @@ -0,0 +1,36 @@ +package ui; + +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileNameExtensionFilter; + +public class FileBox { + private static JFileChooser fileChooser = new JFileChooser(); + private static FileNameExtensionFilter filter = + new FileNameExtensionFilter("Text files", "txt"); + + public static String load(MazeApp mazeApp) { + fileChooser.setFileFilter(filter); + + int returnVal = fileChooser.showOpenDialog(mazeApp); + if(returnVal == JFileChooser.APPROVE_OPTION) { + System.out.println("You chose to open this file: " + + fileChooser.getSelectedFile().getName()); + String filename = fileChooser.getSelectedFile().getPath(); + return filename; + } + return null; + } + + public static String save(MazeApp mazeApp) { + fileChooser.setFileFilter(filter); + + int returnVal = fileChooser.showSaveDialog(mazeApp); + if(returnVal == JFileChooser.APPROVE_OPTION) { + System.out.println("You chose to save to this file: " + + fileChooser.getSelectedFile().getName()); + String filename = fileChooser.getSelectedFile().getPath(); + return filename; + } + return null; + } +} diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index 67447c3..baadb8e 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -5,17 +5,19 @@ import javax.swing.JMenu; public class FileMenu extends JMenu { private final MazeApp mazeApp; - private final QuitMenuItem quitMenuItem; private final LoadMazeMenuItem loadMazeMenuItem; private final SaveMazeMenuItem saveMazeMenuItem; + private final SaveMazeAsMenuItem saveMazeAsMenuItem; + private final QuitMenuItem quitMenuItem; public FileMenu(MazeApp mazeApp) { super("File"); this.mazeApp = mazeApp; - add(quitMenuItem = new QuitMenuItem(mazeApp)); add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp)); add(saveMazeMenuItem = new SaveMazeMenuItem(mazeApp)); + add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp)); + add(quitMenuItem = new QuitMenuItem(mazeApp)); } } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index b0147f6..40d4a49 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -1,16 +1,12 @@ package ui; -import javax.swing.JFileChooser; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; -import javax.swing.filechooser.FileNameExtensionFilter; - -import model.MazeAppModel; -import model.Maze; - +import javax.swing.KeyStroke; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import model.MazeAppModel; + public class LoadMazeMenuItem extends JMenuItem implements ActionListener { @@ -20,25 +16,16 @@ public class LoadMazeMenuItem extends JMenuItem super("Load maze"); this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control O")); addActionListener(this); } public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - JFileChooser fileChooser = new JFileChooser(); - - FileNameExtensionFilter filter = - new FileNameExtensionFilter("Text files", "txt"); - fileChooser.setFileFilter(filter); - - String filename; - int returnVal = fileChooser.showOpenDialog(mazeApp); - if(returnVal == JFileChooser.APPROVE_OPTION) { - System.out.println("You chose to open this file: " + - fileChooser.getSelectedFile().getName()); - filename = fileChooser.getSelectedFile().getPath(); - mazeApp.getMazeAppModel().setMaze(new Maze(filename)); + String filename = FileBox.load(mazeApp); + if (filename != null) { + mazeApp.getMazeAppModel().loadMaze(filename); } } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index f43e4ea..3f33f4c 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -7,6 +7,7 @@ import java.awt.Graphics2D; import java.awt.GridLayout; import javax.swing.JPanel; import maze.Maze; +import model.MazeAppModelMessage; public class MazeDrawing extends JPanel { @@ -17,7 +18,6 @@ public class MazeDrawing extends JPanel { this.mazeApp = mazeApp; setBackground(Color.BLACK); setPreferredSize(new Dimension(500,500)) ; - } @Override @@ -29,13 +29,44 @@ public class MazeDrawing extends JPanel { int width = getWidth(); int height = getHeight(); - mazeApp.getMazeAppModel().paintMaze(g2d, width, height); - + paintMaze(g2d, width, height); + } + + private void paintMaze(Graphics2D g, int width, int height) { + Maze maze = mazeApp.getMazeAppModel().getMaze(); + int dimensionX = maze.getDimensionX(); + int dimensionY = maze.getDimensionY(); + int boxWidth = width / dimensionX; + int boxHeight = height / dimensionY; + + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + if (maze.getMaze()[line][row].isWalkable()) { + if (maze.getMaze()[line][row].isDeparture()) { + g.setColor(Color.GREEN); + } + else if (maze.getMaze()[line][row].isArrival()) { + g.setColor(Color.RED); + } + else { + g.setColor(Color.BLACK); + } + } + else { + g.setColor(Color.WHITE); + } + g.fill3DRect(row * boxWidth, + line * boxHeight, + boxWidth, + boxHeight, + true); + } + } } public void notifyForUpdates(Object param) { - if (param instanceof Maze) { + if (param == MazeAppModelMessage.MazeRenewal) { this.repaint(); } } diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 46908e5..13250a0 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -5,6 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; import javax.swing.JOptionPane; +import javax.swing.KeyStroke; import model.MazeAppModel; @@ -17,6 +18,7 @@ public class QuitMenuItem extends JMenuItem super("Quit"); this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control Q")); addActionListener(this); } @@ -26,11 +28,11 @@ public class QuitMenuItem extends JMenuItem 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); + "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; diff --git a/src/ui/SaveMazeAsMenuItem.java b/src/ui/SaveMazeAsMenuItem.java new file mode 100644 index 0000000..22195f1 --- /dev/null +++ b/src/ui/SaveMazeAsMenuItem.java @@ -0,0 +1,32 @@ +package ui; + +import javax.swing.JMenuItem; +import javax.swing.KeyStroke; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import model.MazeAppModel; + +public class SaveMazeAsMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public SaveMazeAsMenuItem(MazeApp mazeApp) { + + super("Save maze as"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control shift S")); + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + + String filename = FileBox.save(mazeApp); + if (filename != null) { + mazeApp.getMazeAppModel().saveToFile(filename); + } + } +} diff --git a/src/ui/SaveMazeMenuItem.java b/src/ui/SaveMazeMenuItem.java index 4beefbe..f5026d2 100644 --- a/src/ui/SaveMazeMenuItem.java +++ b/src/ui/SaveMazeMenuItem.java @@ -1,16 +1,16 @@ package ui; -import javax.swing.JFileChooser; import javax.swing.JMenuItem; -import javax.swing.filechooser.FileNameExtensionFilter; +import javax.swing.KeyStroke; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Observer; +import java.util.Observable; import model.MazeAppModel; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - public class SaveMazeMenuItem extends JMenuItem - implements ActionListener { + implements ActionListener, Observer { private final MazeApp mazeApp; @@ -18,24 +18,30 @@ public class SaveMazeMenuItem extends JMenuItem super("Save maze"); this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control S")); + mazeApp.getMazeAppModel().addObserver(this); + if (mazeApp.getMazeAppModel().getFilename() == null) { + this.setEnabled(false); + } addActionListener(this); } public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - JFileChooser fileChooser = new JFileChooser(); - FileNameExtensionFilter filter = - new FileNameExtensionFilter("Text files", "txt"); - fileChooser.setFileFilter(filter); + if (mazeAppModel.getFilename() != null) { + mazeAppModel.saveToFile(); + } + } - int returnVal = fileChooser.showSaveDialog(mazeApp); - if(returnVal == JFileChooser.APPROVE_OPTION) { - System.out.println("You chose to save to this file: " + - fileChooser.getSelectedFile().getName()); - String filename = fileChooser.getSelectedFile().getPath(); - mazeApp.getMazeAppModel().saveToFile(filename); + @Override + public void update(Observable obs, Object param) { + if (mazeApp.getMazeAppModel().getFilename() != null) { + this.setEnabled(true); + } + else { + this.setEnabled(true); } } } From 97f9aa4ba06b4d231750283a10545abbab558ca0 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 23 Dec 2017 21:00:03 +0100 Subject: [PATCH 2/5] Add generic maze generation methods maze: * Change emptySquareMaze() to become emptyMaze(int dimension) * emptyMaze now takes 1 or 2 parameteres, and accordingly generate a square or a rectangle maze of the given dimension(s) * Add wallMaze, which generate a maze full of walls. As emptyMaze, it takes one or two parameters model: * Update mazeAppModel tu use the new emptyMaze method --- src/maze/Maze.java | 42 ++++++++++++++++++++++++++++++------- src/model/MazeAppModel.java | 2 +- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/maze/Maze.java b/src/maze/Maze.java index e5a5ece..f626bef 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -84,16 +84,44 @@ public class Maze return copy; } - public static Maze emptySquareMaze() { - int dimensionX = 10; - int dimensionY = 10; - Box[][] maze = new Box[dimensionY][dimensionX]; - for (int line = 0; line < dimensionY; line++) { - for (int row = 0; row < dimensionX; row++) { + public static Maze emptyMaze(int dimension) { + Box[][] maze = new Box[dimension][dimension]; + for (int line = 0; line < dimension; line++) { + for (int row = 0; row < dimension; row++) { maze[line][row] = new EBox("E", line, row, null); } } - return new Maze(dimensionY, dimensionX, maze); + return new Maze(dimension, dimension, maze); + } + + public static Maze emptyMaze(int numberOfLines, int numberOfRows) { + Box[][] maze = new Box[numberOfLines][numberOfRows]; + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + maze[line][row] = new EBox("E", line, row, null); + } + } + return new Maze(numberOfLines, numberOfRows, maze); + } + + public static Maze wallMaze(int dimension) { + Box[][] maze = new Box[dimension][dimension]; + for (int line = 0; line < dimension; line++) { + for (int row = 0; row < dimension; row++) { + maze[line][row] = new WBox("W", line, row, null); + } + } + return new Maze(dimension, dimension, maze); + } + + public static Maze wallMaze(int numberOfLines, int numberOfRows) { + Box[][] maze = new Box[numberOfLines][numberOfRows]; + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + maze[line][row] = new WBox("W", line, row, null); + } + } + return new Maze(numberOfLines, numberOfRows, maze); } public int getCost(VertexInterface start, VertexInterface end) { diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index f43f38d..ae03bda 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -24,7 +24,7 @@ public class MazeAppModel extends Observable { private Boolean modified = false; public MazeAppModel() { - maze = new Maze(Maze.emptySquareMaze()); + maze = new Maze(Maze.emptyMaze(10)); departure = maze.getDeparture(); arrival = maze.getArrival(); boxList = maze.getVertexes(); From 80ffabdd69100f19fd8a7f0ea451da124ba915f6 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 23 Dec 2017 21:21:47 +0100 Subject: [PATCH 3/5] Refresh attributes in MazeAppModel at maze loading model: * Add initModelFromMaze() to refresh the model attributes, such as arrival, departure, previous, etc, at maze change --- src/model/MazeAppModel.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index ae03bda..7ab7954 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -14,7 +14,7 @@ import maze.Maze; public class MazeAppModel extends Observable { - private String filename; + private String filename; private Maze maze; private VertexInterface departure; private VertexInterface arrival; @@ -25,10 +25,16 @@ public class MazeAppModel extends Observable { public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); + initModelFromMaze(); + } + + private void initModelFromMaze() { departure = maze.getDeparture(); arrival = maze.getArrival(); boxList = maze.getVertexes(); if (departure == null || arrival == null) { + previous = null; + shortest = null; return; } previous = (Previous) Dijkstra.dijkstra(maze, departure); @@ -38,6 +44,7 @@ public class MazeAppModel extends Observable { public void loadMaze(String filename) { this.filename = new String(filename); maze = new Maze(this.filename); + initModelFromMaze(); modified = true; setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); From 9b272e5f25f9985f44e5def436a9d9038c0156c8 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Tue, 26 Dec 2017 14:45:11 +0100 Subject: [PATCH 4/5] Ask to save before loading a new maze model: * Deprecate modified from MazeAppModel, which was misused and misunderstood * Add saved to MazeAppModel to keep track if a maze (or some modification) was saved into a file or not ui: * Use the new saved attribute to know if the maze was saved or not * Move the box asking whether to save the maze or not from QuitMenuItem to SaveBox and call SaveBox from QuitMenuItem * SaveBox now checks if the maze has a source file or not, if not it prompts a box to ask where to save the maze * If the maze was modified, the Load Maze button will pop SaveBox before erasing the current maze --- src/model/MazeAppModel.java | 11 ++++----- src/ui/LoadMazeMenuItem.java | 5 +++++ src/ui/QuitMenuItem.java | 22 +++++------------- src/ui/SaveBox.java | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 src/ui/SaveBox.java diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 7ab7954..4214be0 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -21,7 +21,7 @@ public class MazeAppModel extends Observable { private Previous previous; private ArrayList shortest; private ArrayList boxList; - private Boolean modified = false; + private boolean saved = true; public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); @@ -45,7 +45,7 @@ public class MazeAppModel extends Observable { this.filename = new String(filename); maze = new Maze(this.filename); initModelFromMaze(); - modified = true; + saved = true; setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); } @@ -58,18 +58,19 @@ public class MazeAppModel extends Observable { return filename; } - public boolean isModified() { - return modified; + public boolean isSaved() { + return saved; } public void saveToFile() { maze.saveToTextFile(filename); + saved = true; } public void saveToFile(String filename) { this.filename = new String(filename); maze.saveToTextFile(this.filename); - modified = true; + saved = true; setChanged(); notifyObservers(MazeAppModelMessage.FileChange); } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 40d4a49..05cf17e 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -23,6 +23,11 @@ public class LoadMazeMenuItem extends JMenuItem public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){ + return; + } + } String filename = FileBox.load(mazeApp); if (filename != null) { mazeApp.getMazeAppModel().loadMaze(filename); diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 13250a0..c8632ce 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -26,23 +26,11 @@ public class QuitMenuItem extends JMenuItem 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; - } - } + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { + return; + } + } System.exit(0); } } diff --git a/src/ui/SaveBox.java b/src/ui/SaveBox.java new file mode 100644 index 0000000..d333279 --- /dev/null +++ b/src/ui/SaveBox.java @@ -0,0 +1,43 @@ +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.showInternalOptionDialog(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; + } +} From 8bef42e2a6d5836e26fb56edfebd5e2bde3ef864 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Tue, 26 Dec 2017 16:41:11 +0100 Subject: [PATCH 5/5] 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) --- src/ui/MazeApp.java | 40 ++++++++++++++++++++++++++++++++++++++-- src/ui/SaveBox.java | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 2e7b37d..de0f36a 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -6,9 +6,11 @@ import model.MazeAppModel; import java.util.Observable; import java.util.Observer; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; public class MazeApp extends JFrame - implements Observer { + implements Observer, WindowListener { private final MazeMenuBar mazeMenuBar; private final WindowPanel windowPanel; @@ -21,10 +23,11 @@ public class MazeApp extends JFrame this.mazeAppModel = new MazeAppModel(); mazeAppModel.addObserver(this); + addWindowListener(this); setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); setContentPane(windowPanel = new WindowPanel(this)); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); pack(); setVisible(true); @@ -40,4 +43,37 @@ public class MazeApp extends JFrame System.out.println("New maze detected"); 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; + } } diff --git a/src/ui/SaveBox.java b/src/ui/SaveBox.java index d333279..ae354f2 100644 --- a/src/ui/SaveBox.java +++ b/src/ui/SaveBox.java @@ -12,7 +12,7 @@ public class SaveBox { MazeApp mazeApp, String title) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - int response = JOptionPane.showInternalOptionDialog(parent, + int response = JOptionPane.showOptionDialog(parent, message, title, JOptionPane.YES_NO_CANCEL_OPTION,