Skip to content

Commit 47213f7

Browse files
authored
Merge pull request #5 from timlg07/gui
GUI created.
2 parents d0f0b01 + 4064667 commit 47213f7

24 files changed

+1115
-69
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package sudoku.gui;
2+
3+
import javax.swing.SwingUtilities;
4+
5+
import sudoku.gui.model.DisplayData;
6+
7+
public class Main {
8+
9+
public static void main(String[] args) {
10+
DisplayData model = new DisplayData();
11+
SwingUtilities.invokeLater(() -> new SudokuFrame(model));
12+
}
13+
14+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package sudoku.gui;
2+
3+
import java.awt.Color;
4+
import java.awt.Dimension;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.ActionListener;
7+
8+
import javax.swing.BorderFactory;
9+
import javax.swing.JLabel;
10+
import javax.swing.JMenuItem;
11+
import javax.swing.JPopupMenu;
12+
import javax.swing.SwingConstants;
13+
import javax.swing.border.BevelBorder;
14+
import javax.swing.border.Border;
15+
16+
import sudoku.gui.model.DisplayData;
17+
import sudoku.gui.model.DisplayDataChange;
18+
import sudoku.util.Observable;
19+
import sudoku.util.Observer;
20+
21+
public class SudokuCell extends JLabel implements Observer {
22+
23+
private static final long serialVersionUID = 1L;
24+
25+
/**
26+
* The foreground color of not modifiable cells.
27+
*/
28+
private static final Color NOT_MODIFIABLE_FG = Color.RED;
29+
30+
/**
31+
* The lowered bordered of every cell.
32+
*/
33+
private static final Border CELL_BORDER
34+
= BorderFactory.createBevelBorder(BevelBorder.LOWERED);
35+
36+
private static final int FONT_SIZE = 14;
37+
private static final Dimension PREF_SIZE
38+
= new Dimension(FONT_SIZE * 3, FONT_SIZE * 3);
39+
private static final Dimension MIN_SIZE
40+
= new Dimension(FONT_SIZE, FONT_SIZE);
41+
42+
private final int majorCoord;
43+
private final int minorCoord;
44+
private final DisplayData data;
45+
private final JPopupMenu popupMenu;
46+
private int value;
47+
48+
public SudokuCell(int major, int minor, DisplayData data) {
49+
super("", SwingConstants.CENTER);
50+
51+
majorCoord = major;
52+
minorCoord = minor;
53+
this.data = data;
54+
55+
if (data.isCellModifiable(major, minor)) {
56+
popupMenu = new CellPopupMenu(data.getNumbers());
57+
setComponentPopupMenu(popupMenu);
58+
} else {
59+
popupMenu = null;
60+
setForeground(NOT_MODIFIABLE_FG);
61+
}
62+
setBorder(CELL_BORDER);
63+
setPreferredSize(PREF_SIZE);
64+
setMinimumSize(MIN_SIZE);
65+
setFont(getFont().deriveFont((float) FONT_SIZE));
66+
67+
updateValue();
68+
data.attachObserver(this);
69+
}
70+
71+
@Override
72+
public void update(Observable observable, Object argument) {
73+
assert observable instanceof DisplayData;
74+
assert data == ((DisplayData) observable);
75+
assert argument instanceof DisplayDataChange;
76+
77+
switch ((DisplayDataChange) argument) {
78+
case NEW_SUDOKU:
79+
/*
80+
* This cell should no longer be updated as the sudoku it was part
81+
* of got replaced by a new one.
82+
*/
83+
data.detachObserver(this);
84+
break;
85+
86+
case SUDOKU_LOCK:
87+
updatePopupMenuEnabled();
88+
break;
89+
90+
default:
91+
updateValue();
92+
}
93+
}
94+
95+
private void updatePopupMenuEnabled() {
96+
if (data.isOperationOnSudokuAllowed()) {
97+
setComponentPopupMenu(popupMenu);
98+
} else {
99+
setComponentPopupMenu(null);
100+
}
101+
}
102+
103+
private void updateValue() {
104+
int newValue = data.getCell(majorCoord, minorCoord);
105+
if (value != newValue) {
106+
value = newValue;
107+
setText(toString());
108+
}
109+
}
110+
111+
@Override
112+
public String toString() {
113+
return (value == DisplayData.UNSET_CELL) ? "" : Integer.toString(value);
114+
}
115+
116+
117+
private class CellPopupMenu extends JPopupMenu {
118+
119+
private static final long serialVersionUID = 1L;
120+
121+
public CellPopupMenu(int numbers) {
122+
super();
123+
124+
for (int i = 1; i <= numbers; i++) {
125+
JMenuItem item = add(Integer.toString(i));
126+
item.addActionListener(new ChangeCellActionListener(i));
127+
}
128+
129+
JMenuItem removeOption = add("remove");
130+
removeOption.addActionListener(
131+
new ChangeCellActionListener(DisplayData.UNSET_CELL));
132+
}
133+
134+
private class ChangeCellActionListener implements ActionListener {
135+
136+
private final int assignedValue;
137+
138+
public ChangeCellActionListener(int assignedValue) {
139+
this.assignedValue = assignedValue;
140+
}
141+
142+
@Override
143+
public void actionPerformed(ActionEvent e) {
144+
145+
// Use attributes of SudokuCell.this:
146+
data.setCell(majorCoord, minorCoord, assignedValue);
147+
}
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)