src/de/uapcore/sudoku/SudokuTextField.java

Sun, 27 Jan 2013 15:03:57 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 27 Jan 2013 15:03:57 +0100
changeset 6
5bab2e971333
parent 4
b8588e318001
permissions
-rw-r--r--

file IO

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

mercurial