view flys-client/src/main/java/de/intevation/flys/client/client/ui/map/MapToolbar.java @ 1317:45b9b1fc26e2

Improved error handling while using the elevation control - Make selected features in the map visible. flys-client/trunk@2956 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 13 Oct 2011 10:22:39 +0000
parents cf0f906921de
children 9981ba2ee13a
line wrap: on
line source
package de.intevation.flys.client.client.ui.map;

import com.google.gwt.core.client.GWT;

import com.smartgwt.client.types.SelectionType;
import com.smartgwt.client.util.SC;
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;
import com.smartgwt.client.widgets.layout.HLayout;

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;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class MapToolbar extends HLayout {

    protected FLYSConstants MSG = GWT.create(FLYSConstants.class);

    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 ImgButton elevationButton;

    protected DrawControl    drawControl;
    protected MeasureControl measureControl;

    protected Canvas position;


    public MapToolbar(FloodMap floodMap, Canvas wrapper) {
        this(floodMap, wrapper, true);
    }


    public MapToolbar(FloodMap floodMap, Canvas wrapper, boolean digitize) {
        super();

        setWidth100();
        setHeight(30);
        setMembersMargin(10);
        setPadding(5);
        setBorder("1px solid black");

        this.floodMap = floodMap;

        Canvas spacer = new Canvas();
        spacer.setWidth("*");

        zoomToMaxButton = createMaxExtentControl();
        zoomBoxButton   = createZoomBoxControl();
        zoomOutButton   = createZoomOutControl();
        panButton       = createPanControl();
        drawControl     = createDrawControl();
        selectButton    = createSelectFeatureControl();
        measureControl  = createMeasureControl();
        position        = createMousePosition(wrapper);
        removeButton    = createRemoveFeatureControl();
        elevationButton = createElevationControl();

        addMember(zoomToMaxButton);
        addMember(zoomBoxButton);
        addMember(zoomOutButton);
        addMember(panButton);

        if (digitize) {
            addMember(drawControl);
            addMember(selectButton);
            addMember(removeButton);
            addMember(elevationButton);
        }

        addMember(measureControl);
        addMember(spacer);
        addMember(position);
    }


    protected Map getMap() {
        return floodMap.getMap();
    }


    protected void activatePan(boolean activate) {
        if (activate) {
            panButton.select();
            pan.activate();
        }
        else {
            panButton.deselect();
            pan.deactivate();
        }
    }


    protected void activateZoomBox(boolean activate) {
        if (activate) {
            zoomBoxButton.select();
            zoomBox.activate();
        }
        else {
            zoomBoxButton.deselect();
            zoomBox.deactivate();
        }
    }


    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 void activateMeasureControl(boolean activate) {
        measureControl.activate(activate);
    }


    protected ImgButton createButton(String img, ClickHandler handler) {
        ImgButton btn = new ImgButton();

        String baseUrl = GWT.getHostPageBaseURL();
        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);

        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();
            }
        });

        zoomToMax.setTooltip(MSG.zoomMaxExtent());

        return zoomToMax;
    }


    protected ImgButton createZoomBoxControl() {
        zoomBox = new ZoomBox();

        EnableDisableCmd cmd = new EnableDisableCmd() {
            public void enable() {
                activateZoomBox(true);
                activatePan(false);
                activateDrawFeature(false);
                activateSelectFeature(false);
                activateMeasureControl(false);
            }

            public void disable() {
                activateZoomBox(false);
            }
        };

        ImgButton button = createToggleButton(MSG.zoom_in(), cmd);
        button.setTooltip(MSG.zoomIn());

        Map map = getMap();
        map.addControl(zoomBox);

        return button;
    }


    protected ImgButton createZoomOutControl() {
        ImgButton zoomOut = createButton(MSG.zoom_out(), new ClickHandler() {
            public void onClick(ClickEvent event) {
                Map map   = floodMap.getMap();
                int level = map.getZoom();

                if (level > 1) {
                    map.zoomTo(level-1);
                }
            }
        });

        zoomOut.setTooltip(MSG.zoomOut());

        return zoomOut;
    }


    protected ImgButton createPanControl() {
        pan = new DragPan();
        getMap().addControl(pan);

        EnableDisableCmd cmd = new EnableDisableCmd() {
            public void enable() {
                activateZoomBox(false);
                activatePan(true);
                activateDrawFeature(false);
                activateSelectFeature(false);
                activateMeasureControl(false);
            }

            public void disable() {
                activatePan(false);
            }
        };

        final ImgButton button = createToggleButton(MSG.pan(), cmd);
        button.setTooltip(MSG.moveMap());

        return button;
    }


    protected DrawControl createDrawControl() {
        EnableDisableCmd cmd = new EnableDisableCmd() {
            public void enable() {
                activateZoomBox(false);
                activatePan(false);
                activateDrawFeature(true);
                activateSelectFeature(false);
                activateMeasureControl(false);
            }

            public void disable() {
                activateDrawFeature(false);
            }
        };
        return new DrawControl(getMap(), floodMap.getBarrierLayer(), cmd);
    }


    protected ImgButton createSelectFeatureControl() {
        SelectFeatureOptions opts = new SelectFeatureOptions();
        opts.setBox(true);

        // 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) {
                floodMap.selectFeature(feature);
            }
        });

        opts.onUnSelect(new SelectFeature.UnselectFeatureListener() {
            public void onFeatureUnselected(VectorFeature feature) {
                floodMap.disableFeature(feature);
            }
        });

        selectFeature = new SelectFeature(floodMap.getBarrierLayer(), opts);
        getMap().addControl(selectFeature);

        EnableDisableCmd cmd = new EnableDisableCmd() {
            public void enable() {
                activateDrawFeature(false);
                activatePan(false);
                activateZoomBox(false);
                activateSelectFeature(true);
                activateMeasureControl(false);
            }

            public void disable() {
                activateSelectFeature(false);
                floodMap.disableFeatures();
            }
        };

        ImgButton button = createToggleButton(MSG.selectFeature(), cmd);
        button.setTooltip(MSG.selectObject());

        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;
                }

                for (int i = features.length-1; i >= 0; i--) {
                    VectorFeature feature = features[i];

                    Attributes attr = feature.getAttributes();
                    int del = attr.getAttributeAsInt(FloodMap.MARK_SELECTED);

                    if (del == 1) {
                        barriers.removeFeature(feature);
                        feature.destroy();
                    }
                }
            }
        });

        remove.setTooltip(MSG.removeObject());

        return remove;
    }


    protected ImgButton createElevationControl() {
        ImgButton btn = createButton(MSG.adjustElevation(), new ClickHandler() {
            public void onClick(ClickEvent evt) {
                Vector          barriers = floodMap.getBarrierLayer();
                VectorFeature[] features = barriers.getFeatures();

                VectorFeature feature = null;

                if (features == null || features.length == 0) {
                    SC.warn(MSG.error_no_feature_selected());
                    return;
                }

                boolean multipleFeatures = false;

                for (VectorFeature f: features) {
                    Attributes attr = f.getAttributes();
                    if (attr.getAttributeAsInt(FloodMap.MARK_SELECTED) == 1) {
                        if (feature == null) {
                            feature = f;
                        }
                        else {
                            multipleFeatures = true;
                        }
                    }
                }

                if (feature == null) {
                    SC.warn(MSG.error_no_feature_selected());
                    return;
                }

                new ElevationWindow(floodMap, feature).show();

                if (multipleFeatures) {
                    SC.warn(MSG.warning_use_first_feature());
                }
            }
        });

        btn.setTooltip(MSG.adjustElevationTooltip());

        return btn;
    }


    protected Canvas createMousePosition(Canvas mapWrapper) {
        return new MapPositionPanel(floodMap.getMapWidget(), mapWrapper);
    }


    protected MeasureControl createMeasureControl() {
        EnableDisableCmd cmd = new EnableDisableCmd() {
            public void enable() {
                activateDrawFeature(false);
                activatePan(false);
                activateZoomBox(false);
                activateSelectFeature(false);
            }

            public void disable() {
                // do nothing
            }
        };

        return new MeasureControl(floodMap, cmd);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org