Repository navigation
Expand file tree
/
Copy pathJNumberTextField.java
More file actions
204 lines (161 loc) · 4.25 KB
/
Copy pathJNumberTextField.java
File metadata and controls
204 lines (161 loc) · 4.25 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
public class JNumberTextField extends JTextField {
private static final long serialVersionUID = 1L;
private static final char DOT = '.';
private static final char NEGATIVE = '-';
private static final String BLANK = "";
private static final int DEF_PRECISION = 2;
public static final int NUMERIC = 2;
public static final int DECIMAL = 3;
public static final String FM_NUMERIC = "0123456789";
public static final String FM_DECIMAL = FM_NUMERIC + DOT;
private int maxLength = 0;
private int format = NUMERIC;
private String negativeChars = BLANK;
private String allowedChars = null;
private boolean allowNegative = false;
private int precision = 0;
protected PlainDocument numberFieldFilter;
public JNumberTextField() {
this(10, NUMERIC);
}
public JNumberTextField(int maxLen) {
this(maxLen, NUMERIC);
}
public JNumberTextField(int maxLen, int format) {
setAllowNegative(true);
setMaxLength(maxLen);
setFormat(format);
numberFieldFilter = new JNumberFieldFilter();
super.setDocument(numberFieldFilter);
}
public void setMaxLength(int maxLen) {
if (maxLen > 0)
maxLength = maxLen;
else
maxLength = 0;
}
public int getMaxLength() {
return maxLength;
}
public void setPrecision(int precision) {
if (format == NUMERIC)
return;
if (precision >= 0)
this.precision = precision;
else
this.precision = DEF_PRECISION;
}
public int getPrecision() {
return precision;
}
public Number getNumber() {
Number number = null;
if (format == NUMERIC)
number = new Integer(getText());
else
number = new Double(getText());
return number;
}
public void setNumber(Number value) {
setText(String.valueOf(value));
}
public int getInt() {
return Integer.parseInt(getText());
}
public void setInt(int value) {
setText(String.valueOf(value));
}
public float getFloat() {
return (new Float(getText())).floatValue();
}
public void setFloat(float value) {
setText(String.valueOf(value));
}
public double getDouble() {
return (new Double(getText())).doubleValue();
}
public void setDouble(double value) {
setText(String.valueOf(value));
}
public int getFormat() {
return format;
}
public void setFormat(int format) {
switch (format) {
case NUMERIC:
default:
this.format = NUMERIC;
this.precision = 0;
this.allowedChars = FM_NUMERIC;
break;
case DECIMAL:
this.format = DECIMAL;
this.precision = DEF_PRECISION;
this.allowedChars = FM_DECIMAL;
break;
}
}
public void setAllowNegative(boolean value) {
allowNegative = value;
if (value)
negativeChars = "" + NEGATIVE;
else
negativeChars = BLANK;
}
public boolean isAllowNegative() {
return allowNegative;
}
public void setDocument(Document document) {
}
class JNumberFieldFilter extends PlainDocument {
private static final long serialVersionUID = 1L;
public JNumberFieldFilter() {
super();
}
public void insertString(int offset, String str, AttributeSet attr)
throws BadLocationException {
String text = getText(0, offset) + str
+ getText(offset, (getLength() - offset));
if (str == null || text == null)
return;
for (int i = 0; i < str.length(); i++) {
if ((allowedChars + negativeChars).indexOf(str.charAt(i)) == -1)
return;
}
int precisionLength = 0, dotLength = 0, minusLength = 0;
int textLength = text.length();
try {
if (format == NUMERIC) {
if (!((text.equals(negativeChars)) && (text.length() == 1)))
new Long(text);
} else if (format == DECIMAL) {
if (!((text.equals(negativeChars)) && (text.length() == 1)))
new Double(text);
int dotIndex = text.indexOf(DOT);
if (dotIndex != -1) {
dotLength = 1;
precisionLength = textLength - dotIndex - dotLength;
if (precisionLength > precision)
return;
}
}
} catch (Exception ex) {
return;
}
if (text.startsWith("" + NEGATIVE)) {
if (!allowNegative)
return;
else
minusLength = 1;
}
if (maxLength < (textLength - dotLength - precisionLength - minusLength))
return;
super.insertString(offset, str, attr);
}
}
}