src/de/uapcore/sudoku/SudokuTextField.java

changeset 3
ed931970b4ac
parent 2
5179eff8a9b6
child 4
b8588e318001
     1.1 --- a/src/de/uapcore/sudoku/SudokuTextField.java	Sat Jan 26 17:42:07 2013 +0100
     1.2 +++ b/src/de/uapcore/sudoku/SudokuTextField.java	Sat Jan 26 18:38:12 2013 +0100
     1.3 @@ -1,3 +1,29 @@
     1.4 +/*
     1.5 + * Copyright 2013 Mike Becker. All rights reserved.
     1.6 + * 
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions are met:
     1.9 + * 
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + * 
    1.17 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.18 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.19 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.20 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.21 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.22 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.23 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.24 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.25 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.26 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.27 + * POSSIBILITY OF SUCH DAMAGE.
    1.28 + */
    1.29 +
    1.30  package de.uapcore.sudoku;
    1.31  
    1.32  import java.awt.Color;
    1.33 @@ -31,19 +57,23 @@
    1.34          
    1.35          addKeyListener(new KeyAdapter() {
    1.36              private void handle(KeyEvent e) {
    1.37 -                if (getText().length() > 0 && getSelectedText() == null) {
    1.38 -                    int c = e.getKeyCode();
    1.39 -                    if (c != KeyEvent.VK_DELETE &&
    1.40 -                            c != KeyEvent.VK_BACK_SPACE &&
    1.41 -                            !e.isActionKey()) {
    1.42 +                char c = e.getKeyChar();
    1.43 +                if (!e.isAltDown() && !e.isControlDown() &&
    1.44 +                        Character.isLetterOrDigit(c)) {
    1.45 +                    // Perform clean input check
    1.46 +                    if (getText().length() > 0 && getSelectedText() == null) {
    1.47                          e.consume();
    1.48 +                    } else {
    1.49 +                        if (c < '1' || c > '9') {
    1.50 +                            e.consume();
    1.51 +                        } else {
    1.52 +                            setModified(true);
    1.53 +                        }
    1.54                      }
    1.55                  } else {
    1.56 -                    char c = e.getKeyChar();
    1.57 -                    if (c < '0' || c > '9') {
    1.58 -                        e.consume();
    1.59 -                    } else {
    1.60 -                        setModified(true);
    1.61 +                    // We can still be tricked by hotkeys, so do it the hard way
    1.62 +                    if (!getText().matches("^[1-9]$")) {
    1.63 +                        setText("");
    1.64                      }
    1.65                  }
    1.66              }

mercurial