From d8acf6ebe86721c5f6aee9278b7ad88b33bc830e Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 16 Dec 2017 15:15:39 +0100 Subject: [PATCH 1/4] Add ActionListener for QuitMenuItem --- src/model/MazeAppModel.java | 9 +++++++++ src/ui/QuitMenuItem.java | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 1694450..dcb09b9 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -5,6 +5,7 @@ import maze.Maze; public class MazeAppModel { private Maze maze = new Maze("data/labyrinthe.txt"); + private boolean modified = false; public void setMaze(Maze maze) { this.maze = maze; @@ -13,4 +14,12 @@ public class MazeAppModel { public Maze getMaze() { return maze; } + + public boolean isModified() { + return modified; + } + + public void saveToFile() { + maze.saveToTextFile("data/labyOutput.txt"); + } } \ No newline at end of file diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 30b3196..caead12 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -1,8 +1,15 @@ package ui; -import javax.swing.JMenuItem; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; -public class QuitMenuItem extends JMenuItem { +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; + +import model.MazeAppModel; + +public class QuitMenuItem extends JMenuItem + implements ActionListener { private final MazeApp mazeApp; @@ -10,6 +17,31 @@ public class QuitMenuItem extends JMenuItem { super("Quit"); this.mazeApp = mazeApp; + addActionListener(this); } + public void actionPerformed(ActionEvent evt) { + + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + + if (mazeAppModel.isModified()) { + int response = JOptionPane.showInternalOptionDialog(this, + "Maze not saved. Save it ?", + "Quit application", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.WARNING_MESSAGE, + null,null,null) ; + switch (response) { + case JOptionPane.CANCEL_OPTION: + return; + case JOptionPane.OK_OPTION: + mazeAppModel.saveToFile(); + break; + case JOptionPane.NO_OPTION: + break; + } + } + System.exit(0) ; + } + } From a6783e4a0d9d23a90567292931b377c941929507 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 16 Dec 2017 15:47:16 +0100 Subject: [PATCH 2/4] Add LoadMazeMenuItem (not finished) --- src/ui/FileMenu.java | 2 ++ src/ui/LoadMazeMenuItem.java | 40 ++++++++++++++++++++++++++++++++++++ src/ui/QuitMenuItem.java | 29 +++++++++++++------------- 3 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 src/ui/LoadMazeMenuItem.java diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index 56f0b97..c2438d0 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -6,6 +6,7 @@ public class FileMenu extends JMenu { private final MazeApp mazeApp; private final QuitMenuItem quitMenuItem; + private final LoadMazeMenuItem loadMazeMenuItem; public FileMenu(MazeApp mazeApp) { @@ -13,6 +14,7 @@ public class FileMenu extends JMenu { this.mazeApp = mazeApp; add(quitMenuItem = new QuitMenuItem(mazeApp)); + add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp)); } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java new file mode 100644 index 0000000..746181e --- /dev/null +++ b/src/ui/LoadMazeMenuItem.java @@ -0,0 +1,40 @@ +package ui; + +import javax.swing.JFileChooser; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileNameExtensionFilter; + +import model.MazeAppModel; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class LoadMazeMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public LoadMazeMenuItem(MazeApp mazeApp) { + + super("Load maze"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + JFileChooser fileChooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter( + "Text files", "txt"); + fileChooser.setFileFilter(filter); + int returnVal = fileChooser.showOpenDialog(mazeApp); + if(returnVal == JFileChooser.APPROVE_OPTION) { + System.out.println("You chose to open this file: " + + fileChooser.getSelectedFile().getName()); + String filename = fileChooser.getSelectedFile().getName(); + } + mazeApp.getMazeAppModel().setMaze(model.Maze.); + } +} diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index caead12..806379b 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -25,23 +25,22 @@ public class QuitMenuItem extends JMenuItem MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); if (mazeAppModel.isModified()) { - int response = JOptionPane.showInternalOptionDialog(this, + int response = JOptionPane.showInternalOptionDialog(this, "Maze not saved. Save it ?", "Quit application", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, - null,null,null) ; - switch (response) { - case JOptionPane.CANCEL_OPTION: - return; - case JOptionPane.OK_OPTION: - mazeAppModel.saveToFile(); - break; - case JOptionPane.NO_OPTION: - break; - } - } - System.exit(0) ; - } - + null,null,null); + switch (response) { + case JOptionPane.CANCEL_OPTION: + return; + case JOptionPane.OK_OPTION: + mazeAppModel.saveToFile(); + break; + case JOptionPane.NO_OPTION: + break; + } + } + System.exit(0) ; + } } From a5d0df4b96c6467026c5b57ceb48c487bebde07e Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 16 Dec 2017 16:21:57 +0100 Subject: [PATCH 3/4] Modified LoadMenuItem --- data/labyOutput.txt | 4 ++-- src/model/Maze.java | 9 +++++++++ src/model/MazeAppModel.java | 7 +++++-- src/ui/LoadMazeMenuItem.java | 5 +++-- 4 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 src/model/Maze.java diff --git a/data/labyOutput.txt b/data/labyOutput.txt index ca83a98..1436118 100644 --- a/data/labyOutput.txt +++ b/data/labyOutput.txt @@ -4,5 +4,5 @@ EWEEEWWE EWWEWWEE EWWEWWEW EEWEWAEW -WEEEWWWW -WWEWWWWW +WEEEWWWE +WWEWWWWE diff --git a/src/model/Maze.java b/src/model/Maze.java new file mode 100644 index 0000000..3bd6162 --- /dev/null +++ b/src/model/Maze.java @@ -0,0 +1,9 @@ +package model; + +public class Maze extends maze.Maze { + + public Maze(String filename) { + super(filename); + } + +} diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index dcb09b9..f331efe 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -1,14 +1,17 @@ package model; -import maze.Maze; +import java.util.Observable; -public class MazeAppModel { +public class MazeAppModel extends Observable { private Maze maze = new Maze("data/labyrinthe.txt"); private boolean modified = false; public void setMaze(Maze maze) { this.maze = maze; + modified = true; + setChanged(); + notifyObservers(); } public Maze getMaze() { diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 746181e..06d0505 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -6,6 +6,7 @@ import javax.swing.JOptionPane; import javax.swing.filechooser.FileNameExtensionFilter; import model.MazeAppModel; +import model.Maze; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -33,8 +34,8 @@ public class LoadMazeMenuItem extends JMenuItem if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + fileChooser.getSelectedFile().getName()); - String filename = fileChooser.getSelectedFile().getName(); } - mazeApp.getMazeAppModel().setMaze(model.Maze.); + String filename = fileChooser.getSelectedFile().getPath(); + mazeApp.getMazeAppModel().setMaze(new Maze(filename)); } } From bac9b54d58233eb3da7c224026ac355f10444cc4 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 16 Dec 2017 17:40:35 +0100 Subject: [PATCH 4/4] Fixing lines and spaces and restoring labyOutput --- data/labyOutput.txt | 16 ++++++++-------- src/MainTest.java | 12 ++++++------ src/model/Maze.java | 1 - src/model/MazeAppModel.java | 2 +- src/ui/ArrivalDrawingBox.java | 1 - src/ui/DepartureDrawingBox.java | 1 - src/ui/DrawingBox.java | 6 +++--- src/ui/DrawingPanelGrid.java | 13 ++++++------- src/ui/EmptyDrawingBox.java | 1 - src/ui/FileMenu.java | 8 +++----- src/ui/LoadMazeMenuItem.java | 4 ++-- src/ui/MazeApp.java | 12 ++++++------ src/ui/MazeMenuBar.java | 7 +++---- src/ui/QuitMenuItem.java | 8 ++++---- src/ui/WallDrawingBox.java | 1 - src/ui/WindowPanel.java | 7 +++---- 16 files changed, 45 insertions(+), 55 deletions(-) diff --git a/data/labyOutput.txt b/data/labyOutput.txt index 1436118..6e7752a 100644 --- a/data/labyOutput.txt +++ b/data/labyOutput.txt @@ -1,8 +1,8 @@ -WEWWEEEE -EDEWEWWE -EWEEEWWE -EWWEWWEE -EWWEWWEW -EEWEWAEW -WEEEWWWE -WWEWWWWE +WEEEEEWW +EDWWWEEW +WEEWWWEE +WWEEEEEW +EEEWWWWW +EWWWWAWW +EWWEEEWW +EEEEWWWW \ No newline at end of file diff --git a/src/MainTest.java b/src/MainTest.java index b7cd87e..a05dadc 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -7,19 +7,19 @@ import ui.*; public class MainTest { public static void main(String[] args) { - + Maze mazeTest = new Maze("data/labyrinthe.txt") ; - + Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]); - + ArrayList shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]); - + for (VertexInterface vertex : shortest) { System.out.println(vertex); } - + mazeTest.saveToTextFile("data/labyOutput.txt"); - + new MazeApp(); } diff --git a/src/model/Maze.java b/src/model/Maze.java index 3bd6162..adbc938 100644 --- a/src/model/Maze.java +++ b/src/model/Maze.java @@ -5,5 +5,4 @@ public class Maze extends maze.Maze { public Maze(String filename) { super(filename); } - } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index f331efe..2639ee1 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -25,4 +25,4 @@ public class MazeAppModel extends Observable { public void saveToFile() { maze.saveToTextFile("data/labyOutput.txt"); } -} \ No newline at end of file +} diff --git a/src/ui/ArrivalDrawingBox.java b/src/ui/ArrivalDrawingBox.java index 6677e9a..f221d34 100644 --- a/src/ui/ArrivalDrawingBox.java +++ b/src/ui/ArrivalDrawingBox.java @@ -7,5 +7,4 @@ public class ArrivalDrawingBox extends DrawingBox { public ArrivalDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - } diff --git a/src/ui/DepartureDrawingBox.java b/src/ui/DepartureDrawingBox.java index 3c21ec1..bd4ca1b 100644 --- a/src/ui/DepartureDrawingBox.java +++ b/src/ui/DepartureDrawingBox.java @@ -7,5 +7,4 @@ public class DepartureDrawingBox extends DrawingBox { public DepartureDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - } diff --git a/src/ui/DrawingBox.java b/src/ui/DrawingBox.java index 2397c7e..d2f24ed 100644 --- a/src/ui/DrawingBox.java +++ b/src/ui/DrawingBox.java @@ -8,12 +8,12 @@ public abstract class DrawingBox extends JPanel { private final MazeApp mazeApp; private Color color; - + public DrawingBox(MazeApp mazeApp, Color color) { - + this.mazeApp = mazeApp; this.color = color; - + setBackground(color); setPreferredSize(new Dimension(40,40)); } diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index 4671537..6d220d4 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -12,25 +12,25 @@ public class DrawingPanelGrid extends JPanel { private Maze maze; private int dimensionX; private int dimensionY; - + public DrawingPanelGrid(MazeApp mazeApp) { - + this.mazeApp = mazeApp; this.maze = mazeApp.getMazeAppModel().getMaze(); this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); - + setLayout(new GridLayout(this.dimensionX, this.dimensionY)); initFromMaze(maze); - + setPreferredSize(new Dimension(256, 256)); } - + private void initFromMaze(Maze maze) { int dimensionX = maze.getDimensionX(); int dimensionY = maze.getDimensionY(); - + for (int i = 0; i < dimensionX; i++) { for (int j = 0; j < dimensionY; j++) { if (maze.getMaze()[j][i].isWalkable()) { @@ -49,6 +49,5 @@ public class DrawingPanelGrid extends JPanel { } } } - } } diff --git a/src/ui/EmptyDrawingBox.java b/src/ui/EmptyDrawingBox.java index 1b649d3..7fe2c69 100644 --- a/src/ui/EmptyDrawingBox.java +++ b/src/ui/EmptyDrawingBox.java @@ -7,5 +7,4 @@ public class EmptyDrawingBox extends DrawingBox { public EmptyDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - } diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index c2438d0..2cfac46 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -7,15 +7,13 @@ public class FileMenu extends JMenu { private final MazeApp mazeApp; private final QuitMenuItem quitMenuItem; private final LoadMazeMenuItem loadMazeMenuItem; - + public FileMenu(MazeApp mazeApp) { - + super("File"); this.mazeApp = mazeApp; - + add(quitMenuItem = new QuitMenuItem(mazeApp)); add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp)); - } - } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 06d0505..9724fde 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -15,14 +15,14 @@ public class LoadMazeMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; - + public LoadMazeMenuItem(MazeApp mazeApp) { super("Load maze"); this.mazeApp = mazeApp; addActionListener(this); } - + public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 3dbb3e6..0b7f1c1 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -5,24 +5,24 @@ import maze.Maze; import model.MazeAppModel; public class MazeApp extends JFrame { - + private final MazeMenuBar mazeMenuBar; private final WindowPanel windowPanel; private MazeAppModel mazeAppModel = new MazeAppModel(); public MazeApp() { - + super("Maze Solving Application"); - + setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); setContentPane(windowPanel = new WindowPanel(this)) ; - + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - + pack(); setVisible(true); } - + public MazeAppModel getMazeAppModel() { return mazeAppModel; } diff --git a/src/ui/MazeMenuBar.java b/src/ui/MazeMenuBar.java index f8d47da..60fba7a 100644 --- a/src/ui/MazeMenuBar.java +++ b/src/ui/MazeMenuBar.java @@ -6,13 +6,12 @@ public class MazeMenuBar extends JMenuBar { private final MazeApp mazeApp; private final FileMenu fileMenu; - + public MazeMenuBar(MazeApp mazeApp) { - + super(); this.mazeApp = mazeApp; - + add(fileMenu = new FileMenu(mazeApp)); - } } diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 806379b..46908e5 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -12,18 +12,18 @@ public class QuitMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; - + public QuitMenuItem(MazeApp mazeApp) { super("Quit"); this.mazeApp = mazeApp; addActionListener(this); } - + public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - + if (mazeAppModel.isModified()) { int response = JOptionPane.showInternalOptionDialog(this, "Maze not saved. Save it ?", @@ -41,6 +41,6 @@ public class QuitMenuItem extends JMenuItem break; } } - System.exit(0) ; + System.exit(0); } } diff --git a/src/ui/WallDrawingBox.java b/src/ui/WallDrawingBox.java index 207bc7a..9a6483c 100644 --- a/src/ui/WallDrawingBox.java +++ b/src/ui/WallDrawingBox.java @@ -7,5 +7,4 @@ public class WallDrawingBox extends DrawingBox { public WallDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - } diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 55ec5c0..774a57a 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -7,12 +7,11 @@ import java.awt.*; public class WindowPanel extends JPanel { private final DrawingPanelGrid drawingPanelGrid; - + public WindowPanel(MazeApp mazeApp) { - + setLayout(new BorderLayout()); - + add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER); } - }