src/de/uapcore/sudoku/SudokuTextField.java

changeset 3
ed931970b4ac
parent 2
5179eff8a9b6
child 4
b8588e318001
equal deleted inserted replaced
2:5179eff8a9b6 3:ed931970b4ac
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
1 package de.uapcore.sudoku; 27 package de.uapcore.sudoku;
2 28
3 import java.awt.Color; 29 import java.awt.Color;
4 import java.awt.Dimension; 30 import java.awt.Dimension;
5 import java.awt.Font; 31 import java.awt.Font;
29 setMinimumSize(dim); 55 setMinimumSize(dim);
30 setMaximumSize(dim); 56 setMaximumSize(dim);
31 57
32 addKeyListener(new KeyAdapter() { 58 addKeyListener(new KeyAdapter() {
33 private void handle(KeyEvent e) { 59 private void handle(KeyEvent e) {
34 if (getText().length() > 0 && getSelectedText() == null) { 60 char c = e.getKeyChar();
35 int c = e.getKeyCode(); 61 if (!e.isAltDown() && !e.isControlDown() &&
36 if (c != KeyEvent.VK_DELETE && 62 Character.isLetterOrDigit(c)) {
37 c != KeyEvent.VK_BACK_SPACE && 63 // Perform clean input check
38 !e.isActionKey()) { 64 if (getText().length() > 0 && getSelectedText() == null) {
39 e.consume(); 65 e.consume();
66 } else {
67 if (c < '1' || c > '9') {
68 e.consume();
69 } else {
70 setModified(true);
71 }
40 } 72 }
41 } else { 73 } else {
42 char c = e.getKeyChar(); 74 // We can still be tricked by hotkeys, so do it the hard way
43 if (c < '0' || c > '9') { 75 if (!getText().matches("^[1-9]$")) {
44 e.consume(); 76 setText("");
45 } else {
46 setModified(true);
47 } 77 }
48 } 78 }
49 } 79 }
50 80
51 @Override 81 @Override

mercurial