comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/map/FloodMap.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/FloodMap.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 org.gwtopenmaps.openlayers.client.Bounds;
4 import org.gwtopenmaps.openlayers.client.LonLat;
5 import org.gwtopenmaps.openlayers.client.Map;
6 import org.gwtopenmaps.openlayers.client.MapOptions;
7 import org.gwtopenmaps.openlayers.client.MapWidget;
8 import org.gwtopenmaps.openlayers.client.Style;
9 import org.gwtopenmaps.openlayers.client.control.Attribution;
10 import org.gwtopenmaps.openlayers.client.control.ScaleLine;
11 import org.gwtopenmaps.openlayers.client.control.ScaleLineOptions;
12 import org.gwtopenmaps.openlayers.client.event.VectorFeatureAddedListener;
13 import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
14 import org.gwtopenmaps.openlayers.client.format.GeoJSON;
15 import org.gwtopenmaps.openlayers.client.layer.Layer;
16 import org.gwtopenmaps.openlayers.client.layer.Vector;
17 import org.gwtopenmaps.openlayers.client.layer.VectorOptions;
18 import org.gwtopenmaps.openlayers.client.util.Attributes;
19 import org.gwtopenmaps.openlayers.client.util.JObjectArray;
20 import org.gwtopenmaps.openlayers.client.util.JSObject;
21
22
23 public class FloodMap implements VectorFeatureAddedListener {
24
25 public static final String LAYER_BARRIERS = "vector_layer_barriers";
26
27 public static final String MARK_SELECTED = "mark.selected";
28
29 public static final int SELECTED_STROKE_WIDTH = 2;
30
31 protected MapWidget mapWidget;
32 protected Map map;
33 protected Vector barrierLayer;
34 protected String srid;
35 protected Bounds maxExtent;
36 protected ScaleLine scaleLine;
37
38 public FloodMap(String srid, Bounds maxExtent, int width, int height) {
39 this.srid = srid;
40 this.maxExtent = maxExtent;
41 recreateWidget(width, height);
42 getBarrierLayer();
43 }
44
45
46 public void recreateWidget(int width, int height) {
47 final MapOptions opts = new MapOptions();
48 opts.setControls(new JObjectArray(new JSObject[] {}));
49 opts.setNumZoomLevels(16);
50 opts.setProjection(getRiverProjection());
51 opts.setMaxExtent(maxExtent);
52 opts.setUnits("m");
53 opts.setMaxResolution(500); // TODO DO THIS ON THE FLY
54
55 mapWidget = new MapWidget(
56 Integer.toString(width - 4),
57 Integer.toString(height),
58 opts);
59 map = mapWidget.getMap();
60 map.addControl(new Attribution());
61 }
62
63
64 @Override
65 public void onFeatureAdded(FeatureAddedEvent evt) {
66 final VectorFeature feature = evt.getVectorFeature();
67
68 final Attributes attrs = feature.getAttributes();
69 final String type = attrs.getAttributeAsString("typ");
70
71 if (type == null || type.length() == 0) {
72 return;
73 }
74
75 final Style style = getStyle(type);
76 if (style != null) {
77 feature.setStyle(style);
78 }
79
80 // necessary, otherwise the setStyle() has no effect
81 getBarrierLayer().redraw();
82 }
83
84
85 /**
86 * Returns an OpenLayers.Style based on a given type.
87 *
88 * @param type Type can be one of "pipe1", "pipe2", "ditch", "dam",
89 * "ringdike".
90 *
91 * @return an OpenLayers.Style object.
92 */
93 public static Style getStyle(String type) {
94 final Style style = new Style();
95
96 if (type == null) {
97 return null;
98 }
99
100 if (type.equals(DrawControl.BARRIER_PIPE1)
101 || type.equals(DrawControl.BARRIER_PIPE1_VALUE)
102 ) {
103 style.setFillColor("#800080");
104 style.setStrokeColor("#800080");
105 }
106 else if (type.equals(DrawControl.BARRIER_PIPE2)
107 || type.equals(DrawControl.BARRIER_PIPE2_VALUE)
108 ) {
109 style.setFillColor("#808080");
110 style.setStrokeColor("#808080");
111 }
112 else if (type.equals(DrawControl.BARRIER_DAM)
113 || type.equals(DrawControl.BARRIER_DAM_VALUE)
114 ) {
115 style.setFillColor("#008000");
116 style.setStrokeColor("#008000");
117 }
118 else if (type.equals(DrawControl.BARRIER_DITCH)
119 || type.equals(DrawControl.BARRIER_DITCH_VALUE)
120 ) {
121 style.setFillColor("#800000");
122 style.setStrokeColor("#800000");
123 }
124 else if (type.equals(DrawControl.BARRIER_RINGDIKE)
125 || type.equals(DrawControl.BARRIER_RINGDIKE_VALUE)
126 ) {
127 style.setFill(false);
128 style.setStrokeColor("#FF8000");
129 }
130
131 return style;
132 }
133
134
135 public MapWidget getMapWidget() {
136 return mapWidget;
137 }
138
139
140 public Map getMap() {
141 return map;
142 }
143
144
145 public String getRiverProjection() {
146 return "EPSG:" + srid;
147 }
148
149
150 public Bounds getMaxExtent() {
151 return maxExtent;
152 }
153
154
155 public Vector getBarrierLayer() {
156 if (barrierLayer == null) {
157 final VectorOptions opts = new VectorOptions();
158 opts.setProjection(getRiverProjection());
159 opts.setMaxExtent(getMaxExtent());
160
161 barrierLayer = new Vector(LAYER_BARRIERS, opts);
162 barrierLayer.setIsBaseLayer(true);
163
164 map.addLayer(barrierLayer);
165 map.setLayerZIndex(barrierLayer, 1000);
166
167 barrierLayer.addVectorFeatureAddedListener(this);
168 }
169
170 return barrierLayer;
171 }
172
173
174 public String getFeaturesAsGeoJSON() {
175 // disable features before exporting to GeoJSON
176 disableFeatures();
177
178 final VectorFeature[] features = barrierLayer.getFeatures();
179
180 if (features == null || features.length == 0) {
181 return null;
182 }
183
184 return new GeoJSON().write(features);
185 }
186
187
188 public void setSize(String width, String height) {
189 mapWidget.setWidth(width);
190 mapWidget.setHeight(height);
191 final int currentZoom = map.getZoom();
192 final LonLat currentCenter = map.getCenter();
193 map.updateSize();
194 map.zoomTo(currentZoom);
195 map.setCenter(currentCenter);
196 }
197
198
199 public void addLayer(Layer layer) {
200 if (layer != null) {
201 map.addLayer(layer);
202
203 final int index = map.getLayerIndex(layer);
204 final int newIndex = index * (-1) + 1;
205
206 map.raiseLayer(layer, newIndex);
207
208 update();
209 }
210 }
211
212
213 public void hideBarrierLayer () {
214 if (getBarrierLayer() != null) {
215 barrierLayer.setIsVisible(false);
216 }
217 }
218
219 public void showBarrierLayer () {
220 if (getBarrierLayer() != null) {
221 barrierLayer.setIsVisible(true);
222 }
223 }
224
225
226 public void selectFeature(VectorFeature feature) {
227 if (feature != null) {
228 selectFeatures(new VectorFeature[] { feature } );
229 }
230 }
231
232
233 public void selectFeatures(VectorFeature[] features) {
234 if (features == null || features.length == 0) {
235 return;
236 }
237
238 for (final VectorFeature feature: features) {
239 final Attributes attr = feature.getAttributes();
240
241 if (attr.getAttributeAsInt(MARK_SELECTED) == 1) {
242 continue;
243 }
244
245 attr.setAttribute(MARK_SELECTED, 1);
246
247 final Style style = feature.getStyle();
248 final double strokeWidth = style.getStrokeWidth();
249
250 style.setStrokeWidth(strokeWidth+SELECTED_STROKE_WIDTH);
251 }
252
253 getBarrierLayer().redraw();
254 }
255
256
257 public void disableFeatures() {
258 final Vector barriers = getBarrierLayer();
259 final VectorFeature[] features = barriers.getFeatures();
260
261 if (features == null || features.length == 0) {
262 return;
263 }
264
265 disableFeatures(features);
266 }
267
268
269 public void disableFeature(VectorFeature feature) {
270 if (feature != null) {
271 disableFeatures(new VectorFeature[] { feature });
272 }
273 }
274
275
276 public void disableFeatures(VectorFeature[] features) {
277 if (features == null || features.length == 0) {
278 return;
279 }
280
281 for (final VectorFeature feature: features) {
282 final Attributes attr = feature.getAttributes();
283
284 if (attr.getAttributeAsInt(MARK_SELECTED) == 0) {
285 continue;
286 }
287
288 attr.setAttribute(FloodMap.MARK_SELECTED, 0);
289
290 final Style style = feature.getStyle();
291 final double strokeWidth = style.getStrokeWidth();
292
293 style.setStrokeWidth(strokeWidth-SELECTED_STROKE_WIDTH);
294 }
295
296 getBarrierLayer().redraw();
297 }
298
299
300 public void update() {
301 final Layer[] layers = map.getLayers();
302
303 for (final Layer l: layers) {
304 l.redraw();
305 }
306 }
307
308
309 public void updateSize() {
310 this.map.updateSize();
311 }
312
313
314 public void activateScaleLine(boolean activate) {
315 if (activate) {
316 final ScaleLineOptions slOpts = new ScaleLineOptions();
317 slOpts.setBottomInUnits("m");
318 slOpts.setBottomOutUnits("km");
319 slOpts.setTopInUnits("");
320 slOpts.setTopOutUnits("");
321
322 scaleLine = new ScaleLine(slOpts);
323 this.map.addControl(scaleLine);
324 }
325 else if (!activate && scaleLine != null){
326 this.map.removeControl(scaleLine);
327 }
328 }
329 }
330 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org