Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/SingleLocationPanel.java @ 1591:8ab010967f78
Refactored.
flys-client/trunk@3888 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Fri, 03 Feb 2012 08:30:58 +0000 |
parents | 4b773cfd11b5 |
children | ff9d71469b7c |
comparison
equal
deleted
inserted
replaced
1590:4b773cfd11b5 | 1591:8ab010967f78 |
---|---|
40 * This UIProvider creates a widget to enter a single location (km). | 40 * This UIProvider creates a widget to enter a single location (km). |
41 * | 41 * |
42 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | 42 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> |
43 */ | 43 */ |
44 public class SingleLocationPanel | 44 public class SingleLocationPanel |
45 extends AbstractUIProvider | 45 extends LocationPanel |
46 implements RecordClickHandler | 46 implements RecordClickHandler |
47 { | 47 { |
48 /** The DistanceInfoService used to retrieve locations about rivers. */ | 48 /** The DistanceInfoService used to retrieve locations about rivers. */ |
49 protected DistanceInfoServiceAsync distanceInfoService = | 49 protected DistanceInfoServiceAsync distanceInfoService = |
50 GWT.create(DistanceInfoService.class); | 50 GWT.create(DistanceInfoService.class); |
51 | 51 |
52 /** A container that will contain the location or the distance panel. */ | |
53 protected HLayout container; | |
54 | |
55 /** The minimal value that the user is allowed to enter. */ | |
56 protected double min; | |
57 | |
58 /** The maximal value that the user is allowed to enter. */ | |
59 protected double max; | |
60 | |
61 /** The values entered in the location mode. */ | |
62 protected double[] values; | |
63 | |
64 /** The input panel for locations. */ | |
65 protected DoubleArrayPanel locationPanel; | |
66 | |
67 /** The table data. */ | 52 /** The table data. */ |
68 protected DistanceInfoObject[] tableData; | 53 protected DistanceInfoObject[] tableData; |
69 | 54 |
70 /** Name of the data item that keeps locations. */ | 55 /** Name of the data item that keeps locations. */ |
71 protected String DATA_ITEM_NAME = "ld_locations"; | 56 protected String DATA_ITEM_NAME = "ld_locations"; |
95 public Canvas create(DataList data) { | 80 public Canvas create(DataList data) { |
96 VLayout layout = new VLayout(); | 81 VLayout layout = new VLayout(); |
97 layout.setMembersMargin(10); | 82 layout.setMembersMargin(10); |
98 | 83 |
99 Label label = new Label(MSG.location ()); | 84 Label label = new Label(MSG.location ()); |
100 //picker = new LocationPicker(this); | |
101 Canvas widget = createWidget(data); | 85 Canvas widget = createWidget(data); |
102 Canvas submit = getNextButton(); | 86 Canvas submit = getNextButton(); |
103 | 87 |
104 initDefaults(data); | 88 initDefaults(data); |
105 | 89 |
109 label.setHeight(25); | 93 label.setHeight(25); |
110 | 94 |
111 layout.addMember(label); | 95 layout.addMember(label); |
112 layout.addMember(widget); | 96 layout.addMember(widget); |
113 layout.addMember(submit); | 97 layout.addMember(submit); |
114 | |
115 return layout; | |
116 } | |
117 | |
118 | |
119 | |
120 /** | |
121 * This method creates a Canvas element showing the old Data objects in the | |
122 * DataList <i>data</i>. | |
123 */ | |
124 public Canvas createOld(DataList dataList) { | |
125 List<Data> items = dataList.getAll(); | |
126 Data dLocation = getData(items, DATA_ITEM_NAME); | |
127 DataItem[] loc = dLocation.getItems(); | |
128 | |
129 HLayout layout = new HLayout(); | |
130 layout.setWidth("400px"); | |
131 | |
132 Label label = new Label(dataList.getLabel()); | |
133 label.setWidth("200px"); | |
134 | |
135 Canvas back = getBackButton(dataList.getState()); | |
136 | |
137 Label selected = new Label(loc[0].getLabel()); | |
138 selected.setWidth("130px"); | |
139 | |
140 layout.addMember(label); | |
141 layout.addMember(selected); | |
142 layout.addMember(back); | |
143 | 98 |
144 return layout; | 99 return layout; |
145 } | 100 } |
146 | 101 |
147 | 102 |
201 // could not parse, dont know what to do else | 156 // could not parse, dont know what to do else |
202 } | 157 } |
203 } | 158 } |
204 | 159 |
205 | 160 |
161 protected Canvas createWidget(DataList data) { | |
162 VLayout layout = new VLayout(); | |
163 inputLayout = new HLayout(); | |
164 | |
165 // The initial view will display the location input mode. | |
166 locationPanel = new DoubleArrayPanel( | |
167 MSG.unitLocation(), | |
168 getLocationValues(), | |
169 new BlurHandler(){public void onBlur(BlurEvent be) {}}); | |
170 | |
171 picker.getLocationTable().setAutoFetchData(true); | |
172 | |
173 inputLayout.addMember(locationPanel); | |
174 | |
175 layout.addMember(inputLayout); | |
176 | |
177 inputLayout.setMembersMargin(30); | |
178 | |
179 picker.prepareFilter(); | |
180 | |
181 helperContainer.addMember(picker.getLocationTable()); | |
182 helperContainer.addMember(picker.getFilterLayout()); | |
183 helperContainer.addMember(picker.getResultCountForm()); | |
184 setPickerDataSource(); | |
185 return layout; | |
186 } | |
187 | |
188 | |
206 /** | 189 /** |
207 * This method grabs the Data with name <i>name</i> from the list and | 190 * This method grabs the Data with name <i>name</i> from the list and |
208 * returns it. | 191 * returns it. |
209 * | 192 * |
210 * @param items A list of Data. | 193 * @param items A list of Data. |
222 | 205 |
223 return null; | 206 return null; |
224 } | 207 } |
225 | 208 |
226 | 209 |
227 protected Canvas createWidget(DataList data) { | |
228 VLayout layout = new VLayout(); | |
229 container = new HLayout(); | |
230 | |
231 // The initial view will display the location input mode. | |
232 locationPanel = new DoubleArrayPanel( | |
233 MSG.unitLocation(), | |
234 getLocationValues(), | |
235 new BlurHandler(){public void onBlur(BlurEvent be) {}}); | |
236 | |
237 picker.getLocationTable().setAutoFetchData(true); | |
238 | |
239 container.addMember(locationPanel); | |
240 | |
241 layout.addMember(container); | |
242 | |
243 container.setMembersMargin(30); | |
244 | |
245 picker.prepareFilter(); | |
246 | |
247 helperContainer.addMember(picker.getLocationTable()); | |
248 helperContainer.addMember(picker.getFilterLayout()); | |
249 helperContainer.addMember(picker.getResultCountForm()); | |
250 createInputPanel(); | |
251 return layout; | |
252 } | |
253 | |
254 | |
255 @Override | |
256 public List<String> validate() { | |
257 List<String> errors = new ArrayList<String>(); | |
258 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
259 | |
260 saveLocationValues(locationPanel); | |
261 | |
262 if (!locationPanel.validateForm()) { | |
263 errors.add(MSG.wrongFormat()); | |
264 return errors; | |
265 } | |
266 | |
267 double[] values = getLocationValues(); | |
268 double[] good = new double[values.length]; | |
269 int idx = 0; | |
270 | |
271 if (values.length > 1) { | |
272 errors.add(MSG.too_many_values()); | |
273 } | |
274 | |
275 for (double value: values) { | |
276 if (value < min || value > max) { | |
277 String tmp = MSG.error_validate_range(); | |
278 tmp = tmp.replace("$1", nf.format(value)); | |
279 tmp = tmp.replace("$2", nf.format(min)); | |
280 tmp = tmp.replace("$3", nf.format(max)); | |
281 errors.add(tmp); | |
282 } | |
283 else { | |
284 good[idx++] = value; | |
285 } | |
286 } | |
287 | |
288 double[] justGood = new double[idx]; | |
289 for (int i = 0; i < justGood.length; i++) { | |
290 justGood[i] = good[i]; | |
291 } | |
292 | |
293 if (!errors.isEmpty()) { | |
294 locationPanel.setValues(justGood); | |
295 } | |
296 | |
297 return errors; | |
298 } | |
299 | |
300 | |
301 /** Create simple DefaultData with single DataItem inside. */ | |
302 public DefaultData createDataArray(String name, String value) { | |
303 DataItem item = new DefaultDataItem( | |
304 name, | |
305 name, | |
306 value); | |
307 | |
308 return new DefaultData(name, | |
309 null, | |
310 null, | |
311 new DataItem[] {item}); | |
312 } | |
313 | |
314 | |
315 /** | 210 /** |
316 * This method returns the selected data. | 211 * This method returns the selected data. |
317 * | 212 * |
318 * @return the selected/inserted data. | 213 * @return the selected/inserted data. |
319 */ | 214 */ |
330 | 225 |
331 return data; | 226 return data; |
332 } | 227 } |
333 | 228 |
334 | 229 |
335 /** | 230 /** Hook service to the listgrid with possible input values. */ |
336 * Validates and stores all values entered in the location mode. | 231 protected void setPickerDataSource() { |
337 * | |
338 * @param p The DoubleArrayPanel. | |
339 */ | |
340 protected void saveLocationValues(DoubleArrayPanel p) { | |
341 FormItem[] formItems = p.getFields(); | |
342 | |
343 for (FormItem item: formItems) { | |
344 if (item.getFieldName().equals(DoubleArrayPanel.FIELD_NAME)) { | |
345 saveLocationValue(p, item); | |
346 } | |
347 } | |
348 } | |
349 | |
350 | |
351 /** | |
352 * Validates and stores a value entered in the location mode. | |
353 * | |
354 * @param p The DoubleArrayPanel. | |
355 * @param item The item that needs to be validated. | |
356 */ | |
357 protected void saveLocationValue(DoubleArrayPanel p, FormItem item) { | |
358 if (p.validateForm(item)) { | |
359 setLocationValues(p.getInputValues(item)); | |
360 } | |
361 } | |
362 | |
363 | |
364 protected void createInputPanel() { | |
365 Config config = Config.getInstance(); | 232 Config config = Config.getInstance(); |
366 String url = config.getServerUrl(); | 233 String url = config.getServerUrl(); |
367 String river = ""; | 234 String river = ""; |
368 | 235 |
369 ArtifactDescription adescr = artifact.getArtifactDescription(); | 236 ArtifactDescription adescr = artifact.getArtifactDescription(); |
387 } | 254 } |
388 } | 255 } |
389 } | 256 } |
390 | 257 |
391 picker.getLocationTable().setDataSource(new DistanceInfoDataSource( | 258 picker.getLocationTable().setDataSource(new DistanceInfoDataSource( |
392 url, /*river*/ "Saar" , "locations")); | 259 url, river, "locations")); |
393 } | |
394 | |
395 | |
396 /** Get the location values. */ | |
397 protected double[] getLocationValues() { | |
398 return values; | |
399 } | |
400 | |
401 | |
402 /** Sets Location values and updates the panel. */ | |
403 protected void setLocationValues(double[] values) { | |
404 this.values = values; | |
405 locationPanel.setValues(values); | |
406 } | 260 } |
407 | 261 |
408 | 262 |
409 /** | 263 /** |
410 * Callback when an item from the input helper was clicked. | 264 * Callback when an item from the input helper was clicked. |