9066
|
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; |
|
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.i18n.client.NumberFormat; |
|
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.IntegerItem; |
|
38 import com.smartgwt.client.widgets.form.fields.StaticTextItem; |
|
39 import com.smartgwt.client.widgets.form.fields.TextItem; |
|
40 import com.smartgwt.client.widgets.grid.ListGrid; |
|
41 import com.smartgwt.client.widgets.grid.ListGridField; |
|
42 import com.smartgwt.client.widgets.grid.events.RecordClickEvent; |
|
43 import com.smartgwt.client.widgets.grid.events.RecordClickHandler; |
|
44 import com.smartgwt.client.widgets.layout.HLayout; |
|
45 import com.smartgwt.client.widgets.layout.VLayout; |
|
46 |
|
47 public class LoadSedimentHeightPanel extends AbstractUIProvider { |
|
48 private static final long serialVersionUID = 1L; |
|
49 |
|
50 protected SedimentLoadInfoServiceAsync sedLoadInfoService = GWT.create(SedimentLoadInfoService.class); // TODO: find right datasource |
|
51 |
|
52 private static final String datakey = "sedimentheight"; |
|
53 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); |
|
54 |
|
55 private TextItem inputItem; |
|
56 |
|
57 private ListGrid sedLoadTable; |
|
58 |
|
59 protected List<String> validInputs; |
|
60 |
|
61 public LoadSedimentHeightPanel() { |
|
62 } |
|
63 |
|
64 @Override |
|
65 public Canvas createOld(final DataList dataList) { |
|
66 final List<Data> items = dataList.getAll(); |
|
67 final Data item = getData(items, datakey); |
|
68 final DataItem[] alreadySetItems = item.getItems(); |
|
69 |
|
70 final String v1 = alreadySetItems[0].getStringValue().replace(" ", ", "); |
|
71 |
|
72 final Label old = new Label(v1); |
|
73 final HLayout layout = new HLayout(); |
|
74 layout.setWidth("400px"); |
|
75 |
|
76 final Label label = new Label(dataList.getLabel()); |
|
77 label.setWidth("200px"); |
|
78 |
|
79 final Canvas back = getBackButton(dataList.getState()); |
|
80 |
|
81 layout.addMember(label); |
|
82 layout.addMember(old); |
|
83 layout.addMember(back); |
|
84 |
|
85 return layout; |
|
86 } |
|
87 |
|
88 @Override |
|
89 public List<String> validate() { |
|
90 return validateYears(); |
|
91 } |
|
92 |
|
93 @Override |
|
94 public Canvas create(final DataList data) { |
|
95 final VLayout layout = new VLayout(); |
|
96 |
|
97 final Canvas helper = createHelper(); |
|
98 this.helperContainer.addMember(helper); |
|
99 |
|
100 final Canvas submit = getNextButton(); |
|
101 final Canvas widget = createWidget(data); |
|
102 |
|
103 layout.addMember(widget); |
|
104 layout.addMember(submit); |
|
105 |
|
106 fetchSedimentLoadData(); |
|
107 |
|
108 return layout; |
|
109 } |
|
110 |
|
111 private Canvas createHelper() { |
|
112 this.sedLoadTable = new ListGrid(); |
|
113 this.sedLoadTable.setShowHeaderContextMenu(false); |
|
114 this.sedLoadTable.setWidth100(); |
|
115 this.sedLoadTable.setShowRecordComponents(true); |
|
116 this.sedLoadTable.setShowRecordComponentsByCell(true); |
|
117 this.sedLoadTable.setHeight100(); |
|
118 this.sedLoadTable.setEmptyMessage(this.MSG.empty_table()); |
|
119 this.sedLoadTable.setCanReorderFields(false); |
|
120 |
|
121 final ListGridField date = new ListGridField("date", this.MSG.year()); |
|
122 date.setType(ListGridFieldType.TEXT); |
|
123 date.setWidth(100); |
|
124 |
|
125 final ListGridField descr = new ListGridField("description", this.MSG.description()); |
|
126 descr.setType(ListGridFieldType.TEXT); |
|
127 descr.setWidth("*"); |
|
128 |
|
129 final String baseUrl = GWT.getHostPageBaseURL(); |
|
130 final ListGridField pinFrom = new ListGridField("fromIcon", this.MESSAGES.selection()); |
|
131 pinFrom.setWidth(60); |
|
132 pinFrom.setType(ListGridFieldType.ICON); |
|
133 pinFrom.setCellIcon(baseUrl + this.MESSAGES.markerGreen()); |
|
134 |
|
135 pinFrom.addRecordClickHandler(new RecordClickHandler() { |
|
136 @Override |
|
137 public void onRecordClick(final RecordClickEvent e) { |
|
138 final Record r = e.getRecord(); |
|
139 appendValue(r.getAttribute("date")); |
|
140 } |
|
141 }); |
|
142 |
|
143 this.sedLoadTable.setFields(pinFrom, date, descr); |
|
144 return this.sedLoadTable; |
|
145 } |
|
146 |
|
147 public Canvas createWidget(final DataList data) { |
|
148 final VLayout layout = new VLayout(); |
|
149 |
|
150 final Label title = new Label(data.get(0).getDescription()); |
|
151 title.setHeight("35px"); |
|
152 |
|
153 final DynamicForm form = new DynamicForm(); |
|
154 form.setNumCols(4); |
|
155 |
|
156 final StaticTextItem textItem = new StaticTextItem("TEST"); |
|
157 textItem.setValue("cm"); |
|
158 textItem.setShowTitle(false); |
|
159 |
|
160 this.inputItem = new IntegerItem(this.MSG.year()); |
|
161 this.inputItem.setShowTitle(false); |
|
162 form.setFields(this.inputItem, textItem); |
|
163 |
|
164 layout.addMember(title); |
|
165 layout.addMember(form); |
|
166 |
|
167 return layout; |
|
168 } |
|
169 |
|
170 @Override |
|
171 protected Data[] getData() { |
|
172 validateYears(); |
|
173 if (this.inputItem != null && !this.inputItem.getValueAsString().isEmpty()) { |
|
174 final List<Data> data = new ArrayList<Data>(); |
|
175 |
|
176 final DataItem yearsdata = new DefaultDataItem(datakey, datakey, this.inputItem.getValueAsString().trim()); // DATA-STRING: years (in |
|
177 // uinfo.xml) |
|
178 data.add(new DefaultData(datakey, null, null, new DataItem[] { yearsdata })); |
|
179 |
|
180 return data.toArray(new Data[data.size()]); |
|
181 } |
|
182 return new Data[0]; |
|
183 } |
|
184 |
|
185 protected List<String> validateYears() { |
|
186 final List<String> errors = new ArrayList<String>(); |
|
187 final NumberFormat nf = NumberFormat.getDecimalFormat(); |
|
188 |
|
189 if (this.inputItem.getValueAsString() == null || this.inputItem.getValueAsString().trim().isEmpty()) { |
|
190 errors.add(this.MESSAGES.empty_filter()); |
|
191 return errors; |
|
192 } |
|
193 |
|
194 final String sValue = this.inputItem.getValueAsString();// .trim().split(" "); |
|
195 String filtered = ""; |
|
196 int goodValues = 0; |
|
197 |
|
198 int value = 0; |
|
199 try { |
|
200 value = Integer.parseInt(sValue); |
|
201 } |
|
202 catch (final NumberFormatException e) { |
|
203 errors.add(this.MESSAGES.wrongFormat() + ": " + sValue); |
|
204 |
|
205 } |
|
206 boolean isGood = false; |
|
207 for (final String validYear : this.validInputs) { |
|
208 /* No list contains for strings? */ |
|
209 if (sValue.equals(validYear)) { |
|
210 isGood = true; |
|
211 break; |
|
212 } |
|
213 } |
|
214 if (!isGood) { |
|
215 String tmp = this.MESSAGES.no_data_for_year(); |
|
216 tmp = tmp.replace("$1", sValue); |
|
217 errors.add(tmp); |
|
218 // continue; |
|
219 } |
|
220 goodValues++; |
|
221 if (goodValues > 1) { |
|
222 filtered += " " + Integer.toString(value); |
|
223 } else { |
|
224 filtered = Integer.toString(value); |
|
225 } |
|
226 |
|
227 return errors; |
|
228 } |
|
229 |
|
230 protected void fetchSedimentLoadData() { |
|
231 final Config config = Config.getInstance(); |
|
232 final String locale = config.getLocale(); |
|
233 |
|
234 final ArtifactDescription adescr = this.artifact.getArtifactDescription(); |
|
235 final DataList[] data = adescr.getOldData(); |
|
236 |
|
237 final double[] km = this.artifact.getArtifactDescription().getKMRange(); |
|
238 final String river = this.artifact.getArtifactDescription().getRiver(); |
|
239 |
|
240 String sq_ti_id = ""; |
|
241 this.validInputs = new ArrayList<String>(data.length); |
|
242 for (final DataList element : data) { |
|
243 final Data str = getData(element.getAll(), "sq_ti_id"); |
|
244 if (str != null) { |
|
245 final DataItem[] strItems = str.getItems(); |
|
246 sq_ti_id = strItems[0].getStringValue(); |
|
247 break; |
|
248 } |
|
249 } |
|
250 |
|
251 if (sq_ti_id.isEmpty()) { |
|
252 GWT.log("Failed to find sq time interval id in data."); |
|
253 } |
|
254 |
|
255 this.sedLoadInfoService.getSedimentLoadInfo(locale, river, "single", km[0], km[1], sq_ti_id, new AsyncCallback<SedimentLoadInfoObject[]>() { |
|
256 @Override |
|
257 public void onFailure(final Throwable caught) { |
|
258 GWT.log("Could not recieve sediment load informations."); |
|
259 SC.warn(LoadSedimentHeightPanel.this.MSG.getString(caught.getMessage())); |
|
260 } |
|
261 |
|
262 @Override |
|
263 public void onSuccess(final SedimentLoadInfoObject[] sedLoad) { |
|
264 final int num = sedLoad != null ? sedLoad.length : 0; |
|
265 GWT.log("Recieved " + num + " sediment load informations."); |
|
266 |
|
267 if (num == 0) { |
|
268 return; |
|
269 } |
|
270 |
|
271 addSedimentLoadInfo(sedLoad); |
|
272 } |
|
273 }); |
|
274 } |
|
275 |
|
276 protected void addSedimentLoadInfo(final SedimentLoadInfoObject[] sedLoad) { |
|
277 for (final SedimentLoadInfoObject sl : sedLoad) { |
|
278 final SedimentLoadInfoRecord rec = new SedimentLoadInfoRecord(sl); |
|
279 this.sedLoadTable.addData(rec); |
|
280 this.validInputs.add(rec.getDate()); |
|
281 } |
|
282 } |
|
283 |
|
284 protected void appendValue(final String year) { // SINGLE-Input |
|
285 final String oldYears = this.inputItem.getValueAsString(); |
|
286 this.inputItem.setValue(year); |
|
287 } |
|
288 } |