universe@3: /* universe@3: * Copyright 2013 Mike Becker. All rights reserved. universe@3: * universe@3: * Redistribution and use in source and binary forms, with or without universe@3: * modification, are permitted provided that the following conditions are met: universe@3: * universe@3: * 1. Redistributions of source code must retain the above copyright universe@3: * notice, this list of conditions and the following disclaimer. universe@3: * universe@3: * 2. Redistributions in binary form must reproduce the above copyright universe@3: * notice, this list of conditions and the following disclaimer in the universe@3: * documentation and/or other materials provided with the distribution. universe@3: * universe@3: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@3: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@3: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@3: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@3: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@3: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@3: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@3: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@3: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@3: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@3: * POSSIBILITY OF SUCH DAMAGE. universe@3: */ universe@3: universe@1: package de.uapcore.sudoku; universe@1: universe@1: import java.awt.Color; universe@1: import java.awt.Dimension; universe@1: import java.awt.Font; universe@1: import java.awt.event.FocusAdapter; universe@1: import java.awt.event.FocusEvent; universe@1: import java.awt.event.KeyAdapter; universe@1: import java.awt.event.KeyEvent; universe@1: import javax.swing.JTextField; universe@1: universe@1: /** universe@1: * universe@1: * @author mike universe@1: */ universe@2: public final class SudokuTextField extends JTextField { universe@2: universe@2: private boolean modified; universe@1: universe@1: public SudokuTextField() { universe@1: setBorder(null); universe@1: setBackground(Color.WHITE); universe@1: universe@1: setFont(new Font("Dialog", Font.PLAIN, 18)); universe@1: setHorizontalAlignment(JTextField.CENTER); universe@1: universe@1: Dimension dim = new Dimension(40,40); universe@1: setPreferredSize(dim); universe@1: setMinimumSize(dim); universe@1: setMaximumSize(dim); universe@1: universe@1: addKeyListener(new KeyAdapter() { universe@1: private void handle(KeyEvent e) { universe@3: char c = e.getKeyChar(); universe@3: if (!e.isAltDown() && !e.isControlDown() && universe@3: Character.isLetterOrDigit(c)) { universe@3: // Perform clean input check universe@3: if (getText().length() > 0 && getSelectedText() == null) { universe@1: e.consume(); universe@3: } else { universe@3: if (c < '1' || c > '9') { universe@3: e.consume(); universe@3: } else { universe@3: setModified(true); universe@3: } universe@1: } universe@1: } else { universe@3: // We can still be tricked by hotkeys, so do it the hard way universe@3: if (!getText().matches("^[1-9]$")) { universe@3: setText(""); universe@1: } universe@1: } universe@1: } universe@1: universe@1: @Override universe@1: public void keyPressed(KeyEvent e) { universe@1: handle(e); universe@1: } universe@1: universe@1: @Override universe@1: public void keyTyped(KeyEvent e) { universe@1: handle(e); universe@1: } universe@1: universe@1: @Override universe@1: public void keyReleased(KeyEvent e) { universe@1: handle(e); universe@1: } universe@1: universe@1: }); universe@1: addFocusListener(new FocusAdapter() { universe@1: @Override universe@1: public void focusGained(FocusEvent e) { universe@1: selectAll(); universe@1: } universe@4: @Override universe@4: public void focusLost(FocusEvent e) { universe@4: select(0, 0); universe@4: } universe@1: }); universe@1: } universe@1: universe@2: public int getValue() { universe@2: if (getText().length() > 0) { universe@2: return Integer.valueOf(getText()); universe@2: } else { universe@2: return 0; universe@2: } universe@2: } universe@2: universe@2: public void setValue(int v) { universe@2: if (v == 0) { universe@2: setText(""); universe@2: } else if (v < 10) { universe@2: setText(String.valueOf(v)); universe@2: } else { universe@2: throw new IllegalArgumentException( universe@2: "Sudoku numbers must be in range 0-9 (0 means 'not set')"); universe@2: } universe@2: } universe@2: universe@2: public void setModified(boolean modified) { universe@2: this.modified = modified; universe@2: setForeground(modified?Color.BLUE:Color.BLACK); universe@2: } universe@2: universe@2: public boolean isModified() { universe@2: return modified; universe@2: } universe@1: }