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