ingo@797: package de.intevation.flys.client.client.ui.map; ingo@797: ingo@823: import org.gwtopenmaps.openlayers.client.Bounds; christian@3347: import org.gwtopenmaps.openlayers.client.LonLat; ingo@797: import org.gwtopenmaps.openlayers.client.Map; ingo@797: import org.gwtopenmaps.openlayers.client.MapOptions; ingo@797: import org.gwtopenmaps.openlayers.client.MapWidget; ingo@882: import org.gwtopenmaps.openlayers.client.Style; rrenkert@5032: import org.gwtopenmaps.openlayers.client.control.Attribution; ingo@1398: import org.gwtopenmaps.openlayers.client.control.ScaleLine; ingo@1398: import org.gwtopenmaps.openlayers.client.control.ScaleLineOptions; ingo@882: import org.gwtopenmaps.openlayers.client.event.VectorFeatureAddedListener; ingo@833: import org.gwtopenmaps.openlayers.client.feature.VectorFeature; ingo@833: import org.gwtopenmaps.openlayers.client.format.GeoJSON; ingo@1397: import org.gwtopenmaps.openlayers.client.layer.Layer; ingo@800: import org.gwtopenmaps.openlayers.client.layer.Vector; ingo@823: import org.gwtopenmaps.openlayers.client.layer.VectorOptions; ingo@882: import org.gwtopenmaps.openlayers.client.util.Attributes; ingo@797: import org.gwtopenmaps.openlayers.client.util.JObjectArray; ingo@797: import org.gwtopenmaps.openlayers.client.util.JSObject; ingo@797: ingo@797: ingo@882: public class FloodMap implements VectorFeatureAddedListener { ingo@797: ingo@800: public static final String LAYER_BARRIERS = "vector_layer_barriers"; ingo@800: ingo@1315: public static final String MARK_SELECTED = "mark.selected"; ingo@1315: ingo@1317: public static final int SELECTED_STROKE_WIDTH = 2; ingo@1317: ingo@797: protected MapWidget mapWidget; ingo@797: protected Map map; ingo@800: protected Vector barrierLayer; ingo@823: protected String srid; ingo@823: protected Bounds maxExtent; raimund@1542: protected ScaleLine scaleLine; ingo@797: christian@4476: public FloodMap(String srid, Bounds maxExtent, int width, int height) { ingo@823: this.srid = srid; ingo@823: this.maxExtent = maxExtent; christian@4402: recreateWidget(width, height); christian@3347: getBarrierLayer(); christian@3347: } ingo@823: christian@3347: christian@4476: public void recreateWidget(int width, int height) { christian@5609: final MapOptions opts = new MapOptions(); ingo@797: opts.setControls(new JObjectArray(new JSObject[] {})); ingo@797: opts.setNumZoomLevels(16); ingo@797: opts.setProjection(getRiverProjection()); ingo@823: opts.setMaxExtent(maxExtent); ingo@881: opts.setUnits("m"); ingo@823: opts.setMaxResolution(500); // TODO DO THIS ON THE FLY ingo@797: christian@4476: mapWidget = new MapWidget( christian@4476: Integer.toString(width - 4), christian@4476: Integer.toString(height), christian@4476: opts); ingo@797: map = mapWidget.getMap(); rrenkert@5032: map.addControl(new Attribution()); ingo@797: } ingo@797: ingo@797: ingo@882: @Override ingo@882: public void onFeatureAdded(FeatureAddedEvent evt) { christian@5609: final VectorFeature feature = evt.getVectorFeature(); ingo@882: christian@5609: final Attributes attrs = feature.getAttributes(); christian@5609: final String type = attrs.getAttributeAsString("typ"); ingo@882: ingo@882: if (type == null || type.length() == 0) { ingo@882: return; ingo@882: } ingo@882: christian@5609: final Style style = getStyle(type); ingo@1315: if (style != null) { ingo@1315: feature.setStyle(style); ingo@1315: } ingo@882: ingo@882: // necessary, otherwise the setStyle() has no effect christian@5609: getBarrierLayer().redraw(); ingo@882: } ingo@882: ingo@882: ingo@882: /** ingo@882: * Returns an OpenLayers.Style based on a given type. ingo@882: * ingo@882: * @param type Type can be one of "pipe1", "pipe2", "ditch", "dam", ingo@882: * "ringdike". ingo@882: * ingo@882: * @return an OpenLayers.Style object. ingo@882: */ ingo@882: public static Style getStyle(String type) { christian@5609: final Style style = new Style(); ingo@882: ingo@1315: if (type == null) { ingo@1315: return null; ingo@1315: } ingo@1315: ingo@882: if (type.equals(DrawControl.BARRIER_PIPE1) ingo@882: || type.equals(DrawControl.BARRIER_PIPE1_VALUE) ingo@882: ) { ingo@882: style.setFillColor("#800080"); ingo@882: style.setStrokeColor("#800080"); ingo@882: } ingo@882: else if (type.equals(DrawControl.BARRIER_PIPE2) ingo@882: || type.equals(DrawControl.BARRIER_PIPE2_VALUE) ingo@882: ) { ingo@882: style.setFillColor("#808080"); ingo@882: style.setStrokeColor("#808080"); ingo@882: } ingo@882: else if (type.equals(DrawControl.BARRIER_DAM) ingo@882: || type.equals(DrawControl.BARRIER_DAM_VALUE) ingo@882: ) { ingo@882: style.setFillColor("#008000"); ingo@882: style.setStrokeColor("#008000"); ingo@882: } ingo@882: else if (type.equals(DrawControl.BARRIER_DITCH) ingo@882: || type.equals(DrawControl.BARRIER_DITCH_VALUE) ingo@882: ) { ingo@882: style.setFillColor("#800000"); ingo@882: style.setStrokeColor("#800000"); ingo@882: } ingo@882: else if (type.equals(DrawControl.BARRIER_RINGDIKE) ingo@882: || type.equals(DrawControl.BARRIER_RINGDIKE_VALUE) ingo@882: ) { ingo@882: style.setFill(false); ingo@882: style.setStrokeColor("#FF8000"); ingo@882: } ingo@882: ingo@882: return style; ingo@882: } ingo@882: ingo@882: ingo@797: public MapWidget getMapWidget() { ingo@797: return mapWidget; ingo@797: } ingo@797: ingo@797: ingo@797: public Map getMap() { ingo@797: return map; ingo@797: } ingo@797: ingo@797: ingo@797: public String getRiverProjection() { ingo@823: return "EPSG:" + srid; ingo@797: } ingo@797: ingo@797: ingo@1315: public Bounds getMaxExtent() { ingo@1315: return maxExtent; ingo@1315: } ingo@1315: ingo@1315: ingo@800: public Vector getBarrierLayer() { ingo@800: if (barrierLayer == null) { christian@5609: final VectorOptions opts = new VectorOptions(); ingo@823: opts.setProjection(getRiverProjection()); ingo@1315: opts.setMaxExtent(getMaxExtent()); ingo@823: ingo@823: barrierLayer = new Vector(LAYER_BARRIERS, opts); ingo@806: barrierLayer.setIsBaseLayer(true); ingo@823: ingo@800: map.addLayer(barrierLayer); christian@4553: map.setLayerZIndex(barrierLayer, 1000); ingo@882: ingo@882: barrierLayer.addVectorFeatureAddedListener(this); ingo@800: } ingo@800: ingo@800: return barrierLayer; ingo@800: } ingo@800: ingo@800: ingo@833: public String getFeaturesAsGeoJSON() { ingo@1317: // disable features before exporting to GeoJSON ingo@1317: disableFeatures(); ingo@1317: christian@5609: final VectorFeature[] features = barrierLayer.getFeatures(); ingo@833: ingo@833: if (features == null || features.length == 0) { ingo@833: return null; ingo@833: } ingo@833: ingo@833: return new GeoJSON().write(features); ingo@833: } ingo@833: ingo@833: ingo@797: public void setSize(String width, String height) { ingo@797: mapWidget.setWidth(width); ingo@797: mapWidget.setHeight(height); christian@5609: final int currentZoom = map.getZoom(); christian@5609: final LonLat currentCenter = map.getCenter(); raimund@1386: map.updateSize(); raimund@1386: map.zoomTo(currentZoom); raimund@1386: map.setCenter(currentCenter); ingo@797: } raimund@915: raimund@915: ingo@1448: public void addLayer(Layer layer) { ingo@1448: if (layer != null) { ingo@1448: map.addLayer(layer); ingo@1448: christian@5609: final int index = map.getLayerIndex(layer); christian@5609: final int newIndex = index * (-1) + 1; ingo@1448: ingo@1448: map.raiseLayer(layer, newIndex); ingo@1448: ingo@1448: update(); ingo@1448: } ingo@1448: } ingo@1448: ingo@1448: raimund@915: public void hideBarrierLayer () { christian@5609: if (getBarrierLayer() != null) { raimund@915: barrierLayer.setIsVisible(false); raimund@915: } raimund@915: } raimund@915: raimund@915: public void showBarrierLayer () { christian@5609: if (getBarrierLayer() != null) { raimund@915: barrierLayer.setIsVisible(true); raimund@915: } raimund@915: } ingo@1317: ingo@1317: ingo@1317: public void selectFeature(VectorFeature feature) { ingo@1317: if (feature != null) { ingo@1317: selectFeatures(new VectorFeature[] { feature } ); ingo@1317: } ingo@1317: } ingo@1317: ingo@1317: ingo@1317: public void selectFeatures(VectorFeature[] features) { ingo@1317: if (features == null || features.length == 0) { ingo@1317: return; ingo@1317: } ingo@1317: christian@5609: for (final VectorFeature feature: features) { christian@5609: final Attributes attr = feature.getAttributes(); ingo@1317: ingo@1317: if (attr.getAttributeAsInt(MARK_SELECTED) == 1) { ingo@1317: continue; ingo@1317: } ingo@1317: ingo@1317: attr.setAttribute(MARK_SELECTED, 1); ingo@1317: christian@5609: final Style style = feature.getStyle(); christian@5609: final double strokeWidth = style.getStrokeWidth(); ingo@1317: ingo@1317: style.setStrokeWidth(strokeWidth+SELECTED_STROKE_WIDTH); ingo@1317: } ingo@1317: ingo@1317: getBarrierLayer().redraw(); ingo@1317: } ingo@1317: ingo@1317: ingo@1317: public void disableFeatures() { christian@5609: final Vector barriers = getBarrierLayer(); christian@5609: final VectorFeature[] features = barriers.getFeatures(); ingo@1317: ingo@1317: if (features == null || features.length == 0) { ingo@1317: return; ingo@1317: } ingo@1317: ingo@1317: disableFeatures(features); ingo@1317: } ingo@1317: ingo@1317: ingo@1317: public void disableFeature(VectorFeature feature) { ingo@1317: if (feature != null) { ingo@1317: disableFeatures(new VectorFeature[] { feature }); ingo@1317: } ingo@1317: } ingo@1317: ingo@1317: ingo@1317: public void disableFeatures(VectorFeature[] features) { ingo@1317: if (features == null || features.length == 0) { ingo@1317: return; ingo@1317: } ingo@1317: christian@5609: for (final VectorFeature feature: features) { christian@5609: final Attributes attr = feature.getAttributes(); ingo@1317: ingo@1317: if (attr.getAttributeAsInt(MARK_SELECTED) == 0) { ingo@1317: continue; ingo@1317: } ingo@1317: ingo@1317: attr.setAttribute(FloodMap.MARK_SELECTED, 0); ingo@1317: christian@5609: final Style style = feature.getStyle(); christian@5609: final double strokeWidth = style.getStrokeWidth(); ingo@1317: ingo@1317: style.setStrokeWidth(strokeWidth-SELECTED_STROKE_WIDTH); ingo@1317: } ingo@1317: ingo@1317: getBarrierLayer().redraw(); ingo@1317: } ingo@1397: ingo@1397: ingo@1397: public void update() { christian@5609: final Layer[] layers = map.getLayers(); ingo@1397: christian@5609: for (final Layer l: layers) { ingo@1397: l.redraw(); ingo@1397: } christian@5609: christian@5609: getBarrierLayer(); // Raises Z-Index to 1000 again ingo@1397: } raimund@1542: raimund@1542: raimund@1542: public void activateScaleLine(boolean activate) { raimund@1542: if (activate) { christian@5609: final ScaleLineOptions slOpts = new ScaleLineOptions(); raimund@1542: slOpts.setBottomInUnits("m"); raimund@1542: slOpts.setBottomOutUnits("km"); raimund@1542: slOpts.setTopInUnits(""); raimund@1542: slOpts.setTopOutUnits(""); raimund@1542: raimund@1542: scaleLine = new ScaleLine(slOpts); raimund@1542: this.map.addControl(scaleLine); raimund@1542: } raimund@1542: else if (!activate && scaleLine != null){ raimund@1542: this.map.removeControl(scaleLine); raimund@1542: } raimund@1542: } ingo@797: } ingo@799: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :