Sat, 26 Jan 2013 18:38:12 +0100
added license and main menu
universe@3 | 1 | /* |
universe@3 | 2 | * Copyright 2013 Mike Becker. All rights reserved. |
universe@3 | 3 | * |
universe@3 | 4 | * Redistribution and use in source and binary forms, with or without |
universe@3 | 5 | * modification, are permitted provided that the following conditions are met: |
universe@3 | 6 | * |
universe@3 | 7 | * 1. Redistributions of source code must retain the above copyright |
universe@3 | 8 | * notice, this list of conditions and the following disclaimer. |
universe@3 | 9 | * |
universe@3 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@3 | 11 | * notice, this list of conditions and the following disclaimer in the |
universe@3 | 12 | * documentation and/or other materials provided with the distribution. |
universe@3 | 13 | * |
universe@3 | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@3 | 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@3 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@3 | 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@3 | 18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@3 | 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@3 | 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@3 | 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@3 | 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@3 | 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@3 | 24 | * POSSIBILITY OF SUCH DAMAGE. |
universe@3 | 25 | */ |
universe@3 | 26 | |
universe@1 | 27 | package de.uapcore.sudoku; |
universe@1 | 28 | |
universe@1 | 29 | import java.awt.Color; |
universe@1 | 30 | import java.awt.Dimension; |
universe@1 | 31 | import java.awt.Font; |
universe@1 | 32 | import java.awt.event.FocusAdapter; |
universe@1 | 33 | import java.awt.event.FocusEvent; |
universe@1 | 34 | import java.awt.event.KeyAdapter; |
universe@1 | 35 | import java.awt.event.KeyEvent; |
universe@1 | 36 | import javax.swing.JTextField; |
universe@1 | 37 | |
universe@1 | 38 | /** |
universe@1 | 39 | * |
universe@1 | 40 | * @author mike |
universe@1 | 41 | */ |
universe@2 | 42 | public final class SudokuTextField extends JTextField { |
universe@2 | 43 | |
universe@2 | 44 | private boolean modified; |
universe@1 | 45 | |
universe@1 | 46 | public SudokuTextField() { |
universe@1 | 47 | setBorder(null); |
universe@1 | 48 | setBackground(Color.WHITE); |
universe@1 | 49 | |
universe@1 | 50 | setFont(new Font("Dialog", Font.PLAIN, 18)); |
universe@1 | 51 | setHorizontalAlignment(JTextField.CENTER); |
universe@1 | 52 | |
universe@1 | 53 | Dimension dim = new Dimension(40,40); |
universe@1 | 54 | setPreferredSize(dim); |
universe@1 | 55 | setMinimumSize(dim); |
universe@1 | 56 | setMaximumSize(dim); |
universe@1 | 57 | |
universe@1 | 58 | addKeyListener(new KeyAdapter() { |
universe@1 | 59 | private void handle(KeyEvent e) { |
universe@3 | 60 | char c = e.getKeyChar(); |
universe@3 | 61 | if (!e.isAltDown() && !e.isControlDown() && |
universe@3 | 62 | Character.isLetterOrDigit(c)) { |
universe@3 | 63 | // Perform clean input check |
universe@3 | 64 | if (getText().length() > 0 && getSelectedText() == null) { |
universe@1 | 65 | e.consume(); |
universe@3 | 66 | } else { |
universe@3 | 67 | if (c < '1' || c > '9') { |
universe@3 | 68 | e.consume(); |
universe@3 | 69 | } else { |
universe@3 | 70 | setModified(true); |
universe@3 | 71 | } |
universe@1 | 72 | } |
universe@1 | 73 | } else { |
universe@3 | 74 | // We can still be tricked by hotkeys, so do it the hard way |
universe@3 | 75 | if (!getText().matches("^[1-9]$")) { |
universe@3 | 76 | setText(""); |
universe@1 | 77 | } |
universe@1 | 78 | } |
universe@1 | 79 | } |
universe@1 | 80 | |
universe@1 | 81 | @Override |
universe@1 | 82 | public void keyPressed(KeyEvent e) { |
universe@1 | 83 | handle(e); |
universe@1 | 84 | } |
universe@1 | 85 | |
universe@1 | 86 | @Override |
universe@1 | 87 | public void keyTyped(KeyEvent e) { |
universe@1 | 88 | handle(e); |
universe@1 | 89 | } |
universe@1 | 90 | |
universe@1 | 91 | @Override |
universe@1 | 92 | public void keyReleased(KeyEvent e) { |
universe@1 | 93 | handle(e); |
universe@1 | 94 | } |
universe@1 | 95 | |
universe@1 | 96 | }); |
universe@1 | 97 | addFocusListener(new FocusAdapter() { |
universe@1 | 98 | @Override |
universe@1 | 99 | public void focusGained(FocusEvent e) { |
universe@1 | 100 | selectAll(); |
universe@1 | 101 | } |
universe@1 | 102 | }); |
universe@1 | 103 | } |
universe@1 | 104 | |
universe@2 | 105 | public int getValue() { |
universe@2 | 106 | if (getText().length() > 0) { |
universe@2 | 107 | return Integer.valueOf(getText()); |
universe@2 | 108 | } else { |
universe@2 | 109 | return 0; |
universe@2 | 110 | } |
universe@2 | 111 | } |
universe@2 | 112 | |
universe@2 | 113 | public void setValue(int v) { |
universe@2 | 114 | if (v == 0) { |
universe@2 | 115 | setText(""); |
universe@2 | 116 | } else if (v < 10) { |
universe@2 | 117 | setText(String.valueOf(v)); |
universe@2 | 118 | } else { |
universe@2 | 119 | throw new IllegalArgumentException( |
universe@2 | 120 | "Sudoku numbers must be in range 0-9 (0 means 'not set')"); |
universe@2 | 121 | } |
universe@2 | 122 | } |
universe@2 | 123 | |
universe@2 | 124 | public void setModified(boolean modified) { |
universe@2 | 125 | this.modified = modified; |
universe@2 | 126 | setForeground(modified?Color.BLUE:Color.BLACK); |
universe@2 | 127 | } |
universe@2 | 128 | |
universe@2 | 129 | public boolean isModified() { |
universe@2 | 130 | return modified; |
universe@2 | 131 | } |
universe@1 | 132 | } |