src/de/uapcore/sudoku/SudokuTextField.java

Sat, 26 Jan 2013 18:43:49 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 18:43:49 +0100
changeset 4
b8588e318001
parent 3
ed931970b4ac
child 6
5bab2e971333
permissions
-rw-r--r--

added NEW function

     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 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;
    38 /**
    39  *
    40  * @author mike
    41  */
    42 public final class SudokuTextField extends JTextField {
    44     private boolean modified;
    46     public SudokuTextField() {
    47         setBorder(null);
    48         setBackground(Color.WHITE);
    50         setFont(new Font("Dialog", Font.PLAIN, 18));
    51         setHorizontalAlignment(JTextField.CENTER);
    53         Dimension dim = new Dimension(40,40);
    54         setPreferredSize(dim);
    55         setMinimumSize(dim);
    56         setMaximumSize(dim);
    58         addKeyListener(new KeyAdapter() {
    59             private void handle(KeyEvent e) {
    60                 char c = e.getKeyChar();
    61                 if (!e.isAltDown() && !e.isControlDown() &&
    62                         Character.isLetterOrDigit(c)) {
    63                     // Perform clean input check
    64                     if (getText().length() > 0 && getSelectedText() == null) {
    65                         e.consume();
    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 }

mercurial