comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/QSegmentedInputPanel.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/client/ui/QSegmentedInputPanel.java@d07abdb7ed7f
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.client.ui;
2
3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.i18n.client.NumberFormat;
5 import com.google.gwt.user.client.rpc.AsyncCallback;
6
7 import com.smartgwt.client.types.TitleOrientation;
8 import com.smartgwt.client.types.VerticalAlignment;
9 import com.smartgwt.client.util.SC;
10 import com.smartgwt.client.widgets.Canvas;
11 import com.smartgwt.client.widgets.Label;
12 import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
13 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
14 import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
15 import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
16 import com.smartgwt.client.widgets.layout.HLayout;
17 import com.smartgwt.client.widgets.layout.VLayout;
18 import com.smartgwt.client.widgets.tab.Tab;
19 import com.smartgwt.client.widgets.tab.TabSet;
20
21 import de.intevation.flys.client.client.Config;
22 import de.intevation.flys.client.client.FLYSConstants;
23 import de.intevation.flys.client.client.services.WQInfoService;
24 import de.intevation.flys.client.client.services.WQInfoServiceAsync;
25 import de.intevation.flys.client.client.ui.wq.QDTable;
26 import de.intevation.flys.client.client.ui.wq.WTable;
27 import de.intevation.flys.client.shared.model.ArtifactDescription;
28 import de.intevation.flys.client.shared.model.Data;
29 import de.intevation.flys.client.shared.model.DataItem;
30 import de.intevation.flys.client.shared.model.DataList;
31 import de.intevation.flys.client.shared.model.DefaultData;
32 import de.intevation.flys.client.shared.model.DefaultDataItem;
33 import de.intevation.flys.client.shared.model.WQDataItem;
34 import de.intevation.flys.client.shared.model.WQInfoObject;
35 import de.intevation.flys.client.shared.model.WQInfoRecord;
36
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Map;
42
43
44 /**
45 * This UIProvider creates a widget to enter Q values per segment.
46 */
47 public class QSegmentedInputPanel
48 extends AbstractUIProvider
49 implements ChangeHandler, BlurHandler
50 {
51
52 private static final long serialVersionUID = -8627825064071479905L;
53
54 public static final String FIELD_WQ_MODE = "wq_isq";
55 public static final String FIELD_WQ_Q = "Q";
56
57 public static final String GAUGE_SEPARATOR = ":";
58
59 public static final String GAUGE_PART_SEPARATOR = ";";
60
61 public static final String VALUE_SEPARATOR = ",";
62
63 public static final int ROW_HEIGHT = 20;
64
65 /** The constant field name for choosing single values or range.*/
66 public static final String FIELD_MODE = "mode";
67
68 /** The constant field value for range input mode.*/
69 public static final String FIELD_MODE_RANGE = "range";
70
71 protected WQInfoServiceAsync wqInfoService =
72 GWT.create(WQInfoService.class);
73
74 /** The message class that provides i18n strings.*/
75 protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
76
77 /** Stores the input panels related to their keys.*/
78 protected Map<String, DoubleArrayPanel> wqranges;
79
80 /** Stores the min/max values for each q range.*/
81 protected Map<String, double[]> qranges;
82
83 protected QDTable qdTable;
84
85 protected WTable wTable;
86
87 protected TabSet tabs;
88
89
90 public QSegmentedInputPanel() {
91 wqranges = new HashMap<String, DoubleArrayPanel>();
92 qranges = new HashMap<String, double[]>();
93 qdTable = new QDTable();
94 wTable = new WTable();
95 }
96
97
98 /** Create main UI Canvas. */
99 @Override
100 public Canvas create(DataList data) {
101 initHelperPanel();
102
103 Canvas submit = getNextButton();
104 Canvas widget = createWidget(data);
105 Label label = new Label(MSG.wqadaptedTitle());
106
107 label.setHeight(25);
108
109 VLayout layout = new VLayout();
110 layout.setMembersMargin(10);
111 layout.setWidth(350);
112
113 layout.addMember(label);
114 layout.addMember(widget);
115 layout.addMember(submit);
116
117 return layout;
118 }
119
120
121 protected void initHelperPanel() {
122 tabs = new TabSet();
123 tabs.setWidth100();
124 tabs.setHeight100();
125
126 // TODO i18n
127 Tab qTab = new Tab("Q / D");
128
129 qTab.setPane(qdTable);
130 qdTable.hideIconFields();
131
132 tabs.addTab(qTab, 1);
133
134 helperContainer.addMember(tabs);
135
136 // TODO Q only would suffice.
137 fetchWQData();
138 }
139
140
141 /** Create display for passive mode. */
142 @Override
143 public Canvas createOld(DataList dataList) {
144 List<Data> all = dataList.getAll();
145 Data wqData = getData(all, "ranges");
146
147 Canvas back = getBackButton(dataList.getState());
148
149 HLayout valLayout = new HLayout();
150 VLayout vlayout = new VLayout();
151 Label wqLabel = new Label(dataList.getLabel());
152
153 wqLabel.setValign(VerticalAlignment.TOP);
154
155 wqLabel.setWidth(200);
156 wqLabel.setHeight(25);
157
158 valLayout.addMember(wqLabel);
159 valLayout.addMember(createOldWQValues(wqData));
160 valLayout.addMember(back);
161
162 vlayout.addMember(valLayout);
163
164 return vlayout;
165 }
166
167
168 /** Create canvas showing previously entered values. */
169 protected Canvas createOldWQValues(Data wqData) {
170 VLayout layout = new VLayout();
171
172 //TODO: Sort by first field, numerically.
173
174 DataItem item = wqData.getItems()[0];
175 String value = item.getStringValue();
176
177 String[] gauges = value.split(GAUGE_SEPARATOR);
178
179 for (String gauge: gauges) {
180 HLayout h = new HLayout();
181
182 String[] parts = gauge.split(GAUGE_PART_SEPARATOR);
183 String[] values = parts[3].split(VALUE_SEPARATOR);
184
185 Label l = new Label(parts[0] + " - " + parts[1] + ": ");
186
187 StringBuilder sb = new StringBuilder();
188 boolean first = true;
189
190 for (String v: values) {
191 if (!first) {
192 sb.append(", ");
193 }
194
195 sb.append(v);
196
197 first = false;
198 }
199
200 Label v = new Label(sb.toString());
201
202 l.setWidth(65);
203 v.setWidth(65);
204
205 h.addMember(l);
206 h.addMember(v);
207
208 layout.addMember(h);
209 }
210
211 return layout;
212 }
213
214
215 protected Canvas createWidget(DataList dataList) {
216 VLayout layout = new VLayout();
217
218 Canvas list = createList(dataList);
219
220 DataItem[] items = getWQItems(dataList);
221 int listHeight = ROW_HEIGHT * items.length;
222
223 layout.addMember(list);
224
225 layout.setHeight(25 + listHeight);
226 layout.setWidth(350);
227
228 return layout;
229 }
230
231
232 @Override
233 public List<String> validate() {
234 List<String> errors = new ArrayList<String>();
235 NumberFormat nf = NumberFormat.getDecimalFormat();
236
237 Iterator<String> iter = wqranges.keySet().iterator();
238
239 while (iter.hasNext()) {
240 List<String> tmpErrors = new ArrayList<String>();
241
242 String key = iter.next();
243 DoubleArrayPanel dap = wqranges.get(key);
244
245 if (!dap.validateForm()) {
246 errors.add(MSG.error_invalid_double_value());
247 return errors;
248 }
249
250 double[] mm = qranges.get(key);
251 if (mm == null) {
252 SC.warn(MSG.error_read_minmax_values());
253 continue;
254 }
255
256 double[] values = dap.getInputValues();
257 // might geht npe here if one field not filled
258 double[] good = new double[values.length];
259
260 int idx = 0;
261
262 for (double value: values) {
263 if (value < mm[0] || value > mm[1]) {
264 String tmp = MSG.error_validate_range();
265 tmp = tmp.replace("$1", nf.format(value));
266 tmp = tmp.replace("$2", nf.format(mm[0]));
267 tmp = tmp.replace("$3", nf.format(mm[1]));
268 tmpErrors.add(tmp);
269 }
270 else {
271 good[idx++] = value;
272 }
273 }
274
275 double[] justGood = new double[idx];
276 for (int i = 0; i < justGood.length; i++) {
277 justGood[i] = good[i];
278 }
279
280 if (!tmpErrors.isEmpty()) {
281 dap.setValues(justGood);
282
283 errors.addAll(tmpErrors);
284 }
285 }
286
287 return errors;
288 }
289
290
291 protected Canvas createList(DataList dataList) {
292 VLayout layout = new VLayout();
293
294 DataItem[] items = getWQItems(dataList);
295
296 for (DataItem item: items) {
297 String title = item.getLabel();
298
299 DoubleArrayPanel dap = new DoubleArrayPanel(
300 createLineTitle(title), null, this, null, TitleOrientation.LEFT);
301
302 wqranges.put(title, dap);
303
304 if (item instanceof WQDataItem) {
305 WQDataItem wq = (WQDataItem) item;
306 double[] mmQ = wq.getQRange();
307
308 qranges.put(title, mmQ);
309 }
310
311 layout.addMember(dap);
312 }
313
314 layout.setHeight(items.length * ROW_HEIGHT);
315
316 return layout;
317 }
318
319
320 protected DataItem[] getWQItems(DataList dataList) {
321 List<Data> data = dataList.getAll();
322
323 for (Data d: data) {
324 String name = d.getLabel();
325
326 // TODO to be gone
327 if (name.equals(FIELD_WQ_MODE)) {
328 continue;
329 }
330
331 return d.getItems();
332 }
333
334 return null;
335 }
336
337
338
339 public String createLineTitle(String key) {
340 String[] splitted = key.split(";");
341
342 return splitted[0] + " - " + splitted[1];
343 }
344
345
346 @Override
347 public Data[] getData() {
348 Data values = getWQValues();
349
350 return new Data[] { values };
351 }
352
353
354 protected Data getWQValues() {
355 String wqvalue = null;
356
357 Iterator<String> iter = wqranges.keySet().iterator();
358 while (iter.hasNext()) {
359 String key = iter.next();
360 DoubleArrayPanel dap = wqranges.get(key);
361
362 double[] values = dap.getInputValues();
363 if (wqvalue == null) {
364 wqvalue = createValueString(key + "; ", values);
365 }
366 else {
367 wqvalue += GAUGE_SEPARATOR + createValueString(key + "; ", values);
368 }
369 }
370
371 // TODO probably ranges
372 DataItem valueItem = new DefaultDataItem(
373 "ranges", "ranges", wqvalue);
374 Data values = new DefaultData(
375 "ranges", null, null, new DataItem[] { valueItem });
376
377 return values;
378 }
379
380
381 protected String createValueString(String key, double[] values) {
382 StringBuilder sb = new StringBuilder();
383
384 boolean first = true;
385
386 for (double value: values) {
387 if (!first) {
388 sb.append(",");
389 }
390
391 sb.append(Double.toString(value));
392
393 first = false;
394 }
395
396 return key + ";" + sb.toString();
397 }
398
399
400 @Override
401 public void onChange(ChangeEvent event) {
402 // TODO IMPLEMENT ME
403 }
404
405
406 @Override
407 public void onBlur(BlurEvent event) {
408 DoubleArrayPanel dap = (DoubleArrayPanel) event.getForm();
409 dap.validateForm(event.getItem());
410 }
411
412
413 protected void fetchWQData() {
414 Config config = Config.getInstance();
415 String locale = config.getLocale ();
416
417 ArtifactDescription adescr = artifact.getArtifactDescription();
418 DataList[] data = adescr.getOldData();
419
420 double[] mm = getMinMaxKM(data);
421 String river = getRiverName(data);
422
423 wqInfoService.getWQInfo(locale, river, mm[0], mm[0],
424 new AsyncCallback<WQInfoObject[]>() {
425 @Override
426 public void onFailure(Throwable caught) {
427 GWT.log("Could not recieve wq informations.");
428 SC.warn(caught.getMessage());
429 }
430
431 @Override
432 public void onSuccess(WQInfoObject[] wqi) {
433 int num = wqi != null ? wqi.length :0;
434 GWT.log("Recieved " + num + " wq informations.");
435
436 if (num == 0) {
437 return;
438 }
439
440 addWQInfo(wqi);
441
442 }
443 }
444 );
445 }
446
447
448 protected void addWQInfo (WQInfoObject[] wqi) {
449 for(WQInfoObject wi: wqi) {
450 WQInfoRecord rec = new WQInfoRecord(wi);
451
452 if (wi.getType().equals("W")) {
453 wTable.addData(rec);
454 }
455 else {
456 qdTable.addData(rec);
457 }
458 }
459 }
460
461
462 /**
463 * Determines the min and max kilometer value selected in a former state. A
464 * bit silly, but we need to run over each value of the "old data" to find
465 * such values because it is not available here.
466 *
467 * @param data The DataList which contains the whole data inserted for the
468 * current artifact.
469 *
470 * @return a double array with [min, max].
471 */
472 protected double[] getMinMaxKM(DataList[] data) {
473 ArtifactDescription adesc = artifact.getArtifactDescription();
474 return adesc.getKMRange();
475 }
476
477
478 /**
479 * Returns the name of the selected river.
480 *
481 * @param data The DataList with all data.
482 *
483 * @return the name of the current river.
484 */
485 protected String getRiverName(DataList[] data) {
486 ArtifactDescription adesc = artifact.getArtifactDescription();
487 return adesc.getRiver();
488 }
489 }
490 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org