src/de/uapcore/sudoku/SudokuTextField.java

changeset 9
576e7a2861ae
parent 8
e70a0e3555fb
child 10
369903afbb29
     1.1 --- a/src/de/uapcore/sudoku/SudokuTextField.java	Fri Feb 01 10:18:47 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,138 +0,0 @@
     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 -import java.awt.Dimension;
    1.34 -import java.awt.Font;
    1.35 -import java.awt.event.FocusAdapter;
    1.36 -import java.awt.event.FocusEvent;
    1.37 -import java.awt.event.KeyAdapter;
    1.38 -import java.awt.event.KeyEvent;
    1.39 -import javax.swing.JTextField;
    1.40 -
    1.41 -/**
    1.42 - *
    1.43 - * @author mike
    1.44 - */
    1.45 -public final class SudokuTextField extends JTextField {
    1.46 -    
    1.47 -    private boolean modified;
    1.48 -    
    1.49 -    public SudokuTextField() {
    1.50 -        setBorder(null);
    1.51 -        setBackground(Color.WHITE);
    1.52 -        
    1.53 -        setFont(new Font("Dialog", Font.PLAIN, 18));
    1.54 -        setHorizontalAlignment(JTextField.CENTER);
    1.55 -        
    1.56 -        Dimension dim = new Dimension(40,40);
    1.57 -        setPreferredSize(dim);
    1.58 -        setMinimumSize(dim);
    1.59 -        setMaximumSize(dim);
    1.60 -        
    1.61 -        addKeyListener(new KeyAdapter() {
    1.62 -            private void handle(KeyEvent e) {
    1.63 -                char c = e.getKeyChar();
    1.64 -                if (!e.isAltDown() && !e.isControlDown() &&
    1.65 -                        !Character.isISOControl(c)) {
    1.66 -                    // Perform clean input check
    1.67 -                    if (getText().length() > 0 && getSelectedText() == null) {
    1.68 -                        if (c != KeyEvent.CHAR_UNDEFINED) {
    1.69 -                            e.consume();
    1.70 -                        }
    1.71 -                    } else {
    1.72 -                        if (c < '1' || c > '9') {
    1.73 -                            e.consume();
    1.74 -                        } else {
    1.75 -                            setModified(true);
    1.76 -                        }
    1.77 -                    }
    1.78 -                } else {
    1.79 -                    // We can still be tricked by hotkeys, so do it the hard way
    1.80 -                    if (!getText().matches("^[1-9]$")) {
    1.81 -                        setText("");
    1.82 -                    }
    1.83 -                }
    1.84 -            }
    1.85 -            
    1.86 -            @Override
    1.87 -            public void keyPressed(KeyEvent e) {
    1.88 -                handle(e);
    1.89 -            }
    1.90 -
    1.91 -            @Override
    1.92 -            public void keyTyped(KeyEvent e) {
    1.93 -                handle(e);
    1.94 -            }
    1.95 -
    1.96 -            @Override
    1.97 -            public void keyReleased(KeyEvent e) {
    1.98 -                handle(e);
    1.99 -            }
   1.100 -            
   1.101 -        });
   1.102 -        addFocusListener(new FocusAdapter() {
   1.103 -            @Override
   1.104 -            public void focusGained(FocusEvent e) {
   1.105 -                selectAll();
   1.106 -            }
   1.107 -            @Override
   1.108 -            public void focusLost(FocusEvent e) {
   1.109 -                select(0, 0);
   1.110 -            }
   1.111 -        });
   1.112 -    }
   1.113 -    
   1.114 -    public int getValue() {
   1.115 -        if (getText().length() > 0) {
   1.116 -            return Integer.valueOf(getText());
   1.117 -        } else {
   1.118 -            return 0;
   1.119 -        }
   1.120 -    }
   1.121 -    
   1.122 -    public void setValue(int v) {
   1.123 -        if (v == 0) {
   1.124 -            setText("");
   1.125 -        } else if (v < 10) {
   1.126 -            setText(String.valueOf(v));
   1.127 -        } else {
   1.128 -            throw new IllegalArgumentException(
   1.129 -                    "Sudoku numbers must be in range 0-9 (0 means 'not set')");
   1.130 -        }
   1.131 -    }
   1.132 -    
   1.133 -    public void setModified(boolean modified) {
   1.134 -        this.modified = modified;
   1.135 -        setForeground(modified?Color.BLUE:Color.BLACK);
   1.136 -    }
   1.137 -    
   1.138 -    public boolean isModified() {
   1.139 -        return modified;
   1.140 -    }
   1.141 -}

mercurial