view flys-client/src/main/java/de/intevation/flys/client/client/ui/DigitizePanel.java @ 3847:f3b821735e39

Calculate the info url via i18n Don't fetch the info url from the artifact service and use i18n to calculate the url by using the official gauge and river number. flys-client/trunk@5582 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Mon, 24 Sep 2012 08:39:22 +0000
parents 88b51e6e0334
children 145980c21700
line wrap: on
line source
package de.intevation.flys.client.client.ui;

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

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

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

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.WMSParams;
import org.gwtopenmaps.openlayers.client.layer.WMSOptions;

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 de.intevation.flys.client.client.Config;
import de.intevation.flys.client.client.ui.map.FloodMap;
import de.intevation.flys.client.client.ui.map.MapPanel;
import de.intevation.flys.client.client.services.MapInfoService;
import de.intevation.flys.client.client.services.MapInfoServiceAsync;


public class DigitizePanel
extends SelectProvider
implements TabSelectedHandler, VisibilityChangedHandler {

    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>() {
            public void onFailure(Throwable caught) {
                String msg = caught.getMessage();

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

            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.
     */
    protected Canvas createWidget(DataList data) {
        GWT.log("DigitizePanel - createWidget()");

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

        LinkedHashMap initial = new LinkedHashMap();

        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() {
                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];

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

        return total;
    }


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

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

        helperContainer.addMember(mapPanel);
        helperContainer.addResizedHandler(new ResizedHandler() {
            public void onResized(ResizedEvent e) {
                Integer height = helperContainer.getHeight();
                Integer width  = helperContainer.getWidth();

                height = height * 99 / 100;
                width  = width  * 99 / 100;

                String w = String.valueOf(width) + "px";
                String h = String.valueOf(height) + "px";

                mapPanel.getFloodMap().setSize(w, h);
            }
        });

        parameterList.registerCollectionViewTabHandler(this);

        WMS axis = getLayer(
            mapInfo.getWmsUrl(), "riveraxis",
            mapInfo.getProjection(), false);
        WMS back = getLayer(
            mapInfo.getBackgroundWmsUrl(), mapInfo.getBackgroundWmsLayers(),
            mapInfo.getProjection(), false);

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

        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.setTransitionEffect("null");
        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;
    }


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

    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