view flys-client/src/main/java/de/intevation/flys/client/client/ui/DigitizePanel.java @ 4553:fac54b8470d4

Map view in DigitizePanel now shows OSM background, river axis and manual drawn barriers again. Please note that this change requires changes in conf/floodmap.xml in artifact-server.
author Christian Lins <christian.lins@intevation.de>
date Fri, 16 Nov 2012 12:36:10 +0100
parents 099d136b215b
children 32a4651eef93
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

import com.smartgwt.client.types.Encoding;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.VisibilityChangedEvent;
import com.smartgwt.client.widgets.events.VisibilityChangedHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.UploadItem;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;

import de.intevation.flys.client.client.Config;
import de.intevation.flys.client.client.services.MapInfoService;
import de.intevation.flys.client.client.services.MapInfoServiceAsync;
import de.intevation.flys.client.client.ui.map.FloodMap;
import de.intevation.flys.client.client.ui.map.MapPanel;
import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.DataList;
import de.intevation.flys.client.shared.model.DefaultData;
import de.intevation.flys.client.shared.model.DefaultDataItem;
import de.intevation.flys.client.shared.model.MapInfo;

import java.util.LinkedHashMap;
import java.util.List;

import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
import org.gwtopenmaps.openlayers.client.format.GeoJSON;
import org.gwtopenmaps.openlayers.client.layer.WMS;
import org.gwtopenmaps.openlayers.client.layer.WMSOptions;
import org.gwtopenmaps.openlayers.client.layer.WMSParams;


public class DigitizePanel
extends SelectProvider
implements TabSelectedHandler, VisibilityChangedHandler {

    private static final long serialVersionUID = 3529775660871273314L;

    protected MapInfoServiceAsync mapInfo = GWT.create(MapInfoService.class);

    protected FloodMap floodMap;

    protected MapPanel mapPanel;

    public static final String UESK_BARRIERS = "uesk.barriers";


    public DigitizePanel() {
    }


    @Override
    public Canvas create(DataList list) {
        List<Data> data = list.getAll();

        helperContainer.addVisibilityChangedHandler(this);

        Data barriers = null;
        for (int i = data.size()-1; i >= 0; i--) {
            Data d = data.get(i);
            if (d.getLabel().equals(UESK_BARRIERS)) {
                barriers = d;
                data.remove(d);
            }
        }

        DataList clone = (DataList) list.clone();
        List<Data> all = clone.getAll();
        all.remove(UESK_BARRIERS);

        Canvas selectBox = super.create(clone);

        final Config cfg    = Config.getInstance();
        final String locale = cfg.getLocale();

        DataItem[] obj = barriers.getItems();

        final String[] geojson = new String[1];
        for (DataItem item: obj) {
            if (item.getLabel().equals(UESK_BARRIERS)) {
                geojson[0] = item.getStringValue();
                break;
            }
        }

        String river = getDataValue("state.winfo.river", "river");
        mapInfo.getMapInfo(locale, river, new AsyncCallback<MapInfo>() {
            @Override
            public void onFailure(Throwable caught) {
                String msg = caught.getMessage();

                GWT.log("Error while fetching MapInfo: " + msg);
                SC.warn(MSG.getString(msg));
            }

            @Override
            public void onSuccess(MapInfo info) {
                createMapWidget(info, geojson[0]);
            }
        });

        return selectBox;
    }


    /**
     * This method creates the content of the widget.
     *
     * @param data The {@link DataList} object.
     *
     * @return a combobox.
     */
    @Override
    protected Canvas createWidget(DataList data) {
        GWT.log("DigitizePanel - createWidget()");

        VLayout layout   = new VLayout();
        layout.setAlign(VerticalAlignment.TOP);
        layout.setHeight(25);

        LinkedHashMap<String, String> initial = new LinkedHashMap<String, String>();

        form = new DynamicForm();

        int size = data.size();

        for (int i = 0; i < size; i++) {
            Data d = data.get(i);

            Label label = new Label(d.getDescription());
            label.setValign(VerticalAlignment.TOP);
            label.setHeight(20);
            label.setWidth(400);

            SelectItem combobox = new SelectItem(d.getLabel());
            combobox.setWidth(250);

            LinkedHashMap<String, String> it = new LinkedHashMap<String, String>();

            boolean  defaultSet = false;
            boolean  first      = true;

            DataItem def      = d.getDefault();
            String   defValue = def != null ? def.getStringValue() : null;

            if (defValue != null && defValue.length() > 0) {
                initial.put(d.getLabel(), def.getStringValue());
                defaultSet = true;
            }

            // I was here.
            for (DataItem item: d.getItems()) {
                if (!defaultSet && first) {
                    initial.put(d.getLabel(), item.getStringValue());
                    first = false;
                }

                it.put(item.getStringValue(), item.getLabel());
            }

            label.setWidth(50);
            combobox.setValueMap(it);
            combobox.setShowTitle(false);
            form.setItems(combobox);

            HTMLPane frame = new HTMLPane();
            frame.setWidth("1px");
            frame.setHeight("1px");
            frame.setContents("<iframe id='uploadTarget' name='uploadTarget'></iframe>");

            final DynamicForm uploadForm = new DynamicForm();
            uploadForm.setAction("flys/fileupload?uuid=" + artifact.getUuid());
            uploadForm.setTarget("uploadTarget");
            uploadForm.setEncoding(Encoding.MULTIPART);
            Label uploadLabel = new Label(MSG.shape_file_upload());
            uploadLabel.setHeight(20);
            UploadItem uploadItem = new UploadItem();
            uploadItem.setShowTitle(false);
            uploadForm.setFields(uploadItem);
            Button submit = new Button(MSG.upload_file());
            submit.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent e) {
                    uploadForm.submitForm();
                }
            });
            layout.addMember(frame);
            layout.addMember(label);
            layout.addMember(form);
            layout.addMember(uploadLabel);
            layout.addMember(uploadForm);
            layout.addMember(submit);
        }

        form.setValues(initial);

        layout.setAlign(VerticalAlignment.TOP);

        return layout;
    }


    @Override
    protected Data[] getData() {
        Data[] data  = super.getData();
        Data[] total = new Data[2];

        if (floodMap != null) {
            DataItem item = new DefaultDataItem(
                UESK_BARRIERS, UESK_BARRIERS, floodMap.getFeaturesAsGeoJSON());
            total[0] = data[0];
            total[1] = new DefaultData(
                UESK_BARRIERS, null, null, new DataItem[] { item });
        }
        else {
            // Happens when OpenLayers is missing
            GWT.log("floodMap is null -> OpenLayers missing?");
        }

        return total;
    }


    public void createMapWidget(MapInfo mapInfo, String geojson) {
        mapPanel = new MapPanel(mapInfo, true);

        floodMap = mapPanel.getFloodMap();
        Map map  = floodMap.getMap();

        helperContainer.addMember(mapPanel);

        parameterList.registerCollectionViewTabHandler(this);

        WMS axis = getLayer(
            mapInfo.getWmsUrl(), "FLYS-Map", // FIXME: Make layer name configurable
            mapInfo.getProjection(), false);
        WMS back = getLayer(
            mapInfo.getBackgroundWmsUrl(), mapInfo.getBackgroundWmsLayers(),
            mapInfo.getProjection(), false);

        map.addLayer(back);
        map.addLayer(axis);

        if (geojson != null && geojson.length() > 0) {
            VectorFeature[] features = new GeoJSON().read(geojson);
            floodMap.getBarrierLayer().addFeatures(features);
        }

        map.zoomToMaxExtent();
    }


    protected WMS getLayer(String url, String layers, String proj, boolean x) {
        WMSParams params = new WMSParams();
        params.setLayers(layers);
        params.setFormat("image/png");
        params.setIsTransparent(!x);

        WMSOptions opts = new WMSOptions();
        opts.setProjection(proj);
        opts.setSingleTile(true);
        opts.setRatio(1);
        opts.setBuffer(0);

        WMS wms = new WMS(layers, url, params, opts);
        wms.setIsVisible(true);
        wms.setIsBaseLayer(x);

        return wms;
    }


    @Override
    public void onTabSelected(TabSelectedEvent tse) {
        if (tse.getTabNum () != 0) {
            floodMap.hideBarrierLayer();
        }
        else {
            floodMap.showBarrierLayer();
        }
    }

    @Override
    public void onVisibilityChanged(VisibilityChangedEvent vce) {
        if (!vce.getIsVisible()) {
            floodMap.hideBarrierLayer();
            mapPanel.getMapToolbar().activateDrawFeature(false);
        }
        else {
            floodMap.showBarrierLayer();
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org