Open app on empty maze and improve load/save #23
No files matched your search
+49
-9
@@ -29,31 +29,71 @@ public class Maze
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Maze(Maze maze){
|
public Maze(Maze maze) {
|
||||||
|
|
||||||
this.dimensionX = maze.getDimensionX();
|
this.dimensionX = maze.getDimensionX();
|
||||||
this.dimensionY = maze.getDimensionY();
|
this.dimensionY = maze.getDimensionY();
|
||||||
this.maze = new Box[dimensionY][dimensionX];
|
this.maze = matrixDeepCopy(maze.getMaze());
|
||||||
|
}
|
||||||
|
|
||||||
for (int line = 0; line < dimensionY; line++) {
|
public Maze(int numberOfLines, int numberOfRows, Box[][] boxes) {
|
||||||
for (int row = 0; row < dimensionX; row++) {
|
this.dimensionY = numberOfLines;
|
||||||
Box box = maze.getMaze()[line][row];
|
this.dimensionX = numberOfRows;
|
||||||
|
this.maze = matrixDeepCopy(boxes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Box[][] matrixDeepCopy (Box[][] original) {
|
||||||
|
int numberOfLines = 0;
|
||||||
|
int numberOfRows = 0;
|
||||||
|
Box[][] copy;
|
||||||
|
/*
|
||||||
|
* We first get the dimensions of the matrix
|
||||||
|
*/
|
||||||
|
for (Box[] lineBoxes: original) {
|
||||||
|
numberOfLines++;
|
||||||
|
}
|
||||||
|
if (numberOfLines > 0) {
|
||||||
|
for (Box box: original[0]) {
|
||||||
|
numberOfRows++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
copy = new Box[numberOfLines][numberOfRows];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Now we proceed to the deep copy of each box
|
||||||
|
*/
|
||||||
|
for (int line = 0; line < numberOfLines; line++) {
|
||||||
|
for (int row = 0; row < numberOfRows; row++) {
|
||||||
|
Box box = original[line][row];
|
||||||
if (box.isWalkable()) {
|
if (box.isWalkable()) {
|
||||||
if (box.isDeparture()) {
|
if (box.isDeparture()) {
|
||||||
this.maze[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this);
|
copy[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this);
|
||||||
}
|
}
|
||||||
else if (box.isArrival()) {
|
else if (box.isArrival()) {
|
||||||
this.maze[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this);
|
copy[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.maze[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this);
|
copy[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.maze[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this);
|
copy[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Maze emptySquareMaze() {
|
||||||
|
int dimensionX = 10;
|
||||||
|
int dimensionY = 10;
|
||||||
|
Box[][] maze = new Box[dimensionY][dimensionX];
|
||||||
|
for (int line = 0; line < dimensionY; line++) {
|
||||||
|
for (int row = 0; row < dimensionX; row++) {
|
||||||
|
maze[line][row] = new EBox("E", line, row, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Maze(dimensionY, dimensionX, maze);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCost(VertexInterface start, VertexInterface end) {
|
public int getCost(VertexInterface start, VertexInterface end) {
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
package model;
|
|
||||||
|
|
||||||
import maze.Maze;
|
|
||||||
|
|
||||||
public class Box extends maze.Box {
|
|
||||||
|
|
||||||
public Box(String label, int x, int y, Maze maze) {
|
|
||||||
super(label, x, y, maze);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package model;
|
|
||||||
|
|
||||||
public class Maze extends maze.Maze {
|
|
||||||
|
|
||||||
public Maze(String filename) {
|
|
||||||
super(filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Maze(Maze maze) {
|
|
||||||
super(maze);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+34
-41
@@ -10,67 +10,60 @@ import java.util.Observer;
|
|||||||
import dijkstra.VertexInterface;
|
import dijkstra.VertexInterface;
|
||||||
import dijkstra.Dijkstra;
|
import dijkstra.Dijkstra;
|
||||||
import dijkstra.Previous;
|
import dijkstra.Previous;
|
||||||
|
import maze.Maze;
|
||||||
|
|
||||||
public class MazeAppModel extends Observable {
|
public class MazeAppModel extends Observable {
|
||||||
|
|
||||||
private Maze maze = new Maze("data/labyrinthe.txt");
|
private String filename;
|
||||||
private VertexInterface departure = maze.getDeparture();
|
private Maze maze;
|
||||||
private VertexInterface arrival = maze.getArrival();
|
private VertexInterface departure;
|
||||||
private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure);
|
private VertexInterface arrival;
|
||||||
private ArrayList<VertexInterface> shortest = previous.getShortestPathTo(arrival);
|
private Previous previous;
|
||||||
private ArrayList<VertexInterface> boxList = maze.getVertexes();
|
private ArrayList<VertexInterface> shortest;
|
||||||
|
private ArrayList<VertexInterface> boxList;
|
||||||
private Boolean modified = false;
|
private Boolean modified = false;
|
||||||
|
|
||||||
public void setMaze(Maze maze) {
|
public MazeAppModel() {
|
||||||
this.maze = new Maze(maze);
|
maze = new Maze(Maze.emptySquareMaze());
|
||||||
|
departure = maze.getDeparture();
|
||||||
|
arrival = maze.getArrival();
|
||||||
|
boxList = maze.getVertexes();
|
||||||
|
if (departure == null || arrival == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
previous = (Previous) Dijkstra.dijkstra(maze, departure);
|
||||||
|
shortest = previous.getShortestPathTo(arrival);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadMaze(String filename) {
|
||||||
|
this.filename = new String(filename);
|
||||||
|
maze = new Maze(this.filename);
|
||||||
modified = true;
|
modified = true;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(maze);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Maze getMaze() {
|
public Maze getMaze() {
|
||||||
return maze;
|
return maze;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getFilename() {
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isModified() {
|
public boolean isModified() {
|
||||||
return modified;
|
return modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveToFile() {
|
public void saveToFile() {
|
||||||
maze.saveToTextFile("data/labyOutput.txt");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveToFile(String filename) {
|
|
||||||
maze.saveToTextFile(filename);
|
maze.saveToTextFile(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paintMaze(Graphics2D g, int width, int height) {
|
public void saveToFile(String filename) {
|
||||||
|
this.filename = new String(filename);
|
||||||
int dimensionX = maze.getDimensionX();
|
maze.saveToTextFile(this.filename);
|
||||||
int dimensionY = maze.getDimensionY();
|
modified = true;
|
||||||
int boxWidth = width/dimensionX;
|
setChanged();
|
||||||
int boxHeight = height/dimensionY;
|
notifyObservers(MazeAppModelMessage.FileChange);
|
||||||
|
|
||||||
for (int line = 0; line < dimensionY; line++) {
|
|
||||||
for (int row = 0; row < dimensionX; row++) {
|
|
||||||
if (maze.getMaze()[line][row].isWalkable()) {
|
|
||||||
if (maze.getMaze()[line][row].isDeparture()) {
|
|
||||||
g.setColor(Color.GREEN);
|
|
||||||
}
|
|
||||||
else if (maze.getMaze()[line][row].isArrival()) {
|
|
||||||
g.setColor(Color.RED);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
g.setColor(Color.BLACK);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
g.setColor(Color.WHITE);
|
|
||||||
}
|
|
||||||
g.fill3DRect(row*boxWidth, line*boxHeight,
|
|
||||||
boxWidth, boxHeight,
|
|
||||||
true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package model;
|
||||||
|
|
||||||
|
public class MazeAppModelMessage {
|
||||||
|
public final static String MazeRenewal = "MazeRenewal";
|
||||||
|
public final static String FileChange = "FileChange";
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package ui;
|
||||||
|
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
|
|
||||||
|
public class FileBox {
|
||||||
|
private static JFileChooser fileChooser = new JFileChooser();
|
||||||
|
private static FileNameExtensionFilter filter =
|
||||||
|
new FileNameExtensionFilter("Text files", "txt");
|
||||||
|
|
||||||
|
public static String load(MazeApp mazeApp) {
|
||||||
|
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().getPath();
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String save(MazeApp mazeApp) {
|
||||||
|
fileChooser.setFileFilter(filter);
|
||||||
|
|
||||||
|
int returnVal = fileChooser.showSaveDialog(mazeApp);
|
||||||
|
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
|
System.out.println("You chose to save to this file: " +
|
||||||
|
fileChooser.getSelectedFile().getName());
|
||||||
|
String filename = fileChooser.getSelectedFile().getPath();
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,17 +5,19 @@ import javax.swing.JMenu;
|
|||||||
public class FileMenu extends JMenu {
|
public class FileMenu extends JMenu {
|
||||||
|
|
||||||
private final MazeApp mazeApp;
|
private final MazeApp mazeApp;
|
||||||
private final QuitMenuItem quitMenuItem;
|
|
||||||
private final LoadMazeMenuItem loadMazeMenuItem;
|
private final LoadMazeMenuItem loadMazeMenuItem;
|
||||||
private final SaveMazeMenuItem saveMazeMenuItem;
|
private final SaveMazeMenuItem saveMazeMenuItem;
|
||||||
|
private final SaveMazeAsMenuItem saveMazeAsMenuItem;
|
||||||
|
private final QuitMenuItem quitMenuItem;
|
||||||
|
|
||||||
public FileMenu(MazeApp mazeApp) {
|
public FileMenu(MazeApp mazeApp) {
|
||||||
|
|
||||||
super("File");
|
super("File");
|
||||||
this.mazeApp = mazeApp;
|
this.mazeApp = mazeApp;
|
||||||
|
|
||||||
add(quitMenuItem = new QuitMenuItem(mazeApp));
|
|
||||||
add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp));
|
add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp));
|
||||||
add(saveMazeMenuItem = new SaveMazeMenuItem(mazeApp));
|
add(saveMazeMenuItem = new SaveMazeMenuItem(mazeApp));
|
||||||
|
add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp));
|
||||||
|
add(quitMenuItem = new QuitMenuItem(mazeApp));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,12 @@
|
|||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import javax.swing.JFileChooser;
|
|
||||||
import javax.swing.JMenuItem;
|
import javax.swing.JMenuItem;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.KeyStroke;
|
||||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
|
||||||
|
|
||||||
import model.MazeAppModel;
|
|
||||||
import model.Maze;
|
|
||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
import model.MazeAppModel;
|
||||||
|
|
||||||
public class LoadMazeMenuItem extends JMenuItem
|
public class LoadMazeMenuItem extends JMenuItem
|
||||||
implements ActionListener {
|
implements ActionListener {
|
||||||
|
|
||||||
@@ -20,25 +16,16 @@ public class LoadMazeMenuItem extends JMenuItem
|
|||||||
|
|
||||||
super("Load maze");
|
super("Load maze");
|
||||||
this.mazeApp = mazeApp;
|
this.mazeApp = mazeApp;
|
||||||
|
this.setAccelerator(KeyStroke.getKeyStroke("control O"));
|
||||||
addActionListener(this);
|
addActionListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void actionPerformed(ActionEvent evt) {
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
|
||||||
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
||||||
JFileChooser fileChooser = new JFileChooser();
|
String filename = FileBox.load(mazeApp);
|
||||||
|
if (filename != null) {
|
||||||
FileNameExtensionFilter filter =
|
mazeApp.getMazeAppModel().loadMaze(filename);
|
||||||
new FileNameExtensionFilter("Text files", "txt");
|
|
||||||
fileChooser.setFileFilter(filter);
|
|
||||||
|
|
||||||
String filename;
|
|
||||||
int returnVal = fileChooser.showOpenDialog(mazeApp);
|
|
||||||
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
|
||||||
System.out.println("You chose to open this file: " +
|
|
||||||
fileChooser.getSelectedFile().getName());
|
|
||||||
filename = fileChooser.getSelectedFile().getPath();
|
|
||||||
mazeApp.getMazeAppModel().setMaze(new Maze(filename));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+35
-4
@@ -7,6 +7,7 @@ import java.awt.Graphics2D;
|
|||||||
import java.awt.GridLayout;
|
import java.awt.GridLayout;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import maze.Maze;
|
import maze.Maze;
|
||||||
|
import model.MazeAppModelMessage;
|
||||||
|
|
||||||
public class MazeDrawing extends JPanel {
|
public class MazeDrawing extends JPanel {
|
||||||
|
|
||||||
@@ -17,7 +18,6 @@ public class MazeDrawing extends JPanel {
|
|||||||
this.mazeApp = mazeApp;
|
this.mazeApp = mazeApp;
|
||||||
setBackground(Color.BLACK);
|
setBackground(Color.BLACK);
|
||||||
setPreferredSize(new Dimension(500,500)) ;
|
setPreferredSize(new Dimension(500,500)) ;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -29,13 +29,44 @@ public class MazeDrawing extends JPanel {
|
|||||||
int width = getWidth();
|
int width = getWidth();
|
||||||
int height = getHeight();
|
int height = getHeight();
|
||||||
|
|
||||||
mazeApp.getMazeAppModel().paintMaze(g2d, width, height);
|
paintMaze(g2d, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void paintMaze(Graphics2D g, int width, int height) {
|
||||||
|
Maze maze = mazeApp.getMazeAppModel().getMaze();
|
||||||
|
int dimensionX = maze.getDimensionX();
|
||||||
|
int dimensionY = maze.getDimensionY();
|
||||||
|
int boxWidth = width / dimensionX;
|
||||||
|
int boxHeight = height / dimensionY;
|
||||||
|
|
||||||
|
for (int line = 0; line < dimensionY; line++) {
|
||||||
|
for (int row = 0; row < dimensionX; row++) {
|
||||||
|
if (maze.getMaze()[line][row].isWalkable()) {
|
||||||
|
if (maze.getMaze()[line][row].isDeparture()) {
|
||||||
|
g.setColor(Color.GREEN);
|
||||||
|
}
|
||||||
|
else if (maze.getMaze()[line][row].isArrival()) {
|
||||||
|
g.setColor(Color.RED);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
}
|
||||||
|
g.fill3DRect(row * boxWidth,
|
||||||
|
line * boxHeight,
|
||||||
|
boxWidth,
|
||||||
|
boxHeight,
|
||||||
|
true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void notifyForUpdates(Object param) {
|
public void notifyForUpdates(Object param) {
|
||||||
|
|
||||||
if (param instanceof Maze) {
|
if (param == MazeAppModelMessage.MazeRenewal) {
|
||||||
this.repaint();
|
this.repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import java.awt.event.ActionListener;
|
|||||||
|
|
||||||
import javax.swing.JMenuItem;
|
import javax.swing.JMenuItem;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
import model.MazeAppModel;
|
import model.MazeAppModel;
|
||||||
|
|
||||||
@@ -17,6 +18,7 @@ public class QuitMenuItem extends JMenuItem
|
|||||||
|
|
||||||
super("Quit");
|
super("Quit");
|
||||||
this.mazeApp = mazeApp;
|
this.mazeApp = mazeApp;
|
||||||
|
this.setAccelerator(KeyStroke.getKeyStroke("control Q"));
|
||||||
addActionListener(this);
|
addActionListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,11 +28,11 @@ public class QuitMenuItem extends JMenuItem
|
|||||||
|
|
||||||
if (mazeAppModel.isModified()) {
|
if (mazeAppModel.isModified()) {
|
||||||
int response = JOptionPane.showInternalOptionDialog(this,
|
int response = JOptionPane.showInternalOptionDialog(this,
|
||||||
"Maze not saved. Save it ?",
|
"Maze not saved. Save it ?",
|
||||||
"Quit application",
|
"Quit application",
|
||||||
JOptionPane.YES_NO_CANCEL_OPTION,
|
JOptionPane.YES_NO_CANCEL_OPTION,
|
||||||
JOptionPane.WARNING_MESSAGE,
|
JOptionPane.WARNING_MESSAGE,
|
||||||
null,null,null);
|
null, null, null);
|
||||||
switch (response) {
|
switch (response) {
|
||||||
case JOptionPane.CANCEL_OPTION:
|
case JOptionPane.CANCEL_OPTION:
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
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,16 +1,16 @@
|
|||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import javax.swing.JFileChooser;
|
|
||||||
import javax.swing.JMenuItem;
|
import javax.swing.JMenuItem;
|
||||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
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;
|
import model.MazeAppModel;
|
||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
public class SaveMazeMenuItem extends JMenuItem
|
public class SaveMazeMenuItem extends JMenuItem
|
||||||
implements ActionListener {
|
implements ActionListener, Observer {
|
||||||
|
|
||||||
private final MazeApp mazeApp;
|
private final MazeApp mazeApp;
|
||||||
|
|
||||||
@@ -18,24 +18,30 @@ public class SaveMazeMenuItem extends JMenuItem
|
|||||||
|
|
||||||
super("Save maze");
|
super("Save maze");
|
||||||
this.mazeApp = mazeApp;
|
this.mazeApp = mazeApp;
|
||||||
|
this.setAccelerator(KeyStroke.getKeyStroke("control S"));
|
||||||
|
mazeApp.getMazeAppModel().addObserver(this);
|
||||||
|
if (mazeApp.getMazeAppModel().getFilename() == null) {
|
||||||
|
this.setEnabled(false);
|
||||||
|
}
|
||||||
addActionListener(this);
|
addActionListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void actionPerformed(ActionEvent evt) {
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
|
||||||
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
||||||
JFileChooser fileChooser = new JFileChooser();
|
|
||||||
|
|
||||||
FileNameExtensionFilter filter =
|
if (mazeAppModel.getFilename() != null) {
|
||||||
new FileNameExtensionFilter("Text files", "txt");
|
mazeAppModel.saveToFile();
|
||||||
fileChooser.setFileFilter(filter);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int returnVal = fileChooser.showSaveDialog(mazeApp);
|
@Override
|
||||||
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
public void update(Observable obs, Object param) {
|
||||||
System.out.println("You chose to save to this file: " +
|
if (mazeApp.getMazeAppModel().getFilename() != null) {
|
||||||
fileChooser.getSelectedFile().getName());
|
this.setEnabled(true);
|
||||||
String filename = fileChooser.getSelectedFile().getPath();
|
}
|
||||||
mazeApp.getMazeAppModel().saveToFile(filename);
|
else {
|
||||||
|
this.setEnabled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user