Merge branch 'menuBar' into mvc

This commit was merged in pull request #17.
This commit is contained in:
regar42 committed 2017-12-16 18:03:56 +01:00
commit 083962eec5
13 files changed
+106 -21

No files matched your search

+8 -8
View File
@@ -1,8 +1,8 @@
WEWWEEEE WEEEEEWW
EDEWEWWE EDWWWEEW
EWEEEWWE WEEWWWEE
EWWEWWEE WWEEEEEW
EWWEWWEW EEEWWWWW
EEWEWAEW EWWWWAWW
WEEEWWWW EWWEEEWW
WWEWWWWW EEEEWWWW
+8
View File
@@ -0,0 +1,8 @@
package model;
public class Maze extends maze.Maze {
public Maze(String filename) {
super(filename);
}
}
+14 -2
View File
@@ -1,16 +1,28 @@
package model; 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 Maze maze = new Maze("data/labyrinthe.txt");
private boolean modified = false;
public void setMaze(Maze maze) { public void setMaze(Maze maze) {
this.maze = maze; this.maze = maze;
modified = true;
setChanged();
notifyObservers();
} }
public Maze getMaze() { public Maze getMaze() {
return maze; return maze;
} }
public boolean isModified() {
return modified;
}
public void saveToFile() {
maze.saveToTextFile("data/labyOutput.txt");
}
} }
-1
View File
@@ -7,5 +7,4 @@ public class ArrivalDrawingBox extends DrawingBox {
public ArrivalDrawingBox(MazeApp mazeApp, Color color) { public ArrivalDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color); super(mazeApp, color);
} }
} }
-1
View File
@@ -7,5 +7,4 @@ public class DepartureDrawingBox extends DrawingBox {
public DepartureDrawingBox(MazeApp mazeApp, Color color) { public DepartureDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color); super(mazeApp, color);
} }
} }
-1
View File
@@ -49,6 +49,5 @@ public class DrawingPanelGrid extends JPanel {
} }
} }
} }
} }
} }
-1
View File
@@ -7,5 +7,4 @@ public class EmptyDrawingBox extends DrawingBox {
public EmptyDrawingBox(MazeApp mazeApp, Color color) { public EmptyDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color); super(mazeApp, color);
} }
} }
+2 -2
View File
@@ -6,6 +6,7 @@ public class FileMenu extends JMenu {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final QuitMenuItem quitMenuItem; private final QuitMenuItem quitMenuItem;
private final LoadMazeMenuItem loadMazeMenuItem;
public FileMenu(MazeApp mazeApp) { public FileMenu(MazeApp mazeApp) {
@@ -13,7 +14,6 @@ public class FileMenu extends JMenu {
this.mazeApp = mazeApp; this.mazeApp = mazeApp;
add(quitMenuItem = new QuitMenuItem(mazeApp)); add(quitMenuItem = new QuitMenuItem(mazeApp));
add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp));
} }
} }
+41
View File
@@ -0,0 +1,41 @@
package ui;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
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;
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().getPath();
mazeApp.getMazeAppModel().setMaze(new Maze(filename));
}
}
-1
View File
@@ -13,6 +13,5 @@ public class MazeMenuBar extends JMenuBar {
this.mazeApp = mazeApp; this.mazeApp = mazeApp;
add(fileMenu = new FileMenu(mazeApp)); add(fileMenu = new FileMenu(mazeApp));
} }
} }
+33 -2
View File
@@ -1,8 +1,15 @@
package ui; 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; private final MazeApp mazeApp;
@@ -10,6 +17,30 @@ public class QuitMenuItem extends JMenuItem {
super("Quit"); super("Quit");
this.mazeApp = mazeApp; 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);
}
} }
-1
View File
@@ -7,5 +7,4 @@ public class WallDrawingBox extends DrawingBox {
public WallDrawingBox(MazeApp mazeApp, Color color) { public WallDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color); super(mazeApp, color);
} }
} }
-1
View File
@@ -14,5 +14,4 @@ public class WindowPanel extends JPanel
add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER); add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER);
} }
} }