comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/LocationDistancePanel.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/LocationDistancePanel.java@b296d435fc69
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
6 import com.smartgwt.client.data.AdvancedCriteria;
7 import com.smartgwt.client.data.Criteria;
8 import com.smartgwt.client.data.Criterion;
9 import com.smartgwt.client.data.Record;
10 import com.smartgwt.client.types.Alignment;
11 import com.smartgwt.client.types.ListGridFieldType;
12 import com.smartgwt.client.types.OperatorId;
13 import com.smartgwt.client.widgets.Canvas;
14 import com.smartgwt.client.widgets.Label;
15 import com.smartgwt.client.widgets.form.DynamicForm;
16 import com.smartgwt.client.widgets.form.fields.FormItem;
17 import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
18 import com.smartgwt.client.widgets.form.fields.SelectItem;
19 import com.smartgwt.client.widgets.form.fields.StaticTextItem;
20 import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
21 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
22 import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
23 import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
24 import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
25 import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
26 import com.smartgwt.client.widgets.grid.CellFormatter;
27 import com.smartgwt.client.widgets.grid.ListGrid;
28 import com.smartgwt.client.widgets.grid.ListGridField;
29 import com.smartgwt.client.widgets.grid.ListGridRecord;
30 import com.smartgwt.client.widgets.grid.events.CellClickEvent;
31 import com.smartgwt.client.widgets.grid.events.CellClickHandler;
32 import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
33 import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
34 import com.smartgwt.client.widgets.layout.HLayout;
35 import com.smartgwt.client.widgets.layout.VLayout;
36 import com.smartgwt.client.widgets.tab.Tab;
37 import com.smartgwt.client.widgets.tab.TabSet;
38 import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
39 import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;
40
41 import de.intevation.flys.client.client.Config;
42 import de.intevation.flys.client.client.FLYSConstants;
43 import de.intevation.flys.client.client.event.FilterHandler;
44 import de.intevation.flys.client.client.event.RangeFilterEvent;
45 import de.intevation.flys.client.client.event.StringFilterEvent;
46 import de.intevation.flys.client.client.services.DistanceInfoService;
47 import de.intevation.flys.client.client.services.DistanceInfoServiceAsync;
48 import de.intevation.flys.client.client.ui.range.DistanceInfoDataSource;
49 import de.intevation.flys.client.shared.model.ArtifactDescription;
50 import de.intevation.flys.client.shared.model.Data;
51 import de.intevation.flys.client.shared.model.DataItem;
52 import de.intevation.flys.client.shared.model.DataList;
53 import de.intevation.flys.client.shared.model.DefaultData;
54 import de.intevation.flys.client.shared.model.DefaultDataItem;
55 import de.intevation.flys.client.shared.model.DistanceInfoObject;
56
57 import java.util.ArrayList;
58 import java.util.LinkedHashMap;
59 import java.util.List;
60
61
62 /**
63 * This UIProvider creates a widget to enter locations or a distance.
64 *
65 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
66 */
67 public class LocationDistancePanel
68 extends AbstractUIProvider
69 implements ChangeHandler, BlurHandler, FilterHandler
70 {
71 private static final long serialVersionUID = -10820092176039372L;
72
73 /** The message class that provides i18n strings. */
74 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
75
76 /** The DistanceInfoService used to retrieve locations about rivers. */
77 protected DistanceInfoServiceAsync distanceInfoService =
78 GWT.create(DistanceInfoService.class);
79
80 public static final String FIELD_MODE = "mode";
81
82 /** The constant name of the input field to enter the start of a distance.*/
83 public static final String FIELD_FROM = "from";
84
85 /** The constant name of the input field to enter the end of a distance.*/
86 public static final String FIELD_TO = "to";
87
88 /** The constant name of the input field to enter locations.*/
89 public static final String FIELD_VALUE_LOCATION = "location";
90
91 /** The constant name of the input field to enter distance.*/
92 public static final String FIELD_VALUE_DISTANCE = "distance";
93
94 /** The constant name of the input field to enter the step width of a
95 * distance.*/
96 public static final String FIELD_WIDTH = "width";
97
98 public static final int WIDTH = 250;
99
100
101 /** The radio group for input mode selection.*/
102 protected DynamicForm mode;
103
104 /** A container that will contain the location or the distance panel.*/
105 protected HLayout container;
106
107 /** The min value for a distance.*/
108 protected double min;
109
110 /** The max value for a distance.*/
111 protected double max;
112
113 /** The 'from' value entered in the distance mode.*/
114 protected double from;
115
116 /** The 'to' value entered in the distance mode.*/
117 protected double to;
118
119 /** The 'step' value entered in the distance mode.*/
120 protected double step;
121
122 /** The values entered in the location mode.*/
123 protected double[] values;
124
125 /** The input panel for locations. */
126 protected DoubleArrayPanel locationPanel;
127
128 /** The input panel for distances. */
129 protected DoubleRangePanel distancePanel;
130
131 /** The tab set containing the location and distance table. */
132 protected TabSet inputTables;
133
134 /** The distance table. */
135 protected ListGrid distanceTable;
136
137 /** The locations table. */
138 protected ListGrid locationsTable;
139
140 /** The locations table for distance input. */
141 protected ListGrid locationDistanceTable;
142
143 /** The table data. */
144 protected DistanceInfoObject[] tableData;
145
146 /** The table filter.*/
147 protected TableFilter filterDescription;
148 protected RangeTableFilter filterRange;
149
150 /** The Combobox for table filter criteria. */
151 protected SelectItem filterCriteria;
152 protected StaticTextItem filterResultCount;
153 protected ListGrid currentFiltered;
154
155 /**
156 * Creates a new LocationDistancePanel instance.
157 */
158 public LocationDistancePanel() {
159 distanceTable = new ListGrid();
160 distanceTable.setAutoFetchData(true);
161
162 locationsTable = new ListGrid();
163 locationsTable.setAutoFetchData(true);
164
165 locationDistanceTable = new ListGrid();
166 locationDistanceTable.setAutoFetchData(true);
167
168 locationDistanceTable.setShowHeaderContextMenu(false);
169 distanceTable.setShowHeaderContextMenu(false);
170 locationsTable.setShowHeaderContextMenu(false);
171 }
172
173
174 /**
175 * This method creates a widget that contains a label, a panel with
176 * checkboxes to switch the input mode between location and distance input,
177 * and a the mode specific panel.
178 *
179 * @param data The data that might be inserted.
180 *
181 * @return a panel.
182 */
183 @Override
184 public Canvas create(DataList data) {
185 VLayout layout = new VLayout();
186 layout.setMembersMargin(10);
187
188 Label label = new Label(MESSAGES.location_distance_state());
189 Canvas widget = createWidget(data);
190 Canvas submit = getNextButton();
191 createDistanceInputPanel();
192
193 initDefaults(data);
194
195 createLocationTableDistance ();
196 createDistanceTable();
197 createLocationTable();
198
199 widget.setHeight(50);
200 label.setHeight(25);
201
202 layout.addMember(label);
203 layout.addMember(widget);
204 layout.addMember(submit);
205
206 return layout;
207 }
208
209
210 /**
211 * This method creates a table that contains the distance values.
212 */
213 protected void createDistanceTable() {
214
215 String baseUrl = GWT.getHostPageBaseURL();
216
217 distanceTable.setWidth100();
218 distanceTable.setShowRecordComponents(true);
219 distanceTable.setShowRecordComponentsByCell(true);
220 distanceTable.setHeight100();
221 distanceTable.setEmptyMessage(MESSAGES.empty_filter());
222 distanceTable.setCanReorderFields(false);
223
224 CellFormatter cf = new CellFormatter() {
225 @Override
226 public String format(
227 Object value,
228 ListGridRecord record,
229 int rowNum, int colNum) {
230 if (value == null) return null;
231 try {
232 NumberFormat nf;
233 double v = Double.parseDouble((String)value);
234 nf = NumberFormat.getFormat("###0.00##");
235 return nf.format(v);
236 }
237 catch (Exception e) {
238 return value.toString();
239 }
240 }
241 };
242
243 ListGridField addDistance = new ListGridField ("", "");
244 addDistance.setType (ListGridFieldType.ICON);
245 addDistance.setWidth (20);
246 addDistance.addRecordClickHandler (new RecordClickHandler () {
247 @Override
248 public void onRecordClick (RecordClickEvent e) {
249 if (!isLocationMode ()) {
250 Record r = e.getRecord();
251 try {
252 double min = Double.parseDouble(r.getAttribute("from"));
253 double max = Double.parseDouble(r.getAttribute("to"));
254 setDistanceValues(min, max);
255 }
256 catch(NumberFormatException nfe) {
257 // Is there anything to do?
258 }
259 }
260 else {
261 double[] selected;
262 Record r = e.getRecord();
263 double min = 0, max = 0;
264 try {
265 min = Double.parseDouble(r.getAttribute("from"));
266 max = Double.parseDouble(r.getAttribute("to"));
267 }
268 catch(NumberFormatException nfe) {
269 // Is there anything to do?
270 }
271 if (getLocationValues() != null) {
272 double[] val = getLocationValues();
273 selected = new double[val.length + 2];
274 for(int i = 0; i < val.length; i++){
275 selected[i] = val[i];
276 }
277 selected[val.length] = min;
278 selected[val.length + 1] = max;
279 }
280 else {
281 selected = new double[2];
282 selected[0] = min;
283 selected[1] = max;
284 }
285 setLocationValues(selected);
286 }
287 }
288 });
289 addDistance.setCellIcon(baseUrl + MESSAGES.markerGreen());
290
291 ListGridField ddescr = new ListGridField("description",
292 MESSAGES.description());
293 ddescr.setType(ListGridFieldType.TEXT);
294 ddescr.setWidth("*");
295 ListGridField from = new ListGridField("from", MESSAGES.from());
296 from.setCellFormatter(cf);
297
298 from.setWidth("12%");
299 ListGridField to = new ListGridField("to", MESSAGES.to());
300 to.setType(ListGridFieldType.FLOAT);
301 to.setCellFormatter(cf);
302
303 to.setWidth("12%");
304 to.setAlign(Alignment.LEFT);
305 ListGridField dside = new ListGridField("riverside",
306 MESSAGES.riverside());
307 dside.setType(ListGridFieldType.TEXT);
308 dside.setWidth("12%");
309
310 ListGridField bottom =
311 new ListGridField("bottom", MESSAGES.bottom_edge());
312 bottom.setType(ListGridFieldType.TEXT);
313 bottom.setWidth("10%");
314 bottom.setCellFormatter(cf);
315
316 ListGridField top =
317 new ListGridField("top", MESSAGES.top_edge());
318 top.setType(ListGridFieldType.TEXT);
319 top.setWidth("10%");
320 top.setCellFormatter(cf);
321
322 distanceTable.setFields(
323 addDistance, ddescr, from, to, dside, bottom, top);
324 }
325
326
327 /**
328 * This method creates a table that contains the location values.
329 */
330 protected void createLocationTable() {
331
332 String baseUrl = GWT.getHostPageBaseURL();
333
334 locationsTable.setWidth100();
335 locationsTable.setShowRecordComponents(true);
336 locationsTable.setShowRecordComponentsByCell(true);
337 locationsTable.setHeight100();
338 locationsTable.setEmptyMessage(MESSAGES.empty_filter());
339 locationsTable.setCanReorderFields(false);
340
341 CellFormatter cf = new CellFormatter() {
342 @Override
343 public String format(
344 Object value,
345 ListGridRecord record,
346 int rowNum, int colNum) {
347 if (value == null) return null;
348 try {
349 NumberFormat nf;
350 double v = Double.parseDouble((String)value);
351 nf = NumberFormat.getFormat("###0.00##");
352 return nf.format(v);
353 }
354 catch (Exception e) {
355 return value.toString();
356 }
357 }
358 };
359
360
361
362 ListGridField addLocation = new ListGridField ("", "");
363 addLocation.setType (ListGridFieldType.ICON);
364 addLocation.setWidth (20);
365
366 addLocation.addRecordClickHandler (new RecordClickHandler () {
367 @Override
368 public void onRecordClick (RecordClickEvent e) {
369 Record record = e.getRecord();
370 double[] selected;
371 if (getLocationValues() != null) {
372 double[] val = getLocationValues();
373 selected = new double[val.length + 1];
374 for(int i = 0; i < val.length; i++){
375 selected[i] = val[i];
376 }
377 try {
378 selected[val.length] =
379 Double.parseDouble(record.getAttribute("from"));
380 }
381 catch(NumberFormatException nfe) {
382 // Is there anything to do here?
383 }
384 }
385 else {
386 selected = new double[1];
387 selected[0] =
388 Double.parseDouble(record.getAttribute("from"));
389 }
390 setLocationValues(selected);
391 }
392 });
393 addLocation.setCellIcon (baseUrl + MESSAGES.markerGreen ());
394
395 ListGridField ldescr = new ListGridField("description",
396 MESSAGES.description());
397 ldescr.setType(ListGridFieldType.TEXT);
398 ldescr.setWidth("*");
399 ListGridField lside = new ListGridField("riverside",
400 MESSAGES.riverside());
401 lside.setType(ListGridFieldType.TEXT);
402 lside.setWidth("12%");
403 ListGridField loc = new ListGridField("from", MESSAGES.locations());
404 loc.setAlign(Alignment.LEFT);
405 loc.setType(ListGridFieldType.FLOAT);
406 loc.setWidth("12%");
407 loc.setCellFormatter(cf);
408
409 ListGridField bottom =
410 new ListGridField("bottom", MESSAGES.bottom_edge());
411 bottom.setType(ListGridFieldType.TEXT);
412 bottom.setWidth("10%");
413 bottom.setCellFormatter(cf);
414
415 ListGridField top =
416 new ListGridField("top", MESSAGES.top_edge());
417 top.setType(ListGridFieldType.TEXT);
418 top.setWidth("10%");
419 top.setCellFormatter(cf);
420
421 locationsTable.setFields(addLocation, ldescr, loc, lside, bottom, top);
422 }
423
424
425 /**
426 * This method creates a table that contains the location values.
427 */
428 protected void createLocationTableDistance (){
429
430 String baseUrl = GWT.getHostPageBaseURL();
431
432 locationDistanceTable.setWidth100();
433 locationDistanceTable.setShowRecordComponents(true);
434 locationDistanceTable.setShowRecordComponentsByCell(true);
435 locationDistanceTable.setHeight100();
436 locationDistanceTable.setEmptyMessage(MESSAGES.empty_filter());
437 locationDistanceTable.setCanReorderFields(false);
438
439 CellFormatter cf = new CellFormatter() {
440 @Override
441 public String format(
442 Object value,
443 ListGridRecord record,
444 int rowNum, int colNum) {
445 if (value == null) return null;
446 try {
447 NumberFormat nf;
448 double v = Double.parseDouble((String)value);
449 nf = NumberFormat.getFormat("###0.00##");
450 return nf.format(v);
451 }
452 catch (Exception e) {
453 return value.toString();
454 }
455 }
456 };
457
458 ListGridField addfrom = new ListGridField ("from", MESSAGES.from());
459 addfrom.setType (ListGridFieldType.ICON);
460 addfrom.setWidth (30);
461 addfrom.setCellIcon(baseUrl + MESSAGES.markerGreen());
462
463 ListGridField addto2 = new ListGridField ("to", MESSAGES.to());
464 addto2.setType (ListGridFieldType.ICON);
465 addto2.setWidth (30);
466 addto2.setCellIcon(baseUrl + MESSAGES.markerRed());
467
468 locationDistanceTable.addCellClickHandler (new CellClickHandler () {
469 @Override
470 public void onCellClick (CellClickEvent e) {
471 if (e.getColNum() == 0) {
472 Record r = e.getRecord ();
473 try {
474 double fromvalue =
475 Double.parseDouble(r.getAttribute("from"));
476 double tovalue = getTo ();
477 setDistanceValues (fromvalue, tovalue);
478 }
479 catch(NumberFormatException nfe) {
480 // Is there anything to do in here?
481 }
482 }
483 else if (e.getColNum() == 1) {
484 Record r = e.getRecord ();
485 try {
486 double fromvalue = getFrom ();
487 double tovalue =
488 Double.parseDouble(r.getAttribute("from"));
489 setDistanceValues (fromvalue, tovalue);
490 }
491 catch(NumberFormatException nfe) {
492 // Is there anything to do in here?
493 }
494 }
495 }
496 });
497 ListGridField bottom =
498 new ListGridField("bottom", MESSAGES.bottom_edge());
499 bottom.setType(ListGridFieldType.TEXT);
500 bottom.setWidth("10%");
501 bottom.setCellFormatter(cf);
502
503 ListGridField top =
504 new ListGridField("top", MESSAGES.top_edge());
505 top.setType(ListGridFieldType.TEXT);
506 top.setWidth("10%");
507 top.setCellFormatter(cf);
508
509 ListGridField ldescr = new ListGridField("description",
510 MESSAGES.description());
511 ldescr.setType(ListGridFieldType.TEXT);
512 ldescr.setWidth("*");
513 ListGridField lside = new ListGridField("riverside",
514 MESSAGES.riverside());
515 lside.setType(ListGridFieldType.TEXT);
516 lside.setWidth("12%");
517 ListGridField loc = new ListGridField("from", MESSAGES.locations());
518 loc.setType(ListGridFieldType.FLOAT);
519 loc.setAlign(Alignment.LEFT);
520 loc.setWidth("12%");
521 loc.setCellFormatter(cf);
522
523 locationDistanceTable.setFields(
524 addfrom, addto2, ldescr, loc, lside, bottom, top);
525 }
526
527
528 @Override
529 public Canvas createOld(DataList dataList) {
530 List<Data> items = dataList.getAll();
531
532 Data dMode = getData(items, "ld_mode");
533 DataItem[] dItems = dMode.getItems();
534
535 boolean rangeMode = true;
536 if (dItems != null && dItems[0] != null) {
537 rangeMode = FIELD_VALUE_DISTANCE.equals(dItems[0].getStringValue());
538 }
539
540 HLayout layout = new HLayout();
541 layout.setWidth("400px");
542
543 Label label = new Label(dataList.getLabel());
544 label.setWidth("200px");
545
546 Canvas back = getBackButton(dataList.getState());
547
548 layout.addMember(label);
549
550 if (rangeMode) {
551 layout.addMember(getOldRangeSelection(dataList));
552 }
553 else {
554 layout.addMember(getOldLocationSelection(dataList));
555 }
556
557 layout.addMember(back);
558
559 return layout;
560 }
561
562
563 /**
564 * Creates a label for the selected range.
565 *
566 * @param dataList The DataList containing all values for this state.
567 *
568 * @return A label displaying the selected values.
569 */
570 protected Label getOldRangeSelection(DataList dataList) {
571 List<Data> items = dataList.getAll();
572
573 Data dFrom = getData(items, "ld_from");
574 Data dTo = getData(items, "ld_to");
575 Data dStep = getData(items, "ld_step");
576
577 DataItem[] from = dFrom.getItems();
578 DataItem[] to = dTo.getItems();
579 DataItem[] step = dStep.getItems();
580
581 StringBuilder sb = new StringBuilder();
582 sb.append(from[0].getLabel());
583 sb.append(" " + MESSAGES.unitFrom() + " ");
584 sb.append(to[0].getLabel());
585 sb.append(" " + MESSAGES.unitTo() + " ");
586 sb.append(step[0].getLabel());
587 sb.append(" " + MESSAGES.unitWidth());
588
589 Label selected = new Label(sb.toString());
590 selected.setWidth("130px");
591
592 return selected;
593 }
594
595
596 /**
597 * Creates a label for the selected locations.
598 *
599 * @param dataList The DataList containing all values for this state.
600 *
601 * @return A label displaying the selected values.
602 */
603 protected Label getOldLocationSelection(DataList dataList) {
604 List<Data> items = dataList.getAll();
605
606 Data dLocations = getData(items, "ld_locations");
607 DataItem[] lItems = dLocations.getItems();
608
609 String[] splitted = lItems[0].getStringValue().split(" ");
610 String value = "";
611 for (int i = 0; i < splitted.length; i++) {
612 try {
613 NumberFormat nf = NumberFormat.getDecimalFormat();
614 double dv = Double.parseDouble(splitted[i]);
615 value += nf.format(dv) + " ";
616 }
617 catch(NumberFormatException nfe) {
618 value += splitted[i] + " ";
619 }
620 }
621
622 Label selected = new Label(value);
623 selected.setWidth(130);
624
625 return selected;
626 }
627
628
629 /**
630 * This method reads the default values defined in the DataItems of the Data
631 * objects in <i>list</i>.
632 *
633 * @param list The DataList container that stores the Data objects.
634 */
635 protected void initDefaults(DataList list) {
636 Data m = getData(list.getAll(), "ld_mode");
637 Data l = getData(list.getAll(), "ld_locations");
638 Data f = getData(list.getAll(), "ld_from");
639 Data t = getData(list.getAll(), "ld_to");
640 Data s = getData(list.getAll(), "ld_step");
641
642 DataItem[] fItems = f.getItems();
643 DataItem[] tItems = t.getItems();
644 DataItem[] sItems = s.getItems();
645
646 min = Double.valueOf(fItems[0].getStringValue());
647 max = Double.valueOf(tItems[0].getStringValue());
648 step = Double.valueOf(sItems[0].getStringValue());
649
650 DataItem mDef = m.getDefault();
651 DataItem lDef = l.getDefault();
652 DataItem fDef = f.getDefault();
653 DataItem tDef = t.getDefault();
654 DataItem sDef = s.getDefault();
655
656 String mDefValue = mDef != null ? mDef.getStringValue() : null;
657 String theMode = mDefValue != null && mDefValue.length() > 0
658 ? mDef.getStringValue()
659 : FIELD_VALUE_DISTANCE;
660
661 mode.setValue(FIELD_MODE, theMode);
662
663 String fDefValue = fDef != null ? fDef.getStringValue() : null;
664 setFrom(fDefValue != null && fDefValue.length() > 0
665 ? Double.valueOf(fDef.getStringValue())
666 : min);
667
668 String tDefValue = tDef != null ? tDef.getStringValue() : null;
669 setTo(tDefValue != null && tDefValue.length() > 0
670 ? Double.valueOf(tDef.getStringValue())
671 : max);
672
673 String sDefValue = sDef != null ? sDef.getStringValue() : null;
674 setStep(sDefValue != null && sDefValue.length() > 0
675 ? Double.valueOf(sDef.getStringValue())
676 : step);
677
678 if (lDef != null) {
679 String lDefValue = lDef != null ? lDef.getStringValue() : null;
680
681 if (lDefValue != null && lDefValue.length() > 0) {
682 setLocationValues(lDef.getStringValue());
683 }
684 }
685
686 if (theMode.equals(FIELD_VALUE_DISTANCE)) {
687 enableDistancePanel();
688 inputTables.selectTab(1);
689 }
690 currentFiltered = (ListGrid)inputTables.getSelectedTab().getPane();
691 }
692
693
694 protected Canvas createWidget(DataList data) {
695 VLayout layout = new VLayout();
696 container = new HLayout();
697 Canvas checkboxPanel = createRadioButtonPanel();
698
699 // the initial view will display the location input mode
700 locationPanel = new DoubleArrayPanel(
701 MESSAGES.unitLocation(),
702 getLocationValues(),
703 this);
704 container.addMember(locationPanel);
705
706 layout.addMember(checkboxPanel);
707 layout.addMember(container);
708
709 container.setMembersMargin(30);
710
711 inputTables = new TabSet();
712 inputTables.addTabSelectedHandler(new TabSelectedHandler() {
713 @Override
714 public void onTabSelected(TabSelectedEvent evt) {
715 filterDescription.clear();
716 filterRange.clear();
717 filterResultCount.setValue("");
718
719 Canvas c = evt.getTabPane();
720 if(c instanceof ListGrid) {
721 currentFiltered = (ListGrid)c;
722 }
723 }
724 });
725
726 Tab locations = new Tab(MESSAGES.locations());
727 Tab distances = new Tab(MESSAGES.distance());
728
729 inputTables.setWidth100();
730 inputTables.setHeight100();
731
732 locations.setPane(locationDistanceTable);
733 distances.setPane(distanceTable);
734
735 inputTables.addTab(locations);
736 inputTables.addTab(distances);
737
738 filterResultCount = new StaticTextItem(MESSAGES.resultCount());
739 filterResultCount.setTitleAlign(Alignment.LEFT);
740 filterResultCount.setTitleStyle("color: #000");
741
742 filterDescription = new TableFilter();
743 filterDescription.setHeight("30px");
744 filterDescription.addFilterHandler(this);
745
746 filterRange = new RangeTableFilter();
747 filterRange.setHeight("30px");
748 filterRange.addFilterHandler(this);
749 filterRange.setVisible(false);
750
751 filterCriteria = new SelectItem();
752 filterCriteria.setShowTitle(false);
753 filterCriteria.setWidth(100);
754 filterCriteria.addChangedHandler(new ChangedHandler() {
755 @Override
756 public void onChanged(ChangedEvent e) {
757 if(e.getValue().toString().equals("range")) {
758 filterRange.setVisible(true);
759 filterDescription.setVisible(false);
760 filterDescription.clear();
761 filterResultCount.setValue("");
762 }
763 else {
764 filterRange.setVisible(false);
765 filterRange.clear();
766 filterDescription.setVisible(true);
767 filterResultCount.setValue("");
768 }
769 }
770 });
771
772 LinkedHashMap<String, String> filterMap =
773 new LinkedHashMap<String, String>();
774 filterMap.put("description", MESSAGES.description());
775 filterMap.put("range", MESSAGES.range());
776 filterCriteria.setValueMap(filterMap);
777 filterCriteria.setValue("description");
778
779 DynamicForm form = new DynamicForm();
780 form.setFields(filterCriteria);
781 inputTables.setHeight("*");
782 DynamicForm form2 = new DynamicForm();
783 form2.setFields(filterResultCount);
784
785 VLayout helper = new VLayout();
786 HLayout filterLayout = new HLayout();
787
788 filterLayout.addMember(form);
789 filterLayout.addMember(filterDescription);
790 filterLayout.addMember(filterRange);
791 filterLayout.setHeight("30px");
792 helper.addMember(inputTables);
793 helper.addMember(filterLayout);
794 helper.addMember(form2);
795 helper.setHeight100();
796 helper.setWidth100();
797
798 helperContainer.addMember(helper);
799 filterLayout.setWidth("200");
800
801 return layout;
802 }
803
804
805 @Override
806 public void onFilterCriteriaChanged(StringFilterEvent event) {
807 String search = event.getFilter();
808
809 if (search != null && search.length() > 0) {
810 Criteria c = new Criteria("description", search);
811
812 locationsTable.filterData(c);
813 distanceTable.filterData(c);
814 locationDistanceTable.filterData(c);
815 filterResultCount.setValue(currentFiltered.getRecords().length);
816 }
817 else {
818 locationsTable.clearCriteria();
819 distanceTable.clearCriteria();
820 locationDistanceTable.clearCriteria();
821 filterResultCount.setValue("");
822 }
823 }
824
825
826 @Override
827 public void onFilterCriteriaChanged(RangeFilterEvent event) {
828 Float from = event.getFrom() - 0.001f;
829 Float to = event.getTo() + 0.001f;
830 GWT.log("filtering range: " + from + " to " + to);
831
832
833 Criterion combinedFilter = null;
834 Criterion locationFilter = null;
835 if (from.equals(Float.NaN) && to.equals(Float.NaN)) {
836 locationsTable.clearCriteria();
837 distanceTable.clearCriteria();
838 locationDistanceTable.clearCriteria();
839 filterResultCount.setValue("");
840 return;
841 }
842 else if (from.equals(Float.NaN)) {
843 combinedFilter = new Criterion("to", OperatorId.LESS_OR_EQUAL, to);
844 locationFilter =
845 new Criterion("from", OperatorId.LESS_OR_EQUAL, to);
846 locationsTable.filterData(locationFilter);
847 distanceTable.filterData(combinedFilter);
848 locationDistanceTable.filterData(combinedFilter);
849 filterResultCount.setValue(currentFiltered.getRecords().length);
850 return;
851 }
852 else if (to.equals(Float.NaN)) {
853 combinedFilter =
854 new Criterion("from", OperatorId.GREATER_OR_EQUAL, from);
855 locationsTable.filterData(combinedFilter);
856 distanceTable.filterData(combinedFilter);
857 locationDistanceTable.filterData(combinedFilter);
858 }
859 else {
860 AdvancedCriteria c1 =
861 new AdvancedCriteria(OperatorId.AND, new Criterion[] {
862 new Criterion("from", OperatorId.GREATER_OR_EQUAL, from),
863 new Criterion("from", OperatorId.LESS_OR_EQUAL, to)
864 });
865
866 AdvancedCriteria c2 =
867 new AdvancedCriteria(OperatorId.AND, new Criterion[] {
868 new Criterion("to", OperatorId.GREATER_OR_EQUAL, from),
869 new Criterion("to", OperatorId.LESS_OR_EQUAL, to)
870 });
871
872 AdvancedCriteria c3 =
873 new AdvancedCriteria(OperatorId.AND, new Criterion[] {
874 new Criterion("from", OperatorId.LESS_OR_EQUAL, to),
875 new Criterion("to", OperatorId.GREATER_OR_EQUAL, from)
876 });
877
878 combinedFilter =
879 new AdvancedCriteria(OperatorId.OR, new Criterion[] {
880 c1, c2, c3
881 });
882 }
883 locationsTable.filterData(combinedFilter);
884 distanceTable.filterData(combinedFilter);
885 locationDistanceTable.filterData(combinedFilter);
886 filterResultCount.setValue(currentFiltered.getRecords().length);
887 }
888
889
890 @Override
891 public List<String> validate() {
892 if (isLocationMode()) {
893 return validateLocations();
894 }
895 else {
896 return validateRange();
897 }
898 }
899
900
901 protected List<String> validateLocations() {
902 List<String> errors = new ArrayList<String>();
903 NumberFormat nf = NumberFormat.getDecimalFormat();
904
905 try {
906 saveLocationValues(locationPanel);
907 }
908 catch (Exception e) {
909 errors.add(MESSAGES.wrongFormat());
910 }
911
912 double[] values = getLocationValues();
913 double[] good = new double[values.length];
914 int idx = 0;
915
916 for (double value: values) {
917 if (value < min || value > max) {
918 String tmp = MESSAGES.error_validate_range();
919 tmp = tmp.replace("$1", nf.format(value));
920 tmp = tmp.replace("$2", nf.format(min));
921 tmp = tmp.replace("$3", nf.format(max));
922 errors.add(tmp);
923 }
924 else {
925 good[idx++] = value;
926 }
927 }
928
929 double[] justGood = new double[idx];
930 for (int i = 0; i < justGood.length; i++) {
931 justGood[i] = good[i];
932 }
933
934 if (!errors.isEmpty()) {
935 locationPanel.setValues(justGood);
936 }
937
938 return errors;
939 }
940
941
942 protected List<String> validateRange() {
943 List<String> errors = new ArrayList<String>();
944 NumberFormat nf = NumberFormat.getDecimalFormat();
945
946 try {
947 saveDistanceValues(distancePanel);
948 }
949 catch (Exception e) {
950 errors.add(MESSAGES.wrongFormat());
951 }
952
953 double from = getFrom();
954 double to = getTo();
955 double step = getStep();
956
957 if (from < min || from > max) {
958 String tmp = MESSAGES.error_validate_lower_range();
959 tmp = tmp.replace("$1", nf.format(from));
960 tmp = tmp.replace("$2", nf.format(min));
961 errors.add(tmp);
962 from = min;
963 }
964
965 if (to < min || to > max) {
966 String tmp = MESSAGES.error_validate_upper_range();
967 tmp = tmp.replace("$1", nf.format(to));
968 tmp = tmp.replace("$2", nf.format(max));
969 errors.add(tmp);
970 to = max;
971 }
972
973 if (!errors.isEmpty()) {
974 distancePanel.setValues(from, to, step);
975 }
976
977 return errors;
978 }
979
980
981 /**
982 * This method returns the selected data.
983 *
984 * @return the selected/inserted data.
985 */
986 @Override
987 public Data[] getData() {
988 List<Data> data = new ArrayList<Data>();
989
990 // If we have entered a value and click right afterwards on the
991 // 'next' button, the BlurEvent is not fired, and the values are not
992 // saved. So, we gonna save those values explicitly.
993 if (isLocationMode()) {
994 Canvas member = container.getMember(0);
995 if (member instanceof DoubleArrayPanel) {
996 DoubleArrayPanel form = (DoubleArrayPanel) member;
997 saveLocationValues(form);
998 }
999
1000 Data dLocations = getDataLocations();
1001 DataItem dFrom = new DefaultDataItem("ld_from", "ld_from", "");
1002 DataItem dTo = new DefaultDataItem("ld_to", "ld_to", "");
1003 DataItem dStep = new DefaultDataItem("ld_step", "ld_step", "");
1004
1005 data.add(dLocations);
1006 data.add(new DefaultData(
1007 "ld_from", null, null, new DataItem[] { dFrom } ));
1008 data.add(new DefaultData(
1009 "ld_to", null, null, new DataItem[] { dTo } ));
1010 data.add(new DefaultData(
1011 "ld_step", null, null, new DataItem[] { dStep } ));
1012 }
1013 else {
1014 Canvas member = container.getMember(0);
1015 if (member instanceof DoubleRangePanel) {
1016 DoubleRangePanel form = (DoubleRangePanel) member;
1017 saveDistanceValues(form);
1018 }
1019
1020 Data dFrom = getDataFrom();
1021 Data dTo = getDataTo();
1022 Data dStep = getDataStep();
1023 DataItem loc = new DefaultDataItem("ld_locations", "ld_locations","");
1024
1025 data.add(dFrom);
1026 data.add(dTo);
1027 data.add(dStep);
1028 data.add(new DefaultData(
1029 "ld_locations", null, null, new DataItem[] { loc } ));
1030 }
1031
1032 Data dMode = getDataMode();
1033 if (dMode != null) {
1034 data.add(dMode);
1035 }
1036
1037 return data.toArray(new Data[data.size()]);
1038 }
1039
1040
1041 /**
1042 * Returns the Data object for the 'mode' attribute.
1043 *
1044 * @return the Data object for the 'mode' attribute.
1045 */
1046 protected Data getDataMode() {
1047 String value = mode.getValueAsString(FIELD_MODE);
1048 DataItem item = new DefaultDataItem("ld_mode", "ld_mode", value);
1049 return new DefaultData("ld_mode", null, null, new DataItem[] { item });
1050 }
1051
1052
1053 protected Data getDataLocations() {
1054 double[] locations = getLocationValues();
1055 boolean first = true;
1056
1057 if (locations == null) {
1058 return null;
1059 }
1060
1061 StringBuilder sb = new StringBuilder();
1062
1063 for (double l: locations) {
1064 if (!first) {
1065 sb.append(" ");
1066 }
1067
1068 sb.append(l);
1069
1070 first = false;
1071 }
1072
1073 DataItem item = new DefaultDataItem(
1074 "ld_locations",
1075 "ld_locations",
1076 sb.toString());
1077
1078 return new DefaultData(
1079 "ld_locations",
1080 null,
1081 null,
1082 new DataItem[] { item });
1083 }
1084
1085
1086 /**
1087 * Returns the Data object for the 'from' attribute.
1088 *
1089 * @return the Data object for the 'from' attribute.
1090 */
1091 protected Data getDataFrom() {
1092 String value = Double.valueOf(getFrom()).toString();
1093 DataItem item = new DefaultDataItem("ld_from", "ld_from", value);
1094 return new DefaultData(
1095 "ld_from", null, null, new DataItem[] { item });
1096 }
1097
1098
1099 /**
1100 * Returns the Data object for the 'to' attribute.
1101 *
1102 * @return the Data object for the 'to' attribute.
1103 */
1104 protected Data getDataTo() {
1105 String value = Double.valueOf(getTo()).toString();
1106 DataItem item = new DefaultDataItem("ld_to", "ld_to", value);
1107 return new DefaultData(
1108 "ld_to", null, null, new DataItem[] { item });
1109 }
1110
1111
1112 /**
1113 * Returns the Data object for the 'step' attribute.
1114 *
1115 * @return the Data object for the 'step' attribute.
1116 */
1117 protected Data getDataStep() {
1118 String value = Double.valueOf(getStep()).toString();
1119 DataItem item = new DefaultDataItem("ld_step","ld_step", value);
1120 return new DefaultData(
1121 "ld_step", null, null, new DataItem[] { item });
1122 }
1123
1124
1125 /**
1126 * Determines the current input mode.
1127 *
1128 * @return true, if 'location' is the current input mode, otherwise false.
1129 */
1130 public boolean isLocationMode() {
1131 String inputMode = mode.getValueAsString(FIELD_MODE);
1132
1133 return inputMode.equals(FIELD_VALUE_LOCATION) ? true : false;
1134 }
1135
1136
1137 /**
1138 * Activates the location panel.
1139 */
1140 protected void enableLocationPanel() {
1141 locationPanel = new DoubleArrayPanel(
1142 MESSAGES.unitLocation(),
1143 getLocationValues(),
1144 this);
1145
1146 container.removeMembers(container.getMembers());
1147 container.addMember(locationPanel);
1148 }
1149
1150
1151 /**
1152 * Activates the distance panel.
1153 */
1154 protected void enableDistancePanel() {
1155 distancePanel = new DoubleRangePanel(
1156 MESSAGES.unitFrom(), MESSAGES.unitTo(), MESSAGES.unitWidth(),
1157 getFrom(), getTo(), getStep(),
1158 400,
1159 this);
1160
1161 container.removeMembers(container.getMembers());
1162 container.addMember(distancePanel);
1163 }
1164
1165
1166 /**
1167 * This method switches the input mode between location and distance input.
1168 *
1169 * @param event The click event fired by a RadioButtonGroupItem.
1170 */
1171 @Override
1172 public void onChange(ChangeEvent event) {
1173 String value = (String) event.getValue();
1174
1175 if (value == null) {
1176 return;
1177 }
1178 if (value.equals(FIELD_VALUE_LOCATION)) {
1179 enableLocationPanel();
1180 filterDescription.clear();
1181 filterRange.clear();
1182 filterResultCount.setValue("");
1183
1184 // Remove the tab containing the locationDistanceTable.
1185 // The 'updateTab()' avoids the tab content to be destroyed.
1186 inputTables.updateTab(0, null);
1187 inputTables.removeTab(0);
1188
1189 // Create a new tab containing the locationTable
1190 Tab t1 = new Tab (MESSAGES.locations());
1191 t1.setPane(locationsTable);
1192 inputTables.addTab(t1, 0);
1193
1194 // Bring this tab to front.
1195 inputTables.selectTab(0);
1196 }
1197 else {
1198 enableDistancePanel();
1199 filterDescription.clear();
1200 filterRange.clear();
1201 filterResultCount.setValue("");
1202
1203 // Remove the tab containing the locationTable.
1204 // The 'updateTab()' avoids the tab content to be destroyed.
1205 inputTables.updateTab(0, null);
1206 inputTables.removeTab(0);
1207
1208 // Create a new tab containing the locationDistanceTable.
1209 Tab t1 = new Tab(MESSAGES.locations());
1210 t1.setPane(locationDistanceTable);
1211 inputTables.addTab(t1, 0);
1212
1213 // Bring the distanceTable tab to front.
1214 inputTables.selectTab(1);
1215 }
1216 }
1217
1218
1219 /**
1220 * This method is used to validate the inserted data in the form fields.
1221 *
1222 * @param event The BlurEvent that gives information about the FormItem that
1223 * has been modified and its value.
1224 */
1225 @Override
1226 public void onBlur(BlurEvent event) {
1227 FormItem item = event.getItem();
1228 String field = item.getFieldName();
1229
1230 if (field == null) {
1231 return;
1232 }
1233
1234 if (field.equals(DoubleArrayPanel.FIELD_NAME)) {
1235 DoubleArrayPanel p = (DoubleArrayPanel) event.getForm();
1236
1237 saveLocationValue(p, item);
1238 }
1239 else {
1240 DoubleRangePanel p = (DoubleRangePanel) event.getForm();
1241
1242 saveDistanceValue(p, item);
1243 }
1244 }
1245
1246
1247
1248 /**
1249 * Validates and stores all values entered in the location mode.
1250 *
1251 * @param p The DoubleArrayPanel.
1252 */
1253 protected void saveLocationValues(DoubleArrayPanel p) {
1254 FormItem[] formItems = p.getFields();
1255
1256 for (FormItem item: formItems) {
1257 if (item.getFieldName().equals(DoubleArrayPanel.FIELD_NAME)) {
1258 saveLocationValue(p, item);
1259 }
1260 }
1261 }
1262
1263
1264 /**
1265 * Validates and stores all values entered in the distance mode.
1266 *
1267 * @param p The DoubleRangePanel.
1268 */
1269 protected void saveDistanceValues(DoubleRangePanel p) {
1270 FormItem[] formItems = p.getFields();
1271
1272 for (FormItem item: formItems) {
1273 saveDistanceValue(p, item);
1274 }
1275 }
1276
1277
1278 /**
1279 * Validates and stores a value entered in the location mode.
1280 *
1281 * @param p The DoubleArrayPanel.
1282 * @param item The item that needs to be validated.
1283 */
1284 protected void saveLocationValue(DoubleArrayPanel p, FormItem item) {
1285 if (p.validateForm(item)) {
1286 setLocationValues(p.getInputValues(item));
1287 }
1288 }
1289
1290
1291 /**
1292 * Validates and stores value entered in the distance mode.
1293 *
1294 * @param p The DoubleRangePanel.
1295 * @param item The item that needs to be validated.
1296 */
1297 protected void saveDistanceValue(DoubleRangePanel p, FormItem item) {
1298 if (p.validateForm(item)) {
1299 setFrom(p.getFrom());
1300 setTo(p.getTo());
1301 setStep(p.getStep());
1302 }
1303 }
1304
1305
1306 /**
1307 * This method creates the panel that contains the checkboxes to switch
1308 * between the input mode 'location' and 'distance'.
1309 *
1310 * @return the checkbox panel.
1311 */
1312 protected Canvas createRadioButtonPanel() {
1313 mode = new DynamicForm();
1314
1315 RadioGroupItem radio = new RadioGroupItem(FIELD_MODE);
1316 radio.setShowTitle(false);
1317 radio.setVertical(false);
1318 radio.setWrap(false);
1319
1320 LinkedHashMap<String, String> values = new LinkedHashMap<String, String>();
1321 values.put(FIELD_VALUE_LOCATION, MESSAGES.location());
1322 values.put(FIELD_VALUE_DISTANCE, MESSAGES.distance());
1323
1324 LinkedHashMap<String, String> initial = new LinkedHashMap<String, String>();
1325 initial.put(FIELD_MODE, FIELD_VALUE_DISTANCE);
1326
1327 radio.setValueMap(values);
1328 radio.addChangeHandler(this);
1329
1330 mode.setFields(radio);
1331 mode.setValues(initial);
1332
1333 return mode;
1334 }
1335
1336
1337 protected void createDistanceInputPanel() {
1338 Config config = Config.getInstance();
1339 String url = config.getServerUrl();
1340 String river = "";
1341
1342 ArtifactDescription adescr = artifact.getArtifactDescription();
1343 DataList[] data = adescr.getOldData();
1344
1345 if (data != null && data.length > 0) {
1346 for (int i = 0; i < data.length; i++) {
1347 DataList dl = data[i];
1348 if (dl.getState().endsWith("river")) {
1349 for (int j = 0; j < dl.size(); j++) {
1350 Data d = dl.get(j);
1351 DataItem[] di = d.getItems();
1352 if (di != null && di.length == 1) {
1353 river = d.getItems()[0].getStringValue();
1354 }
1355 }
1356 }
1357 }
1358 }
1359
1360 distanceTable.setDataSource(new DistanceInfoDataSource(
1361 url, river, "distances"));
1362 locationsTable.setDataSource(new DistanceInfoDataSource(
1363 url, river, "locations"));
1364 locationDistanceTable.setDataSource(new DistanceInfoDataSource(
1365 url, river, "locations"));
1366 }
1367
1368
1369 protected double getFrom() {
1370 return from;
1371 }
1372
1373
1374 protected void setFrom(double from) {
1375 this.from = from;
1376 }
1377
1378
1379 protected double getTo() {
1380 return to;
1381 }
1382
1383
1384 protected void setTo(double to) {
1385 this.to = to;
1386 }
1387
1388
1389 protected double getStep() {
1390 return step;
1391 }
1392
1393
1394 protected void setStep(double step) {
1395 this.step = step;
1396 }
1397
1398
1399 protected double[] getLocationValues() {
1400 return values;
1401 }
1402
1403
1404 protected void setLocationValues(double[] values) {
1405 this.values = values;
1406 locationPanel.setValues(values);
1407 }
1408
1409
1410 protected void setLocationValues(String values) {
1411 String[] vs = values.split(" ");
1412
1413 if (vs == null) {
1414 return;
1415 }
1416
1417 double[] ds = new double[vs.length];
1418 int idx = 0;
1419
1420 for (String s: vs) {
1421 try {
1422 ds[idx++] = Double.valueOf(s);
1423 }
1424 catch (NumberFormatException nfe) {
1425 // do nothing
1426 }
1427 }
1428
1429 setLocationValues(ds);
1430 }
1431
1432
1433 protected void setDistanceValues (double from, double to) {
1434 setFrom(from);
1435 setTo(to);
1436 distancePanel.setValues(from, to, getStep());
1437 }
1438 }
1439 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org