author | malenkov |
Wed, 30 Apr 2014 19:28:05 +0400 | |
changeset 24544 | c0133e7c7162 |
parent 22574 | 7f8ce0c8c20a |
child 25193 | 187a455af8f8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
21278
diff
changeset
|
2 |
* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package javax.swing.text; |
|
26 |
||
27 |
import java.awt.event.ActionEvent; |
|
28 |
import java.io.*; |
|
29 |
import java.text.*; |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
30 |
import java.text.AttributedCharacterIterator.Attribute; |
2 | 31 |
import java.util.*; |
32 |
import javax.swing.*; |
|
33 |
||
34 |
/** |
|
35 |
* <code>InternationalFormatter</code> extends <code>DefaultFormatter</code>, |
|
36 |
* using an instance of <code>java.text.Format</code> to handle the |
|
37 |
* conversion to a String, and the conversion from a String. |
|
38 |
* <p> |
|
39 |
* If <code>getAllowsInvalid()</code> is false, this will ask the |
|
40 |
* <code>Format</code> to format the current text on every edit. |
|
41 |
* <p> |
|
42 |
* You can specify a minimum and maximum value by way of the |
|
43 |
* <code>setMinimum</code> and <code>setMaximum</code> methods. In order |
|
44 |
* for this to work the values returned from <code>stringToValue</code> must be |
|
45 |
* comparable to the min/max values by way of the <code>Comparable</code> |
|
46 |
* interface. |
|
47 |
* <p> |
|
48 |
* Be careful how you configure the <code>Format</code> and the |
|
49 |
* <code>InternationalFormatter</code>, as it is possible to create a |
|
50 |
* situation where certain values can not be input. Consider the date |
|
51 |
* format 'M/d/yy', an <code>InternationalFormatter</code> that is always |
|
52 |
* valid (<code>setAllowsInvalid(false)</code>), is in overwrite mode |
|
53 |
* (<code>setOverwriteMode(true)</code>) and the date 7/1/99. In this |
|
54 |
* case the user will not be able to enter a two digit month or day of |
|
55 |
* month. To avoid this, the format should be 'MM/dd/yy'. |
|
56 |
* <p> |
|
57 |
* If <code>InternationalFormatter</code> is configured to only allow valid |
|
58 |
* values (<code>setAllowsInvalid(false)</code>), every valid edit will result |
|
59 |
* in the text of the <code>JFormattedTextField</code> being completely reset |
|
60 |
* from the <code>Format</code>. |
|
61 |
* The cursor position will also be adjusted as literal characters are |
|
62 |
* added/removed from the resulting String. |
|
63 |
* <p> |
|
64 |
* <code>InternationalFormatter</code>'s behavior of |
|
65 |
* <code>stringToValue</code> is slightly different than that of |
|
66 |
* <code>DefaultTextFormatter</code>, it does the following: |
|
67 |
* <ol> |
|
68 |
* <li><code>parseObject</code> is invoked on the <code>Format</code> |
|
69 |
* specified by <code>setFormat</code> |
|
70 |
* <li>If a Class has been set for the values (<code>setValueClass</code>), |
|
71 |
* supers implementation is invoked to convert the value returned |
|
72 |
* from <code>parseObject</code> to the appropriate class. |
|
73 |
* <li>If a <code>ParseException</code> has not been thrown, and the value |
|
74 |
* is outside the min/max a <code>ParseException</code> is thrown. |
|
75 |
* <li>The value is returned. |
|
76 |
* </ol> |
|
77 |
* <code>InternationalFormatter</code> implements <code>stringToValue</code> |
|
78 |
* in this manner so that you can specify an alternate Class than |
|
79 |
* <code>Format</code> may return. |
|
80 |
* <p> |
|
81 |
* <strong>Warning:</strong> |
|
82 |
* Serialized objects of this class will not be compatible with |
|
83 |
* future Swing releases. The current serialization support is |
|
84 |
* appropriate for short term storage or RMI between applications running |
|
85 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 86 |
* of all JavaBeans™ |
2 | 87 |
* has been added to the <code>java.beans</code> package. |
88 |
* Please see {@link java.beans.XMLEncoder}. |
|
89 |
* |
|
90 |
* @see java.text.Format |
|
91 |
* @see java.lang.Comparable |
|
92 |
* |
|
93 |
* @since 1.4 |
|
94 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
21278
diff
changeset
|
95 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 96 |
public class InternationalFormatter extends DefaultFormatter { |
97 |
/** |
|
98 |
* Used by <code>getFields</code>. |
|
99 |
*/ |
|
100 |
private static final Format.Field[] EMPTY_FIELD_ARRAY =new Format.Field[0]; |
|
101 |
||
102 |
/** |
|
103 |
* Object used to handle the conversion. |
|
104 |
*/ |
|
105 |
private Format format; |
|
106 |
/** |
|
107 |
* Can be used to impose a maximum value. |
|
108 |
*/ |
|
109 |
private Comparable max; |
|
110 |
/** |
|
111 |
* Can be used to impose a minimum value. |
|
112 |
*/ |
|
113 |
private Comparable min; |
|
114 |
||
115 |
/** |
|
116 |
* <code>InternationalFormatter</code>'s behavior is dicatated by a |
|
117 |
* <code>AttributedCharacterIterator</code> that is obtained from |
|
118 |
* the <code>Format</code>. On every edit, assuming |
|
119 |
* allows invalid is false, the <code>Format</code> instance is invoked |
|
120 |
* with <code>formatToCharacterIterator</code>. A <code>BitSet</code> is |
|
121 |
* also kept upto date with the non-literal characters, that is |
|
122 |
* for every index in the <code>AttributedCharacterIterator</code> an |
|
123 |
* entry in the bit set is updated based on the return value from |
|
124 |
* <code>isLiteral(Map)</code>. <code>isLiteral(int)</code> then uses |
|
125 |
* this cached information. |
|
126 |
* <p> |
|
127 |
* If allowsInvalid is false, every edit results in resetting the complete |
|
128 |
* text of the JTextComponent. |
|
129 |
* <p> |
|
130 |
* InternationalFormatterFilter can also provide two actions suitable for |
|
131 |
* incrementing and decrementing. To enable this a subclass must |
|
132 |
* override <code>getSupportsIncrement</code> to return true, and |
|
133 |
* override <code>adjustValue</code> to handle the changing of the |
|
134 |
* value. If you want to support changing the value outside of |
|
135 |
* the valid FieldPositions, you will need to override |
|
136 |
* <code>canIncrement</code>. |
|
137 |
*/ |
|
138 |
/** |
|
139 |
* A bit is set for every index identified in the |
|
140 |
* AttributedCharacterIterator that is not considered decoration. |
|
141 |
* This should only be used if validMask is true. |
|
142 |
*/ |
|
143 |
private transient BitSet literalMask; |
|
144 |
/** |
|
145 |
* Used to iterate over characters. |
|
146 |
*/ |
|
147 |
private transient AttributedCharacterIterator iterator; |
|
148 |
/** |
|
149 |
* True if the Format was able to convert the value to a String and |
|
150 |
* back. |
|
151 |
*/ |
|
152 |
private transient boolean validMask; |
|
153 |
/** |
|
154 |
* Current value being displayed. |
|
155 |
*/ |
|
156 |
private transient String string; |
|
157 |
/** |
|
158 |
* If true, DocumentFilter methods are unconditionally allowed, |
|
159 |
* and no checking is done on their values. This is used when |
|
160 |
* incrementing/decrementing via the actions. |
|
161 |
*/ |
|
162 |
private transient boolean ignoreDocumentMutate; |
|
163 |
||
164 |
||
165 |
/** |
|
166 |
* Creates an <code>InternationalFormatter</code> with no |
|
167 |
* <code>Format</code> specified. |
|
168 |
*/ |
|
169 |
public InternationalFormatter() { |
|
170 |
setOverwriteMode(false); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Creates an <code>InternationalFormatter</code> with the specified |
|
175 |
* <code>Format</code> instance. |
|
176 |
* |
|
177 |
* @param format Format instance used for converting from/to Strings |
|
178 |
*/ |
|
179 |
public InternationalFormatter(Format format) { |
|
180 |
this(); |
|
181 |
setFormat(format); |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* Sets the format that dictates the legal values that can be edited |
|
186 |
* and displayed. |
|
187 |
* |
|
188 |
* @param format <code>Format</code> instance used for converting |
|
189 |
* from/to Strings |
|
190 |
*/ |
|
191 |
public void setFormat(Format format) { |
|
192 |
this.format = format; |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* Returns the format that dictates the legal values that can be edited |
|
197 |
* and displayed. |
|
198 |
* |
|
199 |
* @return Format instance used for converting from/to Strings |
|
200 |
*/ |
|
201 |
public Format getFormat() { |
|
202 |
return format; |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Sets the minimum permissible value. If the <code>valueClass</code> has |
|
207 |
* not been specified, and <code>minimum</code> is non null, the |
|
208 |
* <code>valueClass</code> will be set to that of the class of |
|
209 |
* <code>minimum</code>. |
|
210 |
* |
|
211 |
* @param minimum Minimum legal value that can be input |
|
212 |
* @see #setValueClass |
|
213 |
*/ |
|
214 |
public void setMinimum(Comparable minimum) { |
|
215 |
if (getValueClass() == null && minimum != null) { |
|
216 |
setValueClass(minimum.getClass()); |
|
217 |
} |
|
218 |
min = minimum; |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Returns the minimum permissible value. |
|
223 |
* |
|
224 |
* @return Minimum legal value that can be input |
|
225 |
*/ |
|
226 |
public Comparable getMinimum() { |
|
227 |
return min; |
|
228 |
} |
|
229 |
||
230 |
/** |
|
231 |
* Sets the maximum permissible value. If the <code>valueClass</code> has |
|
232 |
* not been specified, and <code>max</code> is non null, the |
|
233 |
* <code>valueClass</code> will be set to that of the class of |
|
234 |
* <code>max</code>. |
|
235 |
* |
|
236 |
* @param max Maximum legal value that can be input |
|
237 |
* @see #setValueClass |
|
238 |
*/ |
|
239 |
public void setMaximum(Comparable max) { |
|
240 |
if (getValueClass() == null && max != null) { |
|
241 |
setValueClass(max.getClass()); |
|
242 |
} |
|
243 |
this.max = max; |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Returns the maximum permissible value. |
|
248 |
* |
|
249 |
* @return Maximum legal value that can be input |
|
250 |
*/ |
|
251 |
public Comparable getMaximum() { |
|
252 |
return max; |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* Installs the <code>DefaultFormatter</code> onto a particular |
|
257 |
* <code>JFormattedTextField</code>. |
|
258 |
* This will invoke <code>valueToString</code> to convert the |
|
259 |
* current value from the <code>JFormattedTextField</code> to |
|
260 |
* a String. This will then install the <code>Action</code>s from |
|
261 |
* <code>getActions</code>, the <code>DocumentFilter</code> |
|
262 |
* returned from <code>getDocumentFilter</code> and the |
|
263 |
* <code>NavigationFilter</code> returned from |
|
264 |
* <code>getNavigationFilter</code> onto the |
|
265 |
* <code>JFormattedTextField</code>. |
|
266 |
* <p> |
|
267 |
* Subclasses will typically only need to override this if they |
|
268 |
* wish to install additional listeners on the |
|
269 |
* <code>JFormattedTextField</code>. |
|
270 |
* <p> |
|
271 |
* If there is a <code>ParseException</code> in converting the |
|
272 |
* current value to a String, this will set the text to an empty |
|
273 |
* String, and mark the <code>JFormattedTextField</code> as being |
|
274 |
* in an invalid state. |
|
275 |
* <p> |
|
276 |
* While this is a public method, this is typically only useful |
|
277 |
* for subclassers of <code>JFormattedTextField</code>. |
|
278 |
* <code>JFormattedTextField</code> will invoke this method at |
|
279 |
* the appropriate times when the value changes, or its internal |
|
280 |
* state changes. |
|
281 |
* |
|
282 |
* @param ftf JFormattedTextField to format for, may be null indicating |
|
283 |
* uninstall from current JFormattedTextField. |
|
284 |
*/ |
|
285 |
public void install(JFormattedTextField ftf) { |
|
286 |
super.install(ftf); |
|
287 |
updateMaskIfNecessary(); |
|
288 |
// invoked again as the mask should now be valid. |
|
289 |
positionCursorAtInitialLocation(); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Returns a String representation of the Object <code>value</code>. |
|
294 |
* This invokes <code>format</code> on the current <code>Format</code>. |
|
295 |
* |
|
296 |
* @throws ParseException if there is an error in the conversion |
|
297 |
* @param value Value to convert |
|
298 |
* @return String representation of value |
|
299 |
*/ |
|
300 |
public String valueToString(Object value) throws ParseException { |
|
301 |
if (value == null) { |
|
302 |
return ""; |
|
303 |
} |
|
304 |
Format f = getFormat(); |
|
305 |
||
306 |
if (f == null) { |
|
307 |
return value.toString(); |
|
308 |
} |
|
309 |
return f.format(value); |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Returns the <code>Object</code> representation of the |
|
314 |
* <code>String</code> <code>text</code>. |
|
315 |
* |
|
316 |
* @param text <code>String</code> to convert |
|
317 |
* @return <code>Object</code> representation of text |
|
318 |
* @throws ParseException if there is an error in the conversion |
|
319 |
*/ |
|
320 |
public Object stringToValue(String text) throws ParseException { |
|
321 |
Object value = stringToValue(text, getFormat()); |
|
322 |
||
323 |
// Convert to the value class if the Value returned from the |
|
324 |
// Format does not match. |
|
325 |
if (value != null && getValueClass() != null && |
|
326 |
!getValueClass().isInstance(value)) { |
|
327 |
value = super.stringToValue(value.toString()); |
|
328 |
} |
|
329 |
try { |
|
330 |
if (!isValidValue(value, true)) { |
|
331 |
throw new ParseException("Value not within min/max range", 0); |
|
332 |
} |
|
333 |
} catch (ClassCastException cce) { |
|
334 |
throw new ParseException("Class cast exception comparing values: " |
|
335 |
+ cce, 0); |
|
336 |
} |
|
337 |
return value; |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Returns the <code>Format.Field</code> constants associated with |
|
342 |
* the text at <code>offset</code>. If <code>offset</code> is not |
|
343 |
* a valid location into the current text, this will return an |
|
344 |
* empty array. |
|
345 |
* |
|
346 |
* @param offset offset into text to be examined |
|
347 |
* @return Format.Field constants associated with the text at the |
|
348 |
* given position. |
|
349 |
*/ |
|
350 |
public Format.Field[] getFields(int offset) { |
|
351 |
if (getAllowsInvalid()) { |
|
352 |
// This will work if the currently edited value is valid. |
|
353 |
updateMask(); |
|
354 |
} |
|
355 |
||
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
356 |
Map<Attribute, Object> attrs = getAttributes(offset); |
2 | 357 |
|
358 |
if (attrs != null && attrs.size() > 0) { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
359 |
ArrayList<Attribute> al = new ArrayList<Attribute>(); |
2 | 360 |
|
361 |
al.addAll(attrs.keySet()); |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
362 |
return al.toArray(EMPTY_FIELD_ARRAY); |
2 | 363 |
} |
364 |
return EMPTY_FIELD_ARRAY; |
|
365 |
} |
|
366 |
||
367 |
/** |
|
368 |
* Creates a copy of the DefaultFormatter. |
|
369 |
* |
|
370 |
* @return copy of the DefaultFormatter |
|
371 |
*/ |
|
372 |
public Object clone() throws CloneNotSupportedException { |
|
373 |
InternationalFormatter formatter = (InternationalFormatter)super. |
|
374 |
clone(); |
|
375 |
||
376 |
formatter.literalMask = null; |
|
377 |
formatter.iterator = null; |
|
378 |
formatter.validMask = false; |
|
379 |
formatter.string = null; |
|
380 |
return formatter; |
|
381 |
} |
|
382 |
||
383 |
/** |
|
384 |
* If <code>getSupportsIncrement</code> returns true, this returns |
|
385 |
* two Actions suitable for incrementing/decrementing the value. |
|
386 |
*/ |
|
387 |
protected Action[] getActions() { |
|
388 |
if (getSupportsIncrement()) { |
|
389 |
return new Action[] { new IncrementAction("increment", 1), |
|
390 |
new IncrementAction("decrement", -1) }; |
|
391 |
} |
|
392 |
return null; |
|
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* Invokes <code>parseObject</code> on <code>f</code>, returning |
|
397 |
* its value. |
|
398 |
*/ |
|
399 |
Object stringToValue(String text, Format f) throws ParseException { |
|
400 |
if (f == null) { |
|
401 |
return text; |
|
402 |
} |
|
403 |
return f.parseObject(text); |
|
404 |
} |
|
405 |
||
406 |
/** |
|
407 |
* Returns true if <code>value</code> is between the min/max. |
|
408 |
* |
|
409 |
* @param wantsCCE If false, and a ClassCastException is thrown in |
|
410 |
* comparing the values, the exception is consumed and |
|
411 |
* false is returned. |
|
412 |
*/ |
|
413 |
boolean isValidValue(Object value, boolean wantsCCE) { |
|
414 |
Comparable min = getMinimum(); |
|
415 |
||
416 |
try { |
|
417 |
if (min != null && min.compareTo(value) > 0) { |
|
418 |
return false; |
|
419 |
} |
|
420 |
} catch (ClassCastException cce) { |
|
421 |
if (wantsCCE) { |
|
422 |
throw cce; |
|
423 |
} |
|
424 |
return false; |
|
425 |
} |
|
426 |
||
427 |
Comparable max = getMaximum(); |
|
428 |
try { |
|
429 |
if (max != null && max.compareTo(value) < 0) { |
|
430 |
return false; |
|
431 |
} |
|
432 |
} catch (ClassCastException cce) { |
|
433 |
if (wantsCCE) { |
|
434 |
throw cce; |
|
435 |
} |
|
436 |
return false; |
|
437 |
} |
|
438 |
return true; |
|
439 |
} |
|
440 |
||
441 |
/** |
|
442 |
* Returns a Set of the attribute identifiers at <code>index</code>. |
|
443 |
*/ |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
444 |
Map<Attribute, Object> getAttributes(int index) { |
2 | 445 |
if (isValidMask()) { |
446 |
AttributedCharacterIterator iterator = getIterator(); |
|
447 |
||
448 |
if (index >= 0 && index <= iterator.getEndIndex()) { |
|
449 |
iterator.setIndex(index); |
|
450 |
return iterator.getAttributes(); |
|
451 |
} |
|
452 |
} |
|
453 |
return null; |
|
454 |
} |
|
455 |
||
456 |
||
457 |
/** |
|
458 |
* Returns the start of the first run that contains the attribute |
|
459 |
* <code>id</code>. This will return <code>-1</code> if the attribute |
|
460 |
* can not be found. |
|
461 |
*/ |
|
462 |
int getAttributeStart(AttributedCharacterIterator.Attribute id) { |
|
463 |
if (isValidMask()) { |
|
464 |
AttributedCharacterIterator iterator = getIterator(); |
|
465 |
||
466 |
iterator.first(); |
|
467 |
while (iterator.current() != CharacterIterator.DONE) { |
|
468 |
if (iterator.getAttribute(id) != null) { |
|
469 |
return iterator.getIndex(); |
|
470 |
} |
|
471 |
iterator.next(); |
|
472 |
} |
|
473 |
} |
|
474 |
return -1; |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* Returns the <code>AttributedCharacterIterator</code> used to |
|
479 |
* format the last value. |
|
480 |
*/ |
|
481 |
AttributedCharacterIterator getIterator() { |
|
482 |
return iterator; |
|
483 |
} |
|
484 |
||
485 |
/** |
|
486 |
* Updates the AttributedCharacterIterator and bitset, if necessary. |
|
487 |
*/ |
|
488 |
void updateMaskIfNecessary() { |
|
489 |
if (!getAllowsInvalid() && (getFormat() != null)) { |
|
490 |
if (!isValidMask()) { |
|
491 |
updateMask(); |
|
492 |
} |
|
493 |
else { |
|
494 |
String newString = getFormattedTextField().getText(); |
|
495 |
||
496 |
if (!newString.equals(string)) { |
|
497 |
updateMask(); |
|
498 |
} |
|
499 |
} |
|
500 |
} |
|
501 |
} |
|
502 |
||
503 |
/** |
|
504 |
* Updates the AttributedCharacterIterator by invoking |
|
505 |
* <code>formatToCharacterIterator</code> on the <code>Format</code>. |
|
506 |
* If this is successful, |
|
507 |
* <code>updateMask(AttributedCharacterIterator)</code> |
|
508 |
* is then invoked to update the internal bitmask. |
|
509 |
*/ |
|
510 |
void updateMask() { |
|
511 |
if (getFormat() != null) { |
|
512 |
Document doc = getFormattedTextField().getDocument(); |
|
513 |
||
514 |
validMask = false; |
|
515 |
if (doc != null) { |
|
516 |
try { |
|
517 |
string = doc.getText(0, doc.getLength()); |
|
518 |
} catch (BadLocationException ble) { |
|
519 |
string = null; |
|
520 |
} |
|
521 |
if (string != null) { |
|
522 |
try { |
|
523 |
Object value = stringToValue(string); |
|
524 |
AttributedCharacterIterator iterator = getFormat(). |
|
525 |
formatToCharacterIterator(value); |
|
526 |
||
527 |
updateMask(iterator); |
|
528 |
} |
|
529 |
catch (ParseException pe) {} |
|
530 |
catch (IllegalArgumentException iae) {} |
|
531 |
catch (NullPointerException npe) {} |
|
532 |
} |
|
533 |
} |
|
534 |
} |
|
535 |
} |
|
536 |
||
537 |
/** |
|
538 |
* Returns the number of literal characters before <code>index</code>. |
|
539 |
*/ |
|
540 |
int getLiteralCountTo(int index) { |
|
541 |
int lCount = 0; |
|
542 |
||
543 |
for (int counter = 0; counter < index; counter++) { |
|
544 |
if (isLiteral(counter)) { |
|
545 |
lCount++; |
|
546 |
} |
|
547 |
} |
|
548 |
return lCount; |
|
549 |
} |
|
550 |
||
551 |
/** |
|
552 |
* Returns true if the character at index is a literal, that is |
|
553 |
* not editable. |
|
554 |
*/ |
|
555 |
boolean isLiteral(int index) { |
|
556 |
if (isValidMask() && index < string.length()) { |
|
557 |
return literalMask.get(index); |
|
558 |
} |
|
559 |
return false; |
|
560 |
} |
|
561 |
||
562 |
/** |
|
563 |
* Returns the literal character at index. |
|
564 |
*/ |
|
565 |
char getLiteral(int index) { |
|
566 |
if (isValidMask() && string != null && index < string.length()) { |
|
567 |
return string.charAt(index); |
|
568 |
} |
|
569 |
return (char)0; |
|
570 |
} |
|
571 |
||
572 |
/** |
|
21278 | 573 |
* Returns true if the character at offset is navigable too. This |
2 | 574 |
* is implemented in terms of <code>isLiteral</code>, subclasses |
575 |
* may wish to provide different behavior. |
|
576 |
*/ |
|
577 |
boolean isNavigatable(int offset) { |
|
578 |
return !isLiteral(offset); |
|
579 |
} |
|
580 |
||
581 |
/** |
|
582 |
* Overriden to update the mask after invoking supers implementation. |
|
583 |
*/ |
|
584 |
void updateValue(Object value) { |
|
585 |
super.updateValue(value); |
|
586 |
updateMaskIfNecessary(); |
|
587 |
} |
|
588 |
||
589 |
/** |
|
590 |
* Overriden to unconditionally allow the replace if |
|
591 |
* ignoreDocumentMutate is true. |
|
592 |
*/ |
|
593 |
void replace(DocumentFilter.FilterBypass fb, int offset, |
|
594 |
int length, String text, |
|
595 |
AttributeSet attrs) throws BadLocationException { |
|
596 |
if (ignoreDocumentMutate) { |
|
597 |
fb.replace(offset, length, text, attrs); |
|
598 |
return; |
|
599 |
} |
|
600 |
super.replace(fb, offset, length, text, attrs); |
|
601 |
} |
|
602 |
||
603 |
/** |
|
604 |
* Returns the index of the next non-literal character starting at |
|
605 |
* index. If index is not a literal, it will be returned. |
|
606 |
* |
|
607 |
* @param direction Amount to increment looking for non-literal |
|
608 |
*/ |
|
609 |
private int getNextNonliteralIndex(int index, int direction) { |
|
610 |
int max = getFormattedTextField().getDocument().getLength(); |
|
611 |
||
612 |
while (index >= 0 && index < max) { |
|
613 |
if (isLiteral(index)) { |
|
614 |
index += direction; |
|
615 |
} |
|
616 |
else { |
|
617 |
return index; |
|
618 |
} |
|
619 |
} |
|
620 |
return (direction == -1) ? 0 : max; |
|
621 |
} |
|
622 |
||
623 |
/** |
|
624 |
* Overriden in an attempt to honor the literals. |
|
6102
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
625 |
* <p>If we do not allow invalid values and are in overwrite mode, this |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
626 |
* {@code rh.length} is corrected as to preserve trailing literals. |
2 | 627 |
* If not in overwrite mode, and there is text to insert it is |
628 |
* inserted at the next non literal index going forward. If there |
|
629 |
* is only text to remove, it is removed from the next non literal |
|
630 |
* index going backward. |
|
631 |
*/ |
|
632 |
boolean canReplace(ReplaceHolder rh) { |
|
633 |
if (!getAllowsInvalid()) { |
|
634 |
String text = rh.text; |
|
635 |
int tl = (text != null) ? text.length() : 0; |
|
6102
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
636 |
JTextComponent c = getFormattedTextField(); |
2 | 637 |
|
6102
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
638 |
if (tl == 0 && rh.length == 1 && c.getSelectionStart() != rh.offset) { |
2 | 639 |
// Backspace, adjust to actually delete next non-literal. |
640 |
rh.offset = getNextNonliteralIndex(rh.offset, -1); |
|
6102
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
641 |
} else if (getOverwriteMode()) { |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
642 |
int pos = rh.offset; |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
643 |
int textPos = pos; |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
644 |
boolean overflown = false; |
2 | 645 |
|
6102
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
646 |
for (int i = 0; i < rh.length; i++) { |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
647 |
while (isLiteral(pos)) pos++; |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
648 |
if (pos >= string.length()) { |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
649 |
pos = textPos; |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
650 |
overflown = true; |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
651 |
break; |
2 | 652 |
} |
6102
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
653 |
textPos = ++pos; |
2 | 654 |
} |
6102
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
655 |
if (overflown || c.getSelectedText() == null) { |
06e0e43b3f09
6462562: InternationalFormatter inserts text incorrectly
peterz
parents:
5506
diff
changeset
|
656 |
rh.length = pos - rh.offset; |
2 | 657 |
} |
658 |
} |
|
659 |
else if (tl > 0) { |
|
660 |
// insert (or insert and remove) |
|
661 |
rh.offset = getNextNonliteralIndex(rh.offset, 1); |
|
662 |
} |
|
663 |
else { |
|
664 |
// remove only |
|
665 |
rh.offset = getNextNonliteralIndex(rh.offset, -1); |
|
666 |
} |
|
667 |
((ExtendedReplaceHolder)rh).endOffset = rh.offset; |
|
668 |
((ExtendedReplaceHolder)rh).endTextLength = (rh.text != null) ? |
|
669 |
rh.text.length() : 0; |
|
670 |
} |
|
671 |
else { |
|
672 |
((ExtendedReplaceHolder)rh).endOffset = rh.offset; |
|
673 |
((ExtendedReplaceHolder)rh).endTextLength = (rh.text != null) ? |
|
674 |
rh.text.length() : 0; |
|
675 |
} |
|
676 |
boolean can = super.canReplace(rh); |
|
677 |
if (can && !getAllowsInvalid()) { |
|
678 |
((ExtendedReplaceHolder)rh).resetFromValue(this); |
|
679 |
} |
|
680 |
return can; |
|
681 |
} |
|
682 |
||
683 |
/** |
|
684 |
* When in !allowsInvalid mode the text is reset on every edit, thus |
|
685 |
* supers implementation will position the cursor at the wrong position. |
|
686 |
* As such, this invokes supers implementation and then invokes |
|
687 |
* <code>repositionCursor</code> to correctly reset the cursor. |
|
688 |
*/ |
|
689 |
boolean replace(ReplaceHolder rh) throws BadLocationException { |
|
690 |
int start = -1; |
|
691 |
int direction = 1; |
|
692 |
int literalCount = -1; |
|
693 |
||
694 |
if (rh.length > 0 && (rh.text == null || rh.text.length() == 0) && |
|
695 |
(getFormattedTextField().getSelectionStart() != rh.offset || |
|
696 |
rh.length > 1)) { |
|
697 |
direction = -1; |
|
698 |
} |
|
699 |
if (!getAllowsInvalid()) { |
|
700 |
if ((rh.text == null || rh.text.length() == 0) && rh.length > 0) { |
|
701 |
// remove |
|
702 |
start = getFormattedTextField().getSelectionStart(); |
|
703 |
} |
|
704 |
else { |
|
705 |
start = rh.offset; |
|
706 |
} |
|
707 |
literalCount = getLiteralCountTo(start); |
|
708 |
} |
|
709 |
if (super.replace(rh)) { |
|
710 |
if (start != -1) { |
|
711 |
int end = ((ExtendedReplaceHolder)rh).endOffset; |
|
712 |
||
713 |
end += ((ExtendedReplaceHolder)rh).endTextLength; |
|
714 |
repositionCursor(literalCount, end, direction); |
|
715 |
} |
|
716 |
else { |
|
717 |
start = ((ExtendedReplaceHolder)rh).endOffset; |
|
718 |
if (direction == 1) { |
|
719 |
start += ((ExtendedReplaceHolder)rh).endTextLength; |
|
720 |
} |
|
721 |
repositionCursor(start, direction); |
|
722 |
} |
|
723 |
return true; |
|
724 |
} |
|
725 |
return false; |
|
726 |
} |
|
727 |
||
728 |
/** |
|
729 |
* Repositions the cursor. <code>startLiteralCount</code> gives |
|
730 |
* the number of literals to the start of the deleted range, end |
|
731 |
* gives the ending location to adjust from, direction gives |
|
732 |
* the direction relative to <code>end</code> to position the |
|
733 |
* cursor from. |
|
734 |
*/ |
|
735 |
private void repositionCursor(int startLiteralCount, int end, |
|
736 |
int direction) { |
|
737 |
int endLiteralCount = getLiteralCountTo(end); |
|
738 |
||
739 |
if (endLiteralCount != end) { |
|
740 |
end -= startLiteralCount; |
|
741 |
for (int counter = 0; counter < end; counter++) { |
|
742 |
if (isLiteral(counter)) { |
|
743 |
end++; |
|
744 |
} |
|
745 |
} |
|
746 |
} |
|
747 |
repositionCursor(end, 1 /*direction*/); |
|
748 |
} |
|
749 |
||
750 |
/** |
|
751 |
* Returns the character from the mask that has been buffered |
|
752 |
* at <code>index</code>. |
|
753 |
*/ |
|
754 |
char getBufferedChar(int index) { |
|
755 |
if (isValidMask()) { |
|
756 |
if (string != null && index < string.length()) { |
|
757 |
return string.charAt(index); |
|
758 |
} |
|
759 |
} |
|
760 |
return (char)0; |
|
761 |
} |
|
762 |
||
763 |
/** |
|
764 |
* Returns true if the current mask is valid. |
|
765 |
*/ |
|
766 |
boolean isValidMask() { |
|
767 |
return validMask; |
|
768 |
} |
|
769 |
||
770 |
/** |
|
771 |
* Returns true if <code>attributes</code> is null or empty. |
|
772 |
*/ |
|
773 |
boolean isLiteral(Map attributes) { |
|
774 |
return ((attributes == null) || attributes.size() == 0); |
|
775 |
} |
|
776 |
||
777 |
/** |
|
778 |
* Updates the interal bitset from <code>iterator</code>. This will |
|
779 |
* set <code>validMask</code> to true if <code>iterator</code> is |
|
780 |
* non-null. |
|
781 |
*/ |
|
782 |
private void updateMask(AttributedCharacterIterator iterator) { |
|
783 |
if (iterator != null) { |
|
784 |
validMask = true; |
|
785 |
this.iterator = iterator; |
|
786 |
||
787 |
// Update the literal mask |
|
788 |
if (literalMask == null) { |
|
789 |
literalMask = new BitSet(); |
|
790 |
} |
|
791 |
else { |
|
792 |
for (int counter = literalMask.length() - 1; counter >= 0; |
|
793 |
counter--) { |
|
794 |
literalMask.clear(counter); |
|
795 |
} |
|
796 |
} |
|
797 |
||
798 |
iterator.first(); |
|
799 |
while (iterator.current() != CharacterIterator.DONE) { |
|
800 |
Map attributes = iterator.getAttributes(); |
|
801 |
boolean set = isLiteral(attributes); |
|
802 |
int start = iterator.getIndex(); |
|
803 |
int end = iterator.getRunLimit(); |
|
804 |
||
805 |
while (start < end) { |
|
806 |
if (set) { |
|
807 |
literalMask.set(start); |
|
808 |
} |
|
809 |
else { |
|
810 |
literalMask.clear(start); |
|
811 |
} |
|
812 |
start++; |
|
813 |
} |
|
814 |
iterator.setIndex(start); |
|
815 |
} |
|
816 |
} |
|
817 |
} |
|
818 |
||
819 |
/** |
|
820 |
* Returns true if <code>field</code> is non-null. |
|
821 |
* Subclasses that wish to allow incrementing to happen outside of |
|
822 |
* the known fields will need to override this. |
|
823 |
*/ |
|
824 |
boolean canIncrement(Object field, int cursorPosition) { |
|
825 |
return (field != null); |
|
826 |
} |
|
827 |
||
828 |
/** |
|
829 |
* Selects the fields identified by <code>attributes</code>. |
|
830 |
*/ |
|
831 |
void selectField(Object f, int count) { |
|
832 |
AttributedCharacterIterator iterator = getIterator(); |
|
833 |
||
834 |
if (iterator != null && |
|
835 |
(f instanceof AttributedCharacterIterator.Attribute)) { |
|
836 |
AttributedCharacterIterator.Attribute field = |
|
837 |
(AttributedCharacterIterator.Attribute)f; |
|
838 |
||
839 |
iterator.first(); |
|
840 |
while (iterator.current() != CharacterIterator.DONE) { |
|
841 |
while (iterator.getAttribute(field) == null && |
|
842 |
iterator.next() != CharacterIterator.DONE); |
|
843 |
if (iterator.current() != CharacterIterator.DONE) { |
|
844 |
int limit = iterator.getRunLimit(field); |
|
845 |
||
846 |
if (--count <= 0) { |
|
847 |
getFormattedTextField().select(iterator.getIndex(), |
|
848 |
limit); |
|
849 |
break; |
|
850 |
} |
|
851 |
iterator.setIndex(limit); |
|
852 |
iterator.next(); |
|
853 |
} |
|
854 |
} |
|
855 |
} |
|
856 |
} |
|
857 |
||
858 |
/** |
|
859 |
* Returns the field that will be adjusted by adjustValue. |
|
860 |
*/ |
|
861 |
Object getAdjustField(int start, Map attributes) { |
|
862 |
return null; |
|
863 |
} |
|
864 |
||
865 |
/** |
|
21278 | 866 |
* Returns the number of occurrences of <code>f</code> before |
2 | 867 |
* the location <code>start</code> in the current |
868 |
* <code>AttributedCharacterIterator</code>. |
|
869 |
*/ |
|
870 |
private int getFieldTypeCountTo(Object f, int start) { |
|
871 |
AttributedCharacterIterator iterator = getIterator(); |
|
872 |
int count = 0; |
|
873 |
||
874 |
if (iterator != null && |
|
875 |
(f instanceof AttributedCharacterIterator.Attribute)) { |
|
876 |
AttributedCharacterIterator.Attribute field = |
|
877 |
(AttributedCharacterIterator.Attribute)f; |
|
878 |
||
879 |
iterator.first(); |
|
880 |
while (iterator.getIndex() < start) { |
|
881 |
while (iterator.getAttribute(field) == null && |
|
882 |
iterator.next() != CharacterIterator.DONE); |
|
883 |
if (iterator.current() != CharacterIterator.DONE) { |
|
884 |
iterator.setIndex(iterator.getRunLimit(field)); |
|
885 |
iterator.next(); |
|
886 |
count++; |
|
887 |
} |
|
888 |
else { |
|
889 |
break; |
|
890 |
} |
|
891 |
} |
|
892 |
} |
|
893 |
return count; |
|
894 |
} |
|
895 |
||
896 |
/** |
|
897 |
* Subclasses supporting incrementing must override this to handle |
|
898 |
* the actual incrementing. <code>value</code> is the current value, |
|
899 |
* <code>attributes</code> gives the field the cursor is in (may be |
|
900 |
* null depending upon <code>canIncrement</code>) and |
|
901 |
* <code>direction</code> is the amount to increment by. |
|
902 |
*/ |
|
903 |
Object adjustValue(Object value, Map attributes, Object field, |
|
904 |
int direction) throws |
|
905 |
BadLocationException, ParseException { |
|
906 |
return null; |
|
907 |
} |
|
908 |
||
909 |
/** |
|
910 |
* Returns false, indicating InternationalFormatter does not allow |
|
911 |
* incrementing of the value. Subclasses that wish to support |
|
912 |
* incrementing/decrementing the value should override this and |
|
913 |
* return true. Subclasses should also override |
|
914 |
* <code>adjustValue</code>. |
|
915 |
*/ |
|
916 |
boolean getSupportsIncrement() { |
|
917 |
return false; |
|
918 |
} |
|
919 |
||
920 |
/** |
|
921 |
* Resets the value of the JFormattedTextField to be |
|
922 |
* <code>value</code>. |
|
923 |
*/ |
|
924 |
void resetValue(Object value) throws BadLocationException, ParseException { |
|
925 |
Document doc = getFormattedTextField().getDocument(); |
|
926 |
String string = valueToString(value); |
|
927 |
||
928 |
try { |
|
929 |
ignoreDocumentMutate = true; |
|
930 |
doc.remove(0, doc.getLength()); |
|
931 |
doc.insertString(0, string, null); |
|
932 |
} finally { |
|
933 |
ignoreDocumentMutate = false; |
|
934 |
} |
|
935 |
updateValue(value); |
|
936 |
} |
|
937 |
||
938 |
/** |
|
939 |
* Subclassed to update the internal representation of the mask after |
|
940 |
* the default read operation has completed. |
|
941 |
*/ |
|
942 |
private void readObject(ObjectInputStream s) |
|
943 |
throws IOException, ClassNotFoundException { |
|
944 |
s.defaultReadObject(); |
|
945 |
updateMaskIfNecessary(); |
|
946 |
} |
|
947 |
||
948 |
||
949 |
/** |
|
950 |
* Overriden to return an instance of <code>ExtendedReplaceHolder</code>. |
|
951 |
*/ |
|
952 |
ReplaceHolder getReplaceHolder(DocumentFilter.FilterBypass fb, int offset, |
|
953 |
int length, String text, |
|
954 |
AttributeSet attrs) { |
|
955 |
if (replaceHolder == null) { |
|
956 |
replaceHolder = new ExtendedReplaceHolder(); |
|
957 |
} |
|
958 |
return super.getReplaceHolder(fb, offset, length, text, attrs); |
|
959 |
} |
|
960 |
||
961 |
||
962 |
/** |
|
963 |
* As InternationalFormatter replaces the complete text on every edit, |
|
964 |
* ExtendedReplaceHolder keeps track of the offset and length passed |
|
965 |
* into canReplace. |
|
966 |
*/ |
|
967 |
static class ExtendedReplaceHolder extends ReplaceHolder { |
|
968 |
/** Offset of the insert/remove. This may differ from offset in |
|
969 |
* that if !allowsInvalid the text is replaced on every edit. */ |
|
970 |
int endOffset; |
|
971 |
/** Length of the text. This may differ from text.length in |
|
972 |
* that if !allowsInvalid the text is replaced on every edit. */ |
|
973 |
int endTextLength; |
|
974 |
||
975 |
/** |
|
976 |
* Resets the region to delete to be the complete document and |
|
977 |
* the text from invoking valueToString on the current value. |
|
978 |
*/ |
|
979 |
void resetFromValue(InternationalFormatter formatter) { |
|
980 |
// Need to reset the complete string as Format's result can |
|
981 |
// be completely different. |
|
982 |
offset = 0; |
|
983 |
try { |
|
984 |
text = formatter.valueToString(value); |
|
985 |
} catch (ParseException pe) { |
|
986 |
// Should never happen, otherwise canReplace would have |
|
987 |
// returned value. |
|
988 |
text = ""; |
|
989 |
} |
|
990 |
length = fb.getDocument().getLength(); |
|
991 |
} |
|
992 |
} |
|
993 |
||
994 |
||
995 |
/** |
|
996 |
* IncrementAction is used to increment the value by a certain amount. |
|
997 |
* It calls into <code>adjustValue</code> to handle the actual |
|
998 |
* incrementing of the value. |
|
999 |
*/ |
|
1000 |
private class IncrementAction extends AbstractAction { |
|
1001 |
private int direction; |
|
1002 |
||
1003 |
IncrementAction(String name, int direction) { |
|
1004 |
super(name); |
|
1005 |
this.direction = direction; |
|
1006 |
} |
|
1007 |
||
1008 |
public void actionPerformed(ActionEvent ae) { |
|
1009 |
||
1010 |
if (getFormattedTextField().isEditable()) { |
|
1011 |
if (getAllowsInvalid()) { |
|
1012 |
// This will work if the currently edited value is valid. |
|
1013 |
updateMask(); |
|
1014 |
} |
|
1015 |
||
1016 |
boolean validEdit = false; |
|
1017 |
||
1018 |
if (isValidMask()) { |
|
1019 |
int start = getFormattedTextField().getSelectionStart(); |
|
1020 |
||
1021 |
if (start != -1) { |
|
1022 |
AttributedCharacterIterator iterator = getIterator(); |
|
1023 |
||
1024 |
iterator.setIndex(start); |
|
1025 |
||
1026 |
Map attributes = iterator.getAttributes(); |
|
1027 |
Object field = getAdjustField(start, attributes); |
|
1028 |
||
1029 |
if (canIncrement(field, start)) { |
|
1030 |
try { |
|
1031 |
Object value = stringToValue( |
|
1032 |
getFormattedTextField().getText()); |
|
1033 |
int fieldTypeCount = getFieldTypeCountTo( |
|
1034 |
field, start); |
|
1035 |
||
1036 |
value = adjustValue(value, attributes, |
|
1037 |
field, direction); |
|
1038 |
if (value != null && isValidValue(value, false)) { |
|
1039 |
resetValue(value); |
|
1040 |
updateMask(); |
|
1041 |
||
1042 |
if (isValidMask()) { |
|
1043 |
selectField(field, fieldTypeCount); |
|
1044 |
} |
|
1045 |
validEdit = true; |
|
1046 |
} |
|
1047 |
} |
|
1048 |
catch (ParseException pe) { } |
|
1049 |
catch (BadLocationException ble) { } |
|
1050 |
} |
|
1051 |
} |
|
1052 |
} |
|
1053 |
if (!validEdit) { |
|
1054 |
invalidEdit(); |
|
1055 |
} |
|
1056 |
} |
|
1057 |
} |
|
1058 |
} |
|
1059 |
} |