comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java @ 43:6bcd8e3f21a7

Refactored the LocationDistancePanel, so that is uses the DoubleArrayPanel and DoubleRangePanel from the last commit as well. flys-client/trunk@1484 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 16 Mar 2011 16:24:43 +0000
parents 87a44f8e25cc
children f99c5f8e4672
comparison
equal deleted inserted replaced
42:ba7df4a24ae0 43:6bcd8e3f21a7
1 package de.intevation.flys.client.client.ui; 1 package de.intevation.flys.client.client.ui;
2 2
3 import java.util.LinkedHashMap;
3 import java.util.Map; 4 import java.util.Map;
4 5
5 import com.google.gwt.core.client.GWT; 6 import com.google.gwt.core.client.GWT;
6 import com.google.gwt.i18n.client.NumberFormat; 7 import com.google.gwt.i18n.client.NumberFormat;
7 8
38 39
39 40
40 /** The constant name of the input field to enter locations.*/ 41 /** The constant name of the input field to enter locations.*/
41 public static final String FIELD_LOCATION = "location"; 42 public static final String FIELD_LOCATION = "location";
42 43
44 /** The constant name of the input field to enter distance.*/
45 public static final String FIELD_DISTANCE = "distance";
46
43 /** The constant name of the input field to enter the start of a distance.*/ 47 /** The constant name of the input field to enter the start of a distance.*/
44 public static final String FIELD_FROM = "from"; 48 public static final String FIELD_FROM = "from";
45 49
46 /** The constant name of the input field to enter the end of a distance.*/ 50 /** The constant name of the input field to enter the end of a distance.*/
47 public static final String FIELD_TO = "to"; 51 public static final String FIELD_TO = "to";
50 * distance.*/ 54 * distance.*/
51 public static final String FIELD_WIDTH = "width"; 55 public static final String FIELD_WIDTH = "width";
52 56
53 57
54 /** The radio group for input mode selection.*/ 58 /** The radio group for input mode selection.*/
55 protected RadioGroupItem radio; 59 protected DynamicForm mode;
56 60
57 /** A container that will contain the location or the distance panel.*/ 61 /** A container that will contain the location or the distance panel.*/
58 protected HLayout container; 62 protected HLayout container;
63
64 /** The 'from' value entered in the distance mode.*/
65 protected double from;
66
67 /** The 'to' value entered in the distance mode.*/
68 protected double to;
69
70 /** The 'step' value entered in the distance mode.*/
71 protected double step;
72
73 /** The values entered in the location mode.*/
74 protected double[] values;
59 75
60 76
61 /** 77 /**
62 * Creates a new LocationDistancePanel instance. 78 * Creates a new LocationDistancePanel instance.
63 */ 79 */
115 131
116 if (value == null) { 132 if (value == null) {
117 return; 133 return;
118 } 134 }
119 135
120 if (value.equals(MESSAGES.location())) { 136 if (value.equals(FIELD_LOCATION)) {
121 // TODO Insert correct values 137 Canvas locationPanel = new DoubleArrayPanel(
122 Canvas locationPanel = createLocationPanel(new double[] {1.7, 2.5}); 138 MESSAGES.unitLocation(),
139 getLocationValues(),
140 this);
141
123 container.removeMembers(container.getMembers()); 142 container.removeMembers(container.getMembers());
124 container.addMember(locationPanel); 143 container.addMember(locationPanel);
125 } 144 }
126 else if (value.equals(MESSAGES.distance())){ 145 else {
127 // TODO Insert correct values 146 Canvas distancePanel = new DoubleRangePanel(
128 Canvas distancePanel = createDistancePanel(1.0, 3.4, 100); 147 MESSAGES.unitFrom(), MESSAGES.unitTo(), MESSAGES.unitWidth(),
148 getFrom(), getTo(), getStep(),
149 250,
150 this);
151
129 container.removeMembers(container.getMembers()); 152 container.removeMembers(container.getMembers());
130 container.addMember(distancePanel); 153 container.addMember(distancePanel);
131 } 154 }
132 } 155 }
133 156
144 167
145 if (field == null) { 168 if (field == null) {
146 return; 169 return;
147 } 170 }
148 171
149 if (field.equals(FIELD_LOCATION)) { 172 if (field.equals(DoubleArrayPanel.FIELD_NAME)) {
150 validateLocation(event.getForm(), item); 173 DoubleArrayPanel p = (DoubleArrayPanel) event.getForm();
151 } 174
152 else if (field.equals(FIELD_FROM)) { 175 if (p.validateForm(item)) {
153 validateDistance(event.getForm(), item); 176 setLocationValues(p.getInputValues(item));
154 }
155 else if (field.equals(FIELD_TO)) {
156 validateDistance(event.getForm(), item);
157 }
158 else if (field.equals(FIELD_WIDTH)) {
159 validateDistance(event.getForm(), item);
160 }
161 }
162
163
164 /**
165 * This method validates the entered text in the location input field. If
166 * there are values that doesn't represent a valid location, an error is
167 * displayed.
168 *
169 * @param form The DynamicForm.
170 * @param item The FormItem.
171 */
172 protected void validateLocation(DynamicForm form, FormItem item) {
173 String value = (String) item.getValue();
174 String[] parts = value.split(" ");
175
176 if (parts == null) {
177 return;
178 }
179
180 NumberFormat f = NumberFormat.getDecimalFormat();
181 Map errors = form.getErrors();
182
183 try {
184 for (String part: parts) {
185 double location = f.parse(part);
186
187 // TODO save the location
188 } 177 }
189 178 }
190 errors.remove(item.getFieldName()); 179 else {
191 } 180 DoubleRangePanel p = (DoubleRangePanel) event.getForm();
192 catch (NumberFormatException nfe) { 181
193 errors.put(item.getFieldName(), MESSAGES.wrongFormat()); 182 if (p.validateForm(item)) {
194 } 183 setFrom(p.getFrom());
195 184 setTo(p.getTo());
196 form.setErrors(errors, true); 185 setStep(p.getStep());
197 } 186 }
198 187 }
199
200 /**
201 * This method validates the entered text in the distance input fields. If
202 * there are values that doesn't represent a valid float, an error is
203 * displayed.
204 *
205 * @param form The DynamicForm.
206 * @param item The FormItem.
207 */
208 protected void validateDistance(DynamicForm form, FormItem item) {
209 String v = (String) item.getValue();
210
211 NumberFormat f = NumberFormat.getDecimalFormat();
212 Map errors = form.getErrors();
213
214 try {
215 double value = f.parse(v);
216
217 // TODO SAVE THE VALUE
218
219 errors.remove(item.getFieldName());
220 }
221 catch (NumberFormatException nfe) {
222 errors.put(item.getFieldName(), MESSAGES.wrongFormat());
223
224 item.focusInItem();
225 }
226
227 form.setErrors(errors, true);
228 } 188 }
229 189
230 190
231 /** 191 /**
232 * This method creates the panel that contains the checkboxes to switch 192 * This method creates the panel that contains the checkboxes to switch
233 * between the input mode 'location' and 'distance'. 193 * between the input mode 'location' and 'distance'.
234 * 194 *
235 * @return the checkbox panel. 195 * @return the checkbox panel.
236 */ 196 */
237 protected Canvas createRadioButtonPanel() { 197 protected Canvas createRadioButtonPanel() {
238 DynamicForm form = new DynamicForm(); 198 mode = new DynamicForm();
239 199
240 radio = new RadioGroupItem("mode"); 200 RadioGroupItem radio = new RadioGroupItem("mode");
241 radio.setShowTitle(false); 201 radio.setShowTitle(false);
242 radio.setVertical(false); 202 radio.setVertical(false);
243 radio.setValueMap(MESSAGES.location(), MESSAGES.distance()); 203
244 204 LinkedHashMap values = new LinkedHashMap();
205 values.put(FIELD_LOCATION, MESSAGES.location());
206 values.put(FIELD_DISTANCE, MESSAGES.distance());
207
208 radio.setValueMap(values);
245 radio.addChangeHandler(this); 209 radio.addChangeHandler(this);
246 210
247 form.setFields(radio); 211 mode.setFields(radio);
248 212
249 return form; 213 return mode;
250 } 214 }
251 215
252 216
253 /** 217 protected double getFrom() {
254 * This method creates the location panel. It contains just a single input 218 return from;
255 * field. 219 }
256 * 220
257 * @param locations For each location a double value 221
258 * 222 protected void setFrom(double from) {
259 * @return a panel with an input field. 223 this.from = from;
260 */ 224 }
261 protected Canvas createLocationPanel(double[] locations) { 225
262 TextItem ti = new TextItem(FIELD_LOCATION); 226
263 227 protected double getTo() {
264 ti.setTitle(MESSAGES.unitLocation()); 228 return to;
265 ti.addBlurHandler(this); 229 }
266 230
267 NumberFormat f = NumberFormat.getDecimalFormat(); 231
268 232 protected void setTo(double to) {
269 StringBuilder text = new StringBuilder(); 233 this.to = to;
270 boolean firstItem = true; 234 }
271 235
272 for (double loc: locations) { 236
273 if (!firstItem) { 237 protected double getStep() {
274 text.append(" "); 238 return step;
275 } 239 }
276 240
277 text.append(f.format(loc)); 241
278 242 protected void setStep(double step) {
279 firstItem = false; 243 this.step = step;
280 } 244 }
281 245
282 ti.setValue(text.toString()); 246
283 247 protected double[] getLocationValues() {
284 // TODO There is still a colon between the input field and the text - 248 return values;
285 // remove this colon! 249 }
286 250
287 DynamicForm form = new DynamicForm(); 251
288 form.setFields(ti); 252 protected void setLocationValues(double[] values) {
289 form.setTitleOrientation(TitleOrientation.RIGHT); 253 this.values = values;
290 form.setNumCols(2);
291
292 return form;
293 }
294
295
296 /**
297 * This method creates the distance panel. It contains three input fields:
298 * one for a start, one for the end and one for a step width.
299 *
300 * @param start The start point.
301 * @param end The end point.
302 * @param sw The step width.
303 *
304 * @return a panel with three input fields.
305 */
306 protected Canvas createDistancePanel(double start,double end,double sw) {
307 FloatItem from = new FloatItem(FIELD_FROM);
308 FloatItem to = new FloatItem(FIELD_TO);
309 FloatItem width = new FloatItem(FIELD_WIDTH);
310
311 from.addBlurHandler(this);
312 to.addBlurHandler(this);
313 width.addBlurHandler(this);
314
315 // TODO There is still a colon between the input field and the text -
316 // remove this colon!
317
318 NumberFormat f = NumberFormat.getDecimalFormat();
319
320 from.setValue(f.format(start));
321 to.setValue(f.format(end));
322 width.setValue(f.format(sw));
323
324 from.setWidth(50);
325 to.setWidth(50);
326 width.setWidth(50);
327
328 from.setTitle(MESSAGES.unitFrom());
329 to.setTitle(MESSAGES.unitTo());
330 width.setTitle(MESSAGES.unitWidth());
331
332 DynamicForm form = new DynamicForm();
333 form.setFields(from, to, width);
334 form.setTitleOrientation(TitleOrientation.RIGHT);
335 form.setNumCols(6);
336 form.setFixedColWidths(false);
337
338 return form;
339 } 254 }
340 } 255 }
341 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 256 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org