comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/SingleLocationPanel.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-client/src/main/java/org/dive4elements/river/client/client/ui/SingleLocationPanel.java@821a02bbfb4e
children 172338b1407f
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.client.ui;
2
3 import com.google.gwt.i18n.client.NumberFormat;
4
5 import com.smartgwt.client.data.Record;
6
7 import com.smartgwt.client.widgets.grid.events.CellClickEvent;
8
9 import org.dive4elements.river.client.shared.model.Data;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15 * This UIProvider creates a widget to enter a single location (km).
16 */
17 public class SingleLocationPanel
18 extends MultipleLocationPanel
19 {
20 private static final long serialVersionUID = -300641333561787454L;
21
22
23 /**
24 * Creates a new SingleLocationPanel instance.
25 */
26 public SingleLocationPanel() {
27 picker = new LocationPicker(this);
28 }
29
30
31 /** Overridden to restrict to one entered value. */
32 @Override
33 public List<String> validate() {
34 List<String> errors = new ArrayList<String>();
35 NumberFormat nf = NumberFormat.getDecimalFormat();
36
37 saveLocationValues(locationPanel);
38
39 if (!locationPanel.validateForm()) {
40 errors.add(MSG.wrongFormat());
41 return errors;
42 }
43
44 double[] values = getLocationValues();
45 double[] good = new double[values.length];
46 int idx = 0;
47
48 // We want just one value to be allowed.
49 if (values.length > 1) {
50 errors.add(MSG.too_many_values());
51 }
52
53 for (double value: values) {
54 if (value < min || value > max) {
55 String tmp = MSG.error_validate_range();
56 tmp = tmp.replace("$1", nf.format(value));
57 tmp = tmp.replace("$2", nf.format(min));
58 tmp = tmp.replace("$3", nf.format(max));
59 errors.add(tmp);
60 }
61 else {
62 good[idx++] = value;
63 }
64 }
65
66 double[] justGood = new double[idx];
67 for (int i = 0; i < justGood.length; i++) {
68 justGood[i] = good[i];
69 }
70
71 if (!errors.isEmpty()) {
72 locationPanel.setValues(justGood);
73 }
74
75 return errors;
76 }
77
78
79 /**
80 * This method returns the selected data.
81 *
82 * @return the selected/inserted data.
83 */
84 @Override
85 public Data[] getData() {
86 saveLocationValues(locationPanel);
87 double[] values = getLocationValues();
88 Data[] data = new Data[values.length+1];
89
90 for (int i = 0; i < values.length; i++) {
91 data[i] = createDataArray(getDataItemName(),
92 Double.valueOf(values[i]).toString());
93 }
94
95 data[values.length] = createDataArray("ld_mode", "locations");
96
97 return data;
98 }
99
100
101 /* This is a copy of super.super.onRecordClick. Straighten out
102 this weird family. */
103 /**
104 * Callback when an item from the input helper was clicked.
105 * Set the respective km-value in the location value field.
106 * @param e event passed.
107 */
108 @Override
109 public void onCellClick (CellClickEvent e) {
110 Record record = e.getRecord();
111 double[] selected = new double[1];
112 try {
113 selected[0] =
114 Double.parseDouble(record.getAttribute("from"));
115 }
116 catch(NumberFormatException nfe) {
117 // Is there anything else to do here?
118 }
119 setLocationValues(selected);
120 }
121
122
123 @Override
124 protected String getLabelString() {
125 return MSG.single_location();
126 }
127 }
128 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org