view flys-client/src/main/java/de/intevation/flys/client/client/ui/map/GetFeatureInfoWindow.java @ 5818:a4ff4167be1e

Request feature info on all layers and show it as html if the server does not return valid gml. Non queryable layers produce an error message when the request fails. This is good enough
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 24 Apr 2013 17:33:27 +0200
parents e0ab5a566688
children 79717f1cf6eb
line wrap: on
line source
package de.intevation.flys.client.client.ui.map;

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

import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.viewer.DetailViewer;
import com.smartgwt.client.widgets.viewer.DetailViewerField;
import com.smartgwt.client.widgets.viewer.DetailViewerRecord;

import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.shared.model.FeatureInfo;
import de.intevation.flys.client.shared.model.FeatureInfoResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.MissingResourceException;

import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
import org.gwtopenmaps.openlayers.client.util.Attributes;
import org.gwtopenmaps.openlayers.client.util.JSObject;


public class GetFeatureInfoWindow extends Window {

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

    protected List<FeatureInfo> features;

    protected String title;

    protected String featureInfoHTML;


    public static final int ROW_HEIGHT = 25;


    public GetFeatureInfoWindow(List<FeatureInfo> features, String title) {
        super();
        this.features = features;
        this.title = title;

        initLayout();
    }

    public GetFeatureInfoWindow(String featureInfoHTML, String title) {
        super();
        features = null;
        this.title = title;
        this.featureInfoHTML = featureInfoHTML;

        initLayoutHTML();
    }

    protected void initLayoutHTML() {
        HTMLPane pane = new HTMLPane();
        pane.setContents(featureInfoHTML);
        addItem(pane);
        setWidth(500);
        setHeight(300);

        setIsModal(false);
//        setShowModalMask(true);

        centerInPage();
    }

    protected void initLayout() {
        VLayout root = new VLayout();

        for (FeatureInfo feature: features) {
            // Currently this should alway be only one
            root.addMember(createFeatureViewer(feature));
            setTitle(MSG.getFeatureInfoWindowTitle() + " " + title);
        }

        addItem(root);

        setWidth(500);
        setHeight(300);

        setIsModal(false);
//        setShowModalMask(true);

        centerInPage();
    }


    protected DetailViewer createFeatureViewer(FeatureInfo feature) {
        DetailViewer detailViewer = new DetailViewer();
        detailViewer.setWidth(487);

        Map<String, String> attrs = feature.getAttrs();
        Set<Map.Entry<String, String>> entries = attrs.entrySet();
        List <DetailViewerField> fields = new ArrayList<DetailViewerField>();
        DetailViewerRecord dr = new DetailViewerRecord();

        DetailViewerField path_field = null; // Make sure path is always the last element

        for (Map.Entry<String, String> entry: entries) {
            String localized;
            try {
                localized = MSG.getString(entry.getKey());
            } catch (MissingResourceException mre) {
                localized = entry.getKey();
//                We filter unwanted information by localization
//                Uncomment to filter out unlocalized elements
//                continue;
            }
            if (entry.getKey().equals("PATH")) {
                path_field = new DetailViewerField(entry.getKey(), localized);
            } else {
                fields.add(new DetailViewerField(entry.getKey(), localized));
            }
            dr.setAttribute(entry.getKey(), entry.getValue());
        }
        if (path_field != null)
            fields.add(path_field);

        DetailViewerField[] fieldArray = new DetailViewerField[fields.size()];
        detailViewer.setFields(fields.toArray(fieldArray));
        detailViewer.setData(new DetailViewerRecord[]{dr});
        detailViewer.setCanSelectText(true);

        return detailViewer;
    }


    protected String[][] extractProperties(VectorFeature feature) {
        Attributes tmp   = feature.getAttributes();
        JSObject   jsobj = tmp.getJSObject();

        String   tmpNames = jsobj.getPropertyNames();
        String[] allNames = tmpNames.split(",");

        String[][] attr = new String[allNames.length][];

        for (int i = 0, n = attr.length; i < n; i++) {
            attr[i] = new String[] {
                allNames[i],
                jsobj.getPropertyAsString(allNames[i]) };
        }

        return attr;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org