diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index b3e9645..b75cb4f 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -34,10 +34,13 @@ public class MazeAppModel extends Observable { if (departure == null || arrival == null) { previous = null; shortest = null; - return; } - previous = (Previous) Dijkstra.dijkstra(maze, departure); - shortest = previous.getShortestPathTo(arrival); + else { + previous = (Previous) Dijkstra.dijkstra(maze, departure); + shortest = previous.getShortestPathTo(arrival); + } + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void loadMaze(String filename) { @@ -45,8 +48,6 @@ public class MazeAppModel extends Observable { maze = new Maze(this.filename); initModelFromMaze(); saved = true; - setChanged(); - notifyObservers(MazeAppModelMessage.MazeRenewal); } public Maze getMaze() { @@ -86,36 +87,56 @@ public class MazeAppModel extends Observable { notifyObservers(MazeAppModelMessage.FileChange); } - public void toggleBox(int x, int y) { - if (maze.getMaze()[y][x].isWalkable()) { - if (maze.getMaze()[y][x].isDeparture()) { - if (arrival != null) { - int arrivalRow = ((EBox) arrival).getX(); - int arrivalLine = ((EBox) arrival).getY(); - maze.getMaze()[arrivalLine][arrivalRow] = - new EBox("E", arrivalLine, arrivalRow, maze); - } - maze.getMaze()[y][x] = new ABox("A", y, x, maze); + public void toggleBox(int boxLine, int boxRow) { + if (maze.getMaze()[boxLine][boxRow].isWalkable()) { + if (maze.getMaze()[boxLine][boxRow].isDeparture()) { + setABox(boxLine, boxRow); } - else if (maze.getMaze()[y][x].isArrival()) { - maze.getMaze()[y][x] = new EBox("E", y, x, maze); + else if (maze.getMaze()[boxLine][boxRow].isArrival()) { + setEBox(boxLine, boxRow); } else { - maze.getMaze()[y][x] = new WBox("W", y, x, maze); + setWBox(boxLine, boxRow); } } else { - if (departure != null) { - int departureRow = ((EBox) departure).getX(); - int departureLine = ((EBox) departure).getY(); - maze.getMaze()[departureLine][departureRow] = - new EBox("E", departureLine, departureRow, maze); - } - maze.getMaze()[y][x] = new DBox("D", y, x, maze); + setDBox(boxLine, boxRow); } + } + + public void setWBox(int boxLine, int boxRow) { + maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze); + initModelFromMaze(); + saved = false; + } + + public void setEBox(int boxLine, int boxRow) { + maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze); + initModelFromMaze(); + saved = false; + } + + public void setDBox(int boxLine, int boxRow) { + if (departure != null) { + int departureRow = ((EBox) departure).getX(); + int departureLine = ((EBox) departure).getY(); + maze.getMaze()[departureLine][departureRow] = + new EBox("E", departureLine, departureRow, maze); + } + maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze); + initModelFromMaze(); + saved = false; + } + + public void setABox(int boxLine, int boxRow) { + if (arrival != null) { + int arrivalRow = ((EBox) arrival).getX(); + int arrivalLine = ((EBox) arrival).getY(); + maze.getMaze()[arrivalLine][arrivalRow] = + new EBox("E", arrivalLine, arrivalRow, maze); + } + maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze); initModelFromMaze(); saved = false; - setChanged(); - notifyObservers(MazeAppModelMessage.MazeRenewal); } } diff --git a/src/ui/BoxTypeEditionMenu.java b/src/ui/BoxTypeEditionMenu.java new file mode 100644 index 0000000..601fafc --- /dev/null +++ b/src/ui/BoxTypeEditionMenu.java @@ -0,0 +1,25 @@ +package ui; + +import javax.swing.JPopupMenu; +import javax.swing.JMenuItem; +import maze.Box; + +public class BoxTypeEditionMenu extends JPopupMenu { + JMenuItem setWBox; + JMenuItem setEBox; + JMenuItem setDBox; + JMenuItem setABox; + + public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) { + add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow)); + add(setWBox = new SetWBoxMenuItem(mazeApp, boxLine, boxRow)); + add(setDBox = new SetDBoxMenuItem(mazeApp, boxLine, boxRow)); + add(setABox = new SetABoxMenuItem(mazeApp, boxLine, boxRow)); + Box box = (Box) mazeApp.getMazeAppModel().getMaze().getMaze()[boxLine][boxRow]; + setEBox.setEnabled((box.isWalkable() && (box.isDeparture() || + box.isArrival())) || ! box.isWalkable()); + setWBox.setEnabled(box.isWalkable()); + setDBox.setEnabled(! box.isDeparture()); + setABox.setEnabled(! box.isArrival()); + } +} diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 793c00d..f32cae6 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -117,16 +117,28 @@ public class MazeDrawing extends JPanel implements MouseListener { return; } - public void mouseClicked(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); + private int getBoxRow(int x) { int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); - int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); int boxWidth = getWidth() / dimensionX; + return x / boxWidth; + } + + private int getBoxLine(int y) { + int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); int boxHeight = getHeight() / dimensionY; - int boxX = x / boxWidth; - int boxY = y / boxHeight; - mazeApp.getMazeAppModel().toggleBox(boxX, boxY); + return y / boxHeight; + } + + public void mouseClicked(MouseEvent e) { + int boxRow = getBoxRow(e.getX()); + int boxLine = getBoxLine(e.getY()); + if (e.getButton() == MouseEvent.BUTTON3) { + BoxTypeEditionMenu boxTypeEditionMenu = new BoxTypeEditionMenu(mazeApp, boxLine, boxRow); + boxTypeEditionMenu.show(e.getComponent(), e.getX(), e.getY()); + } + else if (e.getButton() == MouseEvent.BUTTON1) { + mazeApp.getMazeAppModel().toggleBox(boxLine, boxRow); + } return; } diff --git a/src/ui/SetABoxMenuItem.java b/src/ui/SetABoxMenuItem.java new file mode 100644 index 0000000..2449a0c --- /dev/null +++ b/src/ui/SetABoxMenuItem.java @@ -0,0 +1,26 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class SetABoxMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int boxLine; + private final int boxRow; + + public SetABoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) { + super("Set Arrival Box"); + this.mazeApp = mazeApp; + this.boxLine = boxLine; + this.boxRow = boxRow; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.getMazeAppModel().setABox(boxLine, boxRow); + } +} diff --git a/src/ui/SetDBoxMenuItem.java b/src/ui/SetDBoxMenuItem.java new file mode 100644 index 0000000..fd9c5fd --- /dev/null +++ b/src/ui/SetDBoxMenuItem.java @@ -0,0 +1,26 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class SetDBoxMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int boxLine; + private final int boxRow; + + public SetDBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) { + super("Set Departure Box"); + this.mazeApp = mazeApp; + this.boxLine = boxLine; + this.boxRow = boxRow; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.getMazeAppModel().setDBox(boxLine, boxRow); + } +} diff --git a/src/ui/SetEBoxMenuItem.java b/src/ui/SetEBoxMenuItem.java new file mode 100644 index 0000000..85a41fd --- /dev/null +++ b/src/ui/SetEBoxMenuItem.java @@ -0,0 +1,26 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class SetEBoxMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int boxLine; + private final int boxRow; + + public SetEBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) { + super("Set Empty Box"); + this.mazeApp = mazeApp; + this.boxLine = boxLine; + this.boxRow = boxRow; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.getMazeAppModel().setEBox(boxLine, boxRow); + } +} diff --git a/src/ui/SetWBoxMenuItem.java b/src/ui/SetWBoxMenuItem.java new file mode 100644 index 0000000..3cc032b --- /dev/null +++ b/src/ui/SetWBoxMenuItem.java @@ -0,0 +1,26 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class SetWBoxMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int boxLine; + private final int boxRow; + + public SetWBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) { + super("Set Wall Box"); + this.mazeApp = mazeApp; + this.boxLine = boxLine; + this.boxRow = boxRow; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.getMazeAppModel().setWBox(boxLine, boxRow); + } +}