comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/MultipleLocationPanel.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/MultipleLocationPanel.java@e70ff0a600a3
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.Record;
7 import com.smartgwt.client.util.SC;
8 import com.smartgwt.client.widgets.Canvas;
9 import com.smartgwt.client.widgets.Label;
10 import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
11 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
12 import com.smartgwt.client.widgets.grid.events.CellClickEvent;
13 import com.smartgwt.client.widgets.grid.events.CellClickHandler;
14 import com.smartgwt.client.widgets.layout.HLayout;
15 import com.smartgwt.client.widgets.layout.VLayout;
16
17 import de.intevation.flys.client.client.Config;
18 import de.intevation.flys.client.client.services.DistanceInfoService;
19 import de.intevation.flys.client.client.services.DistanceInfoServiceAsync;
20 import de.intevation.flys.client.client.ui.range.DistanceInfoDataSource;
21 import de.intevation.flys.client.shared.DoubleUtils;
22 import de.intevation.flys.client.shared.model.ArtifactDescription;
23 import de.intevation.flys.client.shared.model.Data;
24 import de.intevation.flys.client.shared.model.DataItem;
25 import de.intevation.flys.client.shared.model.DataList;
26 import de.intevation.flys.client.shared.model.DistanceInfoObject;
27 import de.intevation.flys.client.shared.model.RangeData;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32
33 /**
34 * This UIProvider creates a widget to enter a single location (km).
35 *
36 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
37 */
38 public class MultipleLocationPanel
39 extends LocationPanel
40 implements CellClickHandler
41 {
42 private static final long serialVersionUID = -3359966826794082718L;
43
44 /** The DistanceInfoService used to retrieve locations about rivers. */
45 protected DistanceInfoServiceAsync distanceInfoService =
46 GWT.create(DistanceInfoService.class);
47
48 /** The table data. */
49 protected DistanceInfoObject[] tableData;
50
51 /** The input helper (usually right side, table to click on, values are
52 * then entered in the texfield. */
53 protected LocationPicker picker;
54
55
56 /**
57 * Creates a new LocationDistancePanel instance.
58 */
59 public MultipleLocationPanel() {
60 picker = new LocationPicker(this);
61 }
62
63
64 /**
65 * This method creates a widget that contains a label, a panel with
66 * checkboxes to switch the input mode between location and distance input,
67 * and a mode specific panel.
68 *
69 * @param data The data that might be inserted.
70 *
71 * @return a panel.
72 */
73 @Override
74 public Canvas create(DataList data) {
75 findDataItemName(data);
76
77 VLayout layout = new VLayout();
78 layout.setMembersMargin(10);
79
80 // Take translated data item name as label, if translation available.
81 String labelString;
82 try {
83 labelString = MSG.getString(getDataItemName());
84 }
85 catch(java.util.MissingResourceException mre) {
86 GWT.log("Cannot find translation for data item name : " + getDataItemName());
87 labelString = getLabelString();
88 }
89 Label label = new Label(labelString);
90 Canvas widget = createWidget(data);
91 Canvas submit = getNextButton();
92
93 initDefaults(data);
94
95 picker.createLocationTable();
96
97 widget.setHeight(50);
98 label.setHeight(25);
99
100 layout.addMember(label);
101 layout.addMember(widget);
102 layout.addMember(submit);
103
104 return layout;
105 }
106
107
108 /**
109 * This method reads the default values defined in the DataItems of the Data
110 * objects in <i>list</i>.
111 *
112 * @param list The DataList container that stores the Data objects.
113 */
114 @Override
115 protected void initDefaults(DataList list) {
116 Data data = list.get(0);
117
118 // Compatibility with MinMax- DataItems:
119 RangeData rangeData = null;
120
121 for (int i = 0, n = list.size(); i < n; i++) {
122 Data tmp = list.get(i);
123
124 if (tmp instanceof RangeData) {
125 rangeData = (RangeData) tmp;
126 }
127 }
128
129 if (rangeData != null) {
130 min = Double.parseDouble(rangeData.getDefaultLower().toString());
131 max = Double.parseDouble(rangeData.getDefaultUpper().toString());
132 // catch ..?
133 }
134 else {
135 DataItem[] items = data.getItems();
136 DataItem iMin = getDataItem(items, "min");
137 DataItem iMax = getDataItem(items, "max");
138
139 try {
140 min = Double.parseDouble(iMin.getStringValue());
141 max = Double.parseDouble(iMax.getStringValue());
142 }
143 catch (NumberFormatException nfe) {
144 SC.warn(MSG.error_read_minmax_values());
145 min = -Double.MAX_VALUE;
146 max = Double.MAX_VALUE;
147 }
148 }
149
150 DataItem def = data.getDefault();
151 if (def != null) {
152 String value = def.getStringValue();
153
154 try {
155 double d = Double.parseDouble(value);
156 setLocationValues(new double[] { d } );
157 }
158 catch (NumberFormatException nfe) {
159 // could not parse, dont know what to do else
160 }
161 }
162 }
163
164
165 @Override
166 protected Canvas createWidget(DataList data) {
167 VLayout layout = new VLayout();
168 inputLayout = new HLayout();
169
170 // The initial view will display the location input mode.
171 locationPanel = new DoubleArrayPanel(
172 MSG.unitLocation(),
173 getLocationValues(),
174 new BlurHandler(){@Override
175 public void onBlur(BlurEvent be) {validate();}});
176
177 picker.getLocationTable().setAutoFetchData(true);
178
179 inputLayout.addMember(locationPanel);
180
181 layout.addMember(inputLayout);
182
183 inputLayout.setMembersMargin(30);
184
185 picker.prepareFilter();
186
187 helperContainer.addMember(picker.getLocationTable());
188 helperContainer.addMember(picker.getFilterLayout());
189 helperContainer.addMember(picker.getResultCountForm());
190 setPickerDataSource();
191 return layout;
192 }
193
194
195 /** Overridden to restrict to one entered value. */
196 @Override
197 public List<String> validate() {
198 List<String> errors = new ArrayList<String>();
199 NumberFormat nf = NumberFormat.getDecimalFormat();
200
201 DataList[] ref = artifact.getArtifactDescription().getOldData();
202 String mode = ref[1].get(0).getStringValue();
203
204 saveLocationValues(locationPanel);
205
206 if (!locationPanel.validateForm()) {
207 errors.add(MSG.wrongFormat());
208 return errors;
209 }
210
211 double[] lValues = getLocationValues();
212 double[] good = new double[lValues.length];
213 int idx = 0;
214
215 double reference =
216 Double.valueOf(ref[2].get(0).getStringValue()).doubleValue();
217 for (double value: lValues) {
218 if (mode.equals("calc.reference.curve") &&
219 value == reference) {
220 errors.add(MSG.error_contains_same_location());
221 return errors;
222 }
223 if (value < min || value > max) {
224 String tmp = MSG.error_validate_range();
225 tmp = tmp.replace("$1", nf.format(value));
226 tmp = tmp.replace("$2", nf.format(min));
227 tmp = tmp.replace("$3", nf.format(max));
228 errors.add(tmp);
229 }
230 else {
231 good[idx++] = value;
232 }
233 }
234
235 double[] justGood = new double[idx];
236 for (int i = 0; i < justGood.length; i++) {
237 justGood[i] = good[i];
238 }
239
240 if (!errors.isEmpty()) {
241 locationPanel.setValues(justGood);
242 }
243
244 return errors;
245 }
246
247
248 /**
249 * This method returns the selected data (to feed).
250 *
251 * @return the selected/inserted data in feedable form.
252 */
253 @Override
254 public Data[] getData() {
255 saveLocationValues(locationPanel);
256 double[] lValues = getLocationValues();
257 Data[] data = new Data[2];
258 boolean first = true;
259 String valueString = "";
260
261 for (int i = 0; i < lValues.length; i++) {
262 if (!first) valueString += " ";
263 else first = false;
264 valueString += Double.valueOf(lValues[i]).toString();
265 }
266
267 data[0] = createDataArray(getDataItemName(), valueString);
268
269 data[1] = createDataArray("ld_mode", "locations");
270
271 return data;
272 }
273
274
275 /** Hook service to the listgrid with possible input values. */
276 protected void setPickerDataSource() {
277 Config config = Config.getInstance();
278 String url = config.getServerUrl();
279 String river = "";
280
281 ArtifactDescription adescr = artifact.getArtifactDescription();
282 DataList[] data = adescr.getOldData();
283
284 // Try to find a "river" data item to set the source for the
285 // list grid.
286 String dataFilter = "locations";
287 if (data != null && data.length > 0) {
288 for (int i = 0; i < data.length; i++) {
289 DataList dl = data[i];
290 if (dl.getState().equals("state.minfo.river")) {
291 dataFilter = "measuringpoint";
292 }
293 if (dl.getState().equals("state.winfo.river") ||
294 dl.getState().equals("state.chart.river") ||
295 dl.getState().equals("state.minfo.river")) {
296 for (int j = 0; j < dl.size(); j++) {
297 Data d = dl.get(j);
298 DataItem[] di = d.getItems();
299 if (di != null && di.length == 1) {
300 river = d.getItems()[0].getStringValue();
301 break;
302 }
303 }
304 }
305 }
306 }
307
308 picker.getLocationTable().setDataSource(new DistanceInfoDataSource(
309 url, river, dataFilter));
310 }
311
312
313 // TODO allow multiple selections here or in LocationPanel
314 /**
315 * Callback when an item from the input helper was clicked.
316 * Set the respective km-value in the location value field.
317 * @param e event passed.
318 */
319 @Override
320 public void onCellClick (CellClickEvent e) {
321 Record record = e.getRecord();
322 double[] old = getLocationValues();
323 double[] selected = DoubleUtils.copyOf(old, old.length + 1);
324 try {
325 selected[old.length] =
326 Double.parseDouble(record.getAttribute("from"));
327 }
328 catch(NumberFormatException nfe) {
329 // Is there anything else to do here?
330 GWT.log(nfe.getMessage());
331 }
332
333 // compare reference location and target location.
334 DataList[] ref = artifact.getArtifactDescription().getOldData();
335 String mode = ref[1].get(0).getStringValue();
336 if (mode.equals("calc.reference.curve") &&
337 ref[2].get(0).getStringValue().equals(record.getAttribute("from")))
338 {
339 SC.warn(MSG.error_same_location());
340 return;
341 }
342
343 setLocationValues(selected);
344 }
345
346
347 /**
348 * Returns the label string for the input panel.
349 */
350 protected String getLabelString() {
351 return MSG.location();
352 }
353 }
354 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org