Sat, 25 Jul 2020 14:01:28 +0200
converts to maven project
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 */
27 package de.uapcore.sudoku;
29 import javax.swing.*;
30 import java.awt.*;
31 import java.awt.event.FocusAdapter;
32 import java.awt.event.FocusEvent;
33 import java.awt.event.KeyAdapter;
34 import java.awt.event.KeyEvent;
36 /**
37 *
38 * @author mike
39 */
40 public final class SudokuTextField extends JTextField {
42 private boolean modified;
44 public SudokuTextField() {
45 setBorder(null);
46 setBackground(Color.WHITE);
48 setFont(new Font("Dialog", Font.PLAIN, 18));
49 setHorizontalAlignment(JTextField.CENTER);
51 Dimension dim = new Dimension(40,40);
52 setPreferredSize(dim);
53 setMinimumSize(dim);
54 setMaximumSize(dim);
56 addKeyListener(new KeyAdapter() {
57 private void handle(KeyEvent e) {
58 char c = e.getKeyChar();
59 if (!e.isAltDown() && !e.isControlDown() &&
60 !Character.isISOControl(c)) {
61 // Perform clean input check
62 if (getText().length() > 0 && getSelectedText() == null) {
63 if (c != KeyEvent.CHAR_UNDEFINED) {
64 e.consume();
65 }
66 } else {
67 if (c < '1' || c > '9') {
68 e.consume();
69 } else {
70 setModified(true);
71 }
72 }
73 } else {
74 // We can still be tricked by hotkeys, so do it the hard way
75 if (!getText().matches("^[1-9]$")) {
76 setText("");
77 }
78 }
79 }
81 @Override
82 public void keyPressed(KeyEvent e) {
83 handle(e);
84 }
86 @Override
87 public void keyTyped(KeyEvent e) {
88 handle(e);
89 }
91 @Override
92 public void keyReleased(KeyEvent e) {
93 handle(e);
94 }
96 });
97 addFocusListener(new FocusAdapter() {
98 @Override
99 public void focusGained(FocusEvent e) {
100 selectAll();
101 }
102 @Override
103 public void focusLost(FocusEvent e) {
104 select(0, 0);
105 }
106 });
107 }
109 public int getValue() {
110 if (getText().length() > 0) {
111 return Integer.valueOf(getText());
112 } else {
113 return 0;
114 }
115 }
117 public void setValue(int v) {
118 if (v == 0) {
119 setText("");
120 } else if (v < 10) {
121 setText(String.valueOf(v));
122 } else {
123 throw new IllegalArgumentException(
124 "Sudoku numbers must be in range 0-9 (0 means 'not set')");
125 }
126 }
128 public void setModified(boolean modified) {
129 this.modified = modified;
130 setForeground(modified?Color.BLUE:Color.BLACK);
131 }
133 public boolean isModified() {
134 return modified;
135 }
136 }