Refactorize code #31

Merged
regar42 merged 5 commits from refactorize into master 2018-01-14 17:02:44 +00:00
39 changed files with 226 additions and 237 deletions
Showing only changes of commit e76db727c7 - Show all commits

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@ package dijkstra;
import java.util.HashSet; import java.util.HashSet;
public class ASet implements ASetInterface { public final class ASet implements ASetInterface {
private HashSet<VertexInterface> vertexHashSet; private HashSet<VertexInterface> vertexHashSet;
+1 -1
View File
@@ -3,7 +3,7 @@ import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Hashtable; import java.util.Hashtable;
public class Dijkstra { public final class Dijkstra {
private static PreviousInterface dijkstra(GraphInterface g, private static PreviousInterface dijkstra(GraphInterface g,
VertexInterface r, VertexInterface r,
+1 -1
View File
@@ -2,7 +2,7 @@ package dijkstra;
import java.util.Hashtable; import java.util.Hashtable;
public class Pi implements PiInterface { public final class Pi implements PiInterface {
private Hashtable<VertexInterface, Integer> hashPi; private Hashtable<VertexInterface, Integer> hashPi;
+1 -1
View File
@@ -6,7 +6,7 @@ import java.util.List;
import maze.Box; import maze.Box;
public class Previous implements PreviousInterface { public final class Previous implements PreviousInterface {
private Hashtable<VertexInterface, VertexInterface> hashPrevious; private Hashtable<VertexInterface, VertexInterface> hashPrevious;
private ArrayList<VertexInterface> shortestPath; private ArrayList<VertexInterface> shortestPath;
+1 -1
View File
@@ -5,7 +5,7 @@ package maze;
* It's the last <code>Box</code> to be walked through and * It's the last <code>Box</code> to be walked through and
* it's an exit to the <code>Maze</code>. * 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) { public ABox(String label, int y, int x, Maze maze) {
super(label, y, x, maze) ; super(label, y, x, maze) ;
+2 -3
View File
@@ -24,15 +24,14 @@ public abstract class Box
private String label; private String label;
private Maze maze; private Maze maze;
private ArrayList<VertexInterface> neighbourList; 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 public Box(String label, int y , int x, Maze maze) { //y first because of graphical maze representation
this.label = label; this.label = label;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.maze = maze; this.maze = maze;
this.candidates = new ArrayList<VertexInterface>();
} }
public ArrayList<VertexInterface> generateNeighbours() { public ArrayList<VertexInterface> generateNeighbours() {
+1 -1
View File
@@ -5,7 +5,7 @@ package maze;
* is first in the <code>Maze</code>. * 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) { public DBox(String label, int y, int x, Maze maze) {
super(label, y, x, maze); super(label, y, x, maze);
+4 -4
View File
@@ -16,7 +16,7 @@ import dijkstra.VertexInterface;
* @author Sylvain Rager * @author Sylvain Rager
* *
*/ */
public class Maze public final class Maze
implements GraphInterface { implements GraphInterface {
private int dimensionX; private int dimensionX;
@@ -404,7 +404,7 @@ public class Maze
* The path to the file containing the representation of the 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; FileReader fr = null;
BufferedReader br = 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; FileOutputStream fos = null;
PrintWriter pw = null; PrintWriter pw = null;
@@ -527,7 +527,7 @@ public class Maze
return dimensionY; return dimensionY;
} }
public Box[][] getMaze() { protected Box[][] getMaze() {
return maze; return maze;
} }
+1 -1
View File
@@ -8,7 +8,7 @@ package maze;
* *
*/ */
public class MazeReadingException public final class MazeReadingException
extends Exception { extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
+1 -1
View File
@@ -5,7 +5,7 @@ package maze;
* that can't be walked through (a wall). * 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); super(label, y, x, maze);
+31 -17
View File
@@ -11,14 +11,13 @@ import dijkstra.*;
import maze.*; import maze.*;
public class MazeAppModel extends Observable { public class MazeAppModel extends Observable {
/** The file into which the maze is to be saved */
private String filename; private String filename;
private Maze maze; private Maze maze;
private VertexInterface departure; private VertexInterface departure;
private VertexInterface arrival; private VertexInterface arrival;
private Previous previous; private Previous previous;
private ArrayList<VertexInterface> shortest; private ArrayList<VertexInterface> shortest;
private ArrayList<VertexInterface> boxList;
private boolean saved = true; private boolean saved = true;
/* /*
* There are three states for pathDrawn : * There are three states for pathDrawn :
@@ -38,7 +37,6 @@ public class MazeAppModel extends Observable {
private void initModelFromMaze() { private void initModelFromMaze() {
departure = maze.getDeparture(); departure = maze.getDeparture();
arrival = maze.getArrival(); arrival = maze.getArrival();
boxList = maze.getVertexes();
if (departure == null || arrival == null) { if (departure == null || arrival == null) {
previous = null; previous = null;
shortest = null; shortest = null;
@@ -52,8 +50,6 @@ public class MazeAppModel extends Observable {
shortest = previous.getShortestPathTo(arrival); shortest = previous.getShortestPathTo(arrival);
} }
} }
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void loadMaze(String filename) { public void loadMaze(String filename) {
@@ -61,16 +57,14 @@ public class MazeAppModel extends Observable {
maze = new Maze(this.filename); maze = new Maze(this.filename);
initModelFromMaze(); initModelFromMaze();
saved = true; saved = true;
setChanged();
notifyObservers(MazeAppModelMessage.MazeLoaded);
} }
public Maze getMaze() { public Maze getMaze() {
return maze; return maze;
} }
public Box[][] getBoxMaze() {
return this.maze.getMaze();
}
public Box getBox(int boxLine, int boxRow) { public Box getBox(int boxLine, int boxRow) {
return this.maze.getBox(boxLine, boxRow); return this.maze.getBox(boxLine, boxRow);
} }
@@ -87,12 +81,12 @@ public class MazeAppModel extends Observable {
return shortest; return shortest;
} }
public ArrayList<VertexInterface> getShortestDeepCopy() { public ArrayList<VertexInterface> getShortestShallowCopy() {
ArrayList<VertexInterface> deepCopy = new ArrayList<VertexInterface>(); ArrayList<VertexInterface> shallowCopy = new ArrayList<VertexInterface>();
for (VertexInterface vertex : this.shortest) { for (VertexInterface vertex : this.shortest) {
deepCopy.add(vertex); shallowCopy.add(vertex);
} }
return deepCopy; return shallowCopy;
} }
public int isPathDrawn() { public int isPathDrawn() {
@@ -106,10 +100,12 @@ public class MazeAppModel extends Observable {
public void saveToFile() { public void saveToFile() {
maze.saveToTextFile(filename); maze.saveToTextFile(filename);
saved = true; saved = true;
setChanged();
notifyObservers(MazeAppModelMessage.MazeSaved);
} }
public void saveToFile(String filename) { public void saveToFile(String filename) {
this.filename = new String(filename); this.filename = filename;
maze.saveToTextFile(this.filename); maze.saveToTextFile(this.filename);
saved = true; saved = true;
setChanged(); setChanged();
@@ -117,7 +113,7 @@ public class MazeAppModel extends Observable {
} }
public void toggleBox(int boxLine, int boxRow) { public void toggleBox(int boxLine, int boxRow) {
if (maze.getMaze()[boxLine][boxRow].isWalkable()) { if (maze.getBox(boxLine, boxRow).isWalkable()) {
setWBox(boxLine, boxRow); setWBox(boxLine, boxRow);
} }
else { else {
@@ -130,6 +126,8 @@ public class MazeAppModel extends Observable {
maze.resetNeighbourLists(); maze.resetNeighbourLists();
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void setEBox(int boxLine, int boxRow) { public void setEBox(int boxLine, int boxRow) {
@@ -137,6 +135,8 @@ public class MazeAppModel extends Observable {
maze.resetNeighbourLists(); maze.resetNeighbourLists();
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void setDBox(int boxLine, int boxRow) { public void setDBox(int boxLine, int boxRow) {
@@ -149,6 +149,8 @@ public class MazeAppModel extends Observable {
maze.resetNeighbourLists(); maze.resetNeighbourLists();
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void setABox(int boxLine, int boxRow) { public void setABox(int boxLine, int boxRow) {
@@ -161,6 +163,8 @@ public class MazeAppModel extends Observable {
maze.resetNeighbourLists(); maze.resetNeighbourLists();
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void newClearMaze(int numberOfLines, int numberOfRows) { public void newClearMaze(int numberOfLines, int numberOfRows) {
@@ -168,6 +172,8 @@ public class MazeAppModel extends Observable {
maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows)); maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows));
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void addEmptyColumn() { public void addEmptyColumn() {
@@ -178,12 +184,16 @@ public class MazeAppModel extends Observable {
maze.addEmptyColumn(posColumn); maze.addEmptyColumn(posColumn);
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void delColumn(int posColumn) { public void delColumn(int posColumn) {
maze.delColumn(posColumn); maze.delColumn(posColumn);
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void delColumn() { public void delColumn() {
@@ -198,12 +208,16 @@ public class MazeAppModel extends Observable {
maze.addEmptyLine(posLine); maze.addEmptyLine(posLine);
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void delLine(int posLine) { public void delLine(int posLine) {
maze.delLine(posLine); maze.delLine(posLine);
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public void delLine() { public void delLine() {
@@ -219,10 +233,10 @@ public class MazeAppModel extends Observable {
} }
public int getDimensionX() { public int getDimensionX() {
return this.maze.getDimensionX(); return maze.getDimensionX();
} }
public int getDimensionY() { public int getDimensionY() {
return this.maze.getDimensionX(); return maze.getDimensionY();
} }
} }
+16 -3
View File
@@ -1,6 +1,19 @@
package model; package model;
public class MazeAppModelMessage { /**
public final static String MazeRenewal = "MazeRenewal"; * This class is used to transport messages about an event happening
public final static String FileChange = "FileChange"; * 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";
} }
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class AddLineMenuItem extends JMenuItem public final class AddLineMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class AddRowMenuItem extends JMenuItem public final class AddRowMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -7,7 +7,7 @@ import javax.swing.Action;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class AnimatePathMenuItem extends JMenuItem public final class AnimatePathMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -4,7 +4,7 @@ import javax.swing.JPopupMenu;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import maze.Box; import maze.Box;
public class BoxTypeEditionMenu extends JPopupMenu { public final class BoxTypeEditionMenu extends JPopupMenu {
JMenuItem setWBox; JMenuItem setWBox;
JMenuItem setEBox; JMenuItem setEBox;
+1 -1
View File
@@ -6,7 +6,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
public class ClearMazeMenuItem extends JMenuItem public final class ClearMazeMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -7,7 +7,7 @@ import model.MazeAppModel;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
public class ClearShortestPathMenuItem extends JMenuItem public final class ClearShortestPathMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class DelLineMenuItem extends JMenuItem public final class DelLineMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class DelRowMenuItem extends JMenuItem public final class DelRowMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -7,7 +7,7 @@ import model.MazeAppModel;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
public class DrawShortestPathMenuItem extends JMenuItem public final class DrawShortestPathMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -3,7 +3,7 @@ package ui;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
public class FileBox { public final class FileBox {
private static JFileChooser fileChooser = new JFileChooser(); private static JFileChooser fileChooser = new JFileChooser();
private static FileNameExtensionFilter filter = private static FileNameExtensionFilter filter =
new FileNameExtensionFilter("Text files", "txt"); new FileNameExtensionFilter("Text files", "txt");
+121 -1
View File
@@ -1,8 +1,17 @@
package ui; package ui;
import javax.swing.JMenu; 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 MazeApp mazeApp;
private final LoadMazeMenuItem loadMazeMenuItem; private final LoadMazeMenuItem loadMazeMenuItem;
@@ -20,4 +29,115 @@ public class FileMenu extends JMenu {
add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp)); add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp));
add(quitMenuItem = new QuitMenuItem(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);
}
}
} }
-35
View File
@@ -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
View File
@@ -13,7 +13,7 @@ import java.util.Observer;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.awt.event.WindowListener; import java.awt.event.WindowListener;
public class MazeApp extends JFrame public final class MazeApp extends JFrame
implements Observer, WindowListener { implements Observer, WindowListener {
private final MazeMenuBar mazeMenuBar; private final MazeMenuBar mazeMenuBar;
@@ -145,6 +145,18 @@ public class MazeApp extends JFrame
return this.mazeAppModel.getBox(boxLine, boxRow); 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) { public void setModelABox(int boxLine, int boxRow) {
this.mazeAppModel.setABox(boxLine, boxRow); this.mazeAppModel.setABox(boxLine, boxRow);
} }
@@ -173,8 +185,8 @@ public class MazeApp extends JFrame
return this.mazeAppModel.getShortest(); return this.mazeAppModel.getShortest();
} }
public ArrayList<VertexInterface> getModelShortestDeepCopy() { public ArrayList<VertexInterface> getModelShortestShallowCopy() {
return this.mazeAppModel.getShortestDeepCopy(); return this.mazeAppModel.getShortestShallowCopy();
} }
public int getModelDimensionX() { public int getModelDimensionX() {
+5 -23
View File
@@ -18,7 +18,7 @@ import model.MazeAppModelMessage;
import maze.EBox; import maze.EBox;
import dijkstra.VertexInterface; import dijkstra.VertexInterface;
public class MazeDrawing extends JPanel implements MouseListener { public final class MazeDrawing extends JPanel implements MouseListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private boolean pathDrawingMode; private boolean pathDrawingMode;
@@ -39,7 +39,7 @@ public class MazeDrawing extends JPanel implements MouseListener {
} }
@Override @Override
protected final void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d); super.paintComponent(g2d);
@@ -90,10 +90,11 @@ public class MazeDrawing extends JPanel implements MouseListener {
public void notifyForUpdates(Object param) { public void notifyForUpdates(Object param) {
if (param == MazeAppModelMessage.MazeRenewal) { if (param == MazeAppModelMessage.MazeRenewal ||
param == MazeAppModelMessage.MazeLoaded) {
if (this.mazeApp.getModelShortest() != null) { if (this.mazeApp.getModelShortest() != null) {
this.finalPath this.finalPath
= this.mazeApp.getModelShortestDeepCopy(); = this.mazeApp.getModelShortestShallowCopy();
Collections.reverse(finalPath); Collections.reverse(finalPath);
} }
this.repaint(); this.repaint();
@@ -172,25 +173,6 @@ public class MazeDrawing extends JPanel implements MouseListener {
boxWidth - 2 * factorWidth, boxWidth - 2 * factorWidth,
boxHeight - 2 * factorHeight); 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()) { if (pathCounter >= finalPath.size()) {
pathDrawingMode = false; pathDrawingMode = false;
+1 -1
View File
@@ -2,7 +2,7 @@ package ui;
import javax.swing.JMenu; import javax.swing.JMenu;
public class MazeMenu extends JMenu { public final class MazeMenu extends JMenu {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final DrawShortestPathMenuItem drawMenuItem; private final DrawShortestPathMenuItem drawMenuItem;
+1 -1
View File
@@ -2,7 +2,7 @@ package ui;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
public class MazeMenuBar extends JMenuBar { public final class MazeMenuBar extends JMenuBar {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final FileMenu fileMenu; private final FileMenu fileMenu;
+1 -1
View File
@@ -7,7 +7,7 @@ import javax.swing.JMenuItem;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
public class NewClearMazeMenuItem extends JMenuItem public final class NewClearMazeMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+2 -2
View File
@@ -16,7 +16,7 @@ import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel; import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout; import javax.swing.SpringLayout;
public class NewMazeSpinnerBox extends JPanel public final class NewMazeSpinnerBox extends JPanel
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
@@ -48,7 +48,7 @@ public class NewMazeSpinnerBox extends JPanel
JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel); JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel);
} }
static protected JSpinner addLabeledSpinner(Container c, protected static JSpinner addLabeledSpinner(Container c,
String label, String label,
SpinnerModel model) { SpinnerModel model) {
-34
View File
@@ -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
View File
@@ -3,15 +3,12 @@ package ui;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import java.awt.Component; import java.awt.Component;
import model.MazeAppModel; public final class SaveBox {
public class SaveBox {
private static String message = "Maze not saved. Save it ?"; private static String message = "Maze not saved. Save it ?";
public static boolean promptAndContinue(Component parent, public static boolean promptAndContinue(Component parent,
MazeApp mazeApp, MazeApp mazeApp,
String title) { String title) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
int response = JOptionPane.showOptionDialog(parent, int response = JOptionPane.showOptionDialog(parent,
message, message,
title, title,
@@ -22,13 +19,13 @@ public class SaveBox {
case JOptionPane.CANCEL_OPTION: case JOptionPane.CANCEL_OPTION:
return false; return false;
case JOptionPane.OK_OPTION: case JOptionPane.OK_OPTION:
if (mazeAppModel.getFilename() != null) { if (mazeApp.getModelFilename() != null) {
mazeAppModel.saveToFile(); mazeApp.saveModelMaze();
} }
else { else {
String filename = FileBox.save(mazeApp); String filename = FileBox.save(mazeApp);
if (filename != null) { if (filename != null) {
mazeApp.getMazeAppModel().saveToFile(filename); mazeApp.saveModelMaze(filename);
} }
else { else {
return promptAndContinue(null, mazeApp, title); return promptAndContinue(null, mazeApp, title);
-32
View File
@@ -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);
}
}
}
-47
View File
@@ -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);
}
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class SetABoxMenuItem extends JMenuItem public final class SetABoxMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class SetDBoxMenuItem extends JMenuItem public final class SetDBoxMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class SetEBoxMenuItem extends JMenuItem public final class SetEBoxMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class SetWBoxMenuItem extends JMenuItem public final class SetWBoxMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -4,7 +4,7 @@ import javax.swing.*;
import maze.Maze; import maze.Maze;
import java.awt.*; import java.awt.*;
public class WindowPanel extends JPanel { public final class WindowPanel extends JPanel {
private final MazeDrawing drawingPanelGrid; private final MazeDrawing drawingPanelGrid;