Mercurial > dive4elements > river
comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/WQAdaptedInputPanel.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/WQAdaptedInputPanel.java@496c8c9d4913 |
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.data.Record; | |
8 import com.smartgwt.client.types.TitleOrientation; | |
9 import com.smartgwt.client.types.VerticalAlignment; | |
10 import com.smartgwt.client.util.SC; | |
11 import com.smartgwt.client.widgets.Canvas; | |
12 import com.smartgwt.client.widgets.Label; | |
13 import com.smartgwt.client.widgets.form.DynamicForm; | |
14 import com.smartgwt.client.widgets.form.fields.RadioGroupItem; | |
15 import com.smartgwt.client.widgets.form.fields.events.BlurEvent; | |
16 import com.smartgwt.client.widgets.form.fields.events.BlurHandler; | |
17 import com.smartgwt.client.widgets.form.fields.events.ChangeEvent; | |
18 import com.smartgwt.client.widgets.form.fields.events.ChangeHandler; | |
19 import com.smartgwt.client.widgets.form.fields.events.FocusEvent; | |
20 import com.smartgwt.client.widgets.form.fields.events.FocusHandler; | |
21 import com.smartgwt.client.widgets.grid.events.CellClickEvent; | |
22 import com.smartgwt.client.widgets.grid.events.CellClickHandler; | |
23 import com.smartgwt.client.widgets.layout.HLayout; | |
24 import com.smartgwt.client.widgets.layout.VLayout; | |
25 import com.smartgwt.client.widgets.tab.Tab; | |
26 import com.smartgwt.client.widgets.tab.TabSet; | |
27 | |
28 import de.intevation.flys.client.client.Config; | |
29 import de.intevation.flys.client.client.FLYSConstants; | |
30 import de.intevation.flys.client.client.services.WQInfoService; | |
31 import de.intevation.flys.client.client.services.WQInfoServiceAsync; | |
32 import de.intevation.flys.client.client.ui.wq.QDTable; | |
33 import de.intevation.flys.client.client.ui.wq.WTable; | |
34 import de.intevation.flys.client.shared.model.ArtifactDescription; | |
35 import de.intevation.flys.client.shared.model.Data; | |
36 import de.intevation.flys.client.shared.model.DataItem; | |
37 import de.intevation.flys.client.shared.model.DataList; | |
38 import de.intevation.flys.client.shared.model.DefaultData; | |
39 import de.intevation.flys.client.shared.model.DefaultDataItem; | |
40 import de.intevation.flys.client.shared.model.WQDataItem; | |
41 import de.intevation.flys.client.shared.model.WQInfoObject; | |
42 import de.intevation.flys.client.shared.model.WQInfoRecord; | |
43 | |
44 import java.util.ArrayList; | |
45 import java.util.HashMap; | |
46 import java.util.Iterator; | |
47 import java.util.LinkedHashMap; | |
48 import java.util.List; | |
49 import java.util.Map; | |
50 | |
51 | |
52 /** | |
53 * This UIProvider creates a widget to enter W or Q data for discharge | |
54 * longitudinal section computations. | |
55 * | |
56 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
57 */ | |
58 public class WQAdaptedInputPanel | |
59 extends AbstractUIProvider | |
60 implements ChangeHandler, BlurHandler, FocusHandler | |
61 { | |
62 private static final long serialVersionUID = -3218827566805476423L; | |
63 | |
64 /** The message class that provides i18n strings. */ | |
65 protected FLYSConstants MESSAGE = GWT.create(FLYSConstants.class); | |
66 | |
67 public static final String FIELD_WQ_MODE = "wq_isq"; | |
68 public static final String FIELD_WQ_W = "W"; | |
69 public static final String FIELD_WQ_Q = "Q"; | |
70 | |
71 public static final String GAUGE_SEPARATOR = ":"; | |
72 | |
73 public static final String GAUGE_PART_SEPARATOR = ";"; | |
74 | |
75 public static final String VALUE_SEPARATOR = ","; | |
76 | |
77 public static final int ROW_HEIGHT = 20; | |
78 | |
79 /** The constant field name for choosing w or q mode. */ | |
80 public static final String FIELD_WQ = "wq"; | |
81 | |
82 /** The constant field name for choosing single values or range. */ | |
83 public static final String FIELD_MODE = "mode"; | |
84 | |
85 /** The constant field value for range input mode. */ | |
86 public static final String FIELD_MODE_RANGE = "range"; | |
87 | |
88 /** Service to fetch W/Q MainValues. */ | |
89 protected WQInfoServiceAsync wqInfoService = | |
90 GWT.create(WQInfoService.class); | |
91 | |
92 /** The message class that provides i18n strings. */ | |
93 protected FLYSConstants MSG = GWT.create(FLYSConstants.class); | |
94 | |
95 /** Stores the input panels related to their keys. */ | |
96 protected Map<String, DoubleArrayPanel> wqranges; | |
97 | |
98 /** List of doubleArrayPanels shown. */ | |
99 protected ArrayList<DoubleArrayPanel> doubleArrayPanels; | |
100 | |
101 /** [startkm,endkm] per gauge in selected range. */ | |
102 protected double[][] gaugeRanges; | |
103 | |
104 /** Stores the min/max values for each q range (gauge). */ | |
105 protected Map<String, double[]> qranges; | |
106 | |
107 /** Stores the min/max values for each w range (gauge). */ | |
108 protected Map<String, double[]> wranges; | |
109 | |
110 /** The RadioGroupItem that determines the w/q input mode. */ | |
111 protected DynamicForm modes; | |
112 | |
113 /** List of wTables in inputhelper section. */ | |
114 protected List<WTable> wTables; | |
115 | |
116 /** List of QDTables in inputhelper section. */ | |
117 protected List<QDTable> qdTables; | |
118 | |
119 /** Tabs in inputhelper area. */ | |
120 protected TabSet tabs; | |
121 | |
122 /** The currently focussed Input element. */ | |
123 protected DoubleArrayPanel itemWithFocus; | |
124 | |
125 | |
126 public WQAdaptedInputPanel() { | |
127 wqranges = new HashMap<String, DoubleArrayPanel>(); | |
128 doubleArrayPanels = new ArrayList<DoubleArrayPanel>(); | |
129 qranges = new HashMap<String, double[]>(); | |
130 wranges = new HashMap<String, double[]>(); | |
131 wTables = new ArrayList<WTable>(); | |
132 qdTables = new ArrayList<QDTable>(); | |
133 } | |
134 | |
135 | |
136 @Override | |
137 public Canvas create(DataList data) { | |
138 readGaugeRanges(data); | |
139 initHelperPanel(); | |
140 | |
141 Canvas submit = getNextButton(); | |
142 Canvas widget = createWidget(data); | |
143 Label label = new Label(MSG.wqadaptedTitle()); | |
144 | |
145 label.setHeight(25); | |
146 | |
147 VLayout layout = new VLayout(); | |
148 layout.setMembersMargin(10); | |
149 layout.setWidth(350); | |
150 | |
151 layout.addMember(label); | |
152 layout.addMember(widget); | |
153 layout.addMember(submit); | |
154 | |
155 fetchWQData(); | |
156 | |
157 initTableListeners(); | |
158 | |
159 return layout; | |
160 } | |
161 | |
162 | |
163 /** Inits the helper panel. */ | |
164 // TODO duplicate in WQInputPanel | |
165 protected void initHelperPanel() { | |
166 tabs = new TabSet(); | |
167 tabs.setWidth100(); | |
168 tabs.setHeight100(); | |
169 | |
170 // For each gauge, add two tabs with helper tables. | |
171 | |
172 for (int i = 0; i< gaugeRanges.length; i++) { | |
173 // Later the tabs title will get adjusted to include gauges name. | |
174 // TODO the tabs title becomes rather long through that (i18n). | |
175 Tab wTab = new Tab(MESSAGE.wq_table_w()); | |
176 Tab qTab = new Tab(MESSAGE.wq_table_q()); | |
177 | |
178 QDTable qdTable = new QDTable(); | |
179 WTable wTable = new WTable(); | |
180 | |
181 wTables.add(wTable); | |
182 qdTables.add(qdTable); | |
183 | |
184 qdTable.showSelect(); | |
185 //wTable.showSelect(); | |
186 wTab.setPane(wTable); | |
187 qTab.setPane(qdTable); | |
188 | |
189 tabs.addTab(wTab, i*2+0); | |
190 tabs.addTab(qTab, i*2+1); | |
191 } | |
192 | |
193 helperContainer.addMember(tabs); | |
194 } | |
195 | |
196 | |
197 /** | |
198 * Initializes the listeners of the WQD tables. | |
199 */ | |
200 // TODO dupe from WQInputPanel | |
201 protected void initTableListeners() { | |
202 int i = 0; | |
203 for (QDTable qdTable: qdTables) { | |
204 // Register listener such that values are filled in on click. | |
205 final QDTable table = qdTable; | |
206 final int fi = i; | |
207 CellClickHandler handler = new CellClickHandler() { | |
208 @Override | |
209 public void onCellClick(CellClickEvent e) { | |
210 if (isWMode() || table.isLocked()) { | |
211 return; | |
212 } | |
213 | |
214 int idx = e.getColNum(); | |
215 Record r = e.getRecord(); | |
216 double val = r.getAttributeAsDouble("value"); | |
217 | |
218 doubleArrayPanels.get(fi).setValues(new double[]{val}); | |
219 // If a named value for first gauge is chosen, try to find and set | |
220 // the values to the other panels too. | |
221 if (fi == 0) { | |
222 String valueName = r.getAttribute("name"); | |
223 int oi = 0; | |
224 // TODO instead of oi use random access. | |
225 for (QDTable otherQDTable: qdTables) { | |
226 if (oi == 0) { | |
227 oi++; | |
228 continue; | |
229 } | |
230 Double value = otherQDTable.findRecordValue(valueName); | |
231 if (value == null) { | |
232 // TODO i18n | |
233 SC.warn("No Value for <name> at <gauge>"); | |
234 } | |
235 else { | |
236 doubleArrayPanels.get(oi).setValues(new double[]{value}); | |
237 } | |
238 oi++; | |
239 } | |
240 } | |
241 else { | |
242 // Focus next. | |
243 if (fi != doubleArrayPanels.size()-1) { | |
244 doubleArrayPanels.get(fi+1).focusInItem(1); | |
245 } | |
246 } | |
247 } | |
248 }; | |
249 | |
250 qdTable.addCellClickHandler(handler); | |
251 i++; | |
252 } | |
253 } | |
254 | |
255 | |
256 @Override | |
257 public Canvas createOld(DataList dataList) { | |
258 List<Data> all = dataList.getAll(); | |
259 Data wqData = getData(all, "wq_values"); | |
260 Data wqMode = getData(all, "wq_isq"); | |
261 boolean isQ = wqMode.getItems()[0].getStringValue().equals("true"); | |
262 Canvas back = getBackButton(dataList.getState()); | |
263 | |
264 HLayout valLayout = new HLayout(); | |
265 HLayout modeLayout = new HLayout(); | |
266 VLayout vlayout = new VLayout(); | |
267 | |
268 Label wqLabel = new Label(dataList.getLabel()); | |
269 Label modeLabel = new Label(""); | |
270 | |
271 wqLabel.setValign(VerticalAlignment.TOP); | |
272 | |
273 wqLabel.setWidth(200); | |
274 wqLabel.setHeight(25); | |
275 modeLabel.setHeight(25); | |
276 modeLabel.setWidth(200); | |
277 | |
278 valLayout.addMember(wqLabel); | |
279 valLayout.addMember(createOldWQValues(wqData, isQ)); | |
280 | |
281 valLayout.addMember(back); | |
282 modeLayout.addMember(modeLabel); | |
283 | |
284 vlayout.addMember(valLayout); | |
285 vlayout.addMember(modeLayout); | |
286 | |
287 return vlayout; | |
288 } | |
289 | |
290 | |
291 /** Create area showing previously entered w or q data. */ | |
292 protected Canvas createOldWQValues(Data wqData, boolean isQ) { | |
293 VLayout layout = new VLayout(); | |
294 | |
295 DataItem item = wqData.getItems()[0]; | |
296 String value = item.getStringValue(); | |
297 | |
298 String[] gauges = value.split(GAUGE_SEPARATOR); | |
299 | |
300 String unit = isQ ? "m³/s" : "cm"; | |
301 | |
302 for (String gauge: gauges) { | |
303 HLayout h = new HLayout(); | |
304 | |
305 String[] parts = gauge.split(GAUGE_PART_SEPARATOR); | |
306 String[] values = parts[3].split(VALUE_SEPARATOR); | |
307 | |
308 Label l = new Label(parts[2] + ": "); | |
309 | |
310 StringBuilder sb = new StringBuilder(); | |
311 boolean first = true; | |
312 | |
313 for (String v: values) { | |
314 if (!first) { | |
315 sb.append(", "); | |
316 } | |
317 | |
318 sb.append(v); | |
319 sb.append(" "); | |
320 sb.append(unit); | |
321 | |
322 first = false; | |
323 } | |
324 | |
325 Label v = new Label(sb.toString()); | |
326 | |
327 l.setWidth(65); | |
328 v.setWidth(65); | |
329 | |
330 h.addMember(l); | |
331 h.addMember(v); | |
332 | |
333 layout.addMember(h); | |
334 } | |
335 | |
336 return layout; | |
337 } | |
338 | |
339 | |
340 /** Create non-input helper part of the UI. */ | |
341 protected Canvas createWidget(DataList dataList) { | |
342 VLayout layout = new VLayout(); | |
343 | |
344 Canvas mode = createMode(dataList); | |
345 Canvas list = createList(dataList); | |
346 | |
347 DataItem[] items = getWQItems(dataList); | |
348 int listHeight = ROW_HEIGHT * items.length; | |
349 | |
350 mode.setHeight(25); | |
351 mode.setWidth(200); | |
352 | |
353 layout.addMember(mode); | |
354 layout.addMember(list); | |
355 | |
356 layout.setHeight(25 + listHeight); | |
357 layout.setWidth(350); | |
358 | |
359 initUserDefaults(dataList); | |
360 | |
361 return layout; | |
362 } | |
363 | |
364 | |
365 @Override | |
366 public List<String> validate() { | |
367 if (isWMode()) { | |
368 return validateW(); | |
369 } | |
370 else { | |
371 return validateQ(); | |
372 } | |
373 } | |
374 | |
375 | |
376 protected List<String> validateRange(Map<String, double[]> ranges) { | |
377 List<String> errors = new ArrayList<String>(); | |
378 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
379 | |
380 Iterator<String> iter = wqranges.keySet().iterator(); | |
381 | |
382 while (iter.hasNext()) { | |
383 List<String> tmpErrors = new ArrayList<String>(); | |
384 | |
385 String key = iter.next(); | |
386 DoubleArrayPanel dap = wqranges.get(key); | |
387 | |
388 if (!dap.validateForm()) { | |
389 errors.add(MSG.error_invalid_double_value()); | |
390 return errors; | |
391 } | |
392 | |
393 double[] mm = ranges.get(key); | |
394 if (mm == null) { | |
395 SC.warn(MSG.error_read_minmax_values()); | |
396 continue; | |
397 } | |
398 | |
399 double[] values = dap.getInputValues(); | |
400 double[] good = new double[values.length]; | |
401 | |
402 int idx = 0; | |
403 | |
404 for (double value: values) { | |
405 if (value < mm[0] || value > mm[1]) { | |
406 String tmp = MSG.error_validate_range(); | |
407 tmp = tmp.replace("$1", nf.format(value)); | |
408 tmp = tmp.replace("$2", nf.format(mm[0])); | |
409 tmp = tmp.replace("$3", nf.format(mm[1])); | |
410 tmpErrors.add(tmp); | |
411 } | |
412 else { | |
413 good[idx++] = value; | |
414 } | |
415 } | |
416 | |
417 double[] justGood = new double[idx]; | |
418 for (int i = 0; i < justGood.length; i++) { | |
419 justGood[i] = good[i]; | |
420 } | |
421 | |
422 if (!tmpErrors.isEmpty()) { | |
423 dap.setValues(justGood); | |
424 | |
425 errors.addAll(tmpErrors); | |
426 } | |
427 } | |
428 | |
429 return errors; | |
430 } | |
431 | |
432 | |
433 protected List<String> validateW() { | |
434 return validateRange(wranges); | |
435 } | |
436 | |
437 | |
438 protected List<String> validateQ() { | |
439 return validateRange(qranges); | |
440 } | |
441 | |
442 | |
443 protected void initUserDefaults(DataList dataList) { | |
444 initUserWQValues(dataList); | |
445 initUserWQMode(dataList); | |
446 } | |
447 | |
448 | |
449 protected void initUserWQMode(DataList dataList) { | |
450 List<Data> allData = dataList.getAll(); | |
451 | |
452 Data dDef = getData(allData, "wq_mode"); | |
453 DataItem def = dDef != null ? dDef.getDefault() : null; | |
454 String value = def != null ? def.getStringValue() : null; | |
455 | |
456 if (value != null && value.equals(FIELD_WQ_W)) { | |
457 modes.setValue(FIELD_WQ_MODE, FIELD_WQ_W); | |
458 } | |
459 else { | |
460 modes.setValue(FIELD_WQ_MODE, FIELD_WQ_Q); | |
461 } | |
462 } | |
463 | |
464 | |
465 protected void initUserWQValues(DataList dataList) { | |
466 List<Data> allData = dataList.getAll(); | |
467 | |
468 Data dDef = getData(allData, "wq_values"); | |
469 DataItem def = dDef != null ? dDef.getDefault() : null; | |
470 String value = def != null ? def.getStringValue() : null; | |
471 | |
472 if (value == null || value.length() == 0) { | |
473 return; | |
474 } | |
475 | |
476 String[] lines = value.split(GAUGE_SEPARATOR); | |
477 | |
478 if (lines == null || lines.length == 0) { | |
479 return; | |
480 } | |
481 | |
482 for (String line: lines) { | |
483 String[] cols = line.split(GAUGE_PART_SEPARATOR); | |
484 String title = createLineTitle(line); | |
485 | |
486 if (cols == null || cols.length < 3) { | |
487 continue; | |
488 } | |
489 | |
490 String[] strValues = cols[2].split(VALUE_SEPARATOR); | |
491 double[] values = new double[strValues.length]; | |
492 | |
493 int idx = 0; | |
494 | |
495 for (String strValue: strValues) { | |
496 try { | |
497 values[idx++] = Double.valueOf(strValue); | |
498 } | |
499 catch (NumberFormatException nfe) { | |
500 // do nothing | |
501 } | |
502 } | |
503 | |
504 String key = cols[0] + GAUGE_PART_SEPARATOR + cols[1]; | |
505 DoubleArrayPanel dap = wqranges.get(key); | |
506 | |
507 if (dap == null) { | |
508 continue; | |
509 } | |
510 | |
511 dap.setValues(values); | |
512 } | |
513 } | |
514 | |
515 /** Populate Gauge Ranges array. */ | |
516 private void readGaugeRanges(DataList dataList) { | |
517 DataItem[] items = getWQItems(dataList); | |
518 gaugeRanges = new double[items.length][2]; | |
519 | |
520 int i = 0; | |
521 | |
522 for (DataItem item: items) { | |
523 String[] startEndKm = item.getLabel().split(";"); | |
524 | |
525 gaugeRanges[i][0] = Double.parseDouble(startEndKm[0]); | |
526 gaugeRanges[i][1] = Double.parseDouble(startEndKm[1]); | |
527 i++; | |
528 } | |
529 } | |
530 | |
531 | |
532 protected Canvas createList(DataList dataList) { | |
533 VLayout layout = new VLayout(); | |
534 | |
535 DataItem[] items = getWQItems(dataList); | |
536 | |
537 int i = 0; | |
538 | |
539 for (DataItem item: items) { | |
540 String title = item.getLabel(); // of form: 70.5;112.0 | |
541 String label = item.getStringValue(); | |
542 | |
543 // Rename W and Q tab to include gauges name. | |
544 tabs.getTab(i*2).setTitle(tabs.getTab(i*2).getTitle() | |
545 + " (" + label + ")"); | |
546 tabs.getTab(i*2+1).setTitle(tabs.getTab(i*2+1).getTitle() | |
547 + " (" + label + ")"); | |
548 | |
549 DoubleArrayPanel dap = new DoubleArrayPanel( | |
550 label, null, this, this, TitleOrientation.LEFT); | |
551 | |
552 wqranges.put(title, dap); | |
553 doubleArrayPanels.add(dap); | |
554 | |
555 if (item instanceof WQDataItem) { | |
556 WQDataItem wq = (WQDataItem) item; | |
557 double[] mmQ = wq.getQRange(); | |
558 double[] mmW = wq.getWRange(); | |
559 | |
560 qranges.put(title, mmQ); | |
561 wranges.put(title, mmW); | |
562 } | |
563 | |
564 layout.addMember(dap); | |
565 i++; | |
566 } | |
567 | |
568 layout.setHeight(items.length * ROW_HEIGHT); | |
569 | |
570 return layout; | |
571 } | |
572 | |
573 | |
574 /** Get items which are not WQ_MODE. */ | |
575 protected DataItem[] getWQItems(DataList dataList) { | |
576 List<Data> data = dataList.getAll(); | |
577 | |
578 for (Data d: data) { | |
579 String name = d.getLabel(); | |
580 | |
581 if (name.equals(FIELD_WQ_MODE)) { | |
582 continue; | |
583 } | |
584 | |
585 return d.getItems(); | |
586 } | |
587 | |
588 return null; | |
589 } | |
590 | |
591 | |
592 /** | |
593 * Create radio button for switching w and q input. | |
594 * Radiobutton-change also triggers helper panel tab selection. | |
595 */ | |
596 protected Canvas createMode(DataList dataList) { | |
597 RadioGroupItem wq = new RadioGroupItem(FIELD_WQ_MODE); | |
598 wq.setShowTitle(false); | |
599 wq.setVertical(false); | |
600 wq.setWidth(200); | |
601 | |
602 LinkedHashMap wqValues = new LinkedHashMap(); | |
603 wqValues.put(FIELD_WQ_W, MSG.wqW()); | |
604 wqValues.put(FIELD_WQ_Q, MSG.wqQatGauge()); | |
605 | |
606 wq.setValueMap(wqValues); | |
607 | |
608 modes = new DynamicForm(); | |
609 modes.setFields(wq); | |
610 modes.setWidth(200); | |
611 wq.addChangeHandler(new ChangeHandler() { | |
612 @Override | |
613 public void onChange(ChangeEvent e) { | |
614 DynamicForm form = e.getForm(); | |
615 | |
616 if(form.getValueAsString(FIELD_WQ_MODE).contains("Q")) { | |
617 tabs.selectTab(0); | |
618 } | |
619 else { | |
620 tabs.selectTab(1); | |
621 } | |
622 } | |
623 }); | |
624 | |
625 | |
626 LinkedHashMap initial = new LinkedHashMap(); | |
627 initial.put(FIELD_WQ_MODE, FIELD_WQ_Q); | |
628 modes.setValues(initial); | |
629 tabs.selectTab(1); | |
630 return modes; | |
631 } | |
632 | |
633 | |
634 public String createLineTitle(String key) { | |
635 String[] splitted = key.split(";"); | |
636 | |
637 return splitted[0] + " - " + splitted[1]; | |
638 } | |
639 | |
640 | |
641 @Override | |
642 public Data[] getData() { | |
643 Data mode = getWQMode(); | |
644 Data values = getWQValues(); | |
645 | |
646 return new Data[] { mode, values }; | |
647 } | |
648 | |
649 | |
650 public boolean isWMode() { | |
651 String mode = (String) modes.getValue(FIELD_WQ_MODE); | |
652 | |
653 return FIELD_WQ_W.equals(mode); | |
654 } | |
655 | |
656 | |
657 protected Data getWQMode() { | |
658 String wqMode = modes.getValueAsString(FIELD_WQ_MODE); | |
659 String value = "false"; | |
660 if (wqMode.equals("Q")) { | |
661 value = "true"; | |
662 } | |
663 DataItem item = new DefaultDataItem("wq_isq", "wq_isq", value); | |
664 Data mode = new DefaultData( | |
665 "wq_isq", null, null, new DataItem[] { item }); | |
666 | |
667 return mode; | |
668 } | |
669 | |
670 | |
671 protected Data getWQValues() { | |
672 String wqvalue = null; | |
673 | |
674 Iterator<String> iter = wqranges.keySet().iterator(); | |
675 while (iter.hasNext()) { | |
676 String key = iter.next(); | |
677 DoubleArrayPanel dap = wqranges.get(key); | |
678 String label = dap.getItemTitle(); | |
679 | |
680 double[] values = dap.getInputValues(); | |
681 if (wqvalue == null) { | |
682 wqvalue = createValueString(key + ";" + label, values); | |
683 } | |
684 else { | |
685 wqvalue += GAUGE_SEPARATOR + createValueString(key + ";" + label, values); | |
686 } | |
687 } | |
688 | |
689 DataItem valueItem = new DefaultDataItem( | |
690 "wq_values", "wq_values", wqvalue); | |
691 Data values = new DefaultData( | |
692 "wq_values", null, null, new DataItem[] { valueItem }); | |
693 | |
694 return values; | |
695 } | |
696 | |
697 | |
698 protected String createValueString(String key, double[] values) { | |
699 StringBuilder sb = new StringBuilder(); | |
700 | |
701 boolean first = true; | |
702 | |
703 for (double value: values) { | |
704 if (!first) { | |
705 sb.append(","); | |
706 } | |
707 | |
708 sb.append(Double.toString(value)); | |
709 | |
710 first = false; | |
711 } | |
712 | |
713 return key + ";" + sb.toString(); | |
714 } | |
715 | |
716 | |
717 @Override | |
718 public void onChange(ChangeEvent event) { | |
719 // TODO IMPLEMENT ME | |
720 } | |
721 | |
722 | |
723 /** Store the currently focussed DoubleArrayPanel and focus helper tab. */ | |
724 @Override | |
725 public void onFocus(FocusEvent event) { | |
726 itemWithFocus = (DoubleArrayPanel) event.getForm(); | |
727 // Switch to respective tab. | |
728 // TODO which makes a focus loss | |
729 int inputIndex = doubleArrayPanels.indexOf(itemWithFocus); | |
730 tabs.selectTab(inputIndex*2 + (isWMode() ? 0 : 1)); | |
731 } | |
732 | |
733 | |
734 @Override | |
735 public void onBlur(BlurEvent event) { | |
736 DoubleArrayPanel dap = (DoubleArrayPanel) event.getForm(); | |
737 dap.validateForm(event.getItem()); | |
738 } | |
739 | |
740 | |
741 /** Get the WQD data from service and stuck them up that tables. */ | |
742 protected void fetchWQData() { | |
743 Config config = Config.getInstance(); | |
744 String locale = config.getLocale (); | |
745 | |
746 ArtifactDescription adescr = artifact.getArtifactDescription(); | |
747 DataList[] data = adescr.getOldData(); | |
748 | |
749 double[] mm = getMinMaxKM(data); | |
750 String river = getRiverName(data); | |
751 | |
752 int i = 0; | |
753 | |
754 // Get Data for respective gauge. | |
755 for (double[] range : gaugeRanges){ | |
756 // Gauge ranges overlap, move start and end a bit closer to each other. | |
757 final double rDiff = (range[1] - range[0]) / 10d; | |
758 final int fi = i; | |
759 wqInfoService.getWQInfo(locale, river, range[0]+rDiff, range[1]-rDiff, | |
760 new AsyncCallback<WQInfoObject[]>() { | |
761 @Override | |
762 public void onFailure(Throwable caught) { | |
763 GWT.log("Could not recieve wq informations."); | |
764 SC.warn(caught.getMessage()); | |
765 } | |
766 | |
767 @Override | |
768 public void onSuccess(WQInfoObject[] wqi) { | |
769 int num = wqi != null ? wqi.length :0; | |
770 GWT.log("Received " + num + " wq informations (" + fi + "."); | |
771 | |
772 if (num == 0) { | |
773 return; | |
774 } | |
775 | |
776 addWQInfo(wqi, fi); | |
777 } | |
778 } | |
779 ); | |
780 i++; | |
781 } | |
782 } | |
783 | |
784 | |
785 /** Add Info to helper table for gauge at index gaugeIdx. */ | |
786 protected void addWQInfo (WQInfoObject[] wqi, int gaugeIdx) { | |
787 for(WQInfoObject wi: wqi) { | |
788 WQInfoRecord rec = new WQInfoRecord(wi); | |
789 | |
790 if (wi.getType().equals("W")) { | |
791 wTables.get(gaugeIdx).addData(rec); | |
792 } | |
793 else { | |
794 qdTables.get(gaugeIdx).addData(rec); | |
795 } | |
796 } | |
797 } | |
798 | |
799 | |
800 /** | |
801 * Determines the min and max kilometer value selected in a former state. A | |
802 * bit silly, but we need to run over each value of the "old data" to find | |
803 * such values because it is not available here. | |
804 * | |
805 * @param data The DataList which contains the whole data inserted for the | |
806 * current artifact. | |
807 * | |
808 * @return a double array with [min, max]. | |
809 */ | |
810 protected double[] getMinMaxKM(DataList[] data) { | |
811 ArtifactDescription adesc = artifact.getArtifactDescription(); | |
812 return adesc.getKMRange(); | |
813 } | |
814 | |
815 | |
816 /** | |
817 * Returns the name of the selected river. | |
818 * | |
819 * @param data The DataList with all data. | |
820 * | |
821 * @return the name of the current river. | |
822 */ | |
823 protected String getRiverName(DataList[] data) { | |
824 ArtifactDescription adesc = artifact.getArtifactDescription(); | |
825 return adesc.getRiver(); | |
826 } | |
827 } | |
828 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |