comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java @ 516:ba606e575663

ISSUE-85 (part II/III) Repaired broken location/range panel. flys-client/trunk@1987 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 24 May 2011 10:52:00 +0000
parents be842e36ce1c
children ec965bf8dfef
comparison
equal deleted inserted replaced
515:27ffaf628b54 516:ba606e575663
64 64
65 /** The DistanceInfoService used to retrieve locations about rivers.*/ 65 /** The DistanceInfoService used to retrieve locations about rivers.*/
66 protected DistanceInfoServiceAsync distanceInfoService = 66 protected DistanceInfoServiceAsync distanceInfoService =
67 GWT.create(DistanceInfoService.class); 67 GWT.create(DistanceInfoService.class);
68 68
69 /** The constant name of the input field to enter locations.*/ 69 public static final String FIELD_MODE = "mode";
70 public static final String FIELD_LOCATION = "location";
71
72 /** The constant name of the input field to enter distance.*/
73 public static final String FIELD_DISTANCE = "distance";
74 70
75 /** The constant name of the input field to enter the start of a distance.*/ 71 /** The constant name of the input field to enter the start of a distance.*/
76 public static final String FIELD_FROM = "from"; 72 public static final String FIELD_FROM = "from";
77 73
78 /** The constant name of the input field to enter the end of a distance.*/ 74 /** The constant name of the input field to enter the end of a distance.*/
79 public static final String FIELD_TO = "to"; 75 public static final String FIELD_TO = "to";
76
77 /** The constant name of the input field to enter locations.*/
78 public static final String FIELD_VALUE_LOCATION = "location";
79
80 /** The constant name of the input field to enter distance.*/
81 public static final String FIELD_VALUE_DISTANCE = "distance";
80 82
81 /** The constant name of the input field to enter the step width of a 83 /** The constant name of the input field to enter the step width of a
82 * distance.*/ 84 * distance.*/
83 public static final String FIELD_WIDTH = "width"; 85 public static final String FIELD_WIDTH = "width";
84 86
154 */ 156 */
155 public Canvas create(DataList data) { 157 public Canvas create(DataList data) {
156 VLayout layout = new VLayout(); 158 VLayout layout = new VLayout();
157 layout.setMembersMargin(10); 159 layout.setMembersMargin(10);
158 160
159 initDefaults(data);
160
161 Label label = new Label(MESSAGES.location_distance_state()); 161 Label label = new Label(MESSAGES.location_distance_state());
162 Canvas widget = createWidget(data); 162 Canvas widget = createWidget(data);
163 Canvas submit = getNextButton(); 163 Canvas submit = getNextButton();
164 createDistanceInputPanel(); 164 createDistanceInputPanel();
165
166 initDefaults(data);
165 167
166 createDistanceTable(); 168 createDistanceTable();
167 createLocationTable(); 169 createLocationTable();
168 createLocationTableDistance (); 170 createLocationTableDistance ();
169 171
348 350
349 351
350 public Canvas createOld(DataList dataList) { 352 public Canvas createOld(DataList dataList) {
351 List<Data> items = dataList.getAll(); 353 List<Data> items = dataList.getAll();
352 354
355 Data dMode = getData(items, "ld_mode");
356 DataItem[] dItems = dMode.getItems();
357
358 boolean rangeMode = true;
359 if (dItems != null && dItems[0] != null) {
360 rangeMode = FIELD_VALUE_DISTANCE.equals(dItems[0].getStringValue());
361 }
362
363 HLayout layout = new HLayout();
364 layout.setWidth("400px");
365
366 Label label = new Label(dataList.getLabel());
367 label.setWidth("200px");
368
369 Canvas back = getBackButton(dataList.getState());
370
371 layout.addMember(label);
372
373 if (rangeMode) {
374 layout.addMember(getOldRangeSelection(dataList));
375 }
376 else {
377 layout.addMember(getOldLocationSelection(dataList));
378 }
379
380 layout.addMember(back);
381
382 return layout;
383 }
384
385
386 /**
387 * Creates a label for the selected range.
388 *
389 * @param dataList The DataList containing all values for this state.
390 *
391 * @return A label displaying the selected values.
392 */
393 protected Label getOldRangeSelection(DataList dataList) {
394 List<Data> items = dataList.getAll();
395
353 Data dFrom = getData(items, "ld_from"); 396 Data dFrom = getData(items, "ld_from");
354 Data dTo = getData(items, "ld_to"); 397 Data dTo = getData(items, "ld_to");
355 Data dStep = getData(items, "ld_step"); 398 Data dStep = getData(items, "ld_step");
356 399
357 DataItem[] from = dFrom.getItems(); 400 DataItem[] from = dFrom.getItems();
358 DataItem[] to = dTo.getItems(); 401 DataItem[] to = dTo.getItems();
359 DataItem[] step = dStep.getItems(); 402 DataItem[] step = dStep.getItems();
360
361 HLayout layout = new HLayout();
362 layout.setWidth("400px");
363
364 Label label = new Label(dataList.getLabel());
365 label.setWidth("200px");
366 403
367 StringBuilder sb = new StringBuilder(); 404 StringBuilder sb = new StringBuilder();
368 sb.append(from[0].getLabel()); 405 sb.append(from[0].getLabel());
369 sb.append(" " + MESSAGES.unitFrom() + " "); 406 sb.append(" " + MESSAGES.unitFrom() + " ");
370 sb.append(to[0].getLabel()); 407 sb.append(to[0].getLabel());
371 sb.append(" " + MESSAGES.unitTo() + " "); 408 sb.append(" " + MESSAGES.unitTo() + " ");
372 sb.append(step[0].getLabel()); 409 sb.append(step[0].getLabel());
373 sb.append(" " + MESSAGES.unitWidth()); 410 sb.append(" " + MESSAGES.unitWidth());
374 411
375 Canvas back = getBackButton(dataList.getState());
376
377 Label selected = new Label(sb.toString()); 412 Label selected = new Label(sb.toString());
378 selected.setWidth("130px"); 413 selected.setWidth("130px");
379 414
380 layout.addMember(label); 415 return selected;
381 layout.addMember(selected); 416 }
382 layout.addMember(back); 417
383 418
384 return layout; 419 /**
420 * Creates a label for the selected locations.
421 *
422 * @param dataList The DataList containing all values for this state.
423 *
424 * @return A label displaying the selected values.
425 */
426 protected Label getOldLocationSelection(DataList dataList) {
427 List<Data> items = dataList.getAll();
428
429 Data dLocations = getData(items, "ld_locations");
430 DataItem[] lItems = dLocations.getItems();
431
432 String value = lItems[0].getStringValue();
433
434 Label selected = new Label(value);
435 selected.setWidth(130);
436
437 return selected;
385 } 438 }
386 439
387 440
388 /** 441 /**
389 * This method reads the default values defined in the DataItems of the Data 442 * This method reads the default values defined in the DataItems of the Data
390 * objects in <i>list</i>. 443 * objects in <i>list</i>.
391 * 444 *
392 * @param list The DataList container that stores the Data objects. 445 * @param list The DataList container that stores the Data objects.
393 */ 446 */
394 protected void initDefaults(DataList list) { 447 protected void initDefaults(DataList list) {
448 Data m = getData(list.getAll(), "ld_mode");
449 Data l = getData(list.getAll(), "ld_locations");
395 Data f = getData(list.getAll(), "ld_from"); 450 Data f = getData(list.getAll(), "ld_from");
396 Data t = getData(list.getAll(), "ld_to"); 451 Data t = getData(list.getAll(), "ld_to");
397 Data s = getData(list.getAll(), "ld_step"); 452 Data s = getData(list.getAll(), "ld_step");
398 453
399 DataItem[] fItems = f.getItems(); 454 DataItem[] fItems = f.getItems();
402 457
403 min = Double.valueOf(fItems[0].getStringValue()); 458 min = Double.valueOf(fItems[0].getStringValue());
404 max = Double.valueOf(tItems[0].getStringValue()); 459 max = Double.valueOf(tItems[0].getStringValue());
405 step = Double.valueOf(sItems[0].getStringValue()); 460 step = Double.valueOf(sItems[0].getStringValue());
406 461
407 this.from = min; 462 DataItem mDef = m.getDefault();
408 this.to = max; 463 DataItem lDef = l.getDefault();
409 this.step = step; 464 DataItem fDef = f.getDefault();
410 } 465 DataItem tDef = t.getDefault();
411 466 DataItem sDef = s.getDefault();
412 467
413 /** 468 String theMode = mDef != null
414 * This method greps the Data with name <i>name</i> from the list and 469 ? mDef.getStringValue()
415 * returns it. 470 : FIELD_VALUE_LOCATION;
416 * 471
417 * @param items A list of Data. 472 mode.setValue(FIELD_MODE, theMode);
418 * @param name The name of the Data that we are searching for. 473
419 * 474 setFrom(fDef != null
420 * @return the Data with the name <i>name</i>. 475 ? Double.valueOf(fDef.getStringValue())
421 */ 476 : min);
422 protected Data getData(List<Data> data, String name) { 477
423 for (Data d: data) { 478 setTo(tDef != null
424 if (name.equals(d.getLabel())) { 479 ? Double.valueOf(tDef.getStringValue())
425 return d; 480 : max);
426 } 481
427 } 482 setStep(sDef != null
428 483 ? Double.valueOf(sDef.getStringValue())
429 return null; 484 : step);
485
486 if (lDef != null) {
487 setLocationValues(lDef.getStringValue());
488 }
489
490 if (theMode.equals(FIELD_VALUE_DISTANCE)) {
491 enableDistancePanel();
492 }
430 } 493 }
431 494
432 495
433 protected Canvas createWidget(DataList data) { 496 protected Canvas createWidget(DataList data) {
434 VLayout layout = new VLayout(); 497 VLayout layout = new VLayout();
488 DoubleRangePanel form = (DoubleRangePanel) member; 551 DoubleRangePanel form = (DoubleRangePanel) member;
489 saveDistanceValues(form); 552 saveDistanceValues(form);
490 } 553 }
491 } 554 }
492 555
493 return new Data[] { getDataFrom(), getDataTo(), getDataStep() }; 556 return new Data[] {
557 getDataMode(),
558 getDataLocations(),
559 getDataFrom(),
560 getDataTo(),
561 getDataStep() };
562 }
563
564
565 /**
566 * Returns the Data object for the 'mode' attribute.
567 *
568 * @return the Data object for the 'mode' attribute.
569 */
570 protected Data getDataMode() {
571 String value = mode.getValueAsString(FIELD_MODE);
572 DataItem item = new DefaultDataItem("ld_mode", "ld_mode", value);
573 return new DefaultData("ld_mode", null, null, new DataItem[] { item });
574 }
575
576
577 protected Data getDataLocations() {
578 double[] locations = getLocationValues();
579 boolean first = true;
580
581 StringBuilder sb = new StringBuilder();
582
583 for (double l: locations) {
584 if (!first) {
585 sb.append(" ");
586 }
587
588 sb.append(l);
589
590 first = false;
591 }
592
593 DataItem item = new DefaultDataItem(
594 "ld_locations",
595 "ld_locations",
596 sb.toString());
597
598 return new DefaultData(
599 "ld_locations",
600 null,
601 null,
602 new DataItem[] { item });
494 } 603 }
495 604
496 605
497 /** 606 /**
498 * Returns the Data object for the 'from' attribute. 607 * Returns the Data object for the 'from' attribute.
499 * 608 *
500 * @return the Data object for the 'from' attribute. 609 * @return the Data object for the 'from' attribute.
501 */ 610 */
502 protected Data getDataFrom() { 611 protected Data getDataFrom() {
503 String value = Double.valueOf(getFinalFrom()).toString(); 612 String value = Double.valueOf(getFrom()).toString();
504 DataItem item = new DefaultDataItem("ld_from", "ld_from", value); 613 DataItem item = new DefaultDataItem("ld_from", "ld_from", value);
505 return new DefaultData( 614 return new DefaultData(
506 "ld_from", null, null, new DataItem[] { item }); 615 "ld_from", null, null, new DataItem[] { item });
507 } 616 }
508 617
511 * Returns the Data object for the 'to' attribute. 620 * Returns the Data object for the 'to' attribute.
512 * 621 *
513 * @return the Data object for the 'to' attribute. 622 * @return the Data object for the 'to' attribute.
514 */ 623 */
515 protected Data getDataTo() { 624 protected Data getDataTo() {
516 String value = Double.valueOf(getFinalTo()).toString(); 625 String value = Double.valueOf(getTo()).toString();
517 DataItem item = new DefaultDataItem("ld_to", "ld_to", value); 626 DataItem item = new DefaultDataItem("ld_to", "ld_to", value);
518 return new DefaultData( 627 return new DefaultData(
519 "ld_to", null, null, new DataItem[] { item }); 628 "ld_to", null, null, new DataItem[] { item });
520 } 629 }
521 630
524 * Returns the Data object for the 'step' attribute. 633 * Returns the Data object for the 'step' attribute.
525 * 634 *
526 * @return the Data object for the 'step' attribute. 635 * @return the Data object for the 'step' attribute.
527 */ 636 */
528 protected Data getDataStep() { 637 protected Data getDataStep() {
529 String value = Double.valueOf(getFinalStep()).toString(); 638 String value = Double.valueOf(getStep()).toString();
530 DataItem item = new DefaultDataItem("ld_step","ld_step", value); 639 DataItem item = new DefaultDataItem("ld_step","ld_step", value);
531 return new DefaultData( 640 return new DefaultData(
532 "ld_step", null, null, new DataItem[] { item }); 641 "ld_step", null, null, new DataItem[] { item });
533 } 642 }
534 643
535 644
536 /** 645 /**
537 * Returns the value of 'from' depending on the selected input mode.
538 *
539 * @return the value of 'from' depending on the selected input mode.
540 */
541 protected double getFinalFrom() {
542 if (isLocationMode()) {
543 double[] values = getLocationValues();
544 double value = Double.MAX_VALUE;
545
546 for (double v: values) {
547 value = value < v ? value : v;
548 }
549
550 return value;
551 }
552 else {
553 return getFrom();
554 }
555 }
556
557
558 /**
559 * Returns the value of 'to' depending on the selected input mode.
560 *
561 * @return the value of 'to' depending on the selected input mode.
562 */
563 protected double getFinalTo() {
564 if (isLocationMode()) {
565 double[] values = getLocationValues();
566 double value = Double.MIN_VALUE;
567
568 for (double v: values) {
569 value = value > v ? value : v;
570 }
571
572 return value;
573 }
574 else {
575 return getTo();
576 }
577 }
578
579
580 /**
581 * Returns the value of 'step' depending on the selected input mode.
582 *
583 * @return the value of 'step' depending on the selected input mode.
584 */
585 protected double getFinalStep() {
586 if (isLocationMode()) {
587 // we have no field to enter the 'step' attribute in the location
588 // mode
589 return 0.0;
590 }
591 else {
592 return getStep();
593 }
594 }
595
596
597 /**
598 * Determines the current input mode. 646 * Determines the current input mode.
599 * 647 *
600 * @return true, if 'location' is the current input mode, otherwise false. 648 * @return true, if 'location' is the current input mode, otherwise false.
601 */ 649 */
602 public boolean isLocationMode() { 650 public boolean isLocationMode() {
603 String inputMode = mode.getValueAsString("mode"); 651 String inputMode = mode.getValueAsString(FIELD_MODE);
604 652
605 return inputMode.equals(FIELD_LOCATION) ? true : false; 653 return inputMode.equals(FIELD_VALUE_LOCATION) ? true : false;
654 }
655
656
657 /**
658 * Activates the location panel.
659 */
660 protected void enableLocationPanel() {
661 locationPanel = new DoubleArrayPanel(
662 MESSAGES.unitLocation(),
663 getLocationValues(),
664 this);
665
666 container.removeMembers(container.getMembers());
667 container.addMember(locationPanel);
668 }
669
670
671 /**
672 * Activates the distance panel.
673 */
674 protected void enableDistancePanel() {
675 distancePanel = new DoubleRangePanel(
676 MESSAGES.unitFrom(), MESSAGES.unitTo(), MESSAGES.unitWidth(),
677 getFrom(), getTo(), getStep(),
678 250,
679 this);
680
681 container.removeMembers(container.getMembers());
682 container.addMember(distancePanel);
606 } 683 }
607 684
608 685
609 /** 686 /**
610 * This method switches the input mode between location and distance input. 687 * This method switches the input mode between location and distance input.
615 String value = (String) event.getValue(); 692 String value = (String) event.getValue();
616 693
617 if (value == null) { 694 if (value == null) {
618 return; 695 return;
619 } 696 }
620 if (value.equals(FIELD_LOCATION)) { 697 if (value.equals(FIELD_VALUE_LOCATION)) {
621 locationPanel = new DoubleArrayPanel( 698 enableLocationPanel();
622 MESSAGES.unitLocation(), 699
623 getLocationValues(),
624 this);
625
626 container.removeMembers(container.getMembers());
627 container.addMember(locationPanel);
628 while (inputTables.getNumTabs() > 0) { 700 while (inputTables.getNumTabs() > 0) {
629 inputTables.removeTab(0); 701 inputTables.removeTab(0);
630 } 702 }
631 Tab t1 = new Tab (MESSAGES.location()); 703 Tab t1 = new Tab (MESSAGES.location());
632 createLocationTable(); 704 createLocationTable();
640 712
641 helperContainer.addChild(inputTables); 713 helperContainer.addChild(inputTables);
642 inputTables.selectTab(0); 714 inputTables.selectTab(0);
643 } 715 }
644 else { 716 else {
645 distancePanel = new DoubleRangePanel( 717 enableDistancePanel();
646 MESSAGES.unitFrom(), MESSAGES.unitTo(), MESSAGES.unitWidth(), 718
647 getFrom(), getTo(), getStep(),
648 250,
649 this);
650
651 container.removeMembers(container.getMembers());
652 container.addMember(distancePanel);
653 while (inputTables.getNumTabs () > 0) { 719 while (inputTables.getNumTabs () > 0) {
654 inputTables.removeTab(0); 720 inputTables.removeTab(0);
655 } 721 }
656 Tab t1 = new Tab(MESSAGES.location ()); 722 Tab t1 = new Tab(MESSAGES.location ());
657 createLocationTableDistance (); 723 createLocationTableDistance ();
764 * @return the checkbox panel. 830 * @return the checkbox panel.
765 */ 831 */
766 protected Canvas createRadioButtonPanel() { 832 protected Canvas createRadioButtonPanel() {
767 mode = new DynamicForm(); 833 mode = new DynamicForm();
768 834
769 RadioGroupItem radio = new RadioGroupItem("mode"); 835 RadioGroupItem radio = new RadioGroupItem(FIELD_MODE);
770 radio.setShowTitle(false); 836 radio.setShowTitle(false);
771 radio.setVertical(false); 837 radio.setVertical(false);
772 838
773 LinkedHashMap values = new LinkedHashMap(); 839 LinkedHashMap values = new LinkedHashMap();
774 values.put(FIELD_LOCATION, MESSAGES.location()); 840 values.put(FIELD_VALUE_LOCATION, MESSAGES.location());
775 values.put(FIELD_DISTANCE, MESSAGES.distance()); 841 values.put(FIELD_VALUE_DISTANCE, MESSAGES.distance());
776 842
777 LinkedHashMap initial = new LinkedHashMap(); 843 LinkedHashMap initial = new LinkedHashMap();
778 initial.put("mode", FIELD_LOCATION); 844 initial.put(FIELD_MODE, FIELD_VALUE_LOCATION);
779 845
780 radio.setValueMap(values); 846 radio.setValueMap(values);
781 radio.addChangeHandler(this); 847 radio.addChangeHandler(this);
782 848
783 mode.setFields(radio); 849 mode.setFields(radio);
887 this.values = values; 953 this.values = values;
888 locationPanel.setValues(values); 954 locationPanel.setValues(values);
889 } 955 }
890 956
891 957
958 protected void setLocationValues(String values) {
959 String[] vs = values.split(" ");
960
961 if (vs == null) {
962 return;
963 }
964
965 double[] ds = new double[vs.length];
966 int idx = 0;
967
968 for (String s: vs) {
969 try {
970 ds[idx++] = Double.valueOf(s);
971 }
972 catch (NumberFormatException nfe) {
973 // do nothing
974 }
975 }
976
977 setLocationValues(ds);
978 }
979
980
892 protected void setDistanceValues (double from, double to) { 981 protected void setDistanceValues (double from, double to) {
893 setFrom(from); 982 setFrom(from);
894 setTo(to); 983 setTo(to);
895 distancePanel.setValues(from, to, getStep()); 984 distancePanel.setValues(from, to, getStep());
896 } 985 }

http://dive4elements.wald.intevation.org