diff flys-client/src/main/java/de/intevation/flys/client/client/ui/map/MapToolbar.java @ 800:95cc560ce7c4

Added map controls for digitizing and removing barriers. flys-client/trunk@2321 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 12 Jul 2011 13:43:49 +0000
parents f4299b90c996
children d45ad7fd3027
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/map/MapToolbar.java	Mon Jul 11 13:29:57 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/map/MapToolbar.java	Tue Jul 12 13:43:49 2011 +0000
@@ -3,6 +3,7 @@
 import com.google.gwt.core.client.GWT;
 
 import com.smartgwt.client.types.SelectionType;
+import com.smartgwt.client.widgets.Canvas;
 import com.smartgwt.client.widgets.ImgButton;
 import com.smartgwt.client.widgets.events.ClickEvent;
 import com.smartgwt.client.widgets.events.ClickHandler;
@@ -10,9 +11,15 @@
 
 import org.gwtopenmaps.openlayers.client.Map;
 import org.gwtopenmaps.openlayers.client.control.DragPan;
+import org.gwtopenmaps.openlayers.client.control.SelectFeature;
+import org.gwtopenmaps.openlayers.client.control.SelectFeatureOptions;
 import org.gwtopenmaps.openlayers.client.control.ZoomBox;
+import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
+import org.gwtopenmaps.openlayers.client.layer.Vector;
+import org.gwtopenmaps.openlayers.client.util.Attributes;
 
 import de.intevation.flys.client.client.FLYSConstants;
+import de.intevation.flys.client.client.utils.EnableDisableCmd;
 
 
 /**
@@ -20,20 +27,28 @@
  */
 public class MapToolbar extends HLayout {
 
-    /** The message class that provides i18n strings.*/
+    public static final String MARK_TO_DELETE = "mark.delete";
+
     protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
 
-    protected FloodMap floodMap;
-    protected DragPan  pan;
-    protected ZoomBox  zoomBox;
+    protected FloodMap      floodMap;
+    protected DragPan       pan;
+    protected ZoomBox       zoomBox;
+    protected SelectFeature selectFeature;
 
     protected ImgButton zoomToMaxButton;
     protected ImgButton zoomBoxButton;
     protected ImgButton zoomOutButton;
     protected ImgButton panButton;
+    protected ImgButton selectButton;
+    protected ImgButton removeButton;
+
+    protected DrawControl drawControl;
+
+    protected Canvas position;
 
 
-    public MapToolbar(FloodMap floodMap) {
+    public MapToolbar(FloodMap floodMap, Canvas wrapper) {
         super();
 
         setWidth100();
@@ -44,15 +59,27 @@
 
         this.floodMap = floodMap;
 
+        Canvas spacer = new Canvas();
+        spacer.setWidth("*");
+
         zoomToMaxButton = createMaxExtentControl();
         zoomBoxButton   = createZoomBoxControl();
         zoomOutButton   = createZoomOutControl();
         panButton       = createPanControl();
+        position        = createMousePosition(wrapper);
+        drawControl     = createDrawControl();
+        selectButton    = createSelectFeatureControl();
+        removeButton    = createRemoveFeatureControl();
 
         addMember(zoomToMaxButton);
         addMember(zoomBoxButton);
         addMember(zoomOutButton);
         addMember(panButton);
+        addMember(drawControl);
+        addMember(selectButton);
+        addMember(removeButton);
+        addMember(spacer);
+        addMember(position);
     }
 
 
@@ -85,21 +112,74 @@
     }
 
 
-    protected ImgButton createMaxExtentControl() {
-        ImgButton zoomToMax = new ImgButton();
+    protected void activateDrawFeature(boolean activate) {
+        drawControl.activate(activate);
+    }
+
+
+    protected void activateSelectFeature(boolean activate) {
+        if (activate) {
+            selectButton.select();
+            selectFeature.activate();
+        }
+        else {
+            selectButton.deselect();
+            selectFeature.deactivate();
+        }
+    }
+
+
+    protected ImgButton createButton(String img, ClickHandler handler) {
+        ImgButton btn = new ImgButton();
 
         String baseUrl = GWT.getHostPageBaseURL();
-        zoomToMax.setSrc(baseUrl + MSG.zoom_all());
-        zoomToMax.setWidth(20);
-        zoomToMax.setHeight(20);
-        zoomToMax.setShowDown(false);
-        zoomToMax.setShowRollOver(false);
-        zoomToMax.setShowDisabled(false);
-        zoomToMax.setShowDisabledIcon(true);
-        zoomToMax.setShowDownIcon(false);
-        zoomToMax.setShowFocusedIcon(false);
+        btn.setSrc(baseUrl + img);
+        btn.setWidth(20);
+        btn.setHeight(20);
+        btn.setShowDown(false);
+        btn.setShowRollOver(false);
+        btn.setShowDisabled(false);
+        btn.setShowDisabledIcon(true);
+        btn.setShowDownIcon(false);
+        btn.setShowFocusedIcon(false);
 
-        zoomToMax.addClickHandler(new ClickHandler() {
+        if (handler != null) {
+            btn.addClickHandler(handler);
+        }
+
+        return btn;
+    }
+
+
+    protected ImgButton createToggleButton(
+        String img,
+        final EnableDisableCmd cmd
+    ) {
+        final ImgButton btn = new ImgButton();
+
+        String baseUrl = GWT.getHostPageBaseURL();
+        btn.setSrc(baseUrl + img);
+        btn.setActionType(SelectionType.CHECKBOX);
+        btn.setSize(20);
+        btn.setShowRollOver(false);
+        btn.setSelected(false);
+        btn.addClickHandler(new ClickHandler() {
+            public void onClick(ClickEvent e) {
+                if (btn.isSelected()) {
+                    cmd.enable();
+                }
+                else {
+                    cmd.disable();
+                }
+            }
+        });
+
+        return btn;
+    }
+
+
+    protected ImgButton createMaxExtentControl() {
+        ImgButton zoomToMax = createButton(MSG.zoom_all(), new ClickHandler() {
             public void onClick(ClickEvent event) {
                 floodMap.getMap().zoomToMaxExtent();
             }
@@ -112,28 +192,21 @@
     protected ImgButton createZoomBoxControl() {
         zoomBox = new ZoomBox();
 
-        final ImgButton button = new ImgButton();
-
-        String baseUrl = GWT.getHostPageBaseURL();
-        button.setSrc(baseUrl + MSG.zoom_in());
-        button.setActionType(SelectionType.CHECKBOX);
-        button.setSize(20);
-        button.setShowRollOver(false);
-        button.setSelected(false);
+        EnableDisableCmd cmd = new EnableDisableCmd() {
+            public void enable() {
+                activateZoomBox(true);
+                activatePan(false);
+                activateDrawFeature(false);
+                activateSelectFeature(false);
+            }
 
-        button.addClickHandler(new ClickHandler() {
-            public void onClick(ClickEvent e) {
-                if (button.isSelected()) {
-                    activatePan(false);
-                    activateZoomBox(true);
-                }
-                else {
-                    activateZoomBox(false);
-                }
+            public void disable() {
+                activateZoomBox(false);
             }
-        });
+        };
 
-        Map map = getMap();
+        ImgButton button = createToggleButton(MSG.zoom_in(), cmd);
+        Map       map    = getMap();
         map.addControl(zoomBox);
 
         return button;
@@ -141,20 +214,7 @@
 
 
     protected ImgButton createZoomOutControl() {
-        ImgButton zoomOut = new ImgButton();
-
-        String baseUrl = GWT.getHostPageBaseURL();
-        zoomOut.setSrc(baseUrl + MSG.zoom_out());
-        zoomOut.setWidth(20);
-        zoomOut.setHeight(20);
-        zoomOut.setShowDown(false);
-        zoomOut.setShowRollOver(false);
-        zoomOut.setShowDisabled(false);
-        zoomOut.setShowDisabledIcon(true);
-        zoomOut.setShowDownIcon(false);
-        zoomOut.setShowFocusedIcon(false);
-
-        zoomOut.addClickHandler(new ClickHandler() {
+        ImgButton zoomOut = createButton(MSG.zoom_out(), new ClickHandler() {
             public void onClick(ClickEvent event) {
                 Map map   = floodMap.getMap();
                 int level = map.getZoom();
@@ -171,32 +231,120 @@
 
     protected ImgButton createPanControl() {
         pan = new DragPan();
-
-        final ImgButton button = new ImgButton();
+        getMap().addControl(pan);
 
-        String baseUrl = GWT.getHostPageBaseURL();
-        button.setSrc(baseUrl + MSG.pan());
-        button.setActionType(SelectionType.CHECKBOX);
-        button.setSize(20);
-        button.setShowRollOver(false);
-        button.setSelected(false);
+        EnableDisableCmd cmd = new EnableDisableCmd() {
+            public void enable() {
+                activateZoomBox(false);
+                activatePan(true);
+                activateDrawFeature(false);
+                activateSelectFeature(false);
+            }
 
-        button.addClickHandler(new ClickHandler() {
-            public void onClick(ClickEvent e) {
-                if (button.isSelected()) {
-                    activateZoomBox(false);
-                    activatePan(true);
+            public void disable() {
+                activatePan(false);
+            }
+        };
+
+        final ImgButton button = createToggleButton(MSG.pan(), cmd);
+
+        return button;
+    }
+
+
+    protected DrawControl createDrawControl() {
+        EnableDisableCmd cmd = new EnableDisableCmd() {
+            public void enable() {
+                activateZoomBox(false);
+                activatePan(false);
+                activateDrawFeature(true);
+                activateSelectFeature(false);
+            }
+
+            public void disable() {
+                activateDrawFeature(false);
+            }
+        };
+        return new DrawControl(getMap(), floodMap.getBarrierLayer(), cmd);
+    }
+
+
+    protected ImgButton createSelectFeatureControl() {
+        SelectFeatureOptions opts = new SelectFeatureOptions();
+
+        // VectorFeatures selected by the SelectFeature control are manually
+        // marked with the string "mark.delete". The control to remove selected
+        // features makes use of this string to determine if the feature should
+        // be deleted (is marked) or not. Actually, we would like to use the
+        // OpenLayers native mechanism to select features, but for some reason
+        // this doesn't work here. After a feature has been selected, the layer
+        // still has no selected features.
+        opts.onSelect(new SelectFeature.SelectFeatureListener() {
+            public void onFeatureSelected(VectorFeature feature) {
+                Attributes attr = feature.getAttributes();
+                attr.setAttribute(MARK_TO_DELETE, 1);
+            }
+        });
+
+        opts.onUnSelect(new SelectFeature.UnselectFeatureListener() {
+            public void onFeatureUnselected(VectorFeature feature) {
+                Attributes attr = feature.getAttributes();
+                attr.setAttribute(MARK_TO_DELETE, 0);
+            }
+        });
+
+        selectFeature = new SelectFeature(floodMap.getBarrierLayer(), opts);
+        getMap().addControl(selectFeature);
+
+        EnableDisableCmd cmd = new EnableDisableCmd() {
+            public void enable() {
+                activateDrawFeature(false);
+                activatePan(false);
+                activateZoomBox(false);
+                activateSelectFeature(true);
+            }
+
+            public void disable() {
+                activateSelectFeature(false);
+            }
+        };
+
+        ImgButton button = createToggleButton(MSG.selectFeature(), cmd);
+
+        return button;
+    }
+
+
+    protected ImgButton createRemoveFeatureControl() {
+        ImgButton remove = createButton(MSG.removeFeature(),new ClickHandler() {
+            public void onClick(ClickEvent event) {
+                Vector          barriers = floodMap.getBarrierLayer();
+                VectorFeature[] features = barriers.getFeatures();
+
+                if (features == null || features.length == 0) {
+                    return;
                 }
-                else {
-                    activatePan(false);
+
+                for (int i = features.length-1; i >= 0; i--) {
+                    VectorFeature feature = features[i];
+
+                    Attributes attr = feature.getAttributes();
+                    int        del  = attr.getAttributeAsInt(MARK_TO_DELETE);
+
+                    if (del == 1) {
+                        barriers.removeFeature(feature);
+                        feature.destroy();
+                    }
                 }
             }
         });
 
-        Map map = getMap();
-        map.addControl(pan);
+        return remove;
+    }
 
-        return button;
+
+    protected Canvas createMousePosition(Canvas mapWrapper) {
+        return new MapPositionPanel(floodMap.getMapWidget(), mapWrapper);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org