Fix add column/line and spinners, cleanup Maintest
* Remove legacy test code in MainTest maze: * Add methods to add/del lines and columns * Add various calls to matrixDeepCopy, allowing to overwrite only a part of a matrix model: * Better delegation to Maze : let Maze handle column/line adds/deletions ui: * Add buttons to context menu to add/remove lines and columns * Add buttons to Maze Menu to delete lines and columns * Fix the spinners, it used to call a new instance of itself, which made impossible to simple get the values from the spinners
This commit is contained in:
1 parent
286023c5d8
commit
024a4f70c3
12 files changed
+403
-135
No files matched your search
@@ -9,17 +9,30 @@ public class AddLineMenuItem extends JMenuItem
|
||||
implements ActionListener {
|
||||
|
||||
private final MazeApp mazeApp;
|
||||
private final int lineNum;
|
||||
|
||||
public AddLineMenuItem(MazeApp mazeApp) {
|
||||
|
||||
super("Add line");
|
||||
this.mazeApp = mazeApp;
|
||||
lineNum = -1;
|
||||
addActionListener(this);
|
||||
}
|
||||
|
||||
public AddLineMenuItem(MazeApp mazeApp, int lineNum) {
|
||||
super("Add line");
|
||||
this.mazeApp = mazeApp;
|
||||
this.lineNum = lineNum;
|
||||
addActionListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
|
||||
this.mazeApp.getMazeAppModel().addEmptyLine();
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (lineNum == -1) {
|
||||
mazeApp.addEmptyLine();
|
||||
}
|
||||
else {
|
||||
mazeApp.addEmptyLine(lineNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,25 +3,37 @@ package ui;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.Action;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.JMenuItem;
|
||||
|
||||
public class AddRowMenuItem extends JMenuItem
|
||||
implements ActionListener {
|
||||
|
||||
private final MazeApp mazeApp;
|
||||
private final int rowNum;
|
||||
|
||||
public AddRowMenuItem(MazeApp mazeApp) {
|
||||
|
||||
super("Add row");
|
||||
this.mazeApp = mazeApp;
|
||||
rowNum = -1;
|
||||
addActionListener(this);
|
||||
}
|
||||
|
||||
public AddRowMenuItem(MazeApp mazeApp, int rowNum) {
|
||||
|
||||
super("Add row");
|
||||
this.mazeApp = mazeApp;
|
||||
this.rowNum = rowNum;
|
||||
addActionListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
|
||||
this.mazeApp.getMazeAppModel().addEmptyColumn();
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (rowNum == -1) {
|
||||
mazeApp.addEmptyColumn();
|
||||
}
|
||||
else {
|
||||
mazeApp.addEmptyColumn(rowNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,10 @@ public class BoxTypeEditionMenu extends JPopupMenu {
|
||||
JMenuItem setEBox;
|
||||
JMenuItem setDBox;
|
||||
JMenuItem setABox;
|
||||
JMenuItem addRow;
|
||||
JMenuItem addLine;
|
||||
JMenuItem delRow;
|
||||
JMenuItem delLine;
|
||||
|
||||
public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) {
|
||||
add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow));
|
||||
@@ -21,5 +25,10 @@ public class BoxTypeEditionMenu extends JPopupMenu {
|
||||
setWBox.setEnabled(box.isWalkable());
|
||||
setDBox.setEnabled(! box.isDeparture());
|
||||
setABox.setEnabled(! box.isArrival());
|
||||
|
||||
add(addRow = new AddRowMenuItem(mazeApp, boxRow));
|
||||
add(addLine = new AddLineMenuItem(mazeApp, boxLine));
|
||||
add(delRow = new DelRowMenuItem(mazeApp, boxRow));
|
||||
add(delLine = new DelLineMenuItem(mazeApp, boxLine));
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,6 @@ package ui;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.Action;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.KeyStroke;
|
||||
|
||||
@@ -23,13 +21,6 @@ public class ClearMazeMenuItem extends JMenuItem
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
int numberOfLines = mazeApp.getMazeAppModel()
|
||||
.getMaze().getDimensionY();
|
||||
int numberOfRows = mazeApp.getMazeAppModel()
|
||||
.getMaze().getDimensionX();
|
||||
this.mazeApp.getMazeAppModel()
|
||||
.newClearMaze(numberOfLines, numberOfRows);
|
||||
System.out.println("test");
|
||||
mazeApp.clearMaze();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package ui;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JMenuItem;
|
||||
|
||||
public class DelLineMenuItem extends JMenuItem
|
||||
implements ActionListener {
|
||||
|
||||
private final MazeApp mazeApp;
|
||||
private final int lineNum;
|
||||
|
||||
public DelLineMenuItem(MazeApp mazeApp) {
|
||||
|
||||
super("Delete line");
|
||||
this.mazeApp = mazeApp;
|
||||
lineNum = -1;
|
||||
addActionListener(this);
|
||||
}
|
||||
|
||||
public DelLineMenuItem(MazeApp mazeApp, int lineNum) {
|
||||
super("Delete line");
|
||||
this.mazeApp = mazeApp;
|
||||
this.lineNum = lineNum;
|
||||
addActionListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (lineNum == -1) {
|
||||
mazeApp.delLine();
|
||||
}
|
||||
else {
|
||||
mazeApp.delLine(lineNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package ui;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JMenuItem;
|
||||
|
||||
public class DelRowMenuItem extends JMenuItem
|
||||
implements ActionListener {
|
||||
|
||||
private final MazeApp mazeApp;
|
||||
private final int rowNum;
|
||||
|
||||
public DelRowMenuItem(MazeApp mazeApp) {
|
||||
super("Delete row");
|
||||
this.mazeApp = mazeApp;
|
||||
rowNum = -1;
|
||||
addActionListener(this);
|
||||
}
|
||||
|
||||
public DelRowMenuItem(MazeApp mazeApp, int rowNum) {
|
||||
super("Delete row");
|
||||
this.mazeApp = mazeApp;
|
||||
this.rowNum = rowNum;
|
||||
addActionListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (rowNum == -1) {
|
||||
mazeApp.delColumn();
|
||||
}
|
||||
else {
|
||||
mazeApp.delColumn(rowNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,4 +87,41 @@ public class MazeApp extends JFrame
|
||||
mazeAppModel.setPathDrawn(false);
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public void addEmptyColumn(int posColumn) {
|
||||
mazeAppModel.addEmptyColumn(posColumn);
|
||||
}
|
||||
|
||||
public void addEmptyColumn() {
|
||||
mazeAppModel.addEmptyColumn();
|
||||
}
|
||||
|
||||
public void delColumn(int posColumn) {
|
||||
mazeAppModel.delColumn(posColumn);
|
||||
}
|
||||
|
||||
public void delColumn() {
|
||||
mazeAppModel.delColumn();
|
||||
}
|
||||
|
||||
public void addEmptyLine(int posLine) {
|
||||
mazeAppModel.addEmptyLine(posLine);
|
||||
}
|
||||
|
||||
public void addEmptyLine() {
|
||||
mazeAppModel.addEmptyLine();
|
||||
}
|
||||
|
||||
public void delLine(int posLine) {
|
||||
mazeAppModel.delLine(posLine);
|
||||
}
|
||||
|
||||
public void delLine() {
|
||||
mazeAppModel.delLine();
|
||||
}
|
||||
|
||||
public void clearMaze() {
|
||||
mazeAppModel.clearMaze();
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ public class MazeMenu extends JMenu {
|
||||
private final ClearMazeMenuItem clearMazeMenuItem;
|
||||
private final AddRowMenuItem addRowMenuItem;
|
||||
private final AddLineMenuItem addLineMenuItem;
|
||||
private final DelRowMenuItem delRowMenuItem;
|
||||
private final DelLineMenuItem delLineMenuItem;
|
||||
|
||||
public MazeMenu(MazeApp mazeApp) {
|
||||
|
||||
@@ -23,5 +25,7 @@ public class MazeMenu extends JMenu {
|
||||
add(clearMazeMenuItem = new ClearMazeMenuItem(mazeApp));
|
||||
add(addRowMenuItem = new AddRowMenuItem(mazeApp));
|
||||
add(addLineMenuItem = new AddLineMenuItem(mazeApp));
|
||||
add(delRowMenuItem = new DelRowMenuItem(mazeApp));
|
||||
add(delLineMenuItem = new DelLineMenuItem(mazeApp));
|
||||
}
|
||||
}
|
||||
@@ -15,23 +15,17 @@ import javax.swing.JSpinner;
|
||||
import javax.swing.SpinnerModel;
|
||||
import javax.swing.SpinnerNumberModel;
|
||||
import javax.swing.SpringLayout;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
public class NewMazeSpinnerBox extends JPanel
|
||||
implements ActionListener,
|
||||
ChangeListener {
|
||||
implements ActionListener {
|
||||
|
||||
private final MazeApp mazeApp;
|
||||
private JDialog spinnerDialog;
|
||||
private int numberOfLines;
|
||||
private int numberOfRows;
|
||||
private SpinnerModel lineSpinnerModel;
|
||||
private SpinnerModel rowSpinnerModel;
|
||||
|
||||
public NewMazeSpinnerBox(MazeApp mazeApp) {
|
||||
|
||||
|
||||
this.mazeApp = mazeApp;
|
||||
JPanel spinnerDialog = new JPanel(new SpringLayout());
|
||||
String[] labels = {"Number of lines :", "Number of rows :"};
|
||||
@@ -40,25 +34,23 @@ public class NewMazeSpinnerBox extends JPanel
|
||||
int maximumValue = 200;
|
||||
|
||||
this.lineSpinnerModel =
|
||||
new SpinnerNumberModel(initialValue, //initial value
|
||||
initialValue, //min
|
||||
maximumValue, //max
|
||||
1);
|
||||
this.lineSpinnerModel.addChangeListener(this);
|
||||
new SpinnerNumberModel(initialValue, //initial value
|
||||
initialValue, //min
|
||||
maximumValue, //max
|
||||
1);
|
||||
JSpinner lineSpinner = addLabeledSpinner(this, labels[0], lineSpinnerModel);
|
||||
|
||||
this.rowSpinnerModel =
|
||||
new SpinnerNumberModel(initialValue, //initial value
|
||||
initialValue, //min
|
||||
maximumValue, //max
|
||||
1);
|
||||
this.rowSpinnerModel.addChangeListener(this);
|
||||
JSpinner rowSpinner = addLabeledSpinner(this, labels[0], rowSpinnerModel);
|
||||
new SpinnerNumberModel(initialValue, //initial value
|
||||
initialValue, //min
|
||||
maximumValue, //max
|
||||
1);
|
||||
JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel);
|
||||
}
|
||||
|
||||
static protected JSpinner addLabeledSpinner(Container c,
|
||||
String label,
|
||||
SpinnerModel model) {
|
||||
String label,
|
||||
SpinnerModel model) {
|
||||
|
||||
JLabel l = new JLabel(label);
|
||||
c.add(l);
|
||||
@@ -76,8 +68,8 @@ public class NewMazeSpinnerBox extends JPanel
|
||||
spinnerDialog.setLayout(new BorderLayout());
|
||||
spinnerDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
|
||||
|
||||
spinnerDialog.add(new NewMazeSpinnerBox(this.mazeApp),
|
||||
BorderLayout.CENTER);
|
||||
spinnerDialog.add(this,
|
||||
BorderLayout.CENTER);
|
||||
|
||||
JButton OKButton = new JButton("OK");
|
||||
OKButton.addActionListener(this);
|
||||
@@ -89,13 +81,9 @@ public class NewMazeSpinnerBox extends JPanel
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
mazeApp.getMazeAppModel(
|
||||
).newClearMaze((int) lineSpinnerModel.getValue(),
|
||||
(int) rowSpinnerModel.getValue());
|
||||
spinnerDialog.dispose();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent arg0) {
|
||||
this.mazeApp.getMazeAppModel()
|
||||
.newClearMaze((int) this.lineSpinnerModel.getValue(),
|
||||
(int) this.rowSpinnerModel.getValue());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user