Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugeTree.java @ 3842:44c1beb78ad1
Move GaugeTree to its own java file
flys-client/trunk@5557 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Bjoern Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Fri, 21 Sep 2012 13:44:56 +0000 |
parents | |
children | 7a096ec98596 |
comparison
equal
deleted
inserted
replaced
3841:5877d6900e34 | 3842:44c1beb78ad1 |
---|---|
1 package de.intevation.flys.client.client.ui; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 import java.util.Iterator; | |
6 | |
7 import com.google.gwt.core.client.GWT; | |
8 import com.google.gwt.i18n.client.NumberFormat; | |
9 import com.google.gwt.user.client.ui.Anchor; | |
10 import com.google.gwt.user.client.ui.DecoratorPanel; | |
11 import com.google.gwt.user.client.ui.Grid; | |
12 import com.google.gwt.user.client.ui.Label; | |
13 import com.google.gwt.user.client.ui.ScrollPanel; | |
14 import com.google.gwt.user.client.ui.Tree; | |
15 import com.google.gwt.user.client.ui.TreeItem; | |
16 | |
17 import com.smartgwt.client.widgets.layout.HLayout; | |
18 | |
19 import de.intevation.flys.client.client.FLYSConstants; | |
20 | |
21 import de.intevation.flys.client.shared.model.Data; | |
22 import de.intevation.flys.client.shared.model.DataItem; | |
23 import de.intevation.flys.client.shared.model.DataList; | |
24 import de.intevation.flys.client.shared.model.GaugeInfo; | |
25 import de.intevation.flys.client.shared.model.RiverInfo; | |
26 | |
27 | |
28 public class GaugeTree extends ScrollPanel { | |
29 | |
30 private Tree tree; | |
31 private DataList[] data; | |
32 | |
33 /** The message class that provides i18n strings.*/ | |
34 protected FLYSConstants MSG = GWT.create(FLYSConstants.class); | |
35 | |
36 public GaugeTree() { | |
37 tree = new Tree(); | |
38 setWidget(tree); | |
39 } | |
40 | |
41 /** | |
42 * Resets the items of the tree. | |
43 * If the list of gauges is empty or null the tree will be empty. | |
44 */ | |
45 public void setGauges(RiverInfo riverinfo) { | |
46 tree.clear(); | |
47 | |
48 List<GaugeInfo> gauges = riverinfo.getGauges(); | |
49 | |
50 if (gauges != null && !gauges.isEmpty()) { | |
51 | |
52 ArrayList<GaugeInfo> emptygauges = new ArrayList<GaugeInfo>(); | |
53 | |
54 if (!riverinfo.isKmUp()) { | |
55 for (GaugeInfo gauge : gauges) { | |
56 addGauge(gauge, emptygauges); | |
57 } | |
58 } | |
59 else { | |
60 for (int i = gauges.size()-1; i >= 0; i--) { | |
61 GaugeInfo gauge = gauges.get(i); | |
62 addGauge(gauge, emptygauges); | |
63 } | |
64 } | |
65 | |
66 // put empty gauges to the end | |
67 for (GaugeInfo gauge : emptygauges) { | |
68 addGauge(gauge); | |
69 } | |
70 | |
71 open(); | |
72 } | |
73 } | |
74 | |
75 private void addGauge(GaugeInfo gauge, List<GaugeInfo> empty) { | |
76 if (gauge.getKmStart() != null && gauge.getKmEnd() != null) { | |
77 addGauge(gauge); | |
78 } | |
79 else { | |
80 empty.add(gauge); | |
81 } | |
82 } | |
83 | |
84 private void addGauge(GaugeInfo gauge) { | |
85 GaugeInfoItem gaugeitem = new GaugeInfoItem(gauge); | |
86 tree.addItem(gaugeitem); | |
87 } | |
88 | |
89 public void openAll() { | |
90 GWT.log("GaugeTree - openAll"); | |
91 for (Iterator<TreeItem> it = tree.treeItemIterator(); it.hasNext();) { | |
92 TreeItem item = it.next(); | |
93 item.setState(true); | |
94 } | |
95 } | |
96 | |
97 public void setData(DataList[] data) { | |
98 this.data = data; | |
99 if (tree.getItemCount() > 0) { | |
100 open(); | |
101 } | |
102 } | |
103 | |
104 public void open() { | |
105 ArrayList<Double> locations = new ArrayList<Double>(); | |
106 | |
107 if (data != null && data.length > 0) { | |
108 for (int i = 0; i < data.length; i++) { | |
109 DataList dl = data[i]; | |
110 String state = dl.getState(); | |
111 GWT.log("GaugeTree - setData " + state); | |
112 if (state.equals("state.winfo.location_distance")) { | |
113 Double ldfrom = null; | |
114 Double ldto = null; | |
115 | |
116 for (int j = dl.size()-1; j >= 0; --j) { | |
117 Data d = dl.get(j); | |
118 String label = d.getLabel(); | |
119 GWT.log("GaugeTree - setData - label " + label + " " + d.getStringValue()); | |
120 if (label.equals("ld_from")) { | |
121 ldfrom = getDoubleValue(d); | |
122 } | |
123 else if (label.equals("ld_to")) { | |
124 ldto = getDoubleValue(d); | |
125 } | |
126 else if (label.equals("ld_locations")) { | |
127 getLocationsFromData(locations, d); | |
128 openOnLocations(locations); | |
129 return; | |
130 } | |
131 } | |
132 if (ldfrom != null) { | |
133 openOnDistance(ldfrom, ldto); | |
134 return; | |
135 } | |
136 } | |
137 else if(state.equals("state.winfo.distance_only") || | |
138 state.equals("state.winfo.distance")) { | |
139 Double ldfrom = null; | |
140 Double ldto = null; | |
141 | |
142 for (int j = dl.size()-1; j >= 0; --j) { | |
143 Data d = dl.get(j); | |
144 String label = d.getLabel(); | |
145 GWT.log("GaugeTree - setData - label " + label + " " + d.getStringValue()); | |
146 if (label.equals("ld_from")) { | |
147 ldfrom = getDoubleValue(d); | |
148 } | |
149 else if (label.equals("ld_to")) { | |
150 ldto = getDoubleValue(d); | |
151 } | |
152 } | |
153 | |
154 if (ldfrom != null) { | |
155 openOnDistance(ldfrom, ldto); | |
156 return; | |
157 } | |
158 } | |
159 else if (state.equals("state.winfo.location")) { | |
160 getLocations("ld_locations", locations, dl); | |
161 openOnLocations(locations); | |
162 return; | |
163 } | |
164 else if (state.equals("state.winfo.reference.curve.input.start")) { | |
165 getLocations("reference_startpoint", locations, dl); | |
166 } | |
167 else if (state.equals("state.winfo.reference.curve.input.end")) { | |
168 getLocations("reference_endpoint", locations, dl); | |
169 } | |
170 else if (state.equals("state.winfo.historicalq.reference_gauge")) { | |
171 for (int j = dl.size()-1; j >= 0; --j) { | |
172 Data d = dl.get(j); | |
173 String label = d.getLabel(); | |
174 if (label.equals("reference_gauge")) { | |
175 String tmp = d.getStringValue(); | |
176 if (tmp != null) { | |
177 Integer gaugereference = Integer.valueOf(tmp); | |
178 if (gaugereference != null) { | |
179 //TODO | |
180 } | |
181 } | |
182 } | |
183 } | |
184 } | |
185 } | |
186 } | |
187 if (!locations.isEmpty()) { | |
188 openOnLocations(locations); | |
189 } | |
190 else { | |
191 openAll(); | |
192 } | |
193 } | |
194 | |
195 private void getLocations(String labelname, List<Double> locations, DataList dl) { | |
196 for (int j = dl.size()-1; j >= 0; --j) { | |
197 Data d = dl.get(j); | |
198 String label = d.getLabel(); | |
199 if (label.equals(labelname)) { | |
200 getLocationsFromData(locations, d); | |
201 } | |
202 } | |
203 } | |
204 | |
205 private void getLocationsFromData(List<Double> locations, Data data) { | |
206 DataItem[] items = data.getItems(); | |
207 for (int k = 0; k < items.length; k++) { | |
208 String tmp = items[k].getStringValue(); | |
209 GWT.log("GaugeTree - getLocationsFromData " + tmp); | |
210 if (tmp != null) { | |
211 if (tmp.contains(" ")) { | |
212 // string contains several values ... | |
213 String[] values = tmp.split(" "); | |
214 for(int i=0; i < values.length; i++) { | |
215 Double value = Double.valueOf(values[i]); | |
216 if (value != null) { | |
217 locations.add(value); | |
218 } | |
219 } | |
220 } | |
221 else { | |
222 Double value = Double.valueOf(tmp); | |
223 if (value != null) { | |
224 locations.add(value); | |
225 } | |
226 } | |
227 } | |
228 } | |
229 } | |
230 | |
231 private Double getDoubleValue(Data d) { | |
232 String tmp = d.getStringValue(); | |
233 if (tmp != null) { | |
234 return Double.valueOf(tmp); | |
235 } | |
236 return null; | |
237 } | |
238 | |
239 public void openOnDistance(Double start, Double end) { | |
240 GWT.log("GaugeTree - openOnDistance " + start + " " + end + " " + | |
241 tree.getItemCount()); | |
242 | |
243 for (Iterator<TreeItem> it = tree.treeItemIterator(); it.hasNext();) { | |
244 TreeItem item = it.next(); | |
245 /* Strange stuff is happening here: | |
246 * GWT Tree.treeItemIterator returns another TreeItem for each | |
247 * GaugeInfoItem */ | |
248 if (item instanceof GaugeInfoItem) { | |
249 boolean setstate = false; | |
250 GaugeInfoItem gitem = (GaugeInfoItem)item; | |
251 if (end == null) { | |
252 if (gitem.getStart() >= start) { | |
253 setstate = true; | |
254 } | |
255 } | |
256 else { | |
257 GWT.log("GaugeTree - openOnDistance gitem " + gitem.getStart() + " " + gitem.getEnd()); | |
258 if ((start >= gitem.getStart() && start <= gitem.getEnd()) || | |
259 (end >= gitem.getStart() && end <= gitem.getEnd()) || | |
260 (start <= gitem.getStart() && end >= gitem.getEnd())) { | |
261 setstate = true; | |
262 } | |
263 } | |
264 item.setState(setstate); | |
265 } | |
266 } | |
267 } | |
268 | |
269 public void openOnLocations(List<Double> locations) { | |
270 GWT.log("GaugeTree - openOnLocations " + locations + " " + | |
271 tree.getItemCount()); | |
272 | |
273 if (locations == null || locations.isEmpty()) { | |
274 return; | |
275 } | |
276 | |
277 for (Iterator<TreeItem> it = tree.treeItemIterator(); it.hasNext();) { | |
278 TreeItem item = it.next(); | |
279 if (item instanceof GaugeInfoItem) { | |
280 GaugeInfoItem gitem = (GaugeInfoItem)item; | |
281 boolean isset = false; | |
282 for (Double location: locations) { | |
283 if (locations == null) { | |
284 continue; | |
285 } | |
286 if (location >= gitem.getStart() && | |
287 location <= gitem.getEnd()) { | |
288 isset = true; | |
289 break; | |
290 } | |
291 } | |
292 item.setState(isset); | |
293 } | |
294 } | |
295 } | |
296 | |
297 class GaugeInfoItem extends TreeItem { | |
298 | |
299 private GaugeInfo gauge; | |
300 | |
301 public GaugeInfoItem(GaugeInfo gauge) { | |
302 GaugeInfoHead gaugeinfohead = new GaugeInfoHead(gauge); | |
303 GaugeInfoPanel gaugeinfopanel = new GaugeInfoPanel(gauge); | |
304 setWidget(gaugeinfohead); | |
305 addItem(gaugeinfopanel); | |
306 this.gauge = gauge; | |
307 } | |
308 | |
309 public Double getStart() { | |
310 return gauge.getKmStart(); | |
311 } | |
312 | |
313 public Double getEnd() { | |
314 return gauge.getKmEnd(); | |
315 } | |
316 } | |
317 | |
318 class GaugeInfoHead extends HLayout { | |
319 | |
320 public GaugeInfoHead(GaugeInfo gauge) { | |
321 setStyleName("gaugeinfohead"); | |
322 setAutoHeight(); | |
323 setAutoWidth(); | |
324 | |
325 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
326 | |
327 Label label = new Label(gauge.getName(), true); | |
328 addMember(label); | |
329 | |
330 Double start; | |
331 Double end; | |
332 | |
333 if (!gauge.isKmUp()) { | |
334 start = gauge.getKmStart(); | |
335 end = gauge.getKmEnd(); | |
336 } | |
337 else { | |
338 start = gauge.getKmEnd(); | |
339 end = gauge.getKmStart(); | |
340 } | |
341 | |
342 String kmtext = ""; | |
343 if (start != null) { | |
344 kmtext += nf.format(start); | |
345 kmtext += " - "; | |
346 } | |
347 if (end != null) { | |
348 kmtext += nf.format(end); | |
349 } | |
350 if (start != null || end != null) { | |
351 kmtext += " km"; | |
352 } | |
353 | |
354 label = new Label(kmtext); | |
355 | |
356 addMember(label); | |
357 | |
358 Double station = gauge.getStation(); | |
359 if (station != null) { | |
360 String stext = nf.format(station); | |
361 stext += " km"; | |
362 label = new Label(stext); | |
363 addMember(label); | |
364 } | |
365 | |
366 addMember(new Anchor(MSG.gauge_info_link(), gauge.getInfoURL())); | |
367 } | |
368 } | |
369 | |
370 class GaugeInfoPanel extends DecoratorPanel { | |
371 | |
372 public GaugeInfoPanel(GaugeInfo gauge) { | |
373 setStyleName("gaugeinfopanel"); | |
374 Grid grid = new Grid(4, 2); | |
375 | |
376 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
377 | |
378 Double minw = gauge.getMinW(); | |
379 Double maxw = gauge.getMaxW(); | |
380 if (minw != null && maxw != null) { | |
381 grid.setText(0, 0, MSG.wq_value_q()); | |
382 grid.setText(0, 1, "" + nf.format(minw) + | |
383 " - " + nf.format(maxw)); | |
384 } | |
385 | |
386 Double minq = gauge.getMinQ(); | |
387 Double maxq = gauge.getMaxQ(); | |
388 if (minq != null && maxq != null) { | |
389 grid.setText(1, 0, MSG.wq_value_w()); | |
390 grid.setText(1, 1, "" + nf.format(minq) + | |
391 " - " + nf.format(maxq)); | |
392 } | |
393 | |
394 Double aeo = gauge.getAeo(); | |
395 if (aeo != null) { | |
396 grid.setText(2, 0, "AEO [kmĀ²]"); | |
397 grid.setText(2, 1, "" + nf.format(aeo)); | |
398 } | |
399 | |
400 Double datum = gauge.getDatum(); | |
401 if (datum != null) { | |
402 grid.setText(3, 0, MSG.gauge_zero() + " [" + | |
403 gauge.getWstUnit() + "]"); | |
404 grid.setText(3, 1, "" + nf.format(datum)); | |
405 } | |
406 | |
407 setWidget(grid); | |
408 } | |
409 } | |
410 | |
411 } |