Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/map/ElevationWindow.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/client/ui/map/ElevationWindow.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.client.ui.map; | |
2 | |
3 import com.google.gwt.core.client.GWT; | |
4 import com.google.gwt.i18n.client.NumberFormat; | |
5 | |
6 import com.smartgwt.client.types.Alignment; | |
7 import com.smartgwt.client.types.SelectionStyle; | |
8 import com.smartgwt.client.types.SortArrow; | |
9 import com.smartgwt.client.types.SortDirection; | |
10 import com.smartgwt.client.util.SC; | |
11 import com.smartgwt.client.widgets.Label; | |
12 import com.smartgwt.client.widgets.IButton; | |
13 import com.smartgwt.client.widgets.Window; | |
14 import com.smartgwt.client.widgets.events.ClickEvent; | |
15 import com.smartgwt.client.widgets.events.ClickHandler; | |
16 import com.smartgwt.client.widgets.events.CloseClickEvent; | |
17 import com.smartgwt.client.widgets.events.CloseClickHandler; | |
18 import com.smartgwt.client.widgets.layout.HLayout; | |
19 import com.smartgwt.client.widgets.layout.VLayout; | |
20 import com.smartgwt.client.widgets.grid.ListGrid; | |
21 import com.smartgwt.client.widgets.grid.ListGridField; | |
22 import com.smartgwt.client.widgets.grid.ListGridRecord; | |
23 import com.smartgwt.client.widgets.grid.events.EditCompleteEvent; | |
24 import com.smartgwt.client.widgets.grid.events.EditCompleteHandler; | |
25 import com.smartgwt.client.widgets.grid.events.RowOutEvent; | |
26 import com.smartgwt.client.widgets.grid.events.RowOutHandler; | |
27 import com.smartgwt.client.widgets.grid.events.RowOverEvent; | |
28 import com.smartgwt.client.widgets.grid.events.RowOverHandler; | |
29 | |
30 import org.gwtopenmaps.openlayers.client.Style; | |
31 import org.gwtopenmaps.openlayers.client.feature.VectorFeature; | |
32 import org.gwtopenmaps.openlayers.client.geometry.Geometry; | |
33 import org.gwtopenmaps.openlayers.client.geometry.LinearRing; | |
34 import org.gwtopenmaps.openlayers.client.geometry.LineString; | |
35 import org.gwtopenmaps.openlayers.client.geometry.Point; | |
36 import org.gwtopenmaps.openlayers.client.geometry.Polygon; | |
37 import org.gwtopenmaps.openlayers.client.layer.Vector; | |
38 import org.gwtopenmaps.openlayers.client.layer.VectorOptions; | |
39 import org.gwtopenmaps.openlayers.client.util.Attributes; | |
40 | |
41 import org.dive4elements.river.client.client.FLYSConstants; | |
42 | |
43 | |
44 | |
45 public class ElevationWindow extends Window { | |
46 | |
47 public static FLYSConstants MSG = GWT.create(FLYSConstants.class); | |
48 | |
49 public static final int WINDOW_WIDTH = 250; | |
50 public static final int WINDOW_HEIGHT = 250; | |
51 | |
52 protected FloodMap floodmap; | |
53 protected Vector layer; | |
54 protected VectorFeature feature; | |
55 | |
56 protected ListGrid grid; | |
57 | |
58 | |
59 private class PointRecord extends ListGridRecord { | |
60 protected VectorFeature point; | |
61 | |
62 public PointRecord(VectorFeature point, double x, double y, String z) { | |
63 super(); | |
64 | |
65 this.point = point; | |
66 setAttribute("x", x); | |
67 setAttribute("y", y); | |
68 setAttribute("z", z); | |
69 } | |
70 | |
71 public VectorFeature getPoint() { | |
72 return point; | |
73 } | |
74 } | |
75 | |
76 | |
77 public ElevationWindow(FloodMap floodmap, VectorFeature feature) { | |
78 super(); | |
79 this.feature = feature; | |
80 this.floodmap = floodmap; | |
81 | |
82 init(); | |
83 } | |
84 | |
85 | |
86 protected void init() { | |
87 addCloseClickHandler(new CloseClickHandler() { | |
88 public void onCloseClick(CloseClickEvent evt) { | |
89 doClose(); | |
90 } | |
91 }); | |
92 | |
93 initLayout(); | |
94 initEdit(); | |
95 updateGrid(); | |
96 } | |
97 | |
98 | |
99 protected void initLayout() { | |
100 setWidth(WINDOW_WIDTH); | |
101 setHeight(WINDOW_HEIGHT); | |
102 setTitle(MSG.ele_window_title()); | |
103 | |
104 VLayout root = new VLayout(); | |
105 root.setMembersMargin(5); | |
106 root.setPadding(5); | |
107 | |
108 root.addMember(getLabel()); | |
109 root.addMember(getGrid()); | |
110 root.addMember(getButtonBar()); | |
111 | |
112 addItem(root); | |
113 centerInPage(); | |
114 } | |
115 | |
116 | |
117 protected void initEdit() { | |
118 VectorOptions opts = new VectorOptions(); | |
119 opts.setProjection(floodmap.getRiverProjection()); | |
120 opts.setMaxExtent(floodmap.getMaxExtent()); | |
121 | |
122 layer = new Vector("tmp", opts); | |
123 layer.setIsBaseLayer(false); | |
124 | |
125 floodmap.getMap().addLayer(layer); | |
126 } | |
127 | |
128 | |
129 public Style getStyle() { | |
130 Style style = new Style(); | |
131 style.setStrokeColor("#000000"); | |
132 style.setStrokeWidth(1); | |
133 style.setFillColor("#FF0000"); | |
134 style.setFillOpacity(0.5); | |
135 style.setPointRadius(5); | |
136 style.setStrokeOpacity(1.0); | |
137 return style; | |
138 } | |
139 | |
140 | |
141 public Style getHighStyle() { | |
142 Style style = new Style(); | |
143 style.setStrokeColor("#000000"); | |
144 style.setStrokeWidth(1); | |
145 style.setFillColor("#FFFF22"); | |
146 style.setFillOpacity(0.5); | |
147 style.setPointRadius(5); | |
148 style.setStrokeOpacity(1.0); | |
149 return style; | |
150 } | |
151 | |
152 | |
153 protected Label getLabel() { | |
154 Label label = new Label(MSG.ele_window_label()); | |
155 label.setHeight(25); | |
156 | |
157 return label; | |
158 } | |
159 | |
160 | |
161 protected ListGrid getGrid() { | |
162 if (grid == null) { | |
163 grid = new ListGrid(); | |
164 grid.setCanEdit(true); | |
165 grid.setCanReorderFields(false); | |
166 grid.setAutoFitMaxWidth(WINDOW_WIDTH); | |
167 grid.setShowHeaderContextMenu(false); | |
168 grid.setShowSortArrow(SortArrow.NONE); | |
169 grid.setSortDirection(SortDirection.DESCENDING); | |
170 grid.setSelectionType(SelectionStyle.NONE); | |
171 | |
172 ListGridField x = new ListGridField("x", MSG.ele_window_x_col()); | |
173 x.setCanEdit(false); | |
174 | |
175 ListGridField y = new ListGridField("y", MSG.ele_window_y_col()); | |
176 y.setCanEdit(false); | |
177 | |
178 ListGridField z = new ListGridField("z", MSG.ele_window_z_col()); | |
179 z.setCanEdit(true); | |
180 | |
181 grid.setFields(x, y, z); | |
182 | |
183 grid.addRowOverHandler(new RowOverHandler() { | |
184 public void onRowOver(RowOverEvent evt) { | |
185 PointRecord pr = (PointRecord) evt.getRecord(); | |
186 VectorFeature p = pr.getPoint(); | |
187 | |
188 p.setStyle(getHighStyle()); | |
189 layer.redraw(); | |
190 } | |
191 }); | |
192 | |
193 grid.addRowOutHandler(new RowOutHandler() { | |
194 public void onRowOut(RowOutEvent evt) { | |
195 PointRecord pr = (PointRecord) evt.getRecord(); | |
196 VectorFeature p = pr.getPoint(); | |
197 | |
198 p.setStyle(getStyle()); | |
199 layer.redraw(); | |
200 } | |
201 }); | |
202 | |
203 grid.addEditCompleteHandler(new EditCompleteHandler() { | |
204 public void onEditComplete(EditCompleteEvent evt) { | |
205 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
206 String z = (String) evt.getNewValues().get("z"); | |
207 | |
208 try { | |
209 nf.parse(z); | |
210 } | |
211 catch (NumberFormatException nfe) { | |
212 SC.warn(MSG.ele_window_format_error() + " " + z); | |
213 | |
214 PointRecord old = (PointRecord) evt.getOldRecord(); | |
215 | |
216 ListGridRecord[] records = grid.getRecords(); | |
217 records[evt.getRowNum()] = old; | |
218 | |
219 grid.setRecords(records); | |
220 } | |
221 | |
222 } | |
223 }); | |
224 } | |
225 | |
226 return grid; | |
227 } | |
228 | |
229 | |
230 protected HLayout getButtonBar() { | |
231 HLayout bar = new HLayout(); | |
232 bar.setAlign(Alignment.CENTER); | |
233 bar.setHeight(25); | |
234 bar.setMembersMargin(15); | |
235 | |
236 bar.addMember(getOKButton()); | |
237 bar.addMember(getCancelButton()); | |
238 | |
239 return bar; | |
240 } | |
241 | |
242 | |
243 protected IButton getOKButton() { | |
244 IButton btn = new IButton(MSG.ele_window_ok_button()); | |
245 | |
246 btn.addClickHandler(new ClickHandler() { | |
247 public void onClick(ClickEvent evt) { | |
248 if (saveElevation()) { | |
249 doClose(); | |
250 } | |
251 | |
252 } | |
253 }); | |
254 | |
255 return btn; | |
256 } | |
257 | |
258 | |
259 protected IButton getCancelButton() { | |
260 IButton btn = new IButton(MSG.ele_window_cancel_button()); | |
261 | |
262 btn.addClickHandler(new ClickHandler() { | |
263 public void onClick(ClickEvent evt) { | |
264 doClose(); | |
265 } | |
266 }); | |
267 | |
268 return btn; | |
269 } | |
270 | |
271 | |
272 public void updateGrid() { | |
273 Attributes attr = feature.getAttributes(); | |
274 Geometry geom = feature.getGeometry(); | |
275 | |
276 String barrierType = null; | |
277 | |
278 if (attr != null) { | |
279 barrierType = attr.getAttributeAsString("typ"); | |
280 } | |
281 else { | |
282 barrierType = DrawControl.BARRIER_DAM_VALUE; | |
283 } | |
284 | |
285 GWT.log("Geometry is from type: " + geom.getClassName()); | |
286 GWT.log("Barrier is from type: " + barrierType); | |
287 | |
288 double[][] points = getPointsFromGeometry(geom); | |
289 double[] ele = extractElevations(attr); | |
290 | |
291 if (points == null) { | |
292 return; | |
293 } | |
294 | |
295 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
296 | |
297 ListGrid grid = getGrid(); | |
298 | |
299 int zLen = ele != null ? ele.length : 0; | |
300 | |
301 for (int i = 0, len = points.length; i < len; i++) { | |
302 double[] point = points[i]; | |
303 VectorFeature p = addPoint(point[0], point[1]); | |
304 | |
305 String value = null; | |
306 | |
307 if (zLen > i) { | |
308 value = nf.format(ele[i]); | |
309 } | |
310 else if (barrierType.equals(DrawControl.BARRIER_DITCH_VALUE)) { | |
311 value = "-9999"; | |
312 } | |
313 else { | |
314 value = "9999"; | |
315 } | |
316 | |
317 grid.addData(new PointRecord(p, point[0], point[1], value)); | |
318 } | |
319 | |
320 grid.redraw(); | |
321 } | |
322 | |
323 | |
324 public static double[] extractElevations(Attributes attr) { | |
325 if (attr == null) { | |
326 return null; | |
327 } | |
328 | |
329 String elevationStr = attr.getAttributeAsString("elevation"); | |
330 | |
331 if (elevationStr == null || elevationStr.length() == 0) { | |
332 return null; | |
333 } | |
334 | |
335 String[] elevations = elevationStr.split(" "); | |
336 | |
337 int len = elevations != null ? elevations.length : 0; | |
338 | |
339 if (len == 0) { | |
340 return null; | |
341 } | |
342 | |
343 double[] res = new double[len]; | |
344 | |
345 for (int i = 0; i < len; i++) { | |
346 try { | |
347 res[i] = Double.valueOf(elevations[i]); | |
348 } | |
349 catch (NumberFormatException nfe) { | |
350 // go on | |
351 } | |
352 } | |
353 | |
354 return res; | |
355 } | |
356 | |
357 | |
358 public static double[][] getPointsFromGeometry(Geometry geom) { | |
359 String clazz = geom.getClassName(); | |
360 | |
361 if (clazz != null && clazz.equals(Geometry.LINESTRING_CLASS_NAME)) { | |
362 return getPointsFromLineString( | |
363 LineString.narrowToLineString(geom.getJSObject())); | |
364 } | |
365 else if (clazz != null && clazz.equals(Geometry.POLYGON_CLASS_NAME)) { | |
366 return getPointsFromPolygon( | |
367 Polygon.narrowToPolygon(geom.getJSObject())); | |
368 } | |
369 else { | |
370 SC.warn(MSG.ele_window_geometry_error() + " " + clazz); | |
371 } | |
372 | |
373 return null; | |
374 } | |
375 | |
376 | |
377 public static double[][] getPointsFromLineString(LineString line) { | |
378 return line.getCoordinateArray(); | |
379 } | |
380 | |
381 | |
382 public static double[][] getPointsFromPolygon(Polygon polygon) { | |
383 LinearRing[] rings = polygon.getComponents(); | |
384 | |
385 return getPointsFromLineString(rings[0]); | |
386 } | |
387 | |
388 | |
389 protected VectorFeature addPoint(double x, double y) { | |
390 VectorFeature point = new VectorFeature(new Point(x, y), getStyle()); | |
391 layer.addFeature(point); | |
392 | |
393 return point; | |
394 } | |
395 | |
396 | |
397 protected boolean saveElevation() { | |
398 ListGridRecord[] records = grid.getRecords(); | |
399 | |
400 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
401 | |
402 StringBuilder sb = new StringBuilder(); | |
403 | |
404 for (ListGridRecord record: records) { | |
405 PointRecord pr = (PointRecord) record; | |
406 String value = pr.getAttributeAsString("z"); | |
407 | |
408 try { | |
409 double z = nf.parse(value); | |
410 sb.append(String.valueOf(z)); | |
411 } | |
412 catch (NumberFormatException nfe) { | |
413 SC.warn(MSG.ele_window_save_error()); | |
414 return false; | |
415 } | |
416 | |
417 sb.append(" "); | |
418 } | |
419 | |
420 Attributes attr = feature.getAttributes(); | |
421 attr.setAttribute("elevation", sb.toString()); | |
422 | |
423 return true; | |
424 } | |
425 | |
426 | |
427 protected void doClose() { | |
428 floodmap.getMap().removeLayer(layer); | |
429 destroy(); | |
430 } | |
431 } | |
432 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |