Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/uinfo/LoadSingleYearPanel.java @ 9077:2b13de1b0897
moved single_year/multi_year common code to AbstractPanel
author | gernotbelger |
---|---|
date | Tue, 22 May 2018 15:32:00 +0200 |
parents | b5d7a9d79837 |
children | fc02833dbcc1 |
comparison
equal
deleted
inserted
replaced
9076:22d8928895a1 | 9077:2b13de1b0897 |
---|---|
6 * documentation coming with Dive4Elements River for details. | 6 * documentation coming with Dive4Elements River for details. |
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.client.client.ui.uinfo; | 9 package org.dive4elements.river.client.client.ui.uinfo; |
10 | 10 |
11 import java.util.ArrayList; | 11 import org.dive4elements.river.client.client.ui.AbstractPanel; |
12 import java.util.List; | |
13 | 12 |
14 import org.dive4elements.river.client.client.Config; | 13 public class LoadSingleYearPanel extends AbstractPanel { |
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 | 14 |
28 import com.google.gwt.core.client.GWT; | 15 @Override |
29 import com.google.gwt.i18n.client.NumberFormat; | 16 protected String getDatakey() { |
30 import com.google.gwt.user.client.rpc.AsyncCallback; | 17 return "singleyear"; |
31 import com.smartgwt.client.data.Record; | 18 } |
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; | |
45 | |
46 public class LoadSingleYearPanel extends AbstractUIProvider { | |
47 protected SedimentLoadInfoServiceAsync sedLoadInfoService = GWT.create(SedimentLoadInfoService.class); // TODO: find right datasource | |
48 | |
49 private static final String datakey = "singleyear"; | |
50 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); | |
51 | |
52 private TextItem inputItem; | |
53 | |
54 private ListGrid sedLoadTable; | |
55 | |
56 protected List<String> validInputs; | |
57 | 19 |
58 public LoadSingleYearPanel() { | 20 public LoadSingleYearPanel() { |
59 } | 21 } |
60 | 22 |
61 @Override | 23 @Override |
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(); | |
66 | |
67 final String v1 = alreadySetItems[0].getStringValue().replace(" ", ", "); | |
68 | |
69 final Label old = new Label(v1); | |
70 final HLayout layout = new HLayout(); | |
71 layout.setWidth("400px"); | |
72 | |
73 final Label label = new Label(dataList.getLabel()); | |
74 label.setWidth("200px"); | |
75 | |
76 final Canvas back = getBackButton(dataList.getState()); | |
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 | |
91 public Canvas create(final DataList data) { | |
92 final VLayout layout = new VLayout(); | |
93 | |
94 final Canvas helper = createHelper(); | |
95 this.helperContainer.addMember(helper); | |
96 | |
97 final Canvas submit = getNextButton(); | |
98 final Canvas widget = createWidget(data); | |
99 | |
100 layout.addMember(widget); | |
101 layout.addMember(submit); | |
102 | |
103 fetchSedimentLoadData(); | |
104 | |
105 return layout; | |
106 } | |
107 | |
108 private Canvas createHelper() { | |
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); | |
117 | |
118 final ListGridField date = new ListGridField("date", this.MSG.year()); | |
119 date.setType(ListGridFieldType.TEXT); | |
120 date.setWidth(100); | |
121 | |
122 final ListGridField descr = new ListGridField("description", this.MSG.description()); | |
123 descr.setType(ListGridFieldType.TEXT); | |
124 descr.setWidth("*"); | |
125 | |
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()); | |
131 | |
132 pinFrom.addRecordClickHandler(new RecordClickHandler() { | |
133 @Override | |
134 public void onRecordClick(final RecordClickEvent e) { | |
135 final Record r = e.getRecord(); | |
136 appendValue(r.getAttribute("date")); | |
137 } | |
138 }); | |
139 | |
140 this.sedLoadTable.setFields(pinFrom, date, descr); | |
141 return this.sedLoadTable; | |
142 } | |
143 | |
144 public Canvas createWidget(final DataList data) { | |
145 final VLayout layout = new VLayout(); | |
146 | |
147 final Label title = new Label(data.get(0).getDescription()); | |
148 title.setHeight("25px"); | |
149 | |
150 final DynamicForm form = new DynamicForm(); | |
151 form.setNumCols(4); | |
152 this.inputItem = new IntegerItem(this.MSG.year()); | |
153 this.inputItem.setShowTitle(false); | |
154 form.setFields(this.inputItem); | |
155 | |
156 layout.addMember(title); | |
157 layout.addMember(form); | |
158 | |
159 return layout; | |
160 } | |
161 | |
162 @Override | |
163 protected Data[] getData() { | |
164 validateYears(); | |
165 if (this.inputItem != null && !this.inputItem.getValueAsString().isEmpty()) { | |
166 final List<Data> data = new ArrayList<Data>(); | |
167 | |
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 })); | |
171 | |
172 return data.toArray(new Data[data.size()]); | |
173 } | |
174 return new Data[0]; | |
175 } | |
176 | |
177 protected List<String> validateYears() { | |
178 final List<String> errors = new ArrayList<String>(); | |
179 final NumberFormat nf = NumberFormat.getDecimalFormat(); | |
180 | |
181 if (this.inputItem.getValueAsString() == null || this.inputItem.getValueAsString().trim().isEmpty()) { | |
182 errors.add(this.MESSAGES.empty_filter()); | |
183 return errors; | |
184 } | |
185 | |
186 final String sValue = this.inputItem.getValueAsString();// .trim().split(" "); | |
187 String filtered = ""; | |
188 int goodValues = 0; | |
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; | |
204 } | |
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); | |
217 } | |
218 | |
219 return errors; | |
220 } | |
221 | |
222 protected void fetchSedimentLoadData() { | |
223 final Config config = Config.getInstance(); | |
224 final String locale = config.getLocale(); | |
225 | |
226 final ArtifactDescription adescr = this.artifact.getArtifactDescription(); | |
227 final DataList[] data = adescr.getOldData(); | |
228 | |
229 final double[] km = this.artifact.getArtifactDescription().getKMRange(); | |
230 final String river = this.artifact.getArtifactDescription().getRiver(); | |
231 | |
232 String sq_ti_id = ""; | |
233 this.validInputs = new ArrayList<String>(data.length); | |
234 for (final DataList element : data) { | |
235 final Data str = getData(element.getAll(), "sq_ti_id"); | |
236 if (str != null) { | |
237 final DataItem[] strItems = str.getItems(); | |
238 sq_ti_id = strItems[0].getStringValue(); | |
239 break; | |
240 } | |
241 } | |
242 | |
243 if (sq_ti_id.isEmpty()) { | |
244 GWT.log("Failed to find sq time interval id in data."); | |
245 } | |
246 | |
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; | |
261 } | |
262 | |
263 addSedimentLoadInfo(sedLoad); | |
264 } | |
265 }); | |
266 } | |
267 | |
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()); | |
273 } | |
274 } | |
275 | |
276 protected void appendValue(final String year) { // SINGLE-Input | 24 protected void appendValue(final String year) { // SINGLE-Input |
277 final String oldYears = this.inputItem.getValueAsString(); | 25 final String oldYears = this.inputItem.getValueAsString(); |
278 this.inputItem.setValue(year); | 26 this.inputItem.setValue(year); |
279 } | 27 } |
280 } | 28 } |