Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/stationinfo/GaugeListGrid.java @ 4956:f46a07c11324
Refactor Pegel- and Messtelleninfo in client ui
Use SmartGWT ListGrid instead of GWT Tree to display the station entires.
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Fri, 01 Feb 2013 16:32:48 +0100 |
parents | |
children | 5652aa0ad9e5 |
comparison
equal
deleted
inserted
replaced
4950:4c7acc3a4ae1 | 4956:f46a07c11324 |
---|---|
1 package de.intevation.flys.client.client.ui.stationinfo; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import com.google.gwt.core.client.GWT; | |
7 import com.google.gwt.event.dom.client.ClickEvent; | |
8 import com.google.gwt.event.dom.client.ClickHandler; | |
9 import com.google.gwt.i18n.client.NumberFormat; | |
10 import com.google.gwt.user.client.ui.Anchor; | |
11 import com.google.gwt.user.client.ui.DecoratorPanel; | |
12 import com.google.gwt.user.client.ui.Grid; | |
13 import com.smartgwt.client.types.ListGridFieldType; | |
14 import com.smartgwt.client.widgets.Canvas; | |
15 import com.smartgwt.client.widgets.Label; | |
16 import com.smartgwt.client.widgets.WidgetCanvas; | |
17 import com.smartgwt.client.widgets.grid.ListGridField; | |
18 import com.smartgwt.client.widgets.grid.ListGridRecord; | |
19 import com.smartgwt.client.widgets.layout.HLayout; | |
20 | |
21 import de.intevation.flys.client.client.FLYS; | |
22 import de.intevation.flys.client.shared.model.Data; | |
23 import de.intevation.flys.client.shared.model.DataItem; | |
24 import de.intevation.flys.client.shared.model.DataList; | |
25 import de.intevation.flys.client.shared.model.GaugeInfo; | |
26 import de.intevation.flys.client.shared.model.RiverInfo; | |
27 | |
28 | |
29 /** | |
30 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a> | |
31 */ | |
32 public class GaugeListGrid extends InfoListGrid { | |
33 | |
34 public GaugeListGrid(FLYS flys) { | |
35 super(flys); | |
36 ListGridField nfield = new ListGridField("name", "Pegel"); | |
37 ListGridField sfield = new ListGridField("kmstart", "Start [km]", 60); | |
38 ListGridField efield = new ListGridField("kmend", "Ende [km]", 60); | |
39 ListGridField stfield = new ListGridField("station", "Station [km]"); | |
40 ListGridField lfield = new ListGridField("link", "Link"); | |
41 lfield.setType(ListGridFieldType.LINK); | |
42 | |
43 this.setFields(nfield, sfield, efield, stfield, lfield); | |
44 } | |
45 | |
46 public void setRiverInfo(RiverInfo riverinfo) { | |
47 List<GaugeInfo> gauges = riverinfo.getGauges(); | |
48 | |
49 if (gauges != null && !gauges.isEmpty()) { | |
50 | |
51 ArrayList<GaugeInfo> emptygauges = new ArrayList<GaugeInfo>(); | |
52 | |
53 if (!riverinfo.isKmUp()) { | |
54 for (GaugeInfo gauge : gauges) { | |
55 addGauge(gauge, emptygauges); | |
56 } | |
57 } | |
58 else { | |
59 for (int i = gauges.size()-1; i >= 0; i--) { | |
60 GaugeInfo gauge = gauges.get(i); | |
61 addGauge(gauge, emptygauges); | |
62 } | |
63 } | |
64 | |
65 // put empty gauges to the end | |
66 for (GaugeInfo gauge : emptygauges) { | |
67 addGauge(gauge); | |
68 } | |
69 } | |
70 } | |
71 | |
72 private void addGauge(GaugeInfo gauge, List<GaugeInfo> empty) { | |
73 if (gauge.getKmStart() != null && gauge.getKmEnd() != null) { | |
74 addGauge(gauge); | |
75 } | |
76 else { | |
77 empty.add(gauge); | |
78 } | |
79 } | |
80 | |
81 private void addGauge(GaugeInfo gauge) { | |
82 this.addData(new GaugeRecord(gauge)); | |
83 } | |
84 | |
85 | |
86 class GaugeInfoHead extends HLayout { | |
87 | |
88 public GaugeInfoHead(FLYS flys, GaugeInfo gauge) { | |
89 setStyleName("gaugeinfohead"); | |
90 setAutoHeight(); | |
91 setAutoWidth(); | |
92 | |
93 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
94 | |
95 Label label = new Label(gauge.getName()); | |
96 addMember(label); | |
97 | |
98 Double start; | |
99 Double end; | |
100 | |
101 if (!gauge.isKmUp()) { | |
102 start = gauge.getKmStart(); | |
103 end = gauge.getKmEnd(); | |
104 } | |
105 else { | |
106 start = gauge.getKmEnd(); | |
107 end = gauge.getKmStart(); | |
108 } | |
109 | |
110 String kmtext = ""; | |
111 if (start != null) { | |
112 kmtext += nf.format(start); | |
113 kmtext += " - "; | |
114 } | |
115 if (end != null) { | |
116 kmtext += nf.format(end); | |
117 } | |
118 if (start != null || end != null) { | |
119 kmtext += " km"; | |
120 } | |
121 | |
122 label = new Label(kmtext); | |
123 | |
124 addMember(label); | |
125 | |
126 Double station = gauge.getStation(); | |
127 if (station != null) { | |
128 String stext = nf.format(station); | |
129 stext += " km"; | |
130 label = new Label(stext); | |
131 addMember(label); | |
132 } | |
133 | |
134 Long number = gauge.getOfficialNumber(); | |
135 String url = number != null ? | |
136 MSG.gauge_url() + number : | |
137 MSG.gauge_url(); | |
138 Anchor anchor = new Anchor(MSG.gauge_info_link(), url, "_blank"); | |
139 addMember(anchor); | |
140 | |
141 addMember(new GaugeCurveAnchor(flys, gauge)); | |
142 } | |
143 } | |
144 | |
145 class GaugeCurveAnchor extends Anchor implements ClickHandler { | |
146 | |
147 private FLYS flys; | |
148 private GaugeInfo gauge; | |
149 | |
150 public GaugeCurveAnchor(FLYS flys, GaugeInfo gauge) { | |
151 super(MSG.gauge_curve_link()); | |
152 this.flys = flys; | |
153 this.gauge = gauge; | |
154 | |
155 addClickHandler(this); | |
156 } | |
157 | |
158 @Override | |
159 public void onClick(ClickEvent ev) { | |
160 GWT.log("GaugeCurveAnchor - onClick " + gauge.getRiverName() + | |
161 " " + gauge.getOfficialNumber()); | |
162 flys.newGaugeDischargeCurve(gauge.getRiverName(), | |
163 gauge.getOfficialNumber()); | |
164 } | |
165 } | |
166 | |
167 class GaugeInfoPanel extends DecoratorPanel { | |
168 | |
169 public GaugeInfoPanel(GaugeInfo gauge) { | |
170 setStyleName("gaugeinfopanel"); | |
171 Grid grid = new Grid(4, 2); | |
172 | |
173 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
174 | |
175 Double minw = gauge.getMinW(); | |
176 Double maxw = gauge.getMaxW(); | |
177 if (minw != null && maxw != null) { | |
178 grid.setText(0, 0, MSG.wq_value_q()); | |
179 grid.setText(0, 1, nf.format(minw) + | |
180 " - " + nf.format(maxw)); | |
181 } | |
182 | |
183 Double minq = gauge.getMinQ(); | |
184 Double maxq = gauge.getMaxQ(); | |
185 if (minq != null && maxq != null) { | |
186 grid.setText(1, 0, MSG.wq_value_w()); | |
187 grid.setText(1, 1, nf.format(minq) + | |
188 " - " + nf.format(maxq)); | |
189 } | |
190 | |
191 Double aeo = gauge.getAeo(); | |
192 if (aeo != null) { | |
193 grid.setText(2, 0, "AEO [km²]"); | |
194 grid.setText(2, 1, nf.format(aeo)); | |
195 } | |
196 | |
197 Double datum = gauge.getDatum(); | |
198 if (datum != null) { | |
199 grid.setText(3, 0, MSG.gauge_zero() + " [" + | |
200 gauge.getWstUnit() + "]"); | |
201 grid.setText(3, 1, nf.format(datum)); | |
202 } | |
203 | |
204 setWidget(grid); | |
205 } | |
206 } | |
207 | |
208 public void open() { | |
209 ArrayList<Double> locations = new ArrayList<Double>(); | |
210 | |
211 if (data != null && data.length > 0) { | |
212 for (int i = 0; i < data.length; i++) { | |
213 DataList dl = data[i]; | |
214 String state = dl.getState(); | |
215 GWT.log("GaugeListGrid - open " + state); | |
216 if (state.equals("state.winfo.location_distance")) { | |
217 Double ldfrom = null; | |
218 Double ldto = null; | |
219 | |
220 for (int j = dl.size()-1; j >= 0; --j) { | |
221 Data d = dl.get(j); | |
222 String label = d.getLabel(); | |
223 GWT.log("GaugeListGrid - setData - label " + label + " " + d.getStringValue()); | |
224 if (label.equals("ld_from")) { | |
225 ldfrom = getDoubleValue(d); | |
226 } | |
227 else if (label.equals("ld_to")) { | |
228 ldto = getDoubleValue(d); | |
229 } | |
230 else if (label.equals("ld_locations")) { | |
231 getLocationsFromData(locations, d); | |
232 openOnLocations(locations); | |
233 return; | |
234 } | |
235 } | |
236 if (ldfrom != null) { | |
237 openOnDistance(ldfrom, ldto); | |
238 return; | |
239 } | |
240 } | |
241 else if(state.equals("state.winfo.distance_only") || | |
242 state.equals("state.winfo.distance")) { | |
243 Double ldfrom = null; | |
244 Double ldto = null; | |
245 | |
246 for (int j = dl.size()-1; j >= 0; --j) { | |
247 Data d = dl.get(j); | |
248 String label = d.getLabel(); | |
249 GWT.log("GaugeListGrid - setData - label " + label + " " + d.getStringValue()); | |
250 if (label.equals("ld_from")) { | |
251 ldfrom = getDoubleValue(d); | |
252 } | |
253 else if (label.equals("ld_to")) { | |
254 ldto = getDoubleValue(d); | |
255 } | |
256 } | |
257 | |
258 if (ldfrom != null) { | |
259 openOnDistance(ldfrom, ldto); | |
260 return; | |
261 } | |
262 } | |
263 else if (state.equals("state.winfo.location")) { | |
264 getLocations("ld_locations", locations, dl); | |
265 openOnLocations(locations); | |
266 return; | |
267 } | |
268 else if (state.equals("state.winfo.reference.curve.input.start")) { | |
269 getLocations("reference_startpoint", locations, dl); | |
270 } | |
271 else if (state.equals("state.winfo.reference.curve.input.end")) { | |
272 getLocations("reference_endpoint", locations, dl); | |
273 } | |
274 else if (state.equals("state.winfo.historicalq.reference_gauge")) { | |
275 for (int j = dl.size()-1; j >= 0; --j) { | |
276 Data d = dl.get(j); | |
277 String label = d.getLabel(); | |
278 if (label.equals("reference_gauge")) { | |
279 String tmp = d.getStringValue(); | |
280 if (tmp != null) { | |
281 Long gaugereference = Long.valueOf(tmp); | |
282 if (gaugereference != null) { | |
283 openOnReference(gaugereference); | |
284 return; | |
285 } | |
286 } | |
287 } | |
288 } | |
289 } | |
290 } | |
291 } | |
292 if (!locations.isEmpty()) { | |
293 openOnLocations(locations); | |
294 } | |
295 else { | |
296 openAll(); | |
297 } | |
298 } | |
299 | |
300 void getLocations(String labelname, List<Double> locations, DataList dl) { | |
301 for (int j = dl.size()-1; j >= 0; --j) { | |
302 Data d = dl.get(j); | |
303 String label = d.getLabel(); | |
304 if (label.equals(labelname)) { | |
305 getLocationsFromData(locations, d); | |
306 } | |
307 } | |
308 } | |
309 | |
310 void getLocationsFromData(List<Double> locations, Data data) { | |
311 DataItem[] items = data.getItems(); | |
312 for (int k = 0; k < items.length; k++) { | |
313 String tmp = items[k].getStringValue(); | |
314 GWT.log("GaugeListGrid - getLocationsFromData " + tmp); | |
315 if (tmp != null) { | |
316 if (tmp.contains(" ")) { | |
317 // string contains several values ... | |
318 String[] values = tmp.split(" "); | |
319 for(int i=0; i < values.length; i++) { | |
320 Double value = Double.valueOf(values[i]); | |
321 if (value != null) { | |
322 locations.add(value); | |
323 } | |
324 } | |
325 } | |
326 else { | |
327 Double value = Double.valueOf(tmp); | |
328 if (value != null) { | |
329 locations.add(value); | |
330 } | |
331 } | |
332 } | |
333 } | |
334 } | |
335 | |
336 public void openOnReference(Long number) { | |
337 GWT.log("GaugeListGrid - openOnReference " + number); | |
338 for (ListGridRecord record: this.getRecords()) { | |
339 GaugeRecord item = (GaugeRecord)record; | |
340 if (item.getOfficialNumber().equals(number)) { | |
341 expandRecord(item); | |
342 } | |
343 else { | |
344 collapseRecord(item); | |
345 } | |
346 } | |
347 } | |
348 | |
349 public void openOnDistance(Double start, Double end) { | |
350 GWT.log("GaugeListGrid - openOnDistance " + start + " " + end); | |
351 | |
352 for (ListGridRecord record: this.getRecords()) { | |
353 GaugeRecord item = (GaugeRecord)record; | |
354 if (end == null && item.getKmStart() != null) { | |
355 if (item.getKmStart() >= start) { | |
356 expandRecord(item); | |
357 } | |
358 else { | |
359 collapseRecord(item); | |
360 } | |
361 } | |
362 else if (item.getKmStart() != null && item.getKmEnd() != null) { | |
363 // as getStart()/getEnd() return Double objects, they can be null and | |
364 // can cause NPEs when comparing with double... strange... | |
365 GWT.log("GaugeListGrid - openOnDistance item " + item.getKmStart() + " " + item.getKmEnd()); | |
366 if ((start >= item.getKmStart() && start <= item.getKmEnd()) || | |
367 (end >= item.getKmStart() && end <= item.getKmEnd()) || | |
368 (start <= item.getKmStart() && end >= item.getKmEnd())) { | |
369 expandRecord(item); | |
370 } | |
371 else { | |
372 collapseRecord(item); | |
373 } | |
374 } | |
375 else { | |
376 collapseRecord(item); | |
377 } | |
378 } | |
379 } | |
380 | |
381 /** | |
382 * Open Gauge entry if a location fits to the gauge | |
383 */ | |
384 public void openOnLocations(List<Double> locations) { | |
385 GWT.log("GaugeListGrid - openOnLocations " + locations); | |
386 | |
387 if (locations == null || locations.isEmpty()) { | |
388 return; | |
389 } | |
390 | |
391 for (ListGridRecord record: this.getRecords()) { | |
392 GaugeRecord item = (GaugeRecord)record; | |
393 boolean isset = false; | |
394 for (Double location: locations) { | |
395 if (locations == null) { | |
396 continue; | |
397 } | |
398 | |
399 Double start = item.getKmStart(); | |
400 Double end = item.getKmEnd(); | |
401 if (start == null || end == null) { | |
402 // should not occur but avoid NullPointerException | |
403 continue; | |
404 } | |
405 | |
406 if (location >= start && location <= end) { | |
407 isset = true; | |
408 break; | |
409 } | |
410 } | |
411 if (isset) { | |
412 expandRecord(item); | |
413 } | |
414 else { | |
415 collapseRecord(item); | |
416 } | |
417 } | |
418 } | |
419 | |
420 @Override | |
421 protected Canvas getExpandPanel(ListGridRecord record) { | |
422 GaugeRecord item = (GaugeRecord)record; | |
423 return new WidgetCanvas(new GaugeInfoPanel(item)); | |
424 } | |
425 } |