Mon, 27 Jul 2020 11:57:42 +0200
renames help menu
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 | 29 | import javax.swing.*; |
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 | 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 | 39 | public final class SudokuTextField extends JTextField { |
40 | ||
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 | 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 | 62 | if (c != KeyEvent.CHAR_UNDEFINED) { |
63 | e.consume(); | |
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 | 101 | @Override |
102 | public void focusLost(FocusEvent e) { | |
103 | select(0, 0); | |
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 | 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 | */ | |
2 | 113 | public int getValue() { |
10 | 114 | if (getText().isEmpty()) { |
115 | return 0; | |
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 | 118 | } |
119 | } | |
10 | 120 | |
121 | /** | |
122 | * Sets the field's value. | |
123 | * | |
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 | 126 | */ |
2 | 127 | public void setValue(int v) { |
128 | if (v == 0) { | |
129 | setText(""); | |
13 | 130 | } else if (v > 0 && v < 10) { |
2 | 131 | setText(String.valueOf(v)); |
132 | } else { | |
133 | throw new IllegalArgumentException( | |
134 | "Sudoku numbers must be in range 0-9 (0 means 'not set')"); | |
135 | } | |
136 | } | |
10 | 137 | |
138 | /** | |
139 | * Sets the modified state of this field. | |
140 | * | |
141 | * Modified fields are displayed in blue color. | |
142 | * | |
143 | * @param modified a flag indicating whether this field is modified | |
144 | */ | |
2 | 145 | public void setModified(boolean modified) { |
146 | this.modified = modified; | |
147 | setForeground(modified?Color.BLUE:Color.BLACK); | |
148 | } | |
10 | 149 | |
150 | /** | |
151 | * Checks whether this field is in modified state. | |
152 | * | |
153 | * @return true if this field is modified | |
154 | */ | |
2 | 155 | public boolean isModified() { |
156 | return modified; | |
157 | } | |
1
f1d7de36b01e
init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
158 | } |