-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJLimitedTextField.java
More file actions
137 lines (126 loc) · 4.01 KB
/
JLimitedTextField.java
File metadata and controls
137 lines (126 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/** Copyright 2011 André Sá de Mello
* This file is part of Unus.
*
* Unus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Unus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Unus. If not, see <http://www.gnu.org/licenses/>.
*/
import javax.swing.*;
import javax.swing.text.*;
/**
* This class provides a custom JTextField which allows only alphanumerical input.
* It also provides utility methods for conversion of strings into adequate values.
*/
public class JLimitedTextField extends PlainDocument {
// CONSTANTS
private static final long serialVersionUID = -3846912517390711715L;
public static final String charTable =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final int MIN = 0;
public static final int MAX = 36;
// Acceptable types
public static final int ALL = 0;
public static final int NUM = 1;
public static final int CHAR = 2;
public static final int POS = 3;
// VARIABLES
private int limit;
private int max, min;
/**
* @param limit limit of characters
* @param type a type constant
*/
JLimitedTextField(int limit, int type) {
super();
this.limit = limit;
switch (type) {
case 0:
min = 0;
max = 36;
break;
case 1:
min = 0;
max = 10;
break;
case 2:
min = 10;
max = 36;
break;
case 3:
min = 1;
max = 10;
break;
default:
min = 0;
max = 36;
break;
}
}
/**
* Creates a configured JTextField and returns it.
* @return a configured JTextField
*/
public JTextField createField () {
JTextField field = new JTextField(limit);
field.setDocument(this);
return field;
}
// Extension for insertString (checks for valid input). Case insensitive.
public void insertString (int offset, String str, AttributeSet attr)
throws BadLocationException
{
if (str == null) return;
if ((getLength() + str.length()) <= limit) {
str = str.toUpperCase(); // Make case-insensitive
for (int i = 0; i < str.length(); ++i)
if (!validate(str.charAt(i))) return;
super.insertString(offset, str, attr);
}
}
/**
* Returns proper numerical value for a char, given needed range.
* If no valid conversion can be made, returns GSudokuBoard.NULL.
* @param ch character to convert
* @param min minimum valid value
* @param max maximum valid value
* @return the correct numerical representation in the range
*/
public static int getValue (char ch, int min, int max) {
if (min < MIN || max > MAX)
throw new IllegalArgumentException("* Out of range!");
ch = Character.toUpperCase(ch); // Make case-insensitive
for (int i = min; i < max; ++i)
if (charTable.charAt(i) == ch) return i;
return GSudokuBoard.NULL;
}
/**
* Returns proper character representation for given numerical value.
* If no valid conversion can be made, returns a space character.
* @param num value to convert
* @return the correct alphanumerical representation of the value
*/
public static char getChar (int num) {
if (num > JLimitedTextField.MAX || num < 0)
return ' ';
return charTable.charAt(num);
}
/**
* Checks if a character is an alphanumerical in range.
* @param ch character to check
* @return validation result
*/
private boolean validate (char ch) {
for (int i = min; i < max; ++i)
if (ch == charTable.charAt(i)) return true;
return false;
}
}