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
35
36
37
38 public class RadialWheelswitchFormatter extends AbstractWheelswitchFormatter
39 {
40
41
42
43
44
45
46
47 public RadialWheelswitchFormatter(String newFormatString)
48 {
49 super(newFormatString);
50 }
51
52
53
54
55
56
57 public RadialWheelswitchFormatter()
58 {
59 super();
60 }
61
62
63
64
65 public void setFormat(String newFormatString)
66 throws IllegalArgumentException {
67 if (newFormatString!=null && newFormatString.indexOf('E')>-1) throw new IllegalArgumentException("Exponential format not supported by this formatter.");
68 super.setFormat(newFormatString);
69 }
70
71 private String parseString(String newValueString, boolean pad) {
72 double newValue = Double.parseDouble(newValueString);
73
74
75 boolean reverse = false;
76 if (Math.abs(value)>Math.abs(newValue)) {
77 reverse = true;
78 }
79
80
81 int sign = 0;
82 if (formatString.startsWith("+")) {
83 sign = 1;
84 }
85 if (newValueString.startsWith("-")) {
86 sign = -1;
87 newValueString = newValueString.substring(1);
88 }
89 if (newValueString.startsWith("+")) {
90 newValueString = newValueString.substring(1);
91 }
92
93
94
95 int decStrLength = newValueString.length();
96 int dotIndex = newValueString.indexOf('.');
97 if (dotIndex>-1) decStrLength = dotIndex;
98
99
100 String secString = "0";
101 if (decStrLength==1) secString = newValueString;
102 if (decStrLength>1) secString = newValueString.substring(decStrLength-2);
103
104 String minString = "0";
105 if (decStrLength==3) minString = newValueString.substring(0,decStrLength-2);
106 if (decStrLength>3) minString = newValueString.substring(decStrLength-4,decStrLength-2);
107
108 String degString = "0";
109 if (decStrLength>4) degString = newValueString.substring(0,decStrLength-4);
110
111
112 double secValue = Double.parseDouble(secString);
113
114 int secOff = (int)secValue/60;
115
116 if (secOff>=1) {
117
118
119
120 if (reverse) {
121 secValue -= 40;
122 secOff = 0;
123 }
124 else secValue %= 60;
125 }
126
127 secString = String.valueOf(secValue);
128
129
130 decStrLength = secString.length();
131 dotIndex = secString.indexOf('.');
132 if (dotIndex>-1) decStrLength = dotIndex;
133 while (decStrLength<2) {
134 secString = "0"+secString;
135 decStrLength++;
136 }
137
138
139 int minValue = Integer.parseInt(minString)+secOff;
140 int minOff = minValue/60;
141 if (minOff>0) {
142 if (reverse) {
143 minValue -= 40;
144 minOff = 0;
145 }
146 else minValue %= 60;
147 }
148 minString = String.valueOf((int)minValue);
149 while (minString.length()<2) minString = "0"+minString;
150
151
152 double degValue = Double.parseDouble(degString)+minOff;
153 degString = String.valueOf((int)degValue);
154
155
156 newValueString = degString+minString+secString;
157
158 if (pad) newValueString = padString(newValueString);
159
160
161 if (sign>0) newValueString = "+" + newValueString;
162 if (sign<0) newValueString = "-" + newValueString;
163
164 return newValueString;
165 }
166
167
168
169
170
171 private String padString(String newValueString) {
172
173 int numFormatInteger = formatString.indexOf('.');
174 int numFormatDecimal = 0;
175 if (numFormatInteger<0) numFormatInteger = formatString.length();
176 else numFormatDecimal = formatString.length()-numFormatInteger-1;
177 if (formatString.startsWith("+")) {
178 numFormatInteger -= 1;
179 }
180
181
182 int numInteger = newValueString.indexOf('.');
183 int numDecimal = 0;
184 if (numInteger<0) numInteger = newValueString.length();
185 else numDecimal = newValueString.length()-numInteger-1;
186
187
188 while (numInteger<numFormatInteger) {
189 newValueString = "0"+newValueString;
190 numInteger++;
191 }
192
193 while (numInteger>numFormatInteger && newValueString.startsWith("0")) {
194 newValueString = newValueString.substring(1);
195 numInteger--;
196 }
197
198
199 if (formatString.indexOf('.')>-1 && newValueString.indexOf('.')<0) newValueString += ".";
200 while (numDecimal<numFormatDecimal) {
201 newValueString += "0";
202 numDecimal++;
203 }
204
205
206 while (numDecimal>numFormatDecimal) {
207 newValueString = newValueString.substring(0,newValueString.length()-1);
208 numDecimal--;
209 }
210
211 return newValueString;
212 }
213
214 protected void internalSetString(String newValueString) {
215
216
217
218
219 newValueString = parseString(newValueString,false);
220
221 value = Double.parseDouble(newValueString);
222
223
224 newValueString = formatter.sprintf(value);
225
226 while (newValueString.startsWith(" ")) newValueString = newValueString.substring(1);
227
228
229 valueString = parseString(newValueString,true);
230
231 }
232 }
233
234