Fix regression on getDimensionX and add finals
* Make all appropriate methods final * Add new messages to MazeAppModelMessage and handle them to act more accurately on observers updates * Make all buttons from 'File' menu subclasses of FileMenu in order to have a cleaner ui folder * Remove legacy code (for example, the former code used to animate the path drawing) * Improve delegation
This commit is contained in:
1 parent
eaba5b6106
commit
e76db727c7
39 files changed
+226
-237
No files matched your search
@@ -2,7 +2,7 @@ package dijkstra;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
public class ASet implements ASetInterface {
|
||||
public final class ASet implements ASetInterface {
|
||||
|
||||
private HashSet<VertexInterface> vertexHashSet;
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -2,7 +2,7 @@ package dijkstra;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
public class Pi implements PiInterface {
|
||||
public final class Pi implements PiInterface {
|
||||
|
||||
private Hashtable<VertexInterface, Integer> hashPi;
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.List;
|
||||
|
||||
import maze.Box;
|
||||
|
||||
public class Previous implements PreviousInterface {
|
||||
public final class Previous implements PreviousInterface {
|
||||
|
||||
private Hashtable<VertexInterface, VertexInterface> hashPrevious;
|
||||
private ArrayList<VertexInterface> shortestPath;
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ package maze;
|
||||
* It's the last <code>Box</code> to be walked through and
|
||||
* it's an exit to the <code>Maze</code>.
|
||||
*/
|
||||
public class ABox extends EBox{
|
||||
public final class ABox extends EBox{
|
||||
|
||||
public ABox(String label, int y, int x, Maze maze) {
|
||||
super(label, y, x, maze) ;
|
||||
|
||||
+2
-3
@@ -24,15 +24,14 @@ public abstract class Box
|
||||
private String label;
|
||||
private Maze maze;
|
||||
private ArrayList<VertexInterface> neighbourList;
|
||||
private ArrayList<VertexInterface> candidates;
|
||||
private ArrayList<VertexInterface> candidates =
|
||||
new ArrayList<VertexInterface>();
|
||||
|
||||
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<VertexInterface>();
|
||||
}
|
||||
|
||||
public ArrayList<VertexInterface> generateNeighbours() {
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ package maze;
|
||||
* is first in the <code>Maze</code>.
|
||||
*/
|
||||
|
||||
public class DBox extends EBox{
|
||||
public final class DBox extends EBox{
|
||||
|
||||
public DBox(String label, int y, int x, Maze maze) {
|
||||
super(label, y, x, maze);
|
||||
|
||||
+4
-4
@@ -16,7 +16,7 @@ import dijkstra.VertexInterface;
|
||||
* @author Sylvain Rager
|
||||
*
|
||||
*/
|
||||
public class Maze
|
||||
public final class Maze
|
||||
implements GraphInterface {
|
||||
|
||||
private int dimensionX;
|
||||
@@ -404,7 +404,7 @@ public class Maze
|
||||
* 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;
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ package maze;
|
||||
*
|
||||
*/
|
||||
|
||||
public class MazeReadingException
|
||||
public final class MazeReadingException
|
||||
extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ 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) {
|
||||
super(label, y, x, maze);
|
||||
|
||||
+31
-17
@@ -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<VertexInterface> shortest;
|
||||
private ArrayList<VertexInterface> 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<VertexInterface> getShortestDeepCopy() {
|
||||
ArrayList<VertexInterface> deepCopy = new ArrayList<VertexInterface>();
|
||||
public ArrayList<VertexInterface> getShortestShallowCopy() {
|
||||
ArrayList<VertexInterface> shallowCopy = new ArrayList<VertexInterface>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
+1
-1
@@ -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");
|
||||
|
||||
+121
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
-3
@@ -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<VertexInterface> getModelShortestDeepCopy() {
|
||||
return this.mazeAppModel.getShortestDeepCopy();
|
||||
public ArrayList<VertexInterface> getModelShortestShallowCopy() {
|
||||
return this.mazeAppModel.getShortestShallowCopy();
|
||||
}
|
||||
|
||||
public int getModelDimensionX() {
|
||||
|
||||
+5
-23
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+4
-7
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in new issue
Block a user