View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.wheelswitch;
21  
22  
23  
24  /**
25   * Formats <code>java.lang.String</code>s to be displayed as digits in  the
26   * <code>Wheelswitch</code>. The format is specified in a format
27   * <code>java.lang.String</code>. <code>WheelswitchFormatter</code> also
28   * stores the value, its bounds (minimum and maximum) and also the unit
29   * displayed by the <code>Wheelswitch</code>.
30   *
31   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
32   * @version $id$
33   */
34  public class WheelswitchFormatter extends AbstractWheelswitchFormatter
35  {
36  	/**
37  	 * Constructs the PlainWheelswitchFormatter and sets the format  string.
38  	 *
39  	 * @param newFormatString
40  	 *
41  	 * @see #setFormat(String)
42  	 */
43  	public WheelswitchFormatter(String newFormatString)
44  	{
45  		super(newFormatString);
46  	}
47  
48  	/**
49  	 * Constructs the PlainWheelswitchFormatter with no format string.
50  	 *
51  	 * @see #setFormat(String)
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 		//sync the exp value with format
123 		newExpValue += newDotIndex - formatDotIndex;
124 
125 		if (newSignTag != 0) {
126 			newExpValue--;
127 		}
128 
129 		if (formatSignTag != 0) {
130 			newExpValue++;
131 		}
132 
133 		//display mantissa as integer
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 			//bugfix by jbobnar: If the value is 0 don't do any manipulation, since 
146 			//this would change what the value that is being set
147 			if (newValue != 0.) {
148 				newExpValue--;
149 			}
150 			substract = false;
151 		}
152 
153 		if (newValue == 0. && substract) {
154 			newExpValue+=newExpIndex-3;
155 		}
156 		
157 		// Ike: Check if eponent over double range
158 		// TODO: this does not eliminates problem if decimal in whole value over range, even if exp is in range.
159 		if (newExpValue<-323) {
160 			newExpValue=-323;
161 		} else {
162 			if (newExpValue>307) {
163 				newExpValue=307;
164 			}
165 		}
166 
167 		//sync mantissa with format
168 		while (newValueString.length() < formatExpIndex - formatSignTag
169 			- ((formatDotIndex < formatExpIndex) ? (1) : (0))) {
170 			newValueString += "0";
171 		}
172 
173 		//if exponent should be displayed
174 		if (formatExpIndex + formatExpSignTag + 1 < tempFormatString.length()) {
175 			//parse exponent
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 			//parse mantissa
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 			//add exponent
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 			//parse mantissa only
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 /* __oOo__ */