-
Notifications
You must be signed in to change notification settings - Fork 0
/
CellPanel.java
307 lines (265 loc) · 8.23 KB
/
CellPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* YAJI Sudoku (Yet Another Java Implementation of Sudoku puzzle)
* Copyright (C) 2014 Andrey Ilatovskiy
*
* This file is part of YAJI Sudoku.
*
* YAJI Sudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* YAJI Sudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with YAJI Sudoku. If not, see <http://www.gnu.org/licenses/>.
*/
// individual sudoku cell GUI element
package org.ailatovskiy.yajisudoku;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
@SuppressWarnings("serial")
public class CellPanel extends JPanel
{
// refs
private final SudokuPanel sudokuPanel;
private final Sudoku sudoku;
private final int square;
private final int cell;
private final Colors colors;
private final GridBagConstraints constraints;
private final JLabel numberLabel;
private final PossibilitiesPanel possibilitiesPanel;
private final Border borderEmpty;
private final Border borderSelected;
private final Border borderUnselected;
private final Border borderConflict;
private final MouseHandler mouseHandler;
private final ComponentHandler componentHandler;
// font scale factors
private final static float HSCALE = 0.7f;
private final static float WSCALE = 0.5f;
// default: both elements are not shown
private boolean numberIsShown = false;
private boolean possibilitiesAreShown = false;
// constructor
CellPanel(SudokuPanel sudokuPanel_, Sudoku sudoku_,
int square_, int cell_, Colors colors_)
{
// refs
sudokuPanel = sudokuPanel_;
sudoku = sudoku_;
square = square_;
cell = cell_;
colors = colors_;
// number label (shown if number is set)
// default is 0, that is never shown
numberLabel = new JLabel("0", SwingConstants.CENTER);
// possibilities panel (shown if number is not set)
possibilitiesPanel = new PossibilitiesPanel(sudokuPanel,
sudoku, square, cell, colors, this);
setLayout(new GridBagLayout() );
// one xy-resizable element in the center
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.CENTER;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
// all possible borders
borderEmpty = BorderFactory.createEmptyBorder();
borderConflict = BorderFactory.createLineBorder(
colors.getColor(Colors.CONFLICT) );
borderSelected = BorderFactory.createLineBorder(
colors.getColor(Colors.BGSELECTED) );
borderUnselected = BorderFactory.createLineBorder(
colors.getColor(Colors.BGPOSSIBLE) );
// event handlers
componentHandler = new ComponentHandler();
addComponentListener(componentHandler);
mouseHandler = new MouseHandler();
addMouseListener(mouseHandler);
} // end constructor
// return true if cell color index is the same
//+ as of selected color on the color panel
private boolean isColor()
{
return (sudoku.getColorIndex(square, cell) == colors.getColorIndex() );
} // end method isColor
// show number label or possibilities panel in the cell
public void updatePanel()
{
if (sudoku.isSet(square, cell) )
{ // number is set
// hide possibilities if shown
if (possibilitiesAreShown)
{
remove(possibilitiesPanel);
possibilitiesAreShown = false;
} // end if possibilitiesAreShown
// show number if not shown
if (!numberIsShown)
{
add(numberLabel, constraints);
numberIsShown = true;
resizeNumberLabel();
} // end if !numberIsShown
// set number
numberLabel.setText(String.format("%1d", sudoku.getNumber(square, cell) ) );
// set color
if (sudoku.isFixed(square, cell) )
{ // number is fixed
numberLabel.setForeground(colors.getColor(Colors.FGFIXED) );
setBackground(colors.getColor(Colors.BGFIXED) );
}
else
{ // number is set but not fixed
numberLabel.setForeground(colors.getColor(Colors.FGSET) );
if (sudoku.isConflict(square, cell) )
{ // highlight conflicts
setBackground(colors.getColor(Colors.CONFLICT) );
}
else
{ // use stored color
setBackground(colors.getColor(sudoku.getColorIndex(square, cell) ) );
} // end if isConflict
} // end if isFixed
// no border
setBorder(borderEmpty);
}
else
{ // number is not set
// hide number if shown
if (numberIsShown)
{
remove(numberLabel);
numberIsShown = false;
} // end if numberIsShown
// display possibilities if not shown
if (!possibilitiesAreShown)
{
add(possibilitiesPanel, constraints);
possibilitiesAreShown = true;
} // end if !possibilitiesAreShown
possibilitiesPanel.updatePanel();
// set border
if (sudoku.isConflict(square, cell) )
{ // highlight conflict on the cell panel level
setBorder(borderConflict);
}
else
{
setBorder(borderUnselected);
} // end if isConflict
} // end if isSet
mouseHandler.activate(!sudoku.isSolved() ); // activate if not solved
componentHandler.activate(); // active if cell panel elements are shown
} // end method updatePanel
// adjust font size of number label
public void resizeNumberLabel()
{
Font font = numberLabel.getFont();
// use current size of the cell panel
float size = Math.min(getHeight() * HSCALE, getWidth() * WSCALE);
numberLabel.setFont(font.deriveFont(size) );
} // end method resizeNumberLabel
public void select()
{
setBorder(borderSelected);
} // end method select
public void deselect()
{
if (sudoku.isSet(square, cell) )
{ // number is set
setBorder(borderEmpty); // no border for number
}
else if (!sudoku.isConflict(square, cell) )
{ // default
setBorder(borderUnselected); // default for possibilities
}
else
{ // conflict
setBorder(borderConflict); // if isConflict
} // end if isSet
} // end method deselect
// unset number
public void unset()
{
sudoku.unset(square, cell);
sudoku.updateSudoku();
sudokuPanel.updatePanel();
} // end method unset
private class MouseHandler extends MouseAdapter
{
// do nothing until activated
private boolean active = false;
private boolean selected = false;
public void activate(boolean activate)
{
active = activate;
} // end method activate
// select cell on mouse hovering
@Override
public void mouseEntered(MouseEvent e)
{
if (active && sudoku.isSet(square, cell)
&& !sudoku.isFixed(square, cell) && isColor() )
{
select();
selected = true;
} // end if active && isSet && !isFixed && isColor
} // end method mouseEntered
// unset number on mouse click (any button)
@Override
public void mouseClicked(MouseEvent e)
{
if (active && selected)
{
unset();
} // end if active && selected
} // end method mouseClicked
// deselect cell when mouse exits
@Override
public void mouseExited(MouseEvent e)
{
if (active && selected)
{
deselect();
selected = false;
} // end if active && selected
} // end method mouseEntered
} // end inner class MouseHandler
// adjust font size of the number label if the panel is resized
private class ComponentHandler extends ComponentAdapter
{
// do nothing until activated
private boolean active = false;
public void activate()
{
active = true;
} // end method activate
@Override
public void componentResized(ComponentEvent e)
{
if (active && sudoku.isSet(square, cell) )
{
resizeNumberLabel();
} // end if active && isSet
} // end method componentResized
} // end inner class ComponentHandler
} // end class CellPanel