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@9: import javax.swing.*; universe@9: import java.awt.*; 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: universe@1: /** universe@10: * A custom text field specifically for Sudoku number fields. 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@6: !Character.isISOControl(c)) { universe@3: // Perform clean input check universe@3: if (getText().length() > 0 && getSelectedText() == null) { universe@6: if (c != KeyEvent.CHAR_UNDEFINED) { universe@6: e.consume(); universe@6: } 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@10: universe@10: /** universe@10: * Returns the current value in the field or zero if the field is empty. universe@10: * universe@10: * @return the number from 1 to 9 or zero if empty universe@10: */ universe@2: public int getValue() { universe@10: if (getText().isEmpty()) { universe@10: return 0; universe@10: } else { universe@12: return Integer.parseInt(getText()); universe@2: } universe@2: } universe@10: universe@10: /** universe@10: * Sets the field's value. universe@10: * universe@10: * @param v the number from 1 to 9 or zero to clear the field universe@12: * @throws IllegalArgumentException if v is not between 0 and 9 universe@10: */ universe@2: public void setValue(int v) { universe@2: if (v == 0) { universe@2: setText(""); universe@13: } else if (v > 0 && 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@10: universe@10: /** universe@10: * Sets the modified state of this field. universe@10: * universe@10: * Modified fields are displayed in blue color. universe@10: * universe@10: * @param modified a flag indicating whether this field is modified universe@10: */ universe@2: public void setModified(boolean modified) { universe@2: this.modified = modified; universe@2: setForeground(modified?Color.BLUE:Color.BLACK); universe@2: } universe@10: universe@10: /** universe@10: * Checks whether this field is in modified state. universe@10: * universe@10: * @return true if this field is modified universe@10: */ universe@2: public boolean isModified() { universe@2: return modified; universe@2: } universe@1: }