src/de/uapcore/sudoku/SudokuTextField.java

changeset 2
5179eff8a9b6
parent 1
f1d7de36b01e
child 3
ed931970b4ac
equal deleted inserted replaced
1:f1d7de36b01e 2:5179eff8a9b6
11 11
12 /** 12 /**
13 * 13 *
14 * @author mike 14 * @author mike
15 */ 15 */
16 public class SudokuTextField extends JTextField { 16 public final class SudokuTextField extends JTextField {
17
18 private boolean modified;
17 19
18 public SudokuTextField() { 20 public SudokuTextField() {
19 setBorder(null); 21 setBorder(null);
20 setBackground(Color.WHITE); 22 setBackground(Color.WHITE);
21 23
27 setMinimumSize(dim); 29 setMinimumSize(dim);
28 setMaximumSize(dim); 30 setMaximumSize(dim);
29 31
30 addKeyListener(new KeyAdapter() { 32 addKeyListener(new KeyAdapter() {
31 private void handle(KeyEvent e) { 33 private void handle(KeyEvent e) {
32 if (getText().length() > 0) { 34 if (getText().length() > 0 && getSelectedText() == null) {
33 int c = e.getKeyCode(); 35 int c = e.getKeyCode();
34 if (c != KeyEvent.VK_DELETE && 36 if (c != KeyEvent.VK_DELETE &&
35 c != KeyEvent.VK_BACK_SPACE && 37 c != KeyEvent.VK_BACK_SPACE &&
36 !e.isActionKey()) { 38 !e.isActionKey()) {
37 e.consume(); 39 e.consume();
38 } 40 }
39 } else { 41 } else {
40 char c = e.getKeyChar(); 42 char c = e.getKeyChar();
41 if (c < '0' || c > '9') { 43 if (c < '0' || c > '9') {
42 e.consume(); 44 e.consume();
45 } else {
46 setModified(true);
43 } 47 }
44 } 48 }
45 } 49 }
46 50
47 @Override 51 @Override
66 selectAll(); 70 selectAll();
67 } 71 }
68 }); 72 });
69 } 73 }
70 74
75 public int getValue() {
76 if (getText().length() > 0) {
77 return Integer.valueOf(getText());
78 } else {
79 return 0;
80 }
81 }
82
83 public void setValue(int v) {
84 if (v == 0) {
85 setText("");
86 } else if (v < 10) {
87 setText(String.valueOf(v));
88 } else {
89 throw new IllegalArgumentException(
90 "Sudoku numbers must be in range 0-9 (0 means 'not set')");
91 }
92 }
93
94 public void setModified(boolean modified) {
95 this.modified = modified;
96 setForeground(modified?Color.BLUE:Color.BLACK);
97 }
98
99 public boolean isModified() {
100 return modified;
101 }
71 } 102 }

mercurial