diff flys-client/src/main/java/de/intevation/flys/client/client/ui/map/DrawControl.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
children d02c3835df28
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/map/DrawControl.java	Tue Jul 12 13:43:49 2011 +0000
@@ -0,0 +1,182 @@
+package de.intevation.flys.client.client.ui.map;
+
+import java.util.LinkedHashMap;
+
+import com.google.gwt.core.client.GWT;
+
+import com.smartgwt.client.types.SelectionType;
+import com.smartgwt.client.widgets.ImgButton;
+import com.smartgwt.client.widgets.events.ClickEvent;
+import com.smartgwt.client.widgets.events.ClickHandler;
+import com.smartgwt.client.widgets.form.DynamicForm;
+import com.smartgwt.client.widgets.form.fields.SelectItem;
+import com.smartgwt.client.widgets.form.fields.FormItem;
+import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
+import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
+import com.smartgwt.client.widgets.layout.HLayout;
+
+import org.gwtopenmaps.openlayers.client.Map;
+import org.gwtopenmaps.openlayers.client.control.Control;
+import org.gwtopenmaps.openlayers.client.control.DrawFeature;
+import org.gwtopenmaps.openlayers.client.handler.Handler;
+import org.gwtopenmaps.openlayers.client.handler.PathHandler;
+import org.gwtopenmaps.openlayers.client.handler.PolygonHandler;
+import org.gwtopenmaps.openlayers.client.layer.Vector;
+
+import de.intevation.flys.client.client.FLYSConstants;
+import de.intevation.flys.client.client.utils.EnableDisableCmd;
+
+
+public class DrawControl extends HLayout {
+
+    public static final String BARRIER_PIPE1    = "pipe1";
+    public static final String BARRIER_PIPE2    = "pipe2";
+    public static final String BARRIER_DITCH    = "ditch";
+    public static final String BARRIER_DAM      = "dam";
+    public static final String BARRIER_RINGDIKE = "ring_dike";
+
+    public static final String FIELD_BARRIER_TYPE = "field_barrier_type";
+
+
+    protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
+
+    protected EnableDisableCmd cmd;
+
+    protected ImgButton   button;
+    protected DynamicForm form;
+
+    protected Map    map;
+    protected Vector layer;
+
+    protected Control control;
+
+
+    public DrawControl(Map map, Vector layer, EnableDisableCmd cmd) {
+        this.map   = map;
+        this.layer = layer;
+        this.cmd   = cmd;
+
+        initialize();
+    }
+
+
+    protected void initialize() {
+        setWidth(100);
+        setMembersMargin(0);
+
+        button = new ImgButton();
+
+        String baseUrl = GWT.getHostPageBaseURL();
+        button.setSrc(baseUrl + MSG.digitize());
+        button.setActionType(SelectionType.CHECKBOX);
+        button.setSize(20);
+        button.setShowRollOver(false);
+        button.setSelected(false);
+
+        button.addClickHandler(new ClickHandler() {
+            public void onClick(ClickEvent e) {
+                if (button.isSelected()) {
+                    cmd.enable();
+                }
+                else {
+                    cmd.disable();
+                }
+            }
+        });
+
+        form = new DynamicForm();
+        form.setWidth(75);
+        form.setTitlePrefix("");
+        form.setTitleSuffix("");
+
+        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
+        map.put(BARRIER_PIPE1, MSG.getString(BARRIER_PIPE1));
+        map.put(BARRIER_PIPE2, MSG.getString(BARRIER_PIPE2));
+        map.put(BARRIER_DITCH, MSG.getString(BARRIER_DITCH));
+        map.put(BARRIER_DAM, MSG.getString(BARRIER_DAM));
+        map.put(BARRIER_RINGDIKE, MSG.getString(BARRIER_RINGDIKE));
+
+        SelectItem box = new SelectItem(FIELD_BARRIER_TYPE);
+        box.setTitle("");
+        box.setWidth(75);
+        box.setValueMap(map);
+        box.addChangedHandler(new ChangedHandler() {
+            public void onChanged(ChangedEvent e) {
+                setSelectedControl();
+            }
+        });
+
+        form.setFields(box);
+
+        addMember(button);
+        addMember(form);
+
+        activate(false);
+    }
+
+
+    protected String getSelectedType() {
+        return form.getValueAsString(FIELD_BARRIER_TYPE);
+    }
+
+
+    protected void removeControl() {
+        if (control != null) {
+            control.deactivate();
+            map.removeControl(control);
+        }
+    }
+
+
+    protected void setSelectedControl() {
+        removeControl();
+
+        String type = getSelectedType();
+
+        if (type == null || type.length() == 0) {
+            return;
+        }
+
+        if (type.equalsIgnoreCase(BARRIER_RINGDIKE)) {
+            control = createDrawPolygonControl();
+        }
+        else {
+            control = createDrawLineControl();
+        }
+
+        map.addControl(control);
+        control.activate();
+    }
+
+
+    protected Control createDrawControl(Handler handler) {
+        return new DrawFeature(layer, handler);
+    }
+
+
+    protected Control createDrawPolygonControl() {
+        return createDrawControl(new PolygonHandler());
+    }
+
+
+    protected Control createDrawLineControl() {
+        return createDrawControl(new PathHandler());
+    }
+
+
+    public void activate(boolean activate) {
+        FormItem item = form.getField(FIELD_BARRIER_TYPE);
+
+        if (activate) {
+            button.select();
+            item.enable();
+            setSelectedControl();
+        }
+        else {
+            removeControl();
+            button.deselect();
+            item.disable();
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org