author | dav |
Mon, 07 Apr 2008 14:53:51 +0400 | |
changeset 438 | 2ae294e4518c |
parent 2 | 90ce3da70b43 |
child 1287 | a04aca99c77a |
child 715 | f16baef3a20e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
package javax.swing.text; |
|
26 |
||
27 |
import java.lang.reflect.*; |
|
28 |
import java.text.*; |
|
29 |
import java.util.*; |
|
30 |
import javax.swing.text.*; |
|
31 |
||
32 |
/** |
|
33 |
* <code>NumberFormatter</code> subclasses <code>InternationalFormatter</code> |
|
34 |
* adding special behavior for numbers. Among the specializations are |
|
35 |
* (these are only used if the <code>NumberFormatter</code> does not display |
|
36 |
* invalid nubers, eg <code>setAllowsInvalid(false)</code>): |
|
37 |
* <ul> |
|
38 |
* <li>Pressing +/- (- is determined from the |
|
39 |
* <code>DecimalFormatSymbols</code> associated with the |
|
40 |
* <code>DecimalFormat</code>) in any field but the exponent |
|
41 |
* field will attempt to change the sign of the number to |
|
42 |
* positive/negative. |
|
43 |
* <li>Pressing +/- (- is determined from the |
|
44 |
* <code>DecimalFormatSymbols</code> associated with the |
|
45 |
* <code>DecimalFormat</code>) in the exponent field will |
|
46 |
* attemp to change the sign of the exponent to positive/negative. |
|
47 |
* </ul> |
|
48 |
* <p> |
|
49 |
* If you are displaying scientific numbers, you may wish to turn on |
|
50 |
* overwrite mode, <code>setOverwriteMode(true)</code>. For example: |
|
51 |
* <pre> |
|
52 |
* DecimalFormat decimalFormat = new DecimalFormat("0.000E0"); |
|
53 |
* NumberFormatter textFormatter = new NumberFormatter(decimalFormat); |
|
54 |
* textFormatter.setOverwriteMode(true); |
|
55 |
* textFormatter.setAllowsInvalid(false); |
|
56 |
* </pre> |
|
57 |
* <p> |
|
58 |
* If you are going to allow the user to enter decimal |
|
59 |
* values, you should either force the DecimalFormat to contain at least |
|
60 |
* one decimal (<code>#.0###</code>), or allow the value to be invalid |
|
61 |
* <code>setAllowsInvalid(true)</code>. Otherwise users may not be able to |
|
62 |
* input decimal values. |
|
63 |
* <p> |
|
64 |
* <code>NumberFormatter</code> provides slightly different behavior to |
|
65 |
* <code>stringToValue</code> than that of its superclass. If you have |
|
66 |
* specified a Class for values, {@link #setValueClass}, that is one of |
|
67 |
* of <code>Integer</code>, <code>Long</code>, <code>Float</code>, |
|
68 |
* <code>Double</code>, <code>Byte</code> or <code>Short</code> and |
|
69 |
* the Format's <code>parseObject</code> returns an instance of |
|
70 |
* <code>Number</code>, the corresponding instance of the value class |
|
71 |
* will be created using the constructor appropriate for the primitive |
|
72 |
* type the value class represents. For example: |
|
73 |
* <code>setValueClass(Integer.class)</code> will cause the resulting |
|
74 |
* value to be created via |
|
75 |
* <code>new Integer(((Number)formatter.parseObject(string)).intValue())</code>. |
|
76 |
* This is typically useful if you |
|
77 |
* wish to set a min/max value as the various <code>Number</code> |
|
78 |
* implementations are generally not comparable to each other. This is also |
|
79 |
* useful if for some reason you need a specific <code>Number</code> |
|
80 |
* implementation for your values. |
|
81 |
* <p> |
|
82 |
* <strong>Warning:</strong> |
|
83 |
* Serialized objects of this class will not be compatible with |
|
84 |
* future Swing releases. The current serialization support is |
|
85 |
* appropriate for short term storage or RMI between applications running |
|
86 |
* the same version of Swing. As of 1.4, support for long term storage |
|
87 |
* of all JavaBeans<sup><font size="-2">TM</font></sup> |
|
88 |
* has been added to the <code>java.beans</code> package. |
|
89 |
* Please see {@link java.beans.XMLEncoder}. |
|
90 |
* |
|
91 |
* @since 1.4 |
|
92 |
*/ |
|
93 |
public class NumberFormatter extends InternationalFormatter { |
|
94 |
/** The special characters from the Format instance. */ |
|
95 |
private String specialChars; |
|
96 |
||
97 |
/** |
|
98 |
* Creates a <code>NumberFormatter</code> with the a default |
|
99 |
* <code>NumberFormat</code> instance obtained from |
|
100 |
* <code>NumberFormat.getNumberInstance()</code>. |
|
101 |
*/ |
|
102 |
public NumberFormatter() { |
|
103 |
this(NumberFormat.getNumberInstance()); |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Creates a NumberFormatter with the specified Format instance. |
|
108 |
* |
|
109 |
* @param format Format used to dictate legal values |
|
110 |
*/ |
|
111 |
public NumberFormatter(NumberFormat format) { |
|
112 |
super(format); |
|
113 |
setFormat(format); |
|
114 |
setAllowsInvalid(true); |
|
115 |
setCommitsOnValidEdit(false); |
|
116 |
setOverwriteMode(false); |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Sets the format that dictates the legal values that can be edited |
|
121 |
* and displayed. |
|
122 |
* <p> |
|
123 |
* If you have used the nullary constructor the value of this property |
|
124 |
* will be determined for the current locale by way of the |
|
125 |
* <code>NumberFormat.getNumberInstance()</code> method. |
|
126 |
* |
|
127 |
* @param format NumberFormat instance used to dictate legal values |
|
128 |
*/ |
|
129 |
public void setFormat(Format format) { |
|
130 |
super.setFormat(format); |
|
131 |
||
132 |
DecimalFormatSymbols dfs = getDecimalFormatSymbols(); |
|
133 |
||
134 |
if (dfs != null) { |
|
135 |
StringBuffer sb = new StringBuffer(); |
|
136 |
||
137 |
sb.append(dfs.getCurrencySymbol()); |
|
138 |
sb.append(dfs.getDecimalSeparator()); |
|
139 |
sb.append(dfs.getGroupingSeparator()); |
|
140 |
sb.append(dfs.getInfinity()); |
|
141 |
sb.append(dfs.getInternationalCurrencySymbol()); |
|
142 |
sb.append(dfs.getMinusSign()); |
|
143 |
sb.append(dfs.getMonetaryDecimalSeparator()); |
|
144 |
sb.append(dfs.getNaN()); |
|
145 |
sb.append(dfs.getPercent()); |
|
146 |
sb.append('+'); |
|
147 |
specialChars = sb.toString(); |
|
148 |
} |
|
149 |
else { |
|
150 |
specialChars = ""; |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Invokes <code>parseObject</code> on <code>f</code>, returning |
|
156 |
* its value. |
|
157 |
*/ |
|
158 |
Object stringToValue(String text, Format f) throws ParseException { |
|
159 |
if (f == null) { |
|
160 |
return text; |
|
161 |
} |
|
162 |
Object value = f.parseObject(text); |
|
163 |
||
164 |
return convertValueToValueClass(value, getValueClass()); |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* Converts the passed in value to the passed in class. This only |
|
169 |
* works if <code>valueClass</code> is one of <code>Integer</code>, |
|
170 |
* <code>Long</code>, <code>Float</code>, <code>Double</code>, |
|
171 |
* <code>Byte</code> or <code>Short</code> and <code>value</code> |
|
172 |
* is an instanceof <code>Number</code>. |
|
173 |
*/ |
|
174 |
private Object convertValueToValueClass(Object value, Class valueClass) { |
|
175 |
if (valueClass != null && (value instanceof Number)) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
176 |
Number numberValue = (Number)value; |
2 | 177 |
if (valueClass == Integer.class) { |
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
178 |
return Integer.valueOf(numberValue.intValue()); |
2 | 179 |
} |
180 |
else if (valueClass == Long.class) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
181 |
return Long.valueOf(numberValue.longValue()); |
2 | 182 |
} |
183 |
else if (valueClass == Float.class) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
184 |
return Float.valueOf(numberValue.floatValue()); |
2 | 185 |
} |
186 |
else if (valueClass == Double.class) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
187 |
return Double.valueOf(numberValue.doubleValue()); |
2 | 188 |
} |
189 |
else if (valueClass == Byte.class) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
190 |
return Byte.valueOf(numberValue.byteValue()); |
2 | 191 |
} |
192 |
else if (valueClass == Short.class) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
193 |
return Short.valueOf(numberValue.shortValue()); |
2 | 194 |
} |
195 |
} |
|
196 |
return value; |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* Returns the character that is used to toggle to positive values. |
|
201 |
*/ |
|
202 |
private char getPositiveSign() { |
|
203 |
return '+'; |
|
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* Returns the character that is used to toggle to negative values. |
|
208 |
*/ |
|
209 |
private char getMinusSign() { |
|
210 |
DecimalFormatSymbols dfs = getDecimalFormatSymbols(); |
|
211 |
||
212 |
if (dfs != null) { |
|
213 |
return dfs.getMinusSign(); |
|
214 |
} |
|
215 |
return '-'; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Returns the character that is used to toggle to negative values. |
|
220 |
*/ |
|
221 |
private char getDecimalSeparator() { |
|
222 |
DecimalFormatSymbols dfs = getDecimalFormatSymbols(); |
|
223 |
||
224 |
if (dfs != null) { |
|
225 |
return dfs.getDecimalSeparator(); |
|
226 |
} |
|
227 |
return '.'; |
|
228 |
} |
|
229 |
||
230 |
/** |
|
231 |
* Returns the DecimalFormatSymbols from the Format instance. |
|
232 |
*/ |
|
233 |
private DecimalFormatSymbols getDecimalFormatSymbols() { |
|
234 |
Format f = getFormat(); |
|
235 |
||
236 |
if (f instanceof DecimalFormat) { |
|
237 |
return ((DecimalFormat)f).getDecimalFormatSymbols(); |
|
238 |
} |
|
239 |
return null; |
|
240 |
} |
|
241 |
||
242 |
/** |
|
243 |
*/ |
|
244 |
private boolean isValidInsertionCharacter(char aChar) { |
|
245 |
return (Character.isDigit(aChar) || specialChars.indexOf(aChar) != -1); |
|
246 |
} |
|
247 |
||
248 |
||
249 |
/** |
|
250 |
* Subclassed to return false if <code>text</code> contains in an invalid |
|
251 |
* character to insert, that is, it is not a digit |
|
252 |
* (<code>Character.isDigit()</code>) and |
|
253 |
* not one of the characters defined by the DecimalFormatSymbols. |
|
254 |
*/ |
|
255 |
boolean isLegalInsertText(String text) { |
|
256 |
if (getAllowsInvalid()) { |
|
257 |
return true; |
|
258 |
} |
|
259 |
for (int counter = text.length() - 1; counter >= 0; counter--) { |
|
260 |
char aChar = text.charAt(counter); |
|
261 |
||
262 |
if (!Character.isDigit(aChar) && |
|
263 |
specialChars.indexOf(aChar) == -1){ |
|
264 |
return false; |
|
265 |
} |
|
266 |
} |
|
267 |
return true; |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* Subclassed to treat the decimal separator, grouping separator, |
|
272 |
* exponent symbol, percent, permille, currency and sign as literals. |
|
273 |
*/ |
|
274 |
boolean isLiteral(Map attrs) { |
|
275 |
if (!super.isLiteral(attrs)) { |
|
276 |
if (attrs == null) { |
|
277 |
return false; |
|
278 |
} |
|
279 |
int size = attrs.size(); |
|
280 |
||
281 |
if (attrs.get(NumberFormat.Field.GROUPING_SEPARATOR) != null) { |
|
282 |
size--; |
|
283 |
if (attrs.get(NumberFormat.Field.INTEGER) != null) { |
|
284 |
size--; |
|
285 |
} |
|
286 |
} |
|
287 |
if (attrs.get(NumberFormat.Field.EXPONENT_SYMBOL) != null) { |
|
288 |
size--; |
|
289 |
} |
|
290 |
if (attrs.get(NumberFormat.Field.PERCENT) != null) { |
|
291 |
size--; |
|
292 |
} |
|
293 |
if (attrs.get(NumberFormat.Field.PERMILLE) != null) { |
|
294 |
size--; |
|
295 |
} |
|
296 |
if (attrs.get(NumberFormat.Field.CURRENCY) != null) { |
|
297 |
size--; |
|
298 |
} |
|
299 |
if (attrs.get(NumberFormat.Field.SIGN) != null) { |
|
300 |
size--; |
|
301 |
} |
|
302 |
if (size == 0) { |
|
303 |
return true; |
|
304 |
} |
|
305 |
return false; |
|
306 |
} |
|
307 |
return true; |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Subclassed to make the decimal separator navigatable, as well |
|
312 |
* as making the character between the integer field and the next |
|
313 |
* field navigatable. |
|
314 |
*/ |
|
315 |
boolean isNavigatable(int index) { |
|
316 |
if (!super.isNavigatable(index)) { |
|
317 |
// Don't skip the decimal, it causes wierd behavior |
|
318 |
if (getBufferedChar(index) == getDecimalSeparator()) { |
|
319 |
return true; |
|
320 |
} |
|
321 |
return false; |
|
322 |
} |
|
323 |
return true; |
|
324 |
} |
|
325 |
||
326 |
/** |
|
327 |
* Returns the first <code>NumberFormat.Field</code> starting |
|
328 |
* <code>index</code> incrementing by <code>direction</code>. |
|
329 |
*/ |
|
330 |
private NumberFormat.Field getFieldFrom(int index, int direction) { |
|
331 |
if (isValidMask()) { |
|
332 |
int max = getFormattedTextField().getDocument().getLength(); |
|
333 |
AttributedCharacterIterator iterator = getIterator(); |
|
334 |
||
335 |
if (index >= max) { |
|
336 |
index += direction; |
|
337 |
} |
|
338 |
while (index >= 0 && index < max) { |
|
339 |
iterator.setIndex(index); |
|
340 |
||
341 |
Map attrs = iterator.getAttributes(); |
|
342 |
||
343 |
if (attrs != null && attrs.size() > 0) { |
|
344 |
Iterator keys = attrs.keySet().iterator(); |
|
345 |
||
346 |
while (keys.hasNext()) { |
|
347 |
Object key = keys.next(); |
|
348 |
||
349 |
if (key instanceof NumberFormat.Field) { |
|
350 |
return (NumberFormat.Field)key; |
|
351 |
} |
|
352 |
} |
|
353 |
} |
|
354 |
index += direction; |
|
355 |
} |
|
356 |
} |
|
357 |
return null; |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Overriden to toggle the value if the positive/minus sign |
|
362 |
* is inserted. |
|
363 |
*/ |
|
364 |
void replace(DocumentFilter.FilterBypass fb, int offset, int length, |
|
365 |
String string, AttributeSet attr) throws BadLocationException { |
|
366 |
if (!getAllowsInvalid() && length == 0 && string != null && |
|
367 |
string.length() == 1 && |
|
368 |
toggleSignIfNecessary(fb, offset, string.charAt(0))) { |
|
369 |
return; |
|
370 |
} |
|
371 |
super.replace(fb, offset, length, string, attr); |
|
372 |
} |
|
373 |
||
374 |
/** |
|
375 |
* Will change the sign of the integer or exponent field if |
|
376 |
* <code>aChar</code> is the positive or minus sign. Returns |
|
377 |
* true if a sign change was attempted. |
|
378 |
*/ |
|
379 |
private boolean toggleSignIfNecessary(DocumentFilter.FilterBypass fb, |
|
380 |
int offset, char aChar) throws |
|
381 |
BadLocationException { |
|
382 |
if (aChar == getMinusSign() || aChar == getPositiveSign()) { |
|
383 |
NumberFormat.Field field = getFieldFrom(offset, -1); |
|
384 |
Object newValue; |
|
385 |
||
386 |
try { |
|
387 |
if (field == null || |
|
388 |
(field != NumberFormat.Field.EXPONENT && |
|
389 |
field != NumberFormat.Field.EXPONENT_SYMBOL && |
|
390 |
field != NumberFormat.Field.EXPONENT_SIGN)) { |
|
391 |
newValue = toggleSign((aChar == getPositiveSign())); |
|
392 |
} |
|
393 |
else { |
|
394 |
// exponent |
|
395 |
newValue = toggleExponentSign(offset, aChar); |
|
396 |
} |
|
397 |
if (newValue != null && isValidValue(newValue, false)) { |
|
398 |
int lc = getLiteralCountTo(offset); |
|
399 |
String string = valueToString(newValue); |
|
400 |
||
401 |
fb.remove(0, fb.getDocument().getLength()); |
|
402 |
fb.insertString(0, string, null); |
|
403 |
updateValue(newValue); |
|
404 |
repositionCursor(getLiteralCountTo(offset) - |
|
405 |
lc + offset, 1); |
|
406 |
return true; |
|
407 |
} |
|
408 |
} catch (ParseException pe) { |
|
409 |
invalidEdit(); |
|
410 |
} |
|
411 |
} |
|
412 |
return false; |
|
413 |
} |
|
414 |
||
415 |
/** |
|
416 |
* Returns true if the range offset to length identifies the only |
|
417 |
* integer field. |
|
418 |
*/ |
|
419 |
private boolean isOnlyIntegerField(int offset, int length) { |
|
420 |
if (isValidMask()) { |
|
421 |
int start = getAttributeStart(NumberFormat.Field.INTEGER); |
|
422 |
||
423 |
if (start != -1) { |
|
424 |
AttributedCharacterIterator iterator = getIterator(); |
|
425 |
||
426 |
iterator.setIndex(start); |
|
427 |
if (offset > start || iterator.getRunLimit( |
|
428 |
NumberFormat.Field.INTEGER) > (offset + length)) { |
|
429 |
return false; |
|
430 |
} |
|
431 |
return true; |
|
432 |
} |
|
433 |
} |
|
434 |
return false; |
|
435 |
} |
|
436 |
||
437 |
/** |
|
438 |
* Invoked to toggle the sign. For this to work the value class |
|
439 |
* must have a single arg constructor that takes a String. |
|
440 |
*/ |
|
441 |
private Object toggleSign(boolean positive) throws ParseException { |
|
442 |
Object value = stringToValue(getFormattedTextField().getText()); |
|
443 |
||
444 |
if (value != null) { |
|
445 |
// toString isn't localized, so that using +/- should work |
|
446 |
// correctly. |
|
447 |
String string = value.toString(); |
|
448 |
||
449 |
if (string != null && string.length() > 0) { |
|
450 |
if (positive) { |
|
451 |
if (string.charAt(0) == '-') { |
|
452 |
string = string.substring(1); |
|
453 |
} |
|
454 |
} |
|
455 |
else { |
|
456 |
if (string.charAt(0) == '+') { |
|
457 |
string = string.substring(1); |
|
458 |
} |
|
459 |
if (string.length() > 0 && string.charAt(0) != '-') { |
|
460 |
string = "-" + string; |
|
461 |
} |
|
462 |
} |
|
463 |
if (string != null) { |
|
464 |
Class valueClass = getValueClass(); |
|
465 |
||
466 |
if (valueClass == null) { |
|
467 |
valueClass = value.getClass(); |
|
468 |
} |
|
469 |
try { |
|
470 |
Constructor cons = valueClass.getConstructor( |
|
471 |
new Class[] { String.class }); |
|
472 |
||
473 |
if (cons != null) { |
|
474 |
return cons.newInstance(new Object[]{string}); |
|
475 |
} |
|
476 |
} catch (Throwable ex) { } |
|
477 |
} |
|
478 |
} |
|
479 |
} |
|
480 |
return null; |
|
481 |
} |
|
482 |
||
483 |
/** |
|
484 |
* Invoked to toggle the sign of the exponent (for scientific |
|
485 |
* numbers). |
|
486 |
*/ |
|
487 |
private Object toggleExponentSign(int offset, char aChar) throws |
|
488 |
BadLocationException, ParseException { |
|
489 |
String string = getFormattedTextField().getText(); |
|
490 |
int replaceLength = 0; |
|
491 |
int loc = getAttributeStart(NumberFormat.Field.EXPONENT_SIGN); |
|
492 |
||
493 |
if (loc >= 0) { |
|
494 |
replaceLength = 1; |
|
495 |
offset = loc; |
|
496 |
} |
|
497 |
if (aChar == getPositiveSign()) { |
|
498 |
string = getReplaceString(offset, replaceLength, null); |
|
499 |
} |
|
500 |
else { |
|
501 |
string = getReplaceString(offset, replaceLength, |
|
502 |
new String(new char[] { aChar })); |
|
503 |
} |
|
504 |
return stringToValue(string); |
|
505 |
} |
|
506 |
} |