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

Mon, 27 Jul 2020 12:08:23 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Jul 2020 12:08:23 +0200
changeset 15
fea6eaa0d520
parent 13
5e69b1bb707f
permissions
-rw-r--r--

fix: window cannot get smaller than the field

3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
1 /*
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
2 * Copyright 2013 Mike Becker. All rights reserved.
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
3 *
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
4 * Redistribution and use in source and binary forms, with or without
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
5 * modification, are permitted provided that the following conditions are met:
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
6 *
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
7 * 1. Redistributions of source code must retain the above copyright
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
8 * notice, this list of conditions and the following disclaimer.
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
9 *
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
12 * documentation and/or other materials provided with the distribution.
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
13 *
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
24 * POSSIBILITY OF SUCH DAMAGE.
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
25 */
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
26
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 package de.uapcore.sudoku;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
9
576e7a2861ae converts to maven project
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
29 import javax.swing.*;
576e7a2861ae converts to maven project
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
30 import java.awt.*;
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 import java.awt.event.FocusAdapter;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 import java.awt.event.FocusEvent;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 import java.awt.event.KeyAdapter;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 import java.awt.event.KeyEvent;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 /**
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
37 * A custom text field specifically for Sudoku number fields.
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
39 public final class SudokuTextField extends JTextField {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
40
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
41 private boolean modified;
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 public SudokuTextField() {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 setBorder(null);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 setBackground(Color.WHITE);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 setFont(new Font("Dialog", Font.PLAIN, 18));
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 setHorizontalAlignment(JTextField.CENTER);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 Dimension dim = new Dimension(40,40);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 setPreferredSize(dim);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 setMinimumSize(dim);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 setMaximumSize(dim);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 addKeyListener(new KeyAdapter() {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 private void handle(KeyEvent e) {
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
57 char c = e.getKeyChar();
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
58 if (!e.isAltDown() && !e.isControlDown() &&
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
59 !Character.isISOControl(c)) {
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
60 // Perform clean input check
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
61 if (getText().length() > 0 && getSelectedText() == null) {
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
62 if (c != KeyEvent.CHAR_UNDEFINED) {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
63 e.consume();
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
64 }
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
65 } else {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
66 if (c < '1' || c > '9') {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
67 e.consume();
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
68 } else {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
69 setModified(true);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
70 }
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 } else {
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
73 // We can still be tricked by hotkeys, so do it the hard way
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
74 if (!getText().matches("^[1-9]$")) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
75 setText("");
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 public void keyPressed(KeyEvent e) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 handle(e);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 public void keyTyped(KeyEvent e) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 handle(e);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 public void keyReleased(KeyEvent e) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 handle(e);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 });
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 addFocusListener(new FocusAdapter() {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 public void focusGained(FocusEvent e) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 selectAll();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 }
4
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
101 @Override
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
102 public void focusLost(FocusEvent e) {
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
103 select(0, 0);
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
104 }
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 });
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
107
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
108 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
109 * Returns the current value in the field or zero if the field is empty.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
110 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
111 * @return the number from 1 to 9 or zero if empty
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
112 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
113 public int getValue() {
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
114 if (getText().isEmpty()) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
115 return 0;
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
116 } else {
12
1c62c6009161 fixes some code inspection issues
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
117 return Integer.parseInt(getText());
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
118 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
119 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
120
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
121 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
122 * Sets the field's value.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
123 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
124 * @param v the number from 1 to 9 or zero to clear the field
12
1c62c6009161 fixes some code inspection issues
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
125 * @throws IllegalArgumentException if v is not between 0 and 9
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
126 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
127 public void setValue(int v) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
128 if (v == 0) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
129 setText("");
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
130 } else if (v > 0 && v < 10) {
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
131 setText(String.valueOf(v));
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
132 } else {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
133 throw new IllegalArgumentException(
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
134 "Sudoku numbers must be in range 0-9 (0 means 'not set')");
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
135 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
136 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
137
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
138 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
139 * Sets the modified state of this field.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
140 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
141 * Modified fields are displayed in blue color.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
142 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
143 * @param modified a flag indicating whether this field is modified
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
144 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
145 public void setModified(boolean modified) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
146 this.modified = modified;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
147 setForeground(modified?Color.BLUE:Color.BLACK);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
148 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
149
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
150 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
151 * Checks whether this field is in modified state.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
152 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
153 * @return true if this field is modified
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
154 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
155 public boolean isModified() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
156 return modified;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
157 }
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 }

mercurial