src/main/java/de/uapcore/sudoku/SudokuTextField.java

changeset 10
369903afbb29
parent 9
576e7a2861ae
child 12
1c62c6009161
equal deleted inserted replaced
9:576e7a2861ae 10:369903afbb29
32 import java.awt.event.FocusEvent; 32 import java.awt.event.FocusEvent;
33 import java.awt.event.KeyAdapter; 33 import java.awt.event.KeyAdapter;
34 import java.awt.event.KeyEvent; 34 import java.awt.event.KeyEvent;
35 35
36 /** 36 /**
37 * 37 * A custom text field specifically for Sudoku number fields.
38 * @author mike
39 */ 38 */
40 public final class SudokuTextField extends JTextField { 39 public final class SudokuTextField extends JTextField {
41 40
42 private boolean modified; 41 private boolean modified;
43 42
103 public void focusLost(FocusEvent e) { 102 public void focusLost(FocusEvent e) {
104 select(0, 0); 103 select(0, 0);
105 } 104 }
106 }); 105 });
107 } 106 }
108 107
108 /**
109 * Returns the current value in the field or zero if the field is empty.
110 *
111 * @return the number from 1 to 9 or zero if empty
112 */
109 public int getValue() { 113 public int getValue() {
110 if (getText().length() > 0) { 114 if (getText().isEmpty()) {
115 return 0;
116 } else {
111 return Integer.valueOf(getText()); 117 return Integer.valueOf(getText());
112 } else {
113 return 0;
114 } 118 }
115 } 119 }
116 120
121 /**
122 * Sets the field's value.
123 *
124 * @param v the number from 1 to 9 or zero to clear the field
125 */
117 public void setValue(int v) { 126 public void setValue(int v) {
118 if (v == 0) { 127 if (v == 0) {
119 setText(""); 128 setText("");
120 } else if (v < 10) { 129 } else if (v < 10) {
121 setText(String.valueOf(v)); 130 setText(String.valueOf(v));
122 } else { 131 } else {
123 throw new IllegalArgumentException( 132 throw new IllegalArgumentException(
124 "Sudoku numbers must be in range 0-9 (0 means 'not set')"); 133 "Sudoku numbers must be in range 0-9 (0 means 'not set')");
125 } 134 }
126 } 135 }
127 136
137 /**
138 * Sets the modified state of this field.
139 *
140 * Modified fields are displayed in blue color.
141 *
142 * @param modified a flag indicating whether this field is modified
143 */
128 public void setModified(boolean modified) { 144 public void setModified(boolean modified) {
129 this.modified = modified; 145 this.modified = modified;
130 setForeground(modified?Color.BLUE:Color.BLACK); 146 setForeground(modified?Color.BLUE:Color.BLACK);
131 } 147 }
132 148
149 /**
150 * Checks whether this field is in modified state.
151 *
152 * @return true if this field is modified
153 */
133 public boolean isModified() { 154 public boolean isModified() {
134 return modified; 155 return modified;
135 } 156 }
136 } 157 }

mercurial