1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.wheelswitch;
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class WheelswitchFormatter extends AbstractWheelswitchFormatter
35 {
36
37
38
39
40
41
42
43 public WheelswitchFormatter(String newFormatString)
44 {
45 super(newFormatString);
46 }
47
48
49
50
51
52
53 public WheelswitchFormatter()
54 {
55 super();
56 }
57
58 protected void internalSetString(String newValueString) {
59 if (debug) System.out.println("WheelswitchFormatter:SET STRING VALUE: " + newValueString);
60
61 double newValue = Double.parseDouble(newValueString);
62 String tempFormatString = formatString;
63
64 if (formatString == null) {
65 tempFormatString = generateFormat();
66 }
67
68 int newExpIndex = Math.max(newValueString.indexOf("E"),
69 newValueString.indexOf("e"));
70
71 if (newExpIndex < 0) {
72 newExpIndex = newValueString.length();
73 }
74
75 int formatExpIndex = tempFormatString.indexOf("E");
76
77 if (formatExpIndex < 0) {
78 formatExpIndex = tempFormatString.length();
79 }
80
81 int newDotIndex = Math.max(newValueString.indexOf("."),
82 newValueString.indexOf(","));
83
84 if (newDotIndex < 0) {
85 newDotIndex = newExpIndex;
86 }
87
88 int formatDotIndex = tempFormatString.indexOf(".");
89
90 if (formatDotIndex < 0) {
91 formatDotIndex = formatExpIndex;
92 }
93
94 int newSignTag = 0;
95
96 if (newValueString.charAt(0) == '+') {
97 newSignTag++;
98 } else if (newValueString.charAt(0) == '-') {
99 newSignTag--;
100 }
101
102 int formatSignTag = 0;
103
104 if (tempFormatString.charAt(0) == '+') {
105 formatSignTag++;
106 }
107
108 int formatExpSignTag = 0;
109
110 if (formatExpIndex + 1 < tempFormatString.length()
111 && tempFormatString.charAt(formatExpIndex + 1) == '+') {
112 formatExpSignTag++;
113 }
114
115 int newExpValue = 0;
116
117 if (newExpIndex < newValueString.length() - 1) {
118 newExpValue += (int)Double.parseDouble(newValueString.substring(newExpIndex
119 + 1));
120 }
121
122
123 newExpValue += newDotIndex - formatDotIndex;
124
125 if (newSignTag != 0) {
126 newExpValue--;
127 }
128
129 if (formatSignTag != 0) {
130 newExpValue++;
131 }
132
133
134 newValueString = newValueString.substring(0, newDotIndex)
135 + ((newDotIndex < newExpIndex - 1)
136 ? (newValueString.substring(newDotIndex + 1, newExpIndex)) : (""));
137
138 if (newSignTag != 0) {
139 newValueString = newValueString.substring(1);
140 }
141
142 boolean substract = true;
143 while (newValueString.charAt(0) == '0' && newValueString.length() > 1) {
144 newValueString = newValueString.substring(1);
145
146
147 if (newValue != 0.) {
148 newExpValue--;
149 }
150 substract = false;
151 }
152
153 if (newValue == 0. && substract) {
154 newExpValue+=newExpIndex-3;
155 }
156
157
158
159 if (newExpValue<-323) {
160 newExpValue=-323;
161 } else {
162 if (newExpValue>307) {
163 newExpValue=307;
164 }
165 }
166
167
168 while (newValueString.length() < formatExpIndex - formatSignTag
169 - ((formatDotIndex < formatExpIndex) ? (1) : (0))) {
170 newValueString += "0";
171 }
172
173
174 if (formatExpIndex + formatExpSignTag + 1 < tempFormatString.length()) {
175
176 String exponent = String.valueOf(newExpValue);
177
178 int newExpSignTag = 1;
179
180 if (newExpValue < 0) {
181 newExpSignTag = -1;
182 exponent = exponent.substring(1);
183 }
184
185
186 newValueString = newValueString.substring(0,
187 formatDotIndex - formatSignTag) + "."
188 + newValueString.substring(formatDotIndex - formatSignTag);
189
190 if (newValue != 0.) {
191 newValueString = formatter.sprintf(Double.parseDouble(
192 newValueString)).replaceAll(",", ".");
193
194 if (newValueString.charAt(0) == '+') {
195 newValueString = newValueString.substring(1);
196 }
197 }
198
199 if (newSignTag == -1) {
200 newValueString = "-" + newValueString;
201 } else if (formatSignTag == 1) {
202 newValueString = "+" + newValueString;
203 }
204
205 newValueString += "E";
206
207
208 if (newExpSignTag == -1) {
209 newValueString += "-";
210 } else if (formatExpSignTag == 1) {
211 newValueString += "+";
212 }
213
214 int exponentLength = tempFormatString.length() - formatExpIndex
215 - formatExpSignTag - 1;
216
217 while (exponent.length() < exponentLength) {
218 exponent = "0" + exponent;
219 }
220
221 newValueString += exponent;
222 } else {
223
224 while (newExpValue > 0) {
225 newValueString += "0";
226 formatDotIndex++;
227 newExpValue--;
228 }
229
230 while (newExpValue < 0) {
231 newValueString = "0" + newValueString;
232 newExpValue++;
233 }
234
235 newValueString = newValueString.substring(0,
236 formatDotIndex - formatSignTag) + "."
237 + newValueString.substring(formatDotIndex - formatSignTag);
238
239 if (newValueString.charAt(0) != '0') {
240 newValueString = formatter.sprintf(Double.parseDouble(
241 newValueString)).replaceAll(",", ".");
242
243 if (newValueString.charAt(0) == '+') {
244 newValueString = newValueString.substring(1);
245 }
246 } else {
247 newValueString = "1" + newValueString.substring(1);
248 newValueString = formatter.sprintf(Double.parseDouble(
249 newValueString)).replaceAll(",", ".");
250
251 if (newValueString.charAt(0) == '+') {
252 newValueString = newValueString.substring(1);
253 }
254
255 newValueString = "0" + newValueString.substring(1);
256 }
257
258 if (newSignTag == -1) {
259 newValueString = "-" + newValueString;
260 } else if (formatSignTag == 1) {
261 newValueString = "+" + newValueString;
262 }
263 }
264
265 value = newValue;
266 valueString = newValueString;
267 }
268 }
269
270