e4b56efa386c44abd455ac25bbb7669b56f19def
[jalviewjs.git] / site / swingjs / j2s / javax / swing / text / InternationalFormatter.js
1 Clazz.declarePackage ("javax.swing.text");
2 Clazz.load (["javax.swing.AbstractAction", "javax.swing.text.DefaultFormatter"], "javax.swing.text.InternationalFormatter", ["java.lang.StringBuffer", "java.util.ArrayList", "$.BitSet", "java.text.AttributedCharacterIterator.Attribute", "java.text.ParseException"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.format = null;
5 this.max = null;
6 this.min = null;
7 this.literalMask = null;
8 this.iterator = null;
9 this.validMask = false;
10 this.string = null;
11 this.ignoreDocumentMutate = false;
12 if (!Clazz.isClassDefined ("javax.swing.text.InternationalFormatter.IncrementAction")) {
13 javax.swing.text.InternationalFormatter.$InternationalFormatter$IncrementAction$ ();
14 }
15 Clazz.instantialize (this, arguments);
16 }, javax.swing.text, "InternationalFormatter", javax.swing.text.DefaultFormatter);
17 Clazz.makeConstructor (c$, 
18 function () {
19 Clazz.superConstructor (this, javax.swing.text.InternationalFormatter, []);
20 this.setOverwriteMode (false);
21 });
22 Clazz.makeConstructor (c$, 
23 function (format) {
24 this.construct ();
25 this.setFormat (format);
26 }, "java.text.Format");
27 Clazz.defineMethod (c$, "setFormat", 
28 function (format) {
29 this.format = format;
30 }, "java.text.Format");
31 Clazz.defineMethod (c$, "getFormat", 
32 function () {
33 return this.format;
34 });
35 Clazz.defineMethod (c$, "setMinimum", 
36 function (minimum) {
37 if (this.getValueClass () == null && minimum != null) {
38 this.setValueClass (minimum.getClass ());
39 }this.min = minimum;
40 }, "Comparable");
41 Clazz.defineMethod (c$, "getMinimum", 
42 function () {
43 return this.min;
44 });
45 Clazz.defineMethod (c$, "setMaximum", 
46 function (max) {
47 if (this.getValueClass () == null && max != null) {
48 this.setValueClass (max.getClass ());
49 }this.max = max;
50 }, "Comparable");
51 Clazz.defineMethod (c$, "getMaximum", 
52 function () {
53 return this.max;
54 });
55 Clazz.defineMethod (c$, "install", 
56 function (ftf) {
57 Clazz.superCall (this, javax.swing.text.InternationalFormatter, "install", [ftf]);
58 this.updateMaskIfNecessary ();
59 this.positionCursorAtInitialLocation ();
60 }, "javax.swing.JFormattedTextField");
61 Clazz.overrideMethod (c$, "valueToString", 
62 function (value) {
63 if (value == null) {
64 return "";
65 }var f = this.getFormat ();
66 if (f == null) {
67 return value.toString ();
68 }return f.format (value);
69 }, "~O");
70 Clazz.defineMethod (c$, "stringToValue", 
71 function (text) {
72 var value = this.stringToValueParse (text, this.getFormat ());
73 if (value != null && this.getValueClass () != null && !this.getValueClass ().isInstance (value)) {
74 value = Clazz.superCall (this, javax.swing.text.InternationalFormatter, "stringToValue", [value.toString ()]);
75 }try {
76 if (!this.isValidValue (value, true)) {
77 throw  new java.text.ParseException ("Value not within min/max range", 0);
78 }} catch (cce) {
79 if (Clazz.exceptionOf (cce, ClassCastException)) {
80 throw  new java.text.ParseException ("Class cast exception comparing values: " + cce, 0);
81 } else {
82 throw cce;
83 }
84 }
85 return value;
86 }, "~S");
87 Clazz.defineMethod (c$, "getFields", 
88 function (offset) {
89 if (this.getAllowsInvalid ()) {
90 this.updateMask ();
91 }var attrs = this.getAttributes (offset);
92 if (attrs != null && attrs.size () > 0) {
93 var al =  new java.util.ArrayList ();
94 al.addAll (attrs.keySet ());
95 return al.toArray (javax.swing.text.InternationalFormatter.EMPTY_FIELD_ARRAY);
96 }return javax.swing.text.InternationalFormatter.EMPTY_FIELD_ARRAY;
97 }, "~N");
98 Clazz.defineMethod (c$, "clone", 
99 function () {
100 var formatter = Clazz.superCall (this, javax.swing.text.InternationalFormatter, "clone", []);
101 formatter.literalMask = null;
102 formatter.iterator = null;
103 formatter.validMask = false;
104 formatter.string = null;
105 return formatter;
106 });
107 Clazz.overrideMethod (c$, "getActions", 
108 function () {
109 if (this.getSupportsIncrement ()) {
110 return  Clazz.newArray (-1, [Clazz.innerTypeInstance (javax.swing.text.InternationalFormatter.IncrementAction, this, null, "increment", 1), Clazz.innerTypeInstance (javax.swing.text.InternationalFormatter.IncrementAction, this, null, "decrement", -1)]);
111 }return null;
112 });
113 Clazz.defineMethod (c$, "stringToValueParse", 
114 function (text, f) {
115 if (f == null) {
116 return text;
117 }return f.parseObject (text);
118 }, "~S,java.text.Format");
119 Clazz.defineMethod (c$, "isValidValue", 
120 function (value, wantsCCE) {
121 var min = this.getMinimum ();
122 try {
123 if (min != null && min.compareTo (value) > 0) {
124 return false;
125 }} catch (cce) {
126 if (Clazz.exceptionOf (cce, ClassCastException)) {
127 if (wantsCCE) {
128 throw cce;
129 }return false;
130 } else {
131 throw cce;
132 }
133 }
134 var max = this.getMaximum ();
135 try {
136 if (max != null && max.compareTo (value) < 0) {
137 return false;
138 }} catch (cce) {
139 if (Clazz.exceptionOf (cce, ClassCastException)) {
140 if (wantsCCE) {
141 throw cce;
142 }return false;
143 } else {
144 throw cce;
145 }
146 }
147 return true;
148 }, "~O,~B");
149 Clazz.defineMethod (c$, "getAttributes", 
150 function (index) {
151 if (this.isValidMask ()) {
152 var iterator = this.getIterator ();
153 if (index >= 0 && index <= iterator.getEndIndex ()) {
154 iterator.setIndex (index);
155 return iterator.getAttributes ();
156 }}return null;
157 }, "~N");
158 Clazz.defineMethod (c$, "getAttributeStart", 
159 function (id) {
160 if (this.isValidMask ()) {
161 var iterator = this.getIterator ();
162 iterator.first ();
163 while (iterator.current () != '\uffff') {
164 if (iterator.getAttribute (id) != null) {
165 return iterator.getIndex ();
166 }iterator.next ();
167 }
168 }return -1;
169 }, "java.text.AttributedCharacterIterator.Attribute");
170 Clazz.defineMethod (c$, "getIterator", 
171 function () {
172 return this.iterator;
173 });
174 Clazz.defineMethod (c$, "updateMaskIfNecessary", 
175 function () {
176 if (!this.getAllowsInvalid () && (this.getFormat () != null)) {
177 if (!this.isValidMask ()) {
178 this.updateMask ();
179 } else {
180 var newString = this.getFormattedTextField ().getText ();
181 if (!newString.equals (this.string)) {
182 this.updateMask ();
183 }}}});
184 Clazz.defineMethod (c$, "updateMask", 
185 function () {
186 if (this.getFormat () != null) {
187 var doc = this.getFormattedTextField ().getDocument ();
188 this.validMask = false;
189 if (doc != null) {
190 try {
191 this.string = doc.getText (0, doc.getLength ());
192 } catch (ble) {
193 if (Clazz.exceptionOf (ble, javax.swing.text.BadLocationException)) {
194 this.string = null;
195 } else {
196 throw ble;
197 }
198 }
199 if (this.string != null) {
200 try {
201 var value = this.stringToValue (this.string);
202 var iterator = this.getFormat ().formatToCharacterIterator (value);
203 this.updateMask (iterator);
204 } catch (e$$) {
205 if (Clazz.exceptionOf (e$$, java.text.ParseException)) {
206 var pe = e$$;
207 {
208 }
209 } else if (Clazz.exceptionOf (e$$, IllegalArgumentException)) {
210 var iae = e$$;
211 {
212 }
213 } else if (Clazz.exceptionOf (e$$, NullPointerException)) {
214 var npe = e$$;
215 {
216 }
217 } else {
218 throw e$$;
219 }
220 }
221 }}}});
222 Clazz.defineMethod (c$, "getLiteralCountTo", 
223 function (index) {
224 var lCount = 0;
225 for (var counter = 0; counter < index; counter++) {
226 if (this.isLiteral (counter)) {
227 lCount++;
228 }}
229 return lCount;
230 }, "~N");
231 Clazz.defineMethod (c$, "isLiteral", 
232 function (index) {
233 if (this.isValidMask () && index < this.string.length) {
234 return this.literalMask.get (index);
235 }return false;
236 }, "~N");
237 Clazz.defineMethod (c$, "getLiteral", 
238 function (index) {
239 if (this.isValidMask () && this.string != null && index < this.string.length) {
240 return this.string.charAt (index);
241 }return String.fromCharCode (0);
242 }, "~N");
243 Clazz.overrideMethod (c$, "isNavigatable", 
244 function (offset) {
245 return !this.isLiteral (offset);
246 }, "~N");
247 Clazz.defineMethod (c$, "updateValue", 
248 function (value) {
249 Clazz.superCall (this, javax.swing.text.InternationalFormatter, "updateValue", [value]);
250 this.updateMaskIfNecessary ();
251 }, "~O");
252 Clazz.defineMethod (c$, "replace", 
253 function (fb, offset, length, text, attrs) {
254 if (this.ignoreDocumentMutate) {
255 fb.replace (offset, length, text, attrs);
256 return;
257 }Clazz.superCall (this, javax.swing.text.InternationalFormatter, "replace", [fb, offset, length, text, attrs]);
258 }, "javax.swing.text.DocumentFilter.FilterBypass,~N,~N,~S,javax.swing.text.AttributeSet");
259 Clazz.defineMethod (c$, "getNextNonliteralIndex", 
260  function (index, direction) {
261 var max = this.getFormattedTextField ().getDocument ().getLength ();
262 while (index >= 0 && index < max) {
263 if (this.isLiteral (index)) {
264 index += direction;
265 } else {
266 return index;
267 }}
268 return (direction == -1) ? 0 : max;
269 }, "~N,~N");
270 Clazz.defineMethod (c$, "canReplace", 
271 function (rh) {
272 if (!this.getAllowsInvalid ()) {
273 var text = rh.text;
274 var tl = (text != null) ? text.length : 0;
275 if (tl == 0 && rh.length == 1 && this.getFormattedTextField ().getSelectionStart () != rh.offset) {
276 rh.offset = this.getNextNonliteralIndex (rh.offset, -1);
277 }if (this.getOverwriteMode ()) {
278 var replace = null;
279 for (var counter = 0, textIndex = 0, max = Math.max (tl, rh.length); counter < max; counter++) {
280 if (this.isLiteral (rh.offset + counter)) {
281 if (replace != null) {
282 replace.append (this.getLiteral (rh.offset + counter));
283 }if (textIndex < tl && text.charAt (textIndex) == this.getLiteral (rh.offset + counter)) {
284 textIndex++;
285 } else if (textIndex == 0) {
286 rh.offset++;
287 rh.length--;
288 counter--;
289 max--;
290 } else if (replace == null) {
291 replace =  new StringBuffer (max);
292 replace.append (text.substring (0, textIndex));
293 replace.append (this.getLiteral (rh.offset + counter));
294 }} else if (textIndex < tl) {
295 if (replace != null) {
296 replace.append (text.charAt (textIndex));
297 }textIndex++;
298 } else {
299 if (replace == null) {
300 replace =  new StringBuffer (max);
301 if (textIndex > 0) {
302 replace.append (text.substring (0, textIndex));
303 }}if (replace != null) {
304 replace.append (' ');
305 }}}
306 if (replace != null) {
307 rh.text = replace.toString ();
308 }} else if (tl > 0) {
309 rh.offset = this.getNextNonliteralIndex (rh.offset, 1);
310 } else {
311 rh.offset = this.getNextNonliteralIndex (rh.offset, -1);
312 }(rh).endOffset = rh.offset;
313 (rh).endTextLength = (rh.text != null) ? rh.text.length : 0;
314 } else {
315 (rh).endOffset = rh.offset;
316 (rh).endTextLength = (rh.text != null) ? rh.text.length : 0;
317 }var can = Clazz.superCall (this, javax.swing.text.InternationalFormatter, "canReplace", [rh]);
318 if (can && !this.getAllowsInvalid ()) {
319 (rh).resetFromValue (this);
320 }return can;
321 }, "javax.swing.text.DefaultFormatter.ReplaceHolder");
322 Clazz.defineMethod (c$, "replace", 
323 function (rh) {
324 var start = -1;
325 var direction = 1;
326 var literalCount = -1;
327 if (rh.length > 0 && (rh.text == null || rh.text.length == 0) && (this.getFormattedTextField ().getSelectionStart () != rh.offset || rh.length > 1)) {
328 direction = -1;
329 }if (!this.getAllowsInvalid ()) {
330 if ((rh.text == null || rh.text.length == 0) && rh.length > 0) {
331 start = this.getFormattedTextField ().getSelectionStart ();
332 } else {
333 start = rh.offset;
334 }literalCount = this.getLiteralCountTo (start);
335 }if (Clazz.superCall (this, javax.swing.text.InternationalFormatter, "replace", [rh])) {
336 if (start != -1) {
337 var end = (rh).endOffset;
338 end += (rh).endTextLength;
339 this.repositionCursor (literalCount, end, direction);
340 } else {
341 start = (rh).endOffset;
342 if (direction == 1) {
343 start += (rh).endTextLength;
344 }this.repositionCursor (start, direction);
345 }return true;
346 }return false;
347 }, "javax.swing.text.DefaultFormatter.ReplaceHolder");
348 Clazz.defineMethod (c$, "repositionCursor", 
349  function (startLiteralCount, end, direction) {
350 var endLiteralCount = this.getLiteralCountTo (end);
351 if (endLiteralCount != end) {
352 end -= startLiteralCount;
353 for (var counter = 0; counter < end; counter++) {
354 if (this.isLiteral (counter)) {
355 end++;
356 }}
357 }this.repositionCursor (end, 1);
358 }, "~N,~N,~N");
359 Clazz.defineMethod (c$, "getBufferedChar", 
360 function (index) {
361 if (this.isValidMask ()) {
362 if (this.string != null && index < this.string.length) {
363 return this.string.charAt (index);
364 }}return String.fromCharCode (0);
365 }, "~N");
366 Clazz.defineMethod (c$, "isValidMask", 
367 function () {
368 return this.validMask;
369 });
370 Clazz.defineMethod (c$, "isLiteral", 
371 function (attributes) {
372 return ((attributes == null) || attributes.size () == 0);
373 }, "java.util.Map");
374 Clazz.defineMethod (c$, "updateMask", 
375  function (iterator) {
376 if (iterator != null) {
377 this.validMask = true;
378 this.iterator = iterator;
379 if (this.literalMask == null) {
380 this.literalMask =  new java.util.BitSet ();
381 } else {
382 for (var counter = this.literalMask.length () - 1; counter >= 0; counter--) {
383 this.literalMask.clear (counter);
384 }
385 }iterator.first ();
386 while (iterator.current () != '\uffff') {
387 var attributes = iterator.getAttributes ();
388 var set = this.isLiteral (attributes);
389 var start = iterator.getIndex ();
390 var end = iterator.getRunLimit ();
391 while (start < end) {
392 if (set) {
393 this.literalMask.set (start);
394 } else {
395 this.literalMask.clear (start);
396 }start++;
397 }
398 iterator.setIndex (start);
399 }
400 }}, "java.text.AttributedCharacterIterator");
401 Clazz.defineMethod (c$, "canIncrement", 
402 function (field, cursorPosition) {
403 return (field != null);
404 }, "~O,~N");
405 Clazz.defineMethod (c$, "selectField", 
406 function (f, count) {
407 var iterator = this.getIterator ();
408 if (iterator != null && (Clazz.instanceOf (f, java.text.AttributedCharacterIterator.Attribute))) {
409 var field = f;
410 iterator.first ();
411 while (iterator.current () != '\uffff') {
412 while (iterator.getAttribute (field) == null && iterator.next () != '\uffff') ;
413 if (iterator.current () != '\uffff') {
414 var limit = iterator.getRunLimit (field);
415 if (--count <= 0) {
416 this.getFormattedTextField ().select (iterator.getIndex (), limit);
417 break;
418 }iterator.setIndex (limit);
419 iterator.next ();
420 }}
421 }}, "~O,~N");
422 Clazz.defineMethod (c$, "getAdjustField", 
423 function (start, attributes) {
424 return null;
425 }, "~N,java.util.Map");
426 Clazz.defineMethod (c$, "getFieldTypeCountTo", 
427  function (f, start) {
428 var iterator = this.getIterator ();
429 var count = 0;
430 if (iterator != null && (Clazz.instanceOf (f, java.text.AttributedCharacterIterator.Attribute))) {
431 var field = f;
432 var index = 0;
433 iterator.first ();
434 while (iterator.getIndex () < start) {
435 while (iterator.getAttribute (field) == null && iterator.next () != '\uffff') ;
436 if (iterator.current () != '\uffff') {
437 iterator.setIndex (iterator.getRunLimit (field));
438 iterator.next ();
439 count++;
440 } else {
441 break;
442 }}
443 }return count;
444 }, "~O,~N");
445 Clazz.defineMethod (c$, "adjustValue", 
446 function (value, attributes, field, direction) {
447 return null;
448 }, "~O,java.util.Map,~O,~N");
449 Clazz.defineMethod (c$, "getSupportsIncrement", 
450 function () {
451 return false;
452 });
453 Clazz.defineMethod (c$, "resetValue", 
454 function (value) {
455 var doc = this.getFormattedTextField ().getDocument ();
456 var string = this.valueToString (value);
457 try {
458 this.ignoreDocumentMutate = true;
459 doc.remove (0, doc.getLength ());
460 doc.insertString (0, string, null);
461 } finally {
462 this.ignoreDocumentMutate = false;
463 }
464 this.updateValue (value);
465 }, "~O");
466 Clazz.defineMethod (c$, "getReplaceHolder", 
467 function (fb, offset, length, text, attrs) {
468 if (this.replaceHolder == null) {
469 this.replaceHolder =  new javax.swing.text.InternationalFormatter.ExtendedReplaceHolder ();
470 }return Clazz.superCall (this, javax.swing.text.InternationalFormatter, "getReplaceHolder", [fb, offset, length, text, attrs]);
471 }, "javax.swing.text.DocumentFilter.FilterBypass,~N,~N,~S,javax.swing.text.AttributeSet");
472 c$.$InternationalFormatter$IncrementAction$ = function () {
473 Clazz.pu$h(self.c$);
474 c$ = Clazz.decorateAsClass (function () {
475 Clazz.prepareCallback (this, arguments);
476 this.direction = 0;
477 Clazz.instantialize (this, arguments);
478 }, javax.swing.text.InternationalFormatter, "IncrementAction", javax.swing.AbstractAction);
479 Clazz.makeConstructor (c$, 
480 function (a, b) {
481 Clazz.superConstructor (this, javax.swing.text.InternationalFormatter.IncrementAction, [a]);
482 this.direction = b;
483 }, "~S,~N");
484 Clazz.overrideMethod (c$, "actionPerformed", 
485 function (a) {
486 if (this.b$["javax.swing.text.InternationalFormatter"].getFormattedTextField ().isEditable ()) {
487 if (this.b$["javax.swing.text.InternationalFormatter"].getAllowsInvalid ()) {
488 this.b$["javax.swing.text.InternationalFormatter"].updateMask ();
489 }var b = false;
490 if (this.b$["javax.swing.text.InternationalFormatter"].isValidMask ()) {
491 var c = this.b$["javax.swing.text.InternationalFormatter"].getFormattedTextField ().getSelectionStart ();
492 if (c != -1) {
493 var d = this.b$["javax.swing.text.InternationalFormatter"].getIterator ();
494 d.setIndex (c);
495 var e = d.getAttributes ();
496 var f = this.b$["javax.swing.text.InternationalFormatter"].getAdjustField (c, e);
497 if (this.b$["javax.swing.text.InternationalFormatter"].canIncrement (f, c)) {
498 try {
499 var g = this.b$["javax.swing.text.InternationalFormatter"].stringToValue (this.b$["javax.swing.text.InternationalFormatter"].getFormattedTextField ().getText ());
500 var h = this.b$["javax.swing.text.InternationalFormatter"].getFieldTypeCountTo (f, c);
501 g = this.b$["javax.swing.text.InternationalFormatter"].adjustValue (g, e, f, this.direction);
502 if (g != null && this.b$["javax.swing.text.InternationalFormatter"].isValidValue (g, false)) {
503 this.b$["javax.swing.text.InternationalFormatter"].resetValue (g);
504 this.b$["javax.swing.text.InternationalFormatter"].updateMask ();
505 if (this.b$["javax.swing.text.InternationalFormatter"].isValidMask ()) {
506 this.b$["javax.swing.text.InternationalFormatter"].selectField (f, h);
507 }b = true;
508 }} catch (e$$) {
509 if (Clazz.exceptionOf (e$$, java.text.ParseException)) {
510 var pe = e$$;
511 {
512 }
513 } else if (Clazz.exceptionOf (e$$, javax.swing.text.BadLocationException)) {
514 var ble = e$$;
515 {
516 }
517 } else {
518 throw e$$;
519 }
520 }
521 }}}if (!b) {
522 this.b$["javax.swing.text.InternationalFormatter"].invalidEdit ();
523 }}}, "java.awt.event.ActionEvent");
524 c$ = Clazz.p0p ();
525 };
526 Clazz.pu$h(self.c$);
527 c$ = Clazz.decorateAsClass (function () {
528 this.endOffset = 0;
529 this.endTextLength = 0;
530 Clazz.instantialize (this, arguments);
531 }, javax.swing.text.InternationalFormatter, "ExtendedReplaceHolder", javax.swing.text.DefaultFormatter.ReplaceHolder);
532 Clazz.defineMethod (c$, "resetFromValue", 
533 function (a) {
534 this.offset = 0;
535 try {
536 this.text = a.valueToString (this.value);
537 } catch (pe) {
538 if (Clazz.exceptionOf (pe, java.text.ParseException)) {
539 this.text = "";
540 } else {
541 throw pe;
542 }
543 }
544 this.length = this.fb.getDocument ().getLength ();
545 }, "javax.swing.text.InternationalFormatter");
546 c$ = Clazz.p0p ();
547 c$.EMPTY_FIELD_ARRAY = c$.prototype.EMPTY_FIELD_ARRAY =  new Array (0);
548 });