16
16
17
17
import sudoku .gui .model .DisplayData ;
18
18
19
+ /**
20
+ * This class models a cell of a graphically visible sudoku board.
21
+ */
19
22
public class SudokuCell extends JLabel {
20
-
21
- private static final long serialVersionUID = 1L ;
22
23
24
+ private static final long serialVersionUID = 7427321882456642556L ;
23
25
24
26
/**
25
27
* The foreground color each unmodifiable cell should have.
@@ -38,21 +40,46 @@ public class SudokuCell extends JLabel {
38
40
*/
39
41
private static final int FONT_SIZE = 14 ;
40
42
43
+ /**
44
+ * The major index of the position this cell has in the board.
45
+ */
41
46
private final int majorIndex ;
47
+
48
+ /**
49
+ * The minor index of the position this cell has in the board.
50
+ */
42
51
private final int minorIndex ;
52
+
53
+ /**
54
+ * The data model that is visualized by the board this cell is a part of.
55
+ */
43
56
private final DisplayData data ;
57
+
58
+ /**
59
+ * The popup menu the user can use to change the cells value.
60
+ */
44
61
private final JPopupMenu popupMenu ;
45
62
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
+ */
46
73
public SudokuCell (
47
74
int major , int minor , DisplayData data , boolean isModifiable ) {
48
75
super ("" , SwingConstants .CENTER );
76
+
49
77
majorIndex = major ;
50
78
minorIndex = minor ;
51
79
this .data = data ;
52
80
53
81
if (isModifiable ) {
54
82
popupMenu = new CellPopupMenu ();
55
- setComponentPopupMenu (popupMenu );
56
83
} else {
57
84
popupMenu = null ;
58
85
setForeground (FIXED_CELL_COLOR );
@@ -64,8 +91,11 @@ public SudokuCell(
64
91
setBorder (CELL_BORDER );
65
92
}
66
93
94
+ /**
95
+ * Sets the preferred and minimum size of this cell depending on the font
96
+ * size of the cell.
97
+ */
67
98
private void setDimensions () {
68
- // maybe constant size but lower the font size?
69
99
int maxDigits = (int ) (Math .log10 (data .getNumbers ()) + 1 );
70
100
int contentSize = maxDigits * FONT_SIZE ;
71
101
int sizeWithPadding = contentSize + (FONT_SIZE * 2 );
@@ -74,15 +104,24 @@ private void setDimensions() {
74
104
setMinimumSize (new Dimension (contentSize , contentSize ));
75
105
}
76
106
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
+ */
77
113
void setPopupMenuEnabled (boolean enabled ) {
78
- // popupMenu.setEnabled(enabled);
79
114
if (enabled ) {
80
115
setComponentPopupMenu (popupMenu );
81
116
} else {
82
117
setComponentPopupMenu (null );
83
118
}
84
119
}
85
120
121
+ /**
122
+ * Updates the value of this cell to the value currently stored in the data
123
+ * model.
124
+ */
86
125
void updateValue () {
87
126
int value = data .getCell (majorIndex , minorIndex );
88
127
String displayValue = (value == DisplayData .UNSET_CELL )
@@ -92,11 +131,18 @@ void updateValue() {
92
131
}
93
132
94
133
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 ;
98
140
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 () {
100
146
for (int i = 1 ; i <= data .getNumbers (); i ++) {
101
147
JMenuItem item = add (Integer .toString (i ));
102
148
item .addActionListener (new ChangeCellActionListener (i ));
@@ -107,14 +153,33 @@ public CellPopupMenu() {
107
153
new ChangeCellActionListener (DisplayData .UNSET_CELL ));
108
154
}
109
155
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 {
111
161
162
+ /**
163
+ * The value this listener was created for.
164
+ */
112
165
private final int assignedValue ;
113
166
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 ) {
115
176
this .assignedValue = assignedValue ;
116
177
}
117
178
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
+ */
118
183
@ Override
119
184
public void actionPerformed (ActionEvent e ) {
120
185
data .setCell (majorIndex , minorIndex , assignedValue );
0 commit comments