src/de/uapcore/sudoku/SudokuTextField.java

Fri, 01 Feb 2013 10:18:47 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 01 Feb 2013 10:18:47 +0100
changeset 8
e70a0e3555fb
parent 6
5bab2e971333
permissions
-rw-r--r--

removed completeness test - the solver automatically returns false when the field is incomplete

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
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 import java.awt.Color;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 import java.awt.Dimension;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 import java.awt.Font;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 import java.awt.event.FocusAdapter;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 import java.awt.event.FocusEvent;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 import java.awt.event.KeyAdapter;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 import java.awt.event.KeyEvent;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 import javax.swing.JTextField;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 /**
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 *
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 * @author mike
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
42 public final class SudokuTextField extends JTextField {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
43
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
44 private boolean modified;
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 public SudokuTextField() {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 setBorder(null);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 setBackground(Color.WHITE);
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 setFont(new Font("Dialog", Font.PLAIN, 18));
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 setHorizontalAlignment(JTextField.CENTER);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 Dimension dim = new Dimension(40,40);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 setPreferredSize(dim);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 setMinimumSize(dim);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 setMaximumSize(dim);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 addKeyListener(new KeyAdapter() {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 private void handle(KeyEvent e) {
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
60 char c = e.getKeyChar();
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
61 if (!e.isAltDown() && !e.isControlDown() &&
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
62 !Character.isISOControl(c)) {
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
63 // Perform clean input check
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
64 if (getText().length() > 0 && getSelectedText() == null) {
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
65 if (c != KeyEvent.CHAR_UNDEFINED) {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
66 e.consume();
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
67 }
3
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 if (c < '1' || c > '9') {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
70 e.consume();
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
71 } else {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
72 setModified(true);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
73 }
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 } else {
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
76 // 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
77 if (!getText().matches("^[1-9]$")) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
78 setText("");
1
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 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 public void keyPressed(KeyEvent e) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 handle(e);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 public void keyTyped(KeyEvent e) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 handle(e);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 public void keyReleased(KeyEvent e) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 handle(e);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 });
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 addFocusListener(new FocusAdapter() {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 public void focusGained(FocusEvent e) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 selectAll();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 }
4
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
104 @Override
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
105 public void focusLost(FocusEvent e) {
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
106 select(0, 0);
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
107 }
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 });
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
111 public int getValue() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
112 if (getText().length() > 0) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
113 return Integer.valueOf(getText());
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
114 } else {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
115 return 0;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
116 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
117 }
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 public void setValue(int v) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
120 if (v == 0) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
121 setText("");
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
122 } else if (v < 10) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
123 setText(String.valueOf(v));
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
124 } else {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
125 throw new IllegalArgumentException(
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
126 "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
127 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
128 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
129
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
130 public void setModified(boolean modified) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
131 this.modified = modified;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
132 setForeground(modified?Color.BLUE:Color.BLACK);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
133 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
134
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
135 public boolean isModified() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
136 return modified;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
137 }
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 }

mercurial