Skip to content

Commit 4b21150

Browse files
committed
Documented code & improved code style.
- JavaDoc added for all classes, methods and fields. - Checkstyle errors fixed - Minor code style improvements & checks added.
1 parent abfa2fc commit 4b21150

File tree

11 files changed

+469
-106
lines changed

11 files changed

+469
-106
lines changed

Sudoku-Solver/src/sudoku/gui/GameBoardPanel.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import sudoku.util.Observable;
1717
import sudoku.util.Observer;
1818

19+
/**
20+
* This class handles the graphical representation of a sudoku board.
21+
*/
1922
public class GameBoardPanel extends Container implements Observer {
2023

2124
private static final long serialVersionUID = 770590847429244978L;
@@ -26,9 +29,14 @@ public class GameBoardPanel extends Container implements Observer {
2629
private static final Border BOX_BORDER
2730
= BorderFactory.createBevelBorder(BevelBorder.LOWERED);
2831

32+
/**
33+
* All cells of the sudoku board.
34+
*/
2935
private final List<SudokuCell> cells = new ArrayList<>();
3036

3137
/**
38+
* Creates a new {@code GameBoardPanel} that gets updated if the given data
39+
* model changes.
3240
*
3341
* @param data The board data that should be visualized, not {@code null}.
3442
*/
@@ -76,6 +84,9 @@ public GameBoardPanel(DisplayData data) {
7684
}
7785
}
7886

87+
/**
88+
* Updates the values of all cells in this board.
89+
*/
7990
@Override
8091
public void update(Observable observable, Object argument) {
8192
/*
@@ -86,6 +97,14 @@ public void update(Observable observable, Object argument) {
8697
cells.forEach(SudokuCell::updateValue);
8798
}
8899

100+
/**
101+
* Enables or disables the popup menus of all cells in this board.
102+
* This should be used to prevent modification of the sudoku board during
103+
* ongoing operations on it.
104+
*
105+
* @param enabled Whether the popup menus should be set to enabled or
106+
* disabled.
107+
*/
89108
public void setPopupsEnabled(boolean enabled) {
90109
cells.forEach(c -> c.setPopupMenuEnabled(enabled));
91110
}

Sudoku-Solver/src/sudoku/gui/SudokuCell.java

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
import sudoku.gui.model.DisplayData;
1818

19+
/**
20+
* This class models a cell of a graphically visible sudoku board.
21+
*/
1922
public class SudokuCell extends JLabel {
20-
21-
private static final long serialVersionUID = 1L;
2223

24+
private static final long serialVersionUID = 7427321882456642556L;
2325

2426
/**
2527
* The foreground color each unmodifiable cell should have.
@@ -38,21 +40,46 @@ public class SudokuCell extends JLabel {
3840
*/
3941
private static final int FONT_SIZE = 14;
4042

43+
/**
44+
* The major index of the position this cell has in the board.
45+
*/
4146
private final int majorIndex;
47+
48+
/**
49+
* The minor index of the position this cell has in the board.
50+
*/
4251
private final int minorIndex;
52+
53+
/**
54+
* The data model that is visualized by the board this cell is a part of.
55+
*/
4356
private final DisplayData data;
57+
58+
/**
59+
* The popup menu the user can use to change the cells value.
60+
*/
4461
private final JPopupMenu popupMenu;
4562

63+
/**
64+
* Creates a new cell at the given position that shows the value of the
65+
* given data model at the same position. Unmodifiable cells are highlighted
66+
* and have no popup menu.
67+
*
68+
* @param major The major coordinate of this cell.
69+
* @param minor The minor coordinate of this cell.
70+
* @param data The data model where the values are stored.
71+
* @param isModifiable Whether this cell can be modified or not.
72+
*/
4673
public SudokuCell(
4774
int major, int minor, DisplayData data, boolean isModifiable) {
4875
super("", SwingConstants.CENTER);
76+
4977
majorIndex = major;
5078
minorIndex = minor;
5179
this.data = data;
5280

5381
if (isModifiable) {
5482
popupMenu = new CellPopupMenu();
55-
setComponentPopupMenu(popupMenu);
5683
} else {
5784
popupMenu = null;
5885
setForeground(FIXED_CELL_COLOR);
@@ -64,8 +91,11 @@ public SudokuCell(
6491
setBorder(CELL_BORDER);
6592
}
6693

94+
/**
95+
* Sets the preferred and minimum size of this cell depending on the font
96+
* size of the cell.
97+
*/
6798
private void setDimensions() {
68-
// maybe constant size but lower the font size?
6999
int maxDigits = (int) (Math.log10(data.getNumbers()) + 1);
70100
int contentSize = maxDigits * FONT_SIZE;
71101
int sizeWithPadding = contentSize + (FONT_SIZE * 2);
@@ -74,15 +104,24 @@ private void setDimensions() {
74104
setMinimumSize(new Dimension(contentSize, contentSize));
75105
}
76106

107+
/**
108+
* Enables or disables the popup menu that is used to change the cells
109+
* value.
110+
*
111+
* @param enabled Whether the popup menu should be enabled or disabled.
112+
*/
77113
void setPopupMenuEnabled(boolean enabled) {
78-
// popupMenu.setEnabled(enabled);
79114
if (enabled) {
80115
setComponentPopupMenu(popupMenu);
81116
} else {
82117
setComponentPopupMenu(null);
83118
}
84119
}
85120

121+
/**
122+
* Updates the value of this cell to the value currently stored in the data
123+
* model.
124+
*/
86125
void updateValue() {
87126
int value = data.getCell(majorIndex, minorIndex);
88127
String displayValue = (value == DisplayData.UNSET_CELL)
@@ -92,11 +131,18 @@ void updateValue() {
92131
}
93132

94133

95-
private class CellPopupMenu extends JPopupMenu {
96-
97-
private static final long serialVersionUID = 1L;
134+
/**
135+
* This class models the custom popup menu of a cell.
136+
*/
137+
private final class CellPopupMenu extends JPopupMenu {
138+
139+
private static final long serialVersionUID = 5643550068619328847L;
98140

99-
public CellPopupMenu() {
141+
/**
142+
* Creates a new popup menu with the numbers a cell can be set to and a
143+
* option to remove the stored value from a cell.
144+
*/
145+
private CellPopupMenu() {
100146
for (int i = 1; i <= data.getNumbers(); i++) {
101147
JMenuItem item = add(Integer.toString(i));
102148
item.addActionListener(new ChangeCellActionListener(i));
@@ -107,14 +153,33 @@ public CellPopupMenu() {
107153
new ChangeCellActionListener(DisplayData.UNSET_CELL));
108154
}
109155

110-
private class ChangeCellActionListener implements ActionListener {
156+
/**
157+
* The listener for change cell actions of the user. This listener can
158+
* change the value of a cell in the data model to its assigned value.
159+
*/
160+
private final class ChangeCellActionListener implements ActionListener {
111161

162+
/**
163+
* The value this listener was created for.
164+
*/
112165
private final int assignedValue;
113166

114-
public ChangeCellActionListener(int assignedValue) {
167+
/**
168+
* Creates a new {@code ChangeCellActionListener} which should
169+
* listen for the selection of the assigned value. When this action
170+
* is performed, the cells value in the data model gets updated to
171+
* the assigned value.
172+
*
173+
* @param assignedValue The value this listener is responsible for.
174+
*/
175+
private ChangeCellActionListener(int assignedValue) {
115176
this.assignedValue = assignedValue;
116177
}
117178

179+
/**
180+
* Sets the value of the cell which this listener was created for to
181+
* the value that was assigned to this listener.
182+
*/
118183
@Override
119184
public void actionPerformed(ActionEvent e) {
120185
data.setCell(majorIndex, minorIndex, assignedValue);

Sudoku-Solver/src/sudoku/gui/SudokuDialogMessages.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@
66

77
import sudoku.gui.model.DisplayData;
88

9+
/**
10+
* This class contains static methods used to show the same messages from
11+
* different classes.
12+
*/
913
public final class SudokuDialogMessages {
1014

1115
/**
12-
* Private constructor to prevent instantiation.
16+
* Private constructor to prevent instantiation.
1317
*/
1418
private SudokuDialogMessages() {
15-
throw new AssertionError();
19+
throw new AssertionError("This class should not be instantiated.");
1620
}
1721

22+
/**
23+
* If the given data model is completely filled, a message is shown which
24+
* informs the user about whether the sudoku is a valid solution or not.
25+
*
26+
* @param parent Determines the Frame in which the dialog is displayed.
27+
* @param data The data model that should be checked if it is full and
28+
* solved.
29+
*/
1830
public static void showMessageIfFilled(Component parent, DisplayData data) {
1931
if (data.isFilled()) {
2032
String message;
@@ -26,23 +38,49 @@ public static void showMessageIfFilled(Component parent, DisplayData data) {
2638
JOptionPane.showMessageDialog(parent, message);
2739
}
2840
}
29-
30-
public static void showError(Component parent, String title, String message) {
31-
JOptionPane.showMessageDialog(
32-
parent, message, title, JOptionPane.ERROR_MESSAGE);
33-
}
3441

42+
/**
43+
* Shows an error message that informs the user about an invalid sudoku.
44+
*
45+
* @param parent Determines the Frame in which the dialog is displayed.
46+
*/
3547
public static void showErrorInvalid(Component parent) {
3648
showError(parent, "Invalid sudoku", "The current sudoku is invalid.");
3749
}
3850

51+
/**
52+
* Shows an error message that informs the user about a sudoku that cannot
53+
* be solved by the program.
54+
*
55+
* @param parent Determines the Frame in which the dialog is displayed.
56+
*/
3957
public static void showErrorUnsolvable(Component parent) {
4058
showError(parent, "Unsolvable sudoku",
41-
"This operation cannot be performed on an unsolvable sudoku.");
59+
"The current sudoku is not solvable.");
4260
}
4361

62+
/**
63+
* Shows an error message that informs the user that the sudoku is already
64+
* filled and a suggestion cannot be made on it.
65+
*
66+
* @param parent Determines the Frame in which the dialog is displayed.
67+
*/
4468
public static void showErrorAlreadyFilled(Component parent) {
4569
showError(parent, "No empty cells",
4670
"Cannot suggest a value if all cells are set.");
4771
}
72+
73+
/**
74+
* Shows a error message with an option pane dialog. The given parameters
75+
* are used for displaying the message dialog.
76+
*
77+
* @param parent Determines the Frame in which the dialog is displayed.
78+
* @param title The title of the error message.
79+
* @param message The description of the error message.
80+
*/
81+
private static void showError(
82+
Component parent, String title, String message) {
83+
JOptionPane.showMessageDialog(
84+
parent, message, title, JOptionPane.ERROR_MESSAGE);
85+
}
4886
}

0 commit comments

Comments
 (0)