c1d3b36cf4270faa4120097ccb0c1ae7825d1f19
[jalviewjs.git] / site / j2s / javax / swing / text / NumberFormatter.js
1 Clazz.declarePackage ("javax.swing.text");
2 Clazz.load (["javax.swing.text.InternationalFormatter", "java.text.NumberFormat"], "javax.swing.text.NumberFormatter", ["java.lang.Byte", "$.Character", "$.Double", "$.Float", "$.Long", "$.Number", "$.Short", "$.StringBuffer", "java.text.DecimalFormat"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.specialChars = null;
5 Clazz.instantialize (this, arguments);
6 }, javax.swing.text, "NumberFormatter", javax.swing.text.InternationalFormatter);
7 Clazz.makeConstructor (c$, 
8 function () {
9 Clazz.superConstructor (this, javax.swing.text.NumberFormatter, []);
10 this.setN (java.text.NumberFormat.getNumberInstance ());
11 });
12 Clazz.makeConstructor (c$, 
13 function (format) {
14 Clazz.superConstructor (this, javax.swing.text.NumberFormatter, []);
15 this.setN (format);
16 }, "java.text.NumberFormat");
17 Clazz.defineMethod (c$, "setN", 
18  function (format) {
19 this.setFormat (format);
20 this.setAllowsInvalid (true);
21 this.setCommitsOnValidEdit (false);
22 this.setOverwriteMode (false);
23 }, "java.text.NumberFormat");
24 Clazz.defineMethod (c$, "setFormat", 
25 function (format) {
26 Clazz.superCall (this, javax.swing.text.NumberFormatter, "setFormat", [format]);
27 var dfs = this.getDecimalFormatSymbols ();
28 if (dfs != null) {
29 var sb =  new StringBuffer ();
30 sb.append (dfs.getCurrencySymbol ());
31 sb.append (dfs.getDecimalSeparator ());
32 sb.append (dfs.getGroupingSeparator ());
33 sb.append (dfs.getInfinity ());
34 sb.append (dfs.getInternationalCurrencySymbol ());
35 sb.append (dfs.getMinusSign ());
36 sb.append (dfs.getMonetaryDecimalSeparator ());
37 sb.append (dfs.getNaN ());
38 sb.append (dfs.getPercent ());
39 sb.append ('+');
40 this.specialChars = sb.toString ();
41 } else {
42 this.specialChars = "";
43 }}, "java.text.Format");
44 Clazz.overrideMethod (c$, "stringToValueParse", 
45 function (text, f) {
46 if (f == null) {
47 return text;
48 }var value = f.parseObject (text);
49 return this.convertValueToValueClass (value, this.getValueClass ());
50 }, "~S,java.text.Format");
51 Clazz.defineMethod (c$, "convertValueToValueClass", 
52  function (value, valueClass) {
53 if (valueClass != null && (Clazz.instanceOf (value, Number))) {
54 if (valueClass === Integer) {
55 return  new Integer ((value).intValue ());
56 } else if (valueClass === Long) {
57 return  new Long ((value).longValue ());
58 } else if (valueClass === Float) {
59 return  new Float ((value).floatValue ());
60 } else if (valueClass === Double) {
61 return  new Double ((value).doubleValue ());
62 } else if (valueClass === Byte) {
63 return  new Byte ((value).byteValue ());
64 } else if (valueClass === Short) {
65 return  new Short ((value).shortValue ());
66 }}return value;
67 }, "~O,Class");
68 Clazz.defineMethod (c$, "getPositiveSign", 
69  function () {
70 return '+';
71 });
72 Clazz.defineMethod (c$, "getMinusSign", 
73  function () {
74 var dfs = this.getDecimalFormatSymbols ();
75 if (dfs != null) {
76 return dfs.getMinusSign ();
77 }return '-';
78 });
79 Clazz.defineMethod (c$, "getDecimalSeparator", 
80  function () {
81 var dfs = this.getDecimalFormatSymbols ();
82 if (dfs != null) {
83 return dfs.getDecimalSeparator ();
84 }return '.';
85 });
86 Clazz.defineMethod (c$, "getDecimalFormatSymbols", 
87  function () {
88 var f = this.getFormat ();
89 if (Clazz.instanceOf (f, java.text.DecimalFormat)) {
90 return (f).getDecimalFormatSymbols ();
91 }return null;
92 });
93 Clazz.overrideMethod (c$, "isLegalInsertText", 
94 function (text) {
95 if (this.getAllowsInvalid ()) {
96 return true;
97 }for (var counter = text.length - 1; counter >= 0; counter--) {
98 var aChar = text.charAt (counter);
99 if (!Character.isDigit (aChar) && this.specialChars.indexOf (aChar) == -1) {
100 return false;
101 }}
102 return true;
103 }, "~S");
104 Clazz.defineMethod (c$, "isLiteral", 
105 function (attrs) {
106 if (!Clazz.superCall (this, javax.swing.text.NumberFormatter, "isLiteral", [attrs])) {
107 if (attrs == null) {
108 return false;
109 }var size = attrs.size ();
110 if (attrs.get (java.text.NumberFormat.Field.GROUPING_SEPARATOR) != null) {
111 size--;
112 if (attrs.get (java.text.NumberFormat.Field.INTEGER) != null) {
113 size--;
114 }}if (attrs.get (java.text.NumberFormat.Field.EXPONENT_SYMBOL) != null) {
115 size--;
116 }if (attrs.get (java.text.NumberFormat.Field.PERCENT) != null) {
117 size--;
118 }if (attrs.get (java.text.NumberFormat.Field.PERMILLE) != null) {
119 size--;
120 }if (attrs.get (java.text.NumberFormat.Field.CURRENCY) != null) {
121 size--;
122 }if (attrs.get (java.text.NumberFormat.Field.SIGN) != null) {
123 size--;
124 }if (size == 0) {
125 return true;
126 }return false;
127 }return true;
128 }, "java.util.Map");
129 Clazz.defineMethod (c$, "isNavigatable", 
130 function (index) {
131 if (!Clazz.superCall (this, javax.swing.text.NumberFormatter, "isNavigatable", [index])) {
132 if (this.getBufferedChar (index) == this.getDecimalSeparator ()) {
133 return true;
134 }return false;
135 }return true;
136 }, "~N");
137 Clazz.defineMethod (c$, "getFieldFrom", 
138  function (index, direction) {
139 if (this.isValidMask ()) {
140 var max = this.getFormattedTextField ().getDocument ().getLength ();
141 var iterator = this.getIterator ();
142 if (index >= max) {
143 index += direction;
144 }while (index >= 0 && index < max) {
145 iterator.setIndex (index);
146 var attrs = iterator.getAttributes ();
147 if (attrs != null && attrs.size () > 0) {
148 var keys = attrs.keySet ().iterator ();
149 while (keys.hasNext ()) {
150 var key = keys.next ();
151 if (Clazz.instanceOf (key, java.text.NumberFormat.Field)) {
152 return key;
153 }}
154 }index += direction;
155 }
156 }return null;
157 }, "~N,~N");
158 Clazz.defineMethod (c$, "replace", 
159 function (fb, offset, length, string, attr) {
160 if (!this.getAllowsInvalid () && length == 0 && string != null && string.length == 1 && this.toggleSignIfNecessary (fb, offset, string.charAt (0))) {
161 return;
162 }Clazz.superCall (this, javax.swing.text.NumberFormatter, "replace", [fb, offset, length, string, attr]);
163 }, "javax.swing.text.DocumentFilter.FilterBypass,~N,~N,~S,javax.swing.text.AttributeSet");
164 Clazz.defineMethod (c$, "toggleSignIfNecessary", 
165  function (fb, offset, aChar) {
166 if (aChar == this.getMinusSign () || aChar == this.getPositiveSign ()) {
167 var field = this.getFieldFrom (offset, -1);
168 var newValue;
169 try {
170 if (field == null || (field !== java.text.NumberFormat.Field.EXPONENT && field !== java.text.NumberFormat.Field.EXPONENT_SYMBOL && field !== java.text.NumberFormat.Field.EXPONENT_SIGN)) {
171 newValue = this.toggleSign ((aChar == this.getPositiveSign ()));
172 } else {
173 newValue = this.toggleExponentSign (offset, aChar);
174 }if (newValue != null && this.isValidValue (newValue, false)) {
175 var lc = this.getLiteralCountTo (offset);
176 var string = this.valueToString (newValue);
177 fb.remove (0, fb.getDocument ().getLength ());
178 fb.insertString (0, string, null);
179 this.updateValue (newValue);
180 this.repositionCursor (this.getLiteralCountTo (offset) - lc + offset, 1);
181 return true;
182 }} catch (pe) {
183 if (Clazz.exceptionOf (pe, java.text.ParseException)) {
184 this.invalidEdit ();
185 } else {
186 throw pe;
187 }
188 }
189 }return false;
190 }, "javax.swing.text.DocumentFilter.FilterBypass,~N,~S");
191 Clazz.defineMethod (c$, "toggleSign", 
192  function (positive) {
193 var value = this.stringToValue (this.getFormattedTextField ().getText ());
194 if (value != null) {
195 var string = value.toString ();
196 if (string != null && string.length > 0) {
197 if (positive) {
198 if (string.charAt (0) == '-') {
199 string = string.substring (1);
200 }} else {
201 if (string.charAt (0) == '+') {
202 string = string.substring (1);
203 }if (string.length > 0 && string.charAt (0) != '-') {
204 string = "-" + string;
205 }}if (string != null) {
206 var valueClass = this.getValueClass ();
207 if (valueClass == null) {
208 valueClass = value.getClass ();
209 }try {
210 var cons = valueClass.getConstructor ( Clazz.newArray (-1, [String]));
211 if (cons != null) {
212 return cons.newInstance ( Clazz.newArray (-1, [string]));
213 }} catch (ex) {
214 }
215 }}}return null;
216 }, "~B");
217 Clazz.defineMethod (c$, "toggleExponentSign", 
218  function (offset, aChar) {
219 var string = this.getFormattedTextField ().getText ();
220 var replaceLength = 0;
221 var loc = this.getAttributeStart (java.text.NumberFormat.Field.EXPONENT_SIGN);
222 if (loc >= 0) {
223 replaceLength = 1;
224 offset = loc;
225 }if (aChar == this.getPositiveSign ()) {
226 string = this.getReplaceString (offset, replaceLength, null);
227 } else {
228 string = this.getReplaceString (offset, replaceLength,  String.instantialize ( Clazz.newCharArray (-1, [aChar])));
229 }return this.stringToValue (string);
230 }, "~N,~S");
231 });