9063
|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by Intevation GmbH |
|
3 * |
|
4 * This file is Free Software under the GNU AGPL (>=v3) |
|
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
6 * documentation coming with Dive4Elements River for details. |
|
7 */ |
|
8 |
|
9 package org.dive4elements.river.client.client.ui.sinfo; |
|
10 |
|
11 import java.util.ArrayList; |
|
12 import java.util.List; |
|
13 |
|
14 import org.dive4elements.river.client.client.Config; |
|
15 import org.dive4elements.river.client.client.FLYSConstants; |
|
16 import org.dive4elements.river.client.client.services.SedimentLoadInfoService; |
|
17 import org.dive4elements.river.client.client.services.SedimentLoadInfoServiceAsync; |
|
18 import org.dive4elements.river.client.client.ui.AbstractUIProvider; |
|
19 import org.dive4elements.river.client.shared.model.ArtifactDescription; |
|
20 import org.dive4elements.river.client.shared.model.Data; |
|
21 import org.dive4elements.river.client.shared.model.DataItem; |
|
22 import org.dive4elements.river.client.shared.model.DataList; |
|
23 import org.dive4elements.river.client.shared.model.DefaultData; |
|
24 import org.dive4elements.river.client.shared.model.DefaultDataItem; |
|
25 import org.dive4elements.river.client.shared.model.SedimentLoadInfoObject; |
|
26 import org.dive4elements.river.client.shared.model.SedimentLoadInfoRecord; |
|
27 |
|
28 import com.google.gwt.core.client.GWT; |
|
29 import com.google.gwt.user.client.rpc.AsyncCallback; |
|
30 import com.smartgwt.client.data.Record; |
|
31 import com.smartgwt.client.types.ListGridFieldType; |
|
32 import com.smartgwt.client.util.SC; |
|
33 import com.smartgwt.client.widgets.Button; |
|
34 import com.smartgwt.client.widgets.Canvas; |
|
35 import com.smartgwt.client.widgets.Label; |
|
36 import com.smartgwt.client.widgets.events.ClickEvent; |
|
37 import com.smartgwt.client.widgets.events.ClickHandler; |
|
38 import com.smartgwt.client.widgets.form.DynamicForm; |
|
39 import com.smartgwt.client.widgets.form.fields.TextItem; |
|
40 import com.smartgwt.client.widgets.form.validator.IsIntegerValidator; |
|
41 import com.smartgwt.client.widgets.grid.ListGrid; |
|
42 import com.smartgwt.client.widgets.grid.ListGridField; |
|
43 import com.smartgwt.client.widgets.grid.ListGridRecord; |
|
44 import com.smartgwt.client.widgets.grid.events.RecordClickEvent; |
|
45 import com.smartgwt.client.widgets.grid.events.RecordClickHandler; |
|
46 import com.smartgwt.client.widgets.layout.HLayout; |
|
47 import com.smartgwt.client.widgets.layout.VLayout; |
|
48 |
|
49 public class CollisionLoadEpochPanel extends AbstractUIProvider { |
|
50 protected SedimentLoadInfoServiceAsync sedLoadInfoService = GWT.create(SedimentLoadInfoService.class); |
|
51 |
|
52 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); |
|
53 |
|
54 protected ListGrid elements; |
|
55 private TextItem start; |
|
56 private TextItem end; |
|
57 private ListGrid sedLoadTable; // TODO: Datenquelle für Collision richtig wählen! Sediment komplett eliminieren |
|
58 |
|
59 protected List<String> validYears; |
|
60 |
|
61 public Canvas createWidget(final DataList data) { |
|
62 final HLayout input = new HLayout(); |
|
63 final VLayout root = new VLayout(); |
|
64 final VLayout grid = new VLayout(); |
|
65 final VLayout intFields = new VLayout(); |
|
66 final Button add = new Button(this.MSG.add_date()); |
|
67 this.elements = new ListGrid(); |
|
68 |
|
69 final Label title = new Label(data.get(0).getDescription()); |
|
70 title.setHeight("25px"); |
|
71 |
|
72 final DynamicForm form = new DynamicForm(); |
|
73 form.setNumCols(4); |
|
74 this.start = new TextItem(this.MSG.from()); |
|
75 this.start.setWidth(60); |
|
76 this.start.setValidators(new IsIntegerValidator()); |
|
77 this.end = new TextItem(this.MSG.to()); |
|
78 this.end.setWidth(60); |
|
79 this.end.setValidators(new IsIntegerValidator()); |
|
80 form.setFields(this.start, this.end); |
|
81 add.addClickHandler(new ClickHandler() { |
|
82 @Override |
|
83 public void onClick(final ClickEvent ce) { |
|
84 final String v1 = CollisionLoadEpochPanel.this.start.getValueAsString(); |
|
85 final String v2 = CollisionLoadEpochPanel.this.end.getValueAsString(); |
|
86 if (v1 == null || v2 == null) { |
|
87 return; |
|
88 } |
|
89 if (!isValidEpoch(v1, v2)) { |
|
90 return; |
|
91 } |
|
92 final ListGridRecord r = new ListGridRecord(); |
|
93 r.setAttribute("from", v1); |
|
94 r.setAttribute("to", v2); |
|
95 CollisionLoadEpochPanel.this.elements.addData(r); |
|
96 } |
|
97 }); |
|
98 |
|
99 final Label sel = new Label(this.MSG.select()); |
|
100 sel.setHeight(25); |
|
101 this.elements.setWidth(185); |
|
102 this.elements.setHeight(120); |
|
103 this.elements.setShowHeaderContextMenu(false); |
|
104 this.elements.setCanReorderFields(false); |
|
105 this.elements.setCanSort(false); |
|
106 this.elements.setCanEdit(false); |
|
107 final ListGridField from = new ListGridField("from", this.MSG.from()); |
|
108 final ListGridField to = new ListGridField("to", this.MSG.to()); |
|
109 from.setWidth(70); |
|
110 to.setWidth(70); |
|
111 |
|
112 final ListGridField removeField = new ListGridField("_removeRecord", "Remove Record") { |
|
113 { |
|
114 setType(ListGridFieldType.ICON); |
|
115 setIcon(GWT.getHostPageBaseURL() + CollisionLoadEpochPanel.this.MSG.removeFeature()); |
|
116 setCanEdit(false); |
|
117 setCanFilter(false); |
|
118 setCanSort(false); |
|
119 setCanGroupBy(false); |
|
120 setCanFreeze(false); |
|
121 setWidth(25); |
|
122 } |
|
123 }; |
|
124 |
|
125 this.elements.addRecordClickHandler(new RecordClickHandler() { |
|
126 @Override |
|
127 public void onRecordClick(final RecordClickEvent event) { |
|
128 // Just handle remove-clicks |
|
129 if (!event.getField().getName().equals(removeField.getName())) { |
|
130 return; |
|
131 } |
|
132 event.getViewer().removeData(event.getRecord()); |
|
133 } |
|
134 }); |
|
135 |
|
136 this.elements.setFields(from, to, removeField); |
|
137 |
|
138 intFields.addMember(form); |
|
139 intFields.addMember(add); |
|
140 grid.addMember(sel); |
|
141 grid.addMember(this.elements); |
|
142 input.addMember(intFields); |
|
143 input.addMember(grid); |
|
144 root.addMember(title); |
|
145 root.addMember(input); |
|
146 |
|
147 return root; |
|
148 } |
|
149 |
|
150 @Override |
|
151 public Canvas createOld(final DataList dataList) { |
|
152 final HLayout layout = new HLayout(); |
|
153 layout.setWidth("400px"); |
|
154 final VLayout vLayout = new VLayout(); |
|
155 vLayout.setWidth(130); |
|
156 final Label label = new Label(dataList.getLabel()); |
|
157 label.setWidth("200px"); |
|
158 label.setHeight(25); |
|
159 |
|
160 final List<Data> items = dataList.getAll(); |
|
161 final Data str = getData(items, "epochs"); |
|
162 final DataItem[] strItems = str.getItems(); |
|
163 |
|
164 final String[] pairs = strItems[0].getLabel().split(";"); |
|
165 for (final String pair : pairs) { |
|
166 final String[] vals = pair.split(","); |
|
167 final Label dateLabel = new Label(vals[0] + " - " + vals[1]); |
|
168 dateLabel.setHeight(20); |
|
169 vLayout.addMember(dateLabel); |
|
170 } |
|
171 final Canvas back = getBackButton(dataList.getState()); |
|
172 layout.addMember(label); |
|
173 layout.addMember(vLayout); |
|
174 layout.addMember(back); |
|
175 |
|
176 return layout; |
|
177 } |
|
178 |
|
179 @Override |
|
180 public Canvas create(final DataList data) { |
|
181 final VLayout layout = new VLayout(); |
|
182 final Canvas helper = createHelper(); |
|
183 this.helperContainer.addMember(helper); |
|
184 |
|
185 final Canvas submit = getNextButton(); |
|
186 final Canvas widget = createWidget(data); |
|
187 |
|
188 layout.addMember(widget); |
|
189 layout.addMember(submit); |
|
190 |
|
191 fetchSedimentLoadData(); |
|
192 |
|
193 return layout; |
|
194 } |
|
195 |
|
196 private Canvas createHelper() { |
|
197 this.sedLoadTable = new ListGrid(); |
|
198 this.sedLoadTable.setShowHeaderContextMenu(false); |
|
199 this.sedLoadTable.setWidth100(); |
|
200 this.sedLoadTable.setShowRecordComponents(true); |
|
201 this.sedLoadTable.setShowRecordComponentsByCell(true); |
|
202 this.sedLoadTable.setHeight100(); |
|
203 this.sedLoadTable.setEmptyMessage(this.MSG.empty_table()); |
|
204 this.sedLoadTable.setCanReorderFields(false); |
|
205 |
|
206 /* Input support pins */ |
|
207 final String baseUrl = GWT.getHostPageBaseURL(); |
|
208 final ListGridField pinFrom = new ListGridField("fromIcon", this.MESSAGES.from()); |
|
209 pinFrom.setWidth(30); |
|
210 pinFrom.setType(ListGridFieldType.ICON); |
|
211 pinFrom.setCellIcon(baseUrl + this.MESSAGES.markerGreen()); |
|
212 |
|
213 final ListGridField pinTo = new ListGridField("toIcon", this.MESSAGES.to()); |
|
214 pinTo.setType(ListGridFieldType.ICON); |
|
215 pinTo.setWidth(30); |
|
216 pinTo.setCellIcon(baseUrl + this.MESSAGES.markerRed()); |
|
217 |
|
218 pinFrom.addRecordClickHandler(new RecordClickHandler() { |
|
219 @Override |
|
220 public void onRecordClick(final RecordClickEvent e) { |
|
221 final Record r = e.getRecord(); |
|
222 CollisionLoadEpochPanel.this.start.setValue(r.getAttribute("date")); |
|
223 } |
|
224 }); |
|
225 |
|
226 pinTo.addRecordClickHandler(new RecordClickHandler() { |
|
227 @Override |
|
228 public void onRecordClick(final RecordClickEvent e) { |
|
229 final Record r = e.getRecord(); |
|
230 CollisionLoadEpochPanel.this.end.setValue(r.getAttribute("date")); |
|
231 } |
|
232 }); |
|
233 |
|
234 final ListGridField date = new ListGridField("date", this.MSG.year()); |
|
235 date.setType(ListGridFieldType.TEXT); |
|
236 date.setWidth(100); |
|
237 |
|
238 final ListGridField descr = new ListGridField("description", this.MSG.description()); |
|
239 descr.setType(ListGridFieldType.TEXT); |
|
240 descr.setWidth("*"); |
|
241 |
|
242 this.sedLoadTable.setFields(pinFrom, pinTo, date, descr); |
|
243 return this.sedLoadTable; |
|
244 } |
|
245 |
|
246 @Override |
|
247 protected Data[] getData() { |
|
248 final List<Data> data = new ArrayList<Data>(); |
|
249 |
|
250 final ListGridRecord[] lgr = this.elements.getRecords(); |
|
251 if (lgr.length == 0) { |
|
252 return new Data[0]; |
|
253 } |
|
254 String d = ""; |
|
255 for (final ListGridRecord element : lgr) { |
|
256 final Record r = element; |
|
257 d += r.getAttribute("from") + "," + r.getAttribute("to"); |
|
258 d += ";"; |
|
259 } |
|
260 |
|
261 final DataItem item = new DefaultDataItem("epochs", null, d); |
|
262 data.add(new DefaultData("epochs", null, null, new DataItem[] { item })); |
|
263 return data.toArray(new Data[data.size()]); |
|
264 } |
|
265 |
|
266 protected void fetchSedimentLoadData() { |
|
267 final Config config = Config.getInstance(); |
|
268 final String locale = config.getLocale(); |
|
269 |
|
270 final ArtifactDescription adescr = this.artifact.getArtifactDescription(); |
|
271 final DataList[] data = adescr.getOldData(); |
|
272 |
|
273 final double[] km = this.artifact.getArtifactDescription().getKMRange(); |
|
274 final String river = this.artifact.getArtifactDescription().getRiver(); |
|
275 |
|
276 String sq_ti_id = ""; |
|
277 this.validYears = new ArrayList<String>(data.length); |
|
278 for (final DataList element : data) { |
|
279 final Data str = getData(element.getAll(), "sq_ti_id"); |
|
280 if (str != null) { |
|
281 final DataItem[] strItems = str.getItems(); |
|
282 sq_ti_id = strItems[0].getStringValue(); |
|
283 break; |
|
284 } |
|
285 } |
|
286 |
|
287 if (sq_ti_id.isEmpty()) { |
|
288 GWT.log("Failed to find sq time interval id in data."); |
|
289 } |
|
290 |
|
291 this.sedLoadInfoService.getSedimentLoadInfo(locale, river, "single", km[0], km[1], sq_ti_id, new AsyncCallback<SedimentLoadInfoObject[]>() { |
|
292 @Override |
|
293 public void onFailure(final Throwable caught) { |
|
294 GWT.log("Could not recieve sediment load informations."); |
|
295 SC.warn(CollisionLoadEpochPanel.this.MSG.getString(caught.getMessage())); |
|
296 } |
|
297 |
|
298 @Override |
|
299 public void onSuccess(final SedimentLoadInfoObject[] sedLoad) { |
|
300 final int num = sedLoad != null ? sedLoad.length : 0; |
|
301 GWT.log("Recieved " + num + " sediment load informations."); |
|
302 |
|
303 if (num == 0) { |
|
304 return; |
|
305 } |
|
306 |
|
307 addSedimentLoadInfo(sedLoad); |
|
308 } |
|
309 }); |
|
310 } |
|
311 |
|
312 protected void addSedimentLoadInfo(final SedimentLoadInfoObject[] sedLoad) { |
|
313 for (final SedimentLoadInfoObject sl : sedLoad) { |
|
314 final SedimentLoadInfoRecord rec = new SedimentLoadInfoRecord(sl); |
|
315 this.sedLoadTable.addData(rec); |
|
316 this.validYears.add(rec.getDate()); |
|
317 } |
|
318 } |
|
319 |
|
320 /* |
|
321 * Validate the epoch input. We do this here and not in an overridden |
|
322 * validate method as we want to validate before an epoch is added |
|
323 * to the list of epochs. |
|
324 */ |
|
325 protected boolean isValidEpoch(final String y1, final String y2) { |
|
326 // First check that both are integer |
|
327 int iY1; |
|
328 int iY2; |
|
329 final List<String> errors = new ArrayList<String>(); |
|
330 try { |
|
331 iY1 = Integer.parseInt(y1); |
|
332 } |
|
333 catch (final NumberFormatException e) { |
|
334 errors.add(this.MESSAGES.wrongFormat() + ": " + y1); |
|
335 } |
|
336 try { |
|
337 iY2 = Integer.parseInt(y2); |
|
338 } |
|
339 catch (final NumberFormatException e) { |
|
340 errors.add(this.MESSAGES.wrongFormat() + ": " + y2); |
|
341 } |
|
342 if (!errors.isEmpty()) { |
|
343 showErrors(errors); |
|
344 return false; |
|
345 } |
|
346 boolean startIsGood = false; |
|
347 boolean endIsGood = false; |
|
348 for (final String validYear : this.validYears) { |
|
349 if (startIsGood || y1.equals(validYear)) { |
|
350 startIsGood = true; |
|
351 } |
|
352 if (endIsGood || y2.equals(validYear)) { |
|
353 endIsGood = true; |
|
354 } |
|
355 if (startIsGood && endIsGood) { |
|
356 break; |
|
357 } |
|
358 /* |
|
359 * alternative check if data lies in between |
|
360 * int aYear = Integer.parseInt(validYear); |
|
361 * if (aYear >= iY1 && aYear <= iY2) { |
|
362 * isGood = true; |
|
363 * break; |
|
364 * } |
|
365 */ |
|
366 } |
|
367 if (!startIsGood) { |
|
368 String tmp = this.MESSAGES.no_data_for_year(); |
|
369 tmp = tmp.replace("$1", y1); |
|
370 errors.add(tmp); |
|
371 } |
|
372 if (!endIsGood) { |
|
373 String tmp = this.MESSAGES.no_data_for_year(); |
|
374 tmp = tmp.replace("$1", y2); |
|
375 errors.add(tmp); |
|
376 } |
|
377 if (!errors.isEmpty()) { |
|
378 showErrors(errors); |
|
379 return false; |
|
380 } |
|
381 return true; |
|
382 } |
|
383 } |