diff --git a/src/dijkstra/ASet.java b/src/dijkstra/ASet.java index 09e41a8..3bfeff3 100644 --- a/src/dijkstra/ASet.java +++ b/src/dijkstra/ASet.java @@ -2,7 +2,7 @@ package dijkstra; import java.util.HashSet; -public class ASet implements ASetInterface { +public final class ASet implements ASetInterface { private HashSet vertexHashSet; diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index d709f32..2437f1a 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -3,7 +3,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; -public class Dijkstra { +public final class Dijkstra { private static PreviousInterface dijkstra(GraphInterface g, VertexInterface r, diff --git a/src/dijkstra/Pi.java b/src/dijkstra/Pi.java index 6b7a7ca..43356bb 100644 --- a/src/dijkstra/Pi.java +++ b/src/dijkstra/Pi.java @@ -2,7 +2,7 @@ package dijkstra; import java.util.Hashtable; -public class Pi implements PiInterface { +public final class Pi implements PiInterface { private Hashtable hashPi; diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index 0b5c613..95f956d 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -6,7 +6,7 @@ import java.util.List; import maze.Box; -public class Previous implements PreviousInterface { +public final class Previous implements PreviousInterface { private Hashtable hashPrevious; private ArrayList shortestPath; @@ -32,13 +32,13 @@ public class Previous implements PreviousInterface { shortestPath.add(vertex); VertexInterface father = getPrevious(vertex); - + if (getPrevious(father) == null) { shortestPath.add(father); return shortestPath; } else { return getShortestPathTo(father); - } + } } } diff --git a/src/maze/ABox.java b/src/maze/ABox.java index 66b5a08..3492c9d 100644 --- a/src/maze/ABox.java +++ b/src/maze/ABox.java @@ -5,9 +5,9 @@ package maze; * It's the last Box to be walked through and * it's an exit to the Maze. */ -public class ABox extends EBox{ +public final class ABox extends EBox{ - public ABox (String label, int y, int x, Maze maze) { + public ABox(String label, int y, int x, Maze maze) { super(label, y, x, maze) ; } diff --git a/src/maze/Box.java b/src/maze/Box.java index db1871f..8b4d4d5 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -24,17 +24,16 @@ public abstract class Box private String label; private Maze maze; private ArrayList neighbourList; - private ArrayList candidates; + private ArrayList candidates = + new ArrayList(); - public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation - + public Box(String label, int y , int x, Maze maze) { //y first because of graphical maze representation this.label = label; this.x = x; this.y = y; this.maze = maze; - this.candidates = new ArrayList(); } - + public ArrayList generateNeighbours() { this.neighbourList = maze.generateNeighbours(this); return this.neighbourList; @@ -63,7 +62,7 @@ public abstract class Box public boolean isDeparture() { return false; } - + public ArrayList getNeighbourList() { return neighbourList; } diff --git a/src/maze/DBox.java b/src/maze/DBox.java index aacfc1d..21057cc 100644 --- a/src/maze/DBox.java +++ b/src/maze/DBox.java @@ -5,9 +5,9 @@ package maze; * is first in the Maze. */ -public class DBox extends EBox{ +public final class DBox extends EBox{ - public DBox (String label, int y, int x, Maze maze) { + public DBox(String label, int y, int x, Maze maze) { super(label, y, x, maze); } diff --git a/src/maze/EBox.java b/src/maze/EBox.java index 8c50e0c..b308f8d 100644 --- a/src/maze/EBox.java +++ b/src/maze/EBox.java @@ -7,7 +7,7 @@ package maze; public class EBox extends Box{ - public EBox (String label, int y, int x, Maze maze) { + public EBox(String label, int y, int x, Maze maze) { super(label, y, x, maze); } } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index a30a2b8..c0c173e 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -16,7 +16,7 @@ import dijkstra.VertexInterface; * @author Sylvain Rager * */ -public class Maze +public final class Maze implements GraphInterface { private int dimensionX; @@ -26,7 +26,7 @@ public class Maze public Maze(String filename) { initFromTextFile(filename); } - + public Maze(Maze maze) { this.dimensionX = maze.getDimensionX(); @@ -369,7 +369,7 @@ public class Maze } public ArrayList generateNeighbours(VertexInterface vertex) { - + Box box = (Box) vertex; int x = box.getX(); int y = box.getY(); @@ -383,28 +383,28 @@ public class Maze if (x > 0 && maze[y][x-1].isWalkable()) { neighbourList.add(maze[y][x-1]); } - + if (x < dimensionX-1 && maze[y][x+1].isWalkable()) { neighbourList.add(maze[y][x+1]); } - + if (y > 0 && maze[y-1][x].isWalkable()) { neighbourList.add(maze[y-1][x]); } - + if (y < dimensionY-1 && maze[y+1][x].isWalkable()) { neighbourList.add(maze[y+1][x]); } - + return neighbourList; } - /**Constructs the maze from a text file, according to each single character on each line. + /**Constructs the maze from a text file, according to each single character on each line. * @param filename * The path to the file containing the representation of the maze. */ - private final void initFromTextFile(String filename) { + private void initFromTextFile(String filename) { FileReader fr = null; BufferedReader br = null; @@ -422,9 +422,9 @@ public class Maze try { //generates the maze fr = new FileReader(filename); br = new BufferedReader(fr); - + String currentLine = null; - + currentLine = br.readLine(); dimensionX = currentLine.length(); //maze's width int currentLineLength; @@ -443,7 +443,7 @@ public class Maze char currentChar = currentLine.charAt(compteurColonne); switch(currentChar) { - + case 'W': maze[compteurLigne][compteurColonne] = new WBox("W", compteurLigne, compteurColonne, this) ; @@ -483,7 +483,7 @@ public class Maze } } - public final void saveToTextFile(String fileName) { + public void saveToTextFile(String fileName) { FileOutputStream fos = null; PrintWriter pw = null; @@ -527,7 +527,7 @@ public class Maze return dimensionY; } - public Box[][] getMaze() { + protected Box[][] getMaze() { return maze; } diff --git a/src/maze/MazeReadingException.java b/src/maze/MazeReadingException.java index 47b5600..2e42204 100644 --- a/src/maze/MazeReadingException.java +++ b/src/maze/MazeReadingException.java @@ -8,7 +8,7 @@ package maze; * */ -public class MazeReadingException +public final class MazeReadingException extends Exception { private static final long serialVersionUID = 1L; diff --git a/src/maze/WBox.java b/src/maze/WBox.java index cb17318..6c4434a 100644 --- a/src/maze/WBox.java +++ b/src/maze/WBox.java @@ -5,9 +5,9 @@ package maze; * that can't be walked through (a wall). */ -public class WBox extends Box { +public final class WBox extends Box { - public WBox (String label, int y, int x, Maze maze) { + public WBox(String label, int y, int x, Maze maze) { super(label, y, x, maze); } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 4e0e939..846db8d 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -11,14 +11,13 @@ import dijkstra.*; import maze.*; public class MazeAppModel extends Observable { - + /** The file into which the maze is to be saved */ private String filename; private Maze maze; private VertexInterface departure; private VertexInterface arrival; private Previous previous; private ArrayList shortest; - private ArrayList boxList; private boolean saved = true; /* * There are three states for pathDrawn : @@ -38,7 +37,6 @@ public class MazeAppModel extends Observable { private void initModelFromMaze() { departure = maze.getDeparture(); arrival = maze.getArrival(); - boxList = maze.getVertexes(); if (departure == null || arrival == null) { previous = null; shortest = null; @@ -52,8 +50,6 @@ public class MazeAppModel extends Observable { shortest = previous.getShortestPathTo(arrival); } } - setChanged(); - notifyObservers(MazeAppModelMessage.MazeRenewal); } public void loadMaze(String filename) { @@ -61,16 +57,14 @@ public class MazeAppModel extends Observable { maze = new Maze(this.filename); initModelFromMaze(); saved = true; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeLoaded); } public Maze getMaze() { return maze; } - public Box[][] getBoxMaze() { - return this.maze.getMaze(); - } - public Box getBox(int boxLine, int boxRow) { return this.maze.getBox(boxLine, boxRow); } @@ -87,12 +81,12 @@ public class MazeAppModel extends Observable { return shortest; } - public ArrayList getShortestDeepCopy() { - ArrayList deepCopy = new ArrayList(); + public ArrayList getShortestShallowCopy() { + ArrayList shallowCopy = new ArrayList(); for (VertexInterface vertex : this.shortest) { - deepCopy.add(vertex); + shallowCopy.add(vertex); } - return deepCopy; + return shallowCopy; } public int isPathDrawn() { @@ -106,10 +100,12 @@ public class MazeAppModel extends Observable { public void saveToFile() { maze.saveToTextFile(filename); saved = true; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeSaved); } public void saveToFile(String filename) { - this.filename = new String(filename); + this.filename = filename; maze.saveToTextFile(this.filename); saved = true; setChanged(); @@ -117,7 +113,7 @@ public class MazeAppModel extends Observable { } public void toggleBox(int boxLine, int boxRow) { - if (maze.getMaze()[boxLine][boxRow].isWalkable()) { + if (maze.getBox(boxLine, boxRow).isWalkable()) { setWBox(boxLine, boxRow); } else { @@ -130,6 +126,8 @@ public class MazeAppModel extends Observable { maze.resetNeighbourLists(); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void setEBox(int boxLine, int boxRow) { @@ -137,6 +135,8 @@ public class MazeAppModel extends Observable { maze.resetNeighbourLists(); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void setDBox(int boxLine, int boxRow) { @@ -149,6 +149,8 @@ public class MazeAppModel extends Observable { maze.resetNeighbourLists(); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void setABox(int boxLine, int boxRow) { @@ -161,6 +163,8 @@ public class MazeAppModel extends Observable { maze.resetNeighbourLists(); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void newClearMaze(int numberOfLines, int numberOfRows) { @@ -168,6 +172,8 @@ public class MazeAppModel extends Observable { maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows)); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void addEmptyColumn() { @@ -178,12 +184,16 @@ public class MazeAppModel extends Observable { maze.addEmptyColumn(posColumn); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void delColumn(int posColumn) { maze.delColumn(posColumn); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void delColumn() { @@ -198,12 +208,16 @@ public class MazeAppModel extends Observable { maze.addEmptyLine(posLine); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void delLine(int posLine) { maze.delLine(posLine); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void delLine() { @@ -219,10 +233,10 @@ public class MazeAppModel extends Observable { } public int getDimensionX() { - return this.maze.getDimensionX(); + return maze.getDimensionX(); } public int getDimensionY() { - return this.maze.getDimensionX(); + return maze.getDimensionY(); } } diff --git a/src/model/MazeAppModelMessage.java b/src/model/MazeAppModelMessage.java index af9cf63..358b2db 100644 --- a/src/model/MazeAppModelMessage.java +++ b/src/model/MazeAppModelMessage.java @@ -1,6 +1,19 @@ package model; -public class MazeAppModelMessage { - public final static String MazeRenewal = "MazeRenewal"; - public final static String FileChange = "FileChange"; +/** + * This class is used to transport messages about an event happening + * to the model + */ +public abstract class MazeAppModelMessage { + /** Indicates that the maze has been renewed + *(a replacement of the maze -resizing, new maze, ...- + * or a change on any box) + */ + public final static String MazeRenewal = "Maze renewed"; + /** Indicates a new file has been selected for the maze to be saved in */ + public final static String FileChange = "File changed"; + /** Indicates the maze has just been saved */ + public final static String MazeSaved = "Maze saved"; + /** Indicates a maze has been loaded from file */ + public final static String MazeLoaded = "Maze loaded"; } diff --git a/src/ui/AddLineMenuItem.java b/src/ui/AddLineMenuItem.java index 095760d..be9a246 100644 --- a/src/ui/AddLineMenuItem.java +++ b/src/ui/AddLineMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class AddLineMenuItem extends JMenuItem +public final class AddLineMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/AddRowMenuItem.java b/src/ui/AddRowMenuItem.java index b49dfd7..2bf355b 100644 --- a/src/ui/AddRowMenuItem.java +++ b/src/ui/AddRowMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class AddRowMenuItem extends JMenuItem +public final class AddRowMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/AnimatePathMenuItem.java b/src/ui/AnimatePathMenuItem.java index f1f2f08..dd37230 100644 --- a/src/ui/AnimatePathMenuItem.java +++ b/src/ui/AnimatePathMenuItem.java @@ -7,7 +7,7 @@ import javax.swing.Action; import javax.swing.Icon; import javax.swing.JMenuItem; -public class AnimatePathMenuItem extends JMenuItem +public final class AnimatePathMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/BoxTypeEditionMenu.java b/src/ui/BoxTypeEditionMenu.java index b49e166..9c83640 100644 --- a/src/ui/BoxTypeEditionMenu.java +++ b/src/ui/BoxTypeEditionMenu.java @@ -4,7 +4,7 @@ import javax.swing.JPopupMenu; import javax.swing.JMenuItem; import maze.Box; -public class BoxTypeEditionMenu extends JPopupMenu { +public final class BoxTypeEditionMenu extends JPopupMenu { JMenuItem setWBox; JMenuItem setEBox; diff --git a/src/ui/ClearMazeMenuItem.java b/src/ui/ClearMazeMenuItem.java index aeed823..24d0d0f 100644 --- a/src/ui/ClearMazeMenuItem.java +++ b/src/ui/ClearMazeMenuItem.java @@ -6,7 +6,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; import javax.swing.KeyStroke; -public class ClearMazeMenuItem extends JMenuItem +public final class ClearMazeMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/ClearShortestPathMenuItem.java b/src/ui/ClearShortestPathMenuItem.java index e5bff7b..7cbd439 100644 --- a/src/ui/ClearShortestPathMenuItem.java +++ b/src/ui/ClearShortestPathMenuItem.java @@ -7,7 +7,7 @@ import model.MazeAppModel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -public class ClearShortestPathMenuItem extends JMenuItem +public final class ClearShortestPathMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/DelLineMenuItem.java b/src/ui/DelLineMenuItem.java index 165ac6e..d33db6a 100644 --- a/src/ui/DelLineMenuItem.java +++ b/src/ui/DelLineMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class DelLineMenuItem extends JMenuItem +public final class DelLineMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/DelRowMenuItem.java b/src/ui/DelRowMenuItem.java index 243b129..c8b876a 100644 --- a/src/ui/DelRowMenuItem.java +++ b/src/ui/DelRowMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class DelRowMenuItem extends JMenuItem +public final class DelRowMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/DrawShortestPathMenuItem.java b/src/ui/DrawShortestPathMenuItem.java index 4338172..b713994 100644 --- a/src/ui/DrawShortestPathMenuItem.java +++ b/src/ui/DrawShortestPathMenuItem.java @@ -7,7 +7,7 @@ import model.MazeAppModel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -public class DrawShortestPathMenuItem extends JMenuItem +public final class DrawShortestPathMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/FileBox.java b/src/ui/FileBox.java index fc651c2..9ad443c 100644 --- a/src/ui/FileBox.java +++ b/src/ui/FileBox.java @@ -3,7 +3,7 @@ package ui; import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; -public class FileBox { +public final class FileBox { private static JFileChooser fileChooser = new JFileChooser(); private static FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files", "txt"); diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index baadb8e..3a246a3 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -1,8 +1,17 @@ package ui; import javax.swing.JMenu; +import javax.swing.JMenuItem; +import javax.swing.KeyStroke; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Observer; +import java.util.Observable; +import javax.swing.JOptionPane; -public class FileMenu extends JMenu { +import model.MazeAppModelMessage; + +public final class FileMenu extends JMenu { private final MazeApp mazeApp; private final LoadMazeMenuItem loadMazeMenuItem; @@ -20,4 +29,115 @@ public class FileMenu extends JMenu { add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp)); add(quitMenuItem = new QuitMenuItem(mazeApp)); } + + + public final class LoadMazeMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public LoadMazeMenuItem(MazeApp mazeApp) { + + super("Load maze"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control O")); + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + + if (! mazeApp.isModelSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){ + return; + } + } + String filename = FileBox.load(mazeApp); + if (filename != null) { + mazeApp.loadModelMaze(filename); + } + } + } + + + public final class SaveMazeMenuItem extends JMenuItem + implements ActionListener, Observer { + + private final MazeApp mazeApp; + + public SaveMazeMenuItem(MazeApp mazeApp) { + + 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) { + if (mazeApp.getModelFilename() != null) { + mazeApp.saveModelMaze(); + } + } + + @Override + public void update(Observable obs, Object param) { + if (param == MazeAppModelMessage.MazeRenewal && + mazeApp.getModelFilename() != null) { + this.setEnabled(true); + } + else { + this.setEnabled(false); + } + } + } + + + public final 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) { + String filename = FileBox.save(mazeApp); + if (filename != null) { + mazeApp.saveModelMaze(filename); + } + } + } + + + public final class QuitMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public QuitMenuItem(MazeApp mazeApp) { + + super("Quit"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control Q")); + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + + if (! mazeApp.isModelSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { + return; + } + } + System.exit(0); + } + } } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java deleted file mode 100644 index 65328e4..0000000 --- a/src/ui/LoadMazeMenuItem.java +++ /dev/null @@ -1,35 +0,0 @@ -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 LoadMazeMenuItem extends JMenuItem - implements ActionListener { - - private final MazeApp mazeApp; - - public LoadMazeMenuItem(MazeApp mazeApp) { - - super("Load maze"); - this.mazeApp = mazeApp; - this.setAccelerator(KeyStroke.getKeyStroke("control O")); - addActionListener(this); - } - - public void actionPerformed(ActionEvent evt) { - - if (mazeApp.isModelSaved()) { - if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){ - return; - } - } - String filename = FileBox.load(mazeApp); - if (filename != null) { - mazeApp.loadModelMaze(filename); - } - } -} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 1612085..5e2b31b 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -13,7 +13,7 @@ import java.util.Observer; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; -public class MazeApp extends JFrame +public final class MazeApp extends JFrame implements Observer, WindowListener { private final MazeMenuBar mazeMenuBar; @@ -145,6 +145,18 @@ public class MazeApp extends JFrame return this.mazeAppModel.getBox(boxLine, boxRow); } + public void saveModelMaze(String filename) { + mazeAppModel.saveToFile(filename); + } + + public void saveModelMaze() { + mazeAppModel.saveToFile(); + } + + public String getModelFilename() { + return mazeAppModel.getFilename(); + } + public void setModelABox(int boxLine, int boxRow) { this.mazeAppModel.setABox(boxLine, boxRow); } @@ -173,8 +185,8 @@ public class MazeApp extends JFrame return this.mazeAppModel.getShortest(); } - public ArrayList getModelShortestDeepCopy() { - return this.mazeAppModel.getShortestDeepCopy(); + public ArrayList getModelShortestShallowCopy() { + return this.mazeAppModel.getShortestShallowCopy(); } public int getModelDimensionX() { @@ -192,4 +204,4 @@ public class MazeApp extends JFrame public void toggleModelBox(int boxLine, int boxRow) { this.mazeAppModel.toggleBox(boxLine, boxRow); } -} \ No newline at end of file +} diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 1a82246..2437cbf 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -18,7 +18,7 @@ import model.MazeAppModelMessage; import maze.EBox; import dijkstra.VertexInterface; -public class MazeDrawing extends JPanel implements MouseListener { +public final class MazeDrawing extends JPanel implements MouseListener { private final MazeApp mazeApp; private boolean pathDrawingMode; @@ -39,7 +39,7 @@ public class MazeDrawing extends JPanel implements MouseListener { } @Override - protected final void paintComponent(Graphics g) { + protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; super.paintComponent(g2d); @@ -90,10 +90,11 @@ public class MazeDrawing extends JPanel implements MouseListener { public void notifyForUpdates(Object param) { - if (param == MazeAppModelMessage.MazeRenewal) { + if (param == MazeAppModelMessage.MazeRenewal || + param == MazeAppModelMessage.MazeLoaded) { if (this.mazeApp.getModelShortest() != null) { this.finalPath - = this.mazeApp.getModelShortestDeepCopy(); + = this.mazeApp.getModelShortestShallowCopy(); Collections.reverse(finalPath); } this.repaint(); @@ -172,25 +173,6 @@ public class MazeDrawing extends JPanel implements MouseListener { boxWidth - 2 * factorWidth, boxHeight - 2 * factorHeight); } -// Timer timer = new Timer(16, new ActionListener(){ -// int compteur = shortestPath.size()-1; -// VertexInterface vertex; -// @Override -// public void actionPerformed(ActionEvent e) { -// if (compteur < 0) { -// ((Timer)e.getSource()).stop(); -// } else { -// vertex = shortestPath.get(compteur); -// EBox box = (EBox) vertex; -// g.fillOval(box.getX() * boxWidth + factorWidth, -// box.getY()*boxHeight + factorHeight, -// boxWidth - 2 * factorWidth, -// boxHeight - 2 * factorHeight); -// compteur -= 1; -// } -// } -// }); -// timer.start(); if (pathCounter >= finalPath.size()) { pathDrawingMode = false; diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index e3840c1..4640758 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -2,7 +2,7 @@ package ui; import javax.swing.JMenu; -public class MazeMenu extends JMenu { +public final class MazeMenu extends JMenu { private final MazeApp mazeApp; private final DrawShortestPathMenuItem drawMenuItem; diff --git a/src/ui/MazeMenuBar.java b/src/ui/MazeMenuBar.java index afe76da..0a51c7a 100644 --- a/src/ui/MazeMenuBar.java +++ b/src/ui/MazeMenuBar.java @@ -2,7 +2,7 @@ package ui; import javax.swing.JMenuBar; -public class MazeMenuBar extends JMenuBar { +public final class MazeMenuBar extends JMenuBar { private final MazeApp mazeApp; private final FileMenu fileMenu; diff --git a/src/ui/NewClearMazeMenuItem.java b/src/ui/NewClearMazeMenuItem.java index bb79730..a0a663b 100644 --- a/src/ui/NewClearMazeMenuItem.java +++ b/src/ui/NewClearMazeMenuItem.java @@ -7,7 +7,7 @@ import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.KeyStroke; -public class NewClearMazeMenuItem extends JMenuItem +public final class NewClearMazeMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/NewMazeSpinnerBox.java b/src/ui/NewMazeSpinnerBox.java index 2e5397c..5fb11f8 100644 --- a/src/ui/NewMazeSpinnerBox.java +++ b/src/ui/NewMazeSpinnerBox.java @@ -16,7 +16,7 @@ import javax.swing.SpinnerModel; import javax.swing.SpinnerNumberModel; import javax.swing.SpringLayout; -public class NewMazeSpinnerBox extends JPanel +public final class NewMazeSpinnerBox extends JPanel implements ActionListener { private final MazeApp mazeApp; @@ -48,7 +48,7 @@ public class NewMazeSpinnerBox extends JPanel JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel); } - static protected JSpinner addLabeledSpinner(Container c, + protected static JSpinner addLabeledSpinner(Container c, String label, SpinnerModel model) { diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java deleted file mode 100644 index c7b46f4..0000000 --- a/src/ui/QuitMenuItem.java +++ /dev/null @@ -1,34 +0,0 @@ -package ui; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import javax.swing.JMenuItem; -import javax.swing.JOptionPane; -import javax.swing.KeyStroke; - -import model.MazeAppModel; - -public class QuitMenuItem extends JMenuItem - implements ActionListener { - - private final MazeApp mazeApp; - - public QuitMenuItem(MazeApp mazeApp) { - - super("Quit"); - this.mazeApp = mazeApp; - this.setAccelerator(KeyStroke.getKeyStroke("control Q")); - addActionListener(this); - } - - public void actionPerformed(ActionEvent evt) { - - if (! mazeApp.isModelSaved()) { - if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { - return; - } - } - System.exit(0); - } -} diff --git a/src/ui/SaveBox.java b/src/ui/SaveBox.java index ae354f2..f065d58 100644 --- a/src/ui/SaveBox.java +++ b/src/ui/SaveBox.java @@ -3,15 +3,12 @@ package ui; import javax.swing.JOptionPane; import java.awt.Component; -import model.MazeAppModel; - -public class SaveBox { +public final 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, @@ -22,13 +19,13 @@ public class SaveBox { case JOptionPane.CANCEL_OPTION: return false; case JOptionPane.OK_OPTION: - if (mazeAppModel.getFilename() != null) { - mazeAppModel.saveToFile(); + if (mazeApp.getModelFilename() != null) { + mazeApp.saveModelMaze(); } else { String filename = FileBox.save(mazeApp); if (filename != null) { - mazeApp.getMazeAppModel().saveToFile(filename); + mazeApp.saveModelMaze(filename); } else { return promptAndContinue(null, mazeApp, title); diff --git a/src/ui/SaveMazeAsMenuItem.java b/src/ui/SaveMazeAsMenuItem.java deleted file mode 100644 index 22195f1..0000000 --- a/src/ui/SaveMazeAsMenuItem.java +++ /dev/null @@ -1,32 +0,0 @@ -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 deleted file mode 100644 index 13b8c8e..0000000 --- a/src/ui/SaveMazeMenuItem.java +++ /dev/null @@ -1,47 +0,0 @@ -package ui; - -import javax.swing.JMenuItem; -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; - -public class SaveMazeMenuItem extends JMenuItem - implements ActionListener, Observer { - - private final MazeApp mazeApp; - - public SaveMazeMenuItem(MazeApp mazeApp) { - - 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(); - - if (mazeAppModel.getFilename() != null) { - mazeAppModel.saveToFile(); - } - } - - @Override - public void update(Observable obs, Object param) { - if (mazeApp.getMazeAppModel().getFilename() != null) { - this.setEnabled(true); - } - else { - this.setEnabled(false); - } - } -} diff --git a/src/ui/SetABoxMenuItem.java b/src/ui/SetABoxMenuItem.java index 02d98e0..11200d7 100644 --- a/src/ui/SetABoxMenuItem.java +++ b/src/ui/SetABoxMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class SetABoxMenuItem extends JMenuItem +public final class SetABoxMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/SetDBoxMenuItem.java b/src/ui/SetDBoxMenuItem.java index 1c46aa5..74530d1 100644 --- a/src/ui/SetDBoxMenuItem.java +++ b/src/ui/SetDBoxMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class SetDBoxMenuItem extends JMenuItem +public final class SetDBoxMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/SetEBoxMenuItem.java b/src/ui/SetEBoxMenuItem.java index f19ae09..a73e054 100644 --- a/src/ui/SetEBoxMenuItem.java +++ b/src/ui/SetEBoxMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class SetEBoxMenuItem extends JMenuItem +public final class SetEBoxMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/SetWBoxMenuItem.java b/src/ui/SetWBoxMenuItem.java index 7888de9..3781a73 100644 --- a/src/ui/SetWBoxMenuItem.java +++ b/src/ui/SetWBoxMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class SetWBoxMenuItem extends JMenuItem +public final class SetWBoxMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 4219cda..72b38b5 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -4,7 +4,7 @@ import javax.swing.*; import maze.Maze; import java.awt.*; -public class WindowPanel extends JPanel { +public final class WindowPanel extends JPanel { private final MazeDrawing drawingPanelGrid;