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.uinfo; |
|
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; |
9066
|
19 import org.dive4elements.river.client.client.ui.PanelHelper; |
9063
|
20 import org.dive4elements.river.client.shared.model.ArtifactDescription; |
|
21 import org.dive4elements.river.client.shared.model.Data; |
|
22 import org.dive4elements.river.client.shared.model.DataItem; |
|
23 import org.dive4elements.river.client.shared.model.DataList; |
|
24 import org.dive4elements.river.client.shared.model.DefaultData; |
|
25 import org.dive4elements.river.client.shared.model.DefaultDataItem; |
|
26 import org.dive4elements.river.client.shared.model.SedimentLoadInfoObject; |
|
27 import org.dive4elements.river.client.shared.model.SedimentLoadInfoRecord; |
|
28 |
|
29 import com.google.gwt.core.client.GWT; |
|
30 import com.google.gwt.user.client.rpc.AsyncCallback; |
|
31 import com.smartgwt.client.data.Record; |
|
32 import com.smartgwt.client.types.ListGridFieldType; |
|
33 import com.smartgwt.client.util.SC; |
|
34 import com.smartgwt.client.widgets.Canvas; |
|
35 import com.smartgwt.client.widgets.Label; |
|
36 import com.smartgwt.client.widgets.form.DynamicForm; |
|
37 import com.smartgwt.client.widgets.form.fields.TextItem; |
|
38 import com.smartgwt.client.widgets.form.validator.IsIntegerValidator; |
|
39 import com.smartgwt.client.widgets.grid.ListGrid; |
|
40 import com.smartgwt.client.widgets.grid.ListGridField; |
|
41 import com.smartgwt.client.widgets.grid.events.RecordClickEvent; |
|
42 import com.smartgwt.client.widgets.grid.events.RecordClickHandler; |
|
43 import com.smartgwt.client.widgets.layout.HLayout; |
|
44 import com.smartgwt.client.widgets.layout.VLayout; |
|
45 |
|
46 public class LoadSingleEpochPanel extends AbstractUIProvider { |
9066
|
47 private static final long serialVersionUID = 1L; |
|
48 |
9063
|
49 protected SedimentLoadInfoServiceAsync sedLoadInfoService = GWT.create(SedimentLoadInfoService.class); |
|
50 |
|
51 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); |
|
52 |
|
53 protected ListGrid elements; |
|
54 private TextItem start; |
|
55 private TextItem end; |
|
56 private ListGrid sedLoadTable; |
|
57 |
|
58 protected List<String> validYears; |
|
59 |
|
60 public Canvas createWidget(final DataList data) { |
9066
|
61 // final HLayout input = new HLayout(); |
9063
|
62 final VLayout root = new VLayout(); |
9066
|
63 // final VLayout intFields = new VLayout(); |
9063
|
64 |
|
65 final Label title = new Label(data.get(0).getDescription()); |
|
66 title.setHeight("25px"); |
|
67 |
|
68 final DynamicForm form = new DynamicForm(); |
|
69 form.setNumCols(4); |
|
70 this.start = new TextItem(this.MSG.from()); |
|
71 this.start.setWidth(60); |
9066
|
72 this.start.setValue(2000);// TODO: get minDate |
|
73 this.start.setCanEdit(false); |
9063
|
74 this.start.setValidators(new IsIntegerValidator()); |
|
75 this.end = new TextItem(this.MSG.to()); |
|
76 this.end.setWidth(60); |
|
77 this.end.setValidators(new IsIntegerValidator()); |
9066
|
78 this.end.setCanEdit(false); |
|
79 this.end.setValue(2005);// TODO: get maxDate |
9063
|
80 form.setFields(this.start, this.end); |
|
81 |
9066
|
82 // intFields.addMember(form); |
|
83 // input.addMember(intFields); |
|
84 root.addMember(title); |
|
85 root.addMember(PanelHelper.getSpacer(10)); |
|
86 root.addMember(form); |
9063
|
87 |
9066
|
88 // root.addMember(input); |
9063
|
89 |
|
90 return root; |
|
91 } |
|
92 |
|
93 @Override |
|
94 public Canvas createOld(final DataList dataList) { |
|
95 final HLayout layout = new HLayout(); |
|
96 layout.setWidth("400px"); |
|
97 final VLayout vLayout = new VLayout(); |
|
98 vLayout.setWidth(130); |
|
99 final Label label = new Label(dataList.getLabel()); |
|
100 label.setWidth("200px"); |
|
101 label.setHeight(25); |
|
102 |
|
103 final List<Data> items = dataList.getAll(); |
9066
|
104 final Data str = getData(items, "totalepoch"); |
9063
|
105 final DataItem[] strItems = str.getItems(); |
|
106 |
|
107 final String[] pairs = strItems[0].getLabel().split(";"); |
|
108 for (final String pair : pairs) { |
|
109 final String[] vals = pair.split(","); |
|
110 final Label dateLabel = new Label(vals[0] + " - " + vals[1]); |
|
111 dateLabel.setHeight(20); |
|
112 vLayout.addMember(dateLabel); |
|
113 } |
|
114 final Canvas back = getBackButton(dataList.getState()); |
|
115 layout.addMember(label); |
|
116 layout.addMember(vLayout); |
|
117 layout.addMember(back); |
|
118 |
|
119 return layout; |
|
120 } |
|
121 |
|
122 @Override |
|
123 public Canvas create(final DataList data) { |
|
124 final VLayout layout = new VLayout(); |
|
125 final Canvas helper = createHelper(); |
|
126 this.helperContainer.addMember(helper); |
|
127 |
|
128 final Canvas submit = getNextButton(); |
|
129 final Canvas widget = createWidget(data); |
|
130 |
|
131 layout.addMember(widget); |
|
132 layout.addMember(submit); |
|
133 |
|
134 fetchSedimentLoadData(); |
|
135 |
|
136 return layout; |
|
137 } |
|
138 |
|
139 private Canvas createHelper() { |
9066
|
140 this.sedLoadTable = new ListGrid(); // TODO: get minMax from right datasource |
9063
|
141 this.sedLoadTable.setShowHeaderContextMenu(false); |
|
142 this.sedLoadTable.setWidth100(); |
|
143 this.sedLoadTable.setShowRecordComponents(true); |
|
144 this.sedLoadTable.setShowRecordComponentsByCell(true); |
|
145 this.sedLoadTable.setHeight100(); |
|
146 this.sedLoadTable.setEmptyMessage(this.MSG.empty_table()); |
|
147 this.sedLoadTable.setCanReorderFields(false); |
|
148 |
|
149 /* Input support pins */ |
|
150 final String baseUrl = GWT.getHostPageBaseURL(); |
|
151 final ListGridField pinFrom = new ListGridField("fromIcon", this.MESSAGES.from()); |
|
152 pinFrom.setWidth(30); |
|
153 pinFrom.setType(ListGridFieldType.ICON); |
|
154 pinFrom.setCellIcon(baseUrl + this.MESSAGES.markerGreen()); |
|
155 |
|
156 final ListGridField pinTo = new ListGridField("toIcon", this.MESSAGES.to()); |
|
157 pinTo.setType(ListGridFieldType.ICON); |
|
158 pinTo.setWidth(30); |
|
159 pinTo.setCellIcon(baseUrl + this.MESSAGES.markerRed()); |
|
160 |
|
161 pinFrom.addRecordClickHandler(new RecordClickHandler() { |
|
162 @Override |
|
163 public void onRecordClick(final RecordClickEvent e) { |
|
164 final Record r = e.getRecord(); |
9066
|
165 LoadSingleEpochPanel.this.start.setValue("darf nicht geändert werden");// r.getAttribute("date")); |
9063
|
166 } |
|
167 }); |
|
168 |
|
169 pinTo.addRecordClickHandler(new RecordClickHandler() { |
|
170 @Override |
|
171 public void onRecordClick(final RecordClickEvent e) { |
|
172 final Record r = e.getRecord(); |
9066
|
173 LoadSingleEpochPanel.this.end.setValue("darf nicht geändert werden"); |
9063
|
174 } |
|
175 }); |
|
176 |
|
177 final ListGridField date = new ListGridField("date", this.MSG.year()); |
|
178 date.setType(ListGridFieldType.TEXT); |
|
179 date.setWidth(100); |
|
180 |
|
181 final ListGridField descr = new ListGridField("description", this.MSG.description()); |
|
182 descr.setType(ListGridFieldType.TEXT); |
|
183 descr.setWidth("*"); |
|
184 |
|
185 this.sedLoadTable.setFields(pinFrom, pinTo, date, descr); |
|
186 return this.sedLoadTable; |
|
187 } |
|
188 |
|
189 @Override |
|
190 protected Data[] getData() { |
|
191 final List<Data> data = new ArrayList<Data>(); |
|
192 |
9066
|
193 // final ListGridRecord[] lgr = this.elements.getRecords(); |
|
194 // if (lgr.length == 0) { |
|
195 // return new Data[0]; |
|
196 // } |
9063
|
197 String d = ""; |
9066
|
198 // // for (final ListGridRecord element : lgr) { |
|
199 // final Record r = element; |
|
200 // d += r.getAttribute("from") + "," + r.getAttribute("to"); |
|
201 // d += ";"; |
|
202 // } |
9063
|
203 |
9066
|
204 d = this.start.getValueAsString() + "," + this.end.getValueAsString() + ";"; |
|
205 |
|
206 final DataItem item = new DefaultDataItem("totalepoch", null, d); |
|
207 data.add(new DefaultData("totalepoch", null, null, new DataItem[] { item })); |
9063
|
208 return data.toArray(new Data[data.size()]); |
|
209 } |
|
210 |
|
211 protected void fetchSedimentLoadData() { |
|
212 final Config config = Config.getInstance(); |
|
213 final String locale = config.getLocale(); |
|
214 |
|
215 final ArtifactDescription adescr = this.artifact.getArtifactDescription(); |
|
216 final DataList[] data = adescr.getOldData(); |
|
217 |
|
218 final double[] km = this.artifact.getArtifactDescription().getKMRange(); |
|
219 final String river = this.artifact.getArtifactDescription().getRiver(); |
|
220 |
|
221 String sq_ti_id = ""; |
|
222 this.validYears = new ArrayList<String>(data.length); |
|
223 for (final DataList element : data) { |
|
224 final Data str = getData(element.getAll(), "sq_ti_id"); |
|
225 if (str != null) { |
|
226 final DataItem[] strItems = str.getItems(); |
|
227 sq_ti_id = strItems[0].getStringValue(); |
|
228 break; |
|
229 } |
|
230 } |
|
231 |
|
232 if (sq_ti_id.isEmpty()) { |
|
233 GWT.log("Failed to find sq time interval id in data."); |
|
234 } |
|
235 |
|
236 this.sedLoadInfoService.getSedimentLoadInfo(locale, river, "single", km[0], km[1], sq_ti_id, new AsyncCallback<SedimentLoadInfoObject[]>() { |
|
237 @Override |
|
238 public void onFailure(final Throwable caught) { |
|
239 GWT.log("Could not recieve sediment load informations."); |
|
240 SC.warn(LoadSingleEpochPanel.this.MSG.getString(caught.getMessage())); |
|
241 } |
|
242 |
|
243 @Override |
|
244 public void onSuccess(final SedimentLoadInfoObject[] sedLoad) { |
|
245 final int num = sedLoad != null ? sedLoad.length : 0; |
|
246 GWT.log("Recieved " + num + " sediment load informations."); |
|
247 |
|
248 if (num == 0) { |
|
249 return; |
|
250 } |
|
251 |
|
252 addSedimentLoadInfo(sedLoad); |
|
253 } |
|
254 }); |
|
255 } |
|
256 |
|
257 protected void addSedimentLoadInfo(final SedimentLoadInfoObject[] sedLoad) { |
|
258 for (final SedimentLoadInfoObject sl : sedLoad) { |
|
259 final SedimentLoadInfoRecord rec = new SedimentLoadInfoRecord(sl); |
|
260 this.sedLoadTable.addData(rec); |
|
261 this.validYears.add(rec.getDate()); |
|
262 } |
|
263 } |
|
264 |
|
265 /* |
|
266 * Validate the epoch input. We do this here and not in an overridden |
|
267 * validate method as we want to validate before an epoch is added |
|
268 * to the list of epochs. |
|
269 */ |
|
270 protected boolean isValidEpoch(final String y1, final String y2) { |
|
271 // First check that both are integer |
|
272 int iY1; |
|
273 int iY2; |
|
274 final List<String> errors = new ArrayList<String>(); |
|
275 try { |
|
276 iY1 = Integer.parseInt(y1); |
|
277 } |
|
278 catch (final NumberFormatException e) { |
|
279 errors.add(this.MESSAGES.wrongFormat() + ": " + y1); |
|
280 } |
|
281 try { |
|
282 iY2 = Integer.parseInt(y2); |
|
283 } |
|
284 catch (final NumberFormatException e) { |
|
285 errors.add(this.MESSAGES.wrongFormat() + ": " + y2); |
|
286 } |
|
287 if (!errors.isEmpty()) { |
|
288 showErrors(errors); |
|
289 return false; |
|
290 } |
|
291 boolean startIsGood = false; |
|
292 boolean endIsGood = false; |
|
293 for (final String validYear : this.validYears) { |
|
294 if (startIsGood || y1.equals(validYear)) { |
|
295 startIsGood = true; |
|
296 } |
|
297 if (endIsGood || y2.equals(validYear)) { |
|
298 endIsGood = true; |
|
299 } |
|
300 if (startIsGood && endIsGood) { |
|
301 break; |
|
302 } |
|
303 /* |
|
304 * alternative check if data lies in between |
|
305 * int aYear = Integer.parseInt(validYear); |
|
306 * if (aYear >= iY1 && aYear <= iY2) { |
|
307 * isGood = true; |
|
308 * break; |
|
309 * } |
|
310 */ |
|
311 } |
|
312 if (!startIsGood) { |
|
313 String tmp = this.MESSAGES.no_data_for_year(); |
|
314 tmp = tmp.replace("$1", y1); |
|
315 errors.add(tmp); |
|
316 } |
|
317 if (!endIsGood) { |
|
318 String tmp = this.MESSAGES.no_data_for_year(); |
|
319 tmp = tmp.replace("$1", y2); |
|
320 errors.add(tmp); |
|
321 } |
|
322 if (!errors.isEmpty()) { |
|
323 showErrors(errors); |
|
324 return false; |
|
325 } |
|
326 return true; |
|
327 } |
|
328 } |