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