comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java @ 41:87a44f8e25cc

Added a new widget that enables the user to enter a location or a distance in a single step. flys-client/trunk@1482 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 16 Mar 2011 09:57:41 +0000
parents
children 6bcd8e3f21a7
comparison
equal deleted inserted replaced
40:1458bc0a20e2 41:87a44f8e25cc
1 package de.intevation.flys.client.client.ui;
2
3 import java.util.Map;
4
5 import com.google.gwt.core.client.GWT;
6 import com.google.gwt.i18n.client.NumberFormat;
7
8 import com.smartgwt.client.types.TitleOrientation;
9 import com.smartgwt.client.widgets.Canvas;
10 import com.smartgwt.client.widgets.Label;
11 import com.smartgwt.client.widgets.form.DynamicForm;
12 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
13 import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
14 import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
15 import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
16 import com.smartgwt.client.widgets.form.fields.FormItem;
17 import com.smartgwt.client.widgets.form.fields.FloatItem;
18 import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
19 import com.smartgwt.client.widgets.form.fields.TextItem;
20 import com.smartgwt.client.widgets.layout.HLayout;
21 import com.smartgwt.client.widgets.layout.VLayout;
22
23 import de.intevation.flys.client.shared.model.Data;
24 import de.intevation.flys.client.client.FLYSMessages;
25
26
27 /**
28 * This UIProvider creates a widget to enter locations or a distance.
29 *
30 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
31 */
32 public class LocationDistancePanel
33 extends AbstractUIProvider
34 implements ChangeHandler, BlurHandler
35 {
36 /** The message class that provides i18n strings.*/
37 protected FLYSMessages MESSAGES = GWT.create(FLYSMessages.class);
38
39
40 /** The constant name of the input field to enter locations.*/
41 public static final String FIELD_LOCATION = "location";
42
43 /** The constant name of the input field to enter the start of a distance.*/
44 public static final String FIELD_FROM = "from";
45
46 /** The constant name of the input field to enter the end of a distance.*/
47 public static final String FIELD_TO = "to";
48
49 /** The constant name of the input field to enter the step width of a
50 * distance.*/
51 public static final String FIELD_WIDTH = "width";
52
53
54 /** The radio group for input mode selection.*/
55 protected RadioGroupItem radio;
56
57 /** A container that will contain the location or the distance panel.*/
58 protected HLayout container;
59
60
61 /**
62 * Creates a new LocationDistancePanel instance.
63 */
64 public LocationDistancePanel() {
65 }
66
67
68 /**
69 * This method creates a widget that contains a label, a panel with
70 * checkboxes to switch the input mode between location and distance input,
71 * and a the mode specific panel.
72 *
73 * @param data The data that might be inserted.
74 *
75 * @return a panel.
76 */
77 public Canvas create(Data data) {
78 VLayout layout = new VLayout();
79
80 Label label = new Label(MESSAGES.location_distance_state());
81 container = new HLayout();
82 Canvas checkboxPanel = createRadioButtonPanel();
83
84 label.setHeight(25);
85
86 layout.addMember(label);
87 layout.addMember(checkboxPanel);
88 layout.addMember(container);
89
90 // TODO Add a table on the right to select locations by mouse click
91
92 return layout;
93 }
94
95
96 /**
97 * This method returns the selected data.
98 *
99 * @return the selected/inserted data.
100 */
101 public Data[] getData() {
102 // TODO Implement me
103
104 return null;
105 }
106
107
108 /**
109 * This method switches the input mode between location and distance input.
110 *
111 * @param event The click event fired by a RadioButtonGroupItem.
112 */
113 public void onChange(ChangeEvent event) {
114 String value = (String) event.getValue();
115
116 if (value == null) {
117 return;
118 }
119
120 if (value.equals(MESSAGES.location())) {
121 // TODO Insert correct values
122 Canvas locationPanel = createLocationPanel(new double[] {1.7, 2.5});
123 container.removeMembers(container.getMembers());
124 container.addMember(locationPanel);
125 }
126 else if (value.equals(MESSAGES.distance())){
127 // TODO Insert correct values
128 Canvas distancePanel = createDistancePanel(1.0, 3.4, 100);
129 container.removeMembers(container.getMembers());
130 container.addMember(distancePanel);
131 }
132 }
133
134
135 /**
136 * This method is used to validate the inserted data in the form fields.
137 *
138 * @param event The BlurEvent that gives information about the FormItem that
139 * has been modified and its value.
140 */
141 public void onBlur(BlurEvent event) {
142 FormItem item = event.getItem();
143 String field = item.getFieldName();
144
145 if (field == null) {
146 return;
147 }
148
149 if (field.equals(FIELD_LOCATION)) {
150 validateLocation(event.getForm(), item);
151 }
152 else if (field.equals(FIELD_FROM)) {
153 validateDistance(event.getForm(), 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 }
189
190 errors.remove(item.getFieldName());
191 }
192 catch (NumberFormatException nfe) {
193 errors.put(item.getFieldName(), MESSAGES.wrongFormat());
194 }
195
196 form.setErrors(errors, true);
197 }
198
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 }
229
230
231 /**
232 * This method creates the panel that contains the checkboxes to switch
233 * between the input mode 'location' and 'distance'.
234 *
235 * @return the checkbox panel.
236 */
237 protected Canvas createRadioButtonPanel() {
238 DynamicForm form = new DynamicForm();
239
240 radio = new RadioGroupItem("mode");
241 radio.setShowTitle(false);
242 radio.setVertical(false);
243 radio.setValueMap(MESSAGES.location(), MESSAGES.distance());
244
245 radio.addChangeHandler(this);
246
247 form.setFields(radio);
248
249 return form;
250 }
251
252
253 /**
254 * This method creates the location panel. It contains just a single input
255 * field.
256 *
257 * @param locations For each location a double value
258 *
259 * @return a panel with an input field.
260 */
261 protected Canvas createLocationPanel(double[] locations) {
262 TextItem ti = new TextItem(FIELD_LOCATION);
263
264 ti.setTitle(MESSAGES.unitLocation());
265 ti.addBlurHandler(this);
266
267 NumberFormat f = NumberFormat.getDecimalFormat();
268
269 StringBuilder text = new StringBuilder();
270 boolean firstItem = true;
271
272 for (double loc: locations) {
273 if (!firstItem) {
274 text.append(" ");
275 }
276
277 text.append(f.format(loc));
278
279 firstItem = false;
280 }
281
282 ti.setValue(text.toString());
283
284 // TODO There is still a colon between the input field and the text -
285 // remove this colon!
286
287 DynamicForm form = new DynamicForm();
288 form.setFields(ti);
289 form.setTitleOrientation(TitleOrientation.RIGHT);
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 }
340 }
341 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org