ingo@797: package de.intevation.flys.client.client.ui.map; ingo@797: ingo@823: import org.gwtopenmaps.openlayers.client.Bounds; 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; ingo@882: import org.gwtopenmaps.openlayers.client.event.VectorFeatureAddedListener; ingo@882: import org.gwtopenmaps.openlayers.client.event.VectorFeatureAddedListener.FeatureAddedEvent; ingo@833: import org.gwtopenmaps.openlayers.client.feature.VectorFeature; ingo@833: import org.gwtopenmaps.openlayers.client.format.GeoJSON; 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; ingo@797: ingo@797: ingo@823: public FloodMap(String srid, Bounds maxExtent) { ingo@823: this.srid = srid; ingo@823: this.maxExtent = maxExtent; ingo@823: ingo@797: 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: ingo@797: mapWidget = new MapWidget("510px", "635px", opts); ingo@797: map = mapWidget.getMap(); ingo@806: ingo@806: getBarrierLayer(); ingo@797: } ingo@797: ingo@797: ingo@882: @Override ingo@882: public void onFeatureAdded(FeatureAddedEvent evt) { ingo@882: VectorFeature feature = evt.getVectorFeature(); ingo@882: ingo@882: Attributes attrs = feature.getAttributes(); ingo@882: String type = attrs.getAttributeAsString("typ"); ingo@882: ingo@882: if (type == null || type.length() == 0) { ingo@882: return; ingo@882: } ingo@882: ingo@1315: 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 ingo@882: barrierLayer.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) { ingo@882: 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) { ingo@823: 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); 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: ingo@833: 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); ingo@797: } raimund@915: raimund@915: raimund@915: public void hideBarrierLayer () { raimund@915: if (barrierLayer != null) { raimund@915: barrierLayer.setIsVisible(false); raimund@915: } raimund@915: } raimund@915: raimund@915: public void showBarrierLayer () { raimund@915: if (barrierLayer != 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: ingo@1317: for (VectorFeature feature: features) { ingo@1317: 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: ingo@1317: Style style = feature.getStyle(); ingo@1317: 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() { ingo@1317: Vector barriers = getBarrierLayer(); ingo@1317: 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: ingo@1317: for (VectorFeature feature: features) { ingo@1317: 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: ingo@1317: Style style = feature.getStyle(); ingo@1317: 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@797: } ingo@799: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :