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