Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DoubleRangePanel.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/client/ui/DoubleRangePanel.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.client.ui; | |
2 | |
3 import com.google.gwt.core.client.GWT; | |
4 import com.google.gwt.i18n.client.NumberFormat; | |
5 | |
6 import com.smartgwt.client.types.Alignment; | |
7 import com.smartgwt.client.widgets.form.DynamicForm; | |
8 import com.smartgwt.client.widgets.form.fields.FloatItem; | |
9 import com.smartgwt.client.widgets.form.fields.FormItem; | |
10 import com.smartgwt.client.widgets.form.fields.StaticTextItem; | |
11 import com.smartgwt.client.widgets.form.fields.events.BlurHandler; | |
12 | |
13 import org.dive4elements.river.client.client.FLYSConstants; | |
14 | |
15 import java.util.Map; | |
16 | |
17 | |
18 /** | |
19 * This class creates a DynamicForm with three input fields. | |
20 * | |
21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
22 */ | |
23 public class DoubleRangePanel | |
24 extends DynamicForm | |
25 { | |
26 /** The message class that provides i18n strings.*/ | |
27 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); | |
28 | |
29 | |
30 /** The constant name of the input field to enter the start of a distance.*/ | |
31 public static final String FIELD_FROM = "from"; | |
32 | |
33 /** The constant name of the input field to enter the end of a distance.*/ | |
34 public static final String FIELD_TO = "to"; | |
35 | |
36 /** The constant name of the input field to enter the step width of a | |
37 * distance.*/ | |
38 public static final String FIELD_WIDTH = "step"; | |
39 | |
40 /** The textboxes */ | |
41 protected FloatItem fromItem; | |
42 protected FloatItem toItem; | |
43 protected FloatItem stepItem; | |
44 | |
45 | |
46 public DoubleRangePanel() { | |
47 } | |
48 | |
49 | |
50 /** | |
51 * Creates a new form with a single input field that displays an array of | |
52 * double values. | |
53 * | |
54 * @param name The name of the TextItem. | |
55 * @param title The title of the TextItem. | |
56 * @param values The double values that should be displayed initially. | |
57 * @param handler The BlurHandler that is used to valide the input. | |
58 */ | |
59 public DoubleRangePanel( | |
60 String titleFrom, String titleTo, String titleStep, | |
61 double from, double to, double step, | |
62 int width, | |
63 BlurHandler handler) | |
64 { | |
65 this( | |
66 titleFrom, titleTo, titleStep, | |
67 from, to, step, | |
68 width, | |
69 handler, | |
70 "right"); | |
71 } | |
72 | |
73 | |
74 public DoubleRangePanel( | |
75 String titleFrom, String titleTo, String titleStep, | |
76 double from, double to, double step, | |
77 int width, | |
78 BlurHandler handler, | |
79 String labelOrientation) | |
80 { | |
81 fromItem = new FloatItem(FIELD_FROM); | |
82 toItem = new FloatItem(FIELD_TO); | |
83 stepItem = new FloatItem(FIELD_WIDTH); | |
84 | |
85 fromItem.addBlurHandler(handler); | |
86 toItem.addBlurHandler(handler); | |
87 stepItem.addBlurHandler(handler); | |
88 | |
89 NumberFormat f = NumberFormat.getDecimalFormat(); | |
90 | |
91 fromItem.setValue(f.format(from)); | |
92 toItem.setValue(f.format(to)); | |
93 stepItem.setValue(f.format(step)); | |
94 | |
95 StaticTextItem fromText = new StaticTextItem("staticFrom"); | |
96 fromText.setValue(titleFrom); | |
97 fromText.setShowTitle(false); | |
98 fromItem.setShowTitle(false); | |
99 | |
100 StaticTextItem toText = new StaticTextItem("staticTo"); | |
101 toText.setValue(titleTo); | |
102 toText.setShowTitle(false); | |
103 toItem.setShowTitle(false); | |
104 | |
105 StaticTextItem stepText = new StaticTextItem("staticStep"); | |
106 stepText.setValue(titleStep); | |
107 stepText.setShowTitle(false); | |
108 stepItem.setShowTitle(false); | |
109 | |
110 int itemWidth = width / 6; | |
111 fromItem.setWidth(itemWidth); | |
112 fromText.setWidth(itemWidth); | |
113 toItem.setWidth(itemWidth); | |
114 toText.setWidth(itemWidth); | |
115 stepItem.setWidth(itemWidth); | |
116 stepText.setWidth(itemWidth); | |
117 | |
118 if (labelOrientation.equals("right")) { | |
119 setFields(fromItem, fromText, toItem, toText, stepItem, stepText); | |
120 } | |
121 else { | |
122 setFields(fromText, fromItem, toText, toItem, stepText, stepItem); | |
123 } | |
124 | |
125 setFixedColWidths(false); | |
126 setNumCols(6); | |
127 setWidth(width); | |
128 setAlign(Alignment.CENTER); | |
129 } | |
130 | |
131 | |
132 /** | |
133 * This method takes distances values and sets them to the textboxes | |
134 * visualizied by this widget. | |
135 * | |
136 * @param from The from value. | |
137 * @param to The to value. | |
138 * @param steps The max steps. | |
139 */ | |
140 public void setValues(double from, double to, double steps) { | |
141 NumberFormat f = NumberFormat.getDecimalFormat(); | |
142 | |
143 fromItem.setValue(f.format(from)); | |
144 toItem.setValue(f.format(to)); | |
145 stepItem.setValue(f.format(steps)); | |
146 } | |
147 | |
148 | |
149 public boolean validateForm() { | |
150 try { | |
151 return | |
152 validateForm(fromItem) && | |
153 validateForm(toItem) && | |
154 validateForm(stepItem); | |
155 } | |
156 catch (NumberFormatException nfe) { | |
157 return false; | |
158 } | |
159 } | |
160 | |
161 /** | |
162 * This method validates the entered text in the input fields. If | |
163 * there are values that doesn't represent a valid float, an error is | |
164 * displayed. | |
165 * | |
166 * @param item The FormItem. | |
167 */ | |
168 @SuppressWarnings("unchecked") | |
169 protected boolean validateForm(FormItem item) { | |
170 if (item instanceof StaticTextItem) { | |
171 return true; | |
172 } | |
173 | |
174 boolean valid = true; | |
175 | |
176 String v = (String) item.getValue(); | |
177 | |
178 NumberFormat f = NumberFormat.getDecimalFormat(); | |
179 @SuppressWarnings("rawtypes") | |
180 Map errors = getErrors(); | |
181 | |
182 try { | |
183 if (v == null) { | |
184 throw new NumberFormatException("empty"); | |
185 } | |
186 | |
187 f.parse(v); | |
188 | |
189 errors.remove(item.getFieldName()); | |
190 } | |
191 catch (NumberFormatException nfe) { | |
192 errors.put(item.getFieldName(), MESSAGES.wrongFormat()); | |
193 | |
194 item.focusInItem(); | |
195 | |
196 valid = false; | |
197 } | |
198 | |
199 setErrors(errors, true); | |
200 | |
201 return valid; | |
202 } | |
203 | |
204 | |
205 /** | |
206 * Returns the double value of <i>value</i>. | |
207 * | |
208 * @return the double value of <i>value</i>. | |
209 */ | |
210 protected double getDouble(String value) { | |
211 NumberFormat f = NumberFormat.getDecimalFormat(); | |
212 | |
213 String[] splitted = value.split(" "); | |
214 | |
215 return f.parse(splitted[0]); | |
216 } | |
217 | |
218 | |
219 /** | |
220 * Returns the start value. | |
221 * | |
222 * @return the start value. | |
223 */ | |
224 public double getFrom() throws NullPointerException { | |
225 String v = getValueAsString(FIELD_FROM); | |
226 | |
227 return getDouble(v); | |
228 } | |
229 | |
230 | |
231 /** | |
232 * Returns the end value. | |
233 * | |
234 * @return the end value. | |
235 */ | |
236 public double getTo() throws NullPointerException { | |
237 String v = getValueAsString(FIELD_TO); | |
238 | |
239 return getDouble(v); | |
240 } | |
241 | |
242 | |
243 /** | |
244 * Returns the step width. | |
245 * | |
246 * @return the step width. | |
247 */ | |
248 public double getStep() throws NullPointerException { | |
249 String v = getValueAsString(FIELD_WIDTH); | |
250 | |
251 return getDouble(v); | |
252 } | |
253 | |
254 | |
255 /** | |
256 * Sets the value of the field with name <i>fieldname</i>. | |
257 * | |
258 * @param value The new value. | |
259 * @param fieldname The name of the field. | |
260 */ | |
261 public void setDoubleValue(double value, String fieldname) { | |
262 NumberFormat f = NumberFormat.getDecimalFormat(); | |
263 setValue(fieldname, f.format(value)); | |
264 } | |
265 | |
266 | |
267 /** | |
268 * Sets a new start value. | |
269 * | |
270 * @param value The new start value. | |
271 */ | |
272 public void setFrom(double value) { | |
273 setDoubleValue(value, FIELD_FROM); | |
274 } | |
275 | |
276 | |
277 /** | |
278 * Sets a new end value. | |
279 * | |
280 * @param value The new end value. | |
281 */ | |
282 public void setTo(double value) { | |
283 setDoubleValue(value, FIELD_TO); | |
284 } | |
285 | |
286 | |
287 /** | |
288 * Sets a new step width. | |
289 * | |
290 * @param value The new step width. | |
291 */ | |
292 public void setStep(double value) { | |
293 setDoubleValue(value, FIELD_WIDTH); | |
294 } | |
295 } | |
296 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |