view flys-client/src/main/java/de/intevation/flys/client/client/ui/map/MapPrintPanel.java @ 5622:b28a6d05e969

Add a new mechanism in mapfish print call to add arbitary data maps Data properties are identified by starting with mapfish-data and they are then split in info value pairs where info can be the description of the information and value the value of the information to be transported in the data map.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 09 Apr 2013 19:04:32 +0200
parents 17e2324c760e
children 10aac4ec2d76
line wrap: on
line source
package de.intevation.flys.client.client.ui.map;

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

import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.types.Alignment;

import org.gwtopenmaps.openlayers.client.Bounds;
import org.gwtopenmaps.openlayers.client.Map;

import de.intevation.flys.client.client.Config;
import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.client.services.CollectionAttributeService;
import de.intevation.flys.client.client.services.CollectionAttributeServiceAsync;
import de.intevation.flys.client.shared.model.Collection;
import de.intevation.flys.client.shared.model.OutputSettings;
import de.intevation.flys.client.shared.model.Property;
import de.intevation.flys.client.shared.model.PropertySetting;
import de.intevation.flys.client.shared.model.Settings;

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

public class MapPrintPanel extends Canvas {

    private static final String MAPFISH_MAPTITLE = "mapfish-mapTitle";
    private static final String MAPFISH_RANGE = "mapfish-data-range";
    private static final String MAPFISH_SUBTITLE = "mapfish-data-subtitle";
    private static final String MAPFISH_STRETCH = "mapfish-data-strech";
    private static final String MAPFISH_CREATED = "mapfish-data-created";
    private static final String MAPFISH_SOURCE = "mapfish-data-source";
    private static final String MAPFISH_CREATOR = "mapfish-data-creator";
    private static final String MAPFISH_DATEPLACE = "mapfish-data-dateplace";

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

    /** CollectionAttribute Update Service. */
    protected CollectionAttributeServiceAsync updater =
        GWT.create(CollectionAttributeService.class);


    protected Collection collection;
    protected Settings settings;
    protected TextItem pageTitle = new TextItem();
    protected TextItem pageRange = new TextItem();
    protected TextItem pageSubtitle = new TextItem();
    protected TextItem pageStrech = new TextItem();
    protected TextItem pageCreated = new TextItem();
    protected TextItem pageSource = new TextItem();
    protected TextItem pageCreator = new TextItem();
    protected TextItem pageDatePlace = new TextItem();
//    protected SelectItem pageFormat = createPageFormatSelectItem();
    protected MapToolbar mapToolbar;
    protected MapPrintWindow parent;

    public MapPrintPanel(Collection collection, MapToolbar mapToolbar, MapPrintWindow parent) {
        this.collection = collection;
        this.mapToolbar = mapToolbar;
        this.parent     = parent;
        initLayout();

        this.settings = collection.getSettings("print-settings");
        if (settings == null) {
            settings = new OutputSettings();
            GWT.log("settings are empty");
        }
        else {
            List<Property> properties = settings.getSettings("default");
            for (Property prop : properties) {
                PropertySetting props = (PropertySetting)prop;
                GWT.log(props.getName() + "=" + props.getValue());
                if (props.getName().equals(MAPFISH_MAPTITLE)) {
                    this.pageTitle.setValue(props.getValue());
                }
                else if (props.getName().equals(MAPFISH_RANGE)) {
                    this.pageRange.setValue(props.getValue());
                }
                else if (props.getName().equals(MAPFISH_SUBTITLE)) {
                    this.pageSubtitle.setValue(props.getValue());
                }
                else if (props.getName().equals(MAPFISH_STRETCH)) {
                    this.pageStrech.setValue(props.getValue());
                }
                else if (props.getName().equals(MAPFISH_CREATED)) {
                    this.pageCreated.setValue(props.getValue());
                }
                else if (props.getName().equals(MAPFISH_SOURCE)) {
                    this.pageSource.setValue(props.getValue());
                }
                else if (props.getName().equals(MAPFISH_CREATOR)) {
                    this.pageCreator.setValue(props.getValue());
                }
                else if (props.getName().equals(MAPFISH_DATEPLACE)) {
                    this.pageDatePlace.setValue(props.getValue());
                } else {
                    GWT.log("Unknown Print property: " + prop.getName());
                }
            }
        }
    }

    protected void initLayout() {
        // TODO: i18n
        this.pageTitle.setTitle("Titel");
        this.pageSubtitle.setTitle("Untertitel");
        this.pageRange.setTitle("Bereich");
        this.pageStrech.setTitle("Strecke");
        this.pageCreated.setTitle("Aufgestellt");
        this.pageSource.setTitle("Datenquelle");
        this.pageCreator.setTitle("Ersteller");
        this.pageDatePlace.setTitle("Ort, Datum");

        ButtonItem printButton = createPrintButtonItem();

        printButton.setAlign(Alignment.RIGHT);

        DynamicForm df = new DynamicForm();
        df.setFields(
//               this.pageFormat,
               this.pageTitle,
               this.pageSubtitle,
               this.pageRange,
               this.pageStrech,
               this.pageCreated,
               this.pageSource,
               this.pageCreator,
               this.pageDatePlace,
               printButton);
        addChild(df);
    }

/*
 * Commented out because we only provide a layout for A4 Landscape atm

    protected SelectItem createPageFormatSelectItem() {
        LinkedHashMap values = new LinkedHashMap();
        // TODO: i18n
        values.put("A4 landscape", "DIN A4 (Querformat)");
        //values.put("A4 portrait", "DIN A4 (Hochformat)");
        //values.put("A0 portrait", "DIN A0 (Hochformat)");

        SelectItem selItem = new SelectItem();
        selItem.setTitle("Seitengröße:"); // TODO: i18n
        selItem.setValueMap(values);
        selItem.setDefaultToFirstOption(true);

        return selItem;
    }
*/
    protected ButtonItem createPrintButtonItem() {
        ButtonItem btn = new ButtonItem();
        GWT.log("Button created");
        btn.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                GWT.log("Print Button Click");
                updateCollection();
                Window.open(createPrintUrl(), "_blank", "");
                parent.destroy();
            }
        });
        btn.setTitle(MSG.print());
        return btn;
    }

    private String createPrintUrl() {
        MapOutputTab ot = (MapOutputTab)mapToolbar.getOutputTab();
        Collection collection = ot.getCollection();
        String uuid = collection.identifier();

        String mapType = collection.getOutputModes().containsKey("floodmap")
            ? "floodmap"
            : "map";

        StringBuilder url = new StringBuilder();
        url.append(GWT.getModuleBaseURL());
        url.append("map-print?");

        Map map = mapToolbar.getMap();
        Bounds bounds = map.getExtent();

        if (bounds != null) {
            try {
                url.append("minx=");
                url.append(bounds.getLowerLeftX());
                url.append("&");

                url.append("maxx=");
                url.append(bounds.getUpperRightX());
                url.append("&");

                url.append("miny=");
                url.append(bounds.getLowerLeftY());
                url.append("&");

                url.append("maxy=");
                url.append(bounds.getUpperRightY());
                url.append("&");
            }
            catch (Exception e) {
                // XXX: Ignore it. bounds.getXXX() throw
                // exceptions when bound is invalid. :-/
            }
        }

        url.append("uuid=");
        url.append(uuid);
        url.append("&maptype=");
        url.append(mapType);

        appendPrintToUrl(collection, url);

        return url.toString();
    }

    private void appendPrintToUrl(Collection collection, StringBuilder url) {
        Settings settings = collection.getSettings("print-settings");
        if (settings != null) {
            List<Property> properties = settings.getSettings("default");
            for (Property prop : properties) {
                PropertySetting props = (PropertySetting)prop;
                url.append("&");
                url.append(props.getName());
                url.append("=");
                url.append(props.getValue());
            }
        }
    }

    protected void updateCollection() {
        final Config config = Config.getInstance();
        final String loc    = config.getLocale();

        GWT.log("MapPrintPanel.updateCollection via RPC now");

        List<Property> properties = new ArrayList<Property>();
        properties.add(new PropertySetting(MAPFISH_MAPTITLE, this.pageTitle.getValueAsString()));
//        properties.add(new PropertySetting(MAPFISH_LAYOUT, this.pageFormat.getValueAsString()));
        properties.add(new PropertySetting(MAPFISH_SUBTITLE, this.pageSubtitle.getValueAsString()));
        properties.add(new PropertySetting(MAPFISH_RANGE, this.pageRange.getValueAsString()));
        properties.add(new PropertySetting(MAPFISH_STRETCH, this.pageStrech.getValueAsString()));
        properties.add(new PropertySetting(MAPFISH_CREATED, this.pageCreated.getValueAsString()));
        properties.add(new PropertySetting(MAPFISH_SOURCE, this.pageSource.getValueAsString()));
        properties.add(new PropertySetting(MAPFISH_CREATOR, this.pageCreator.getValueAsString()));
        properties.add(new PropertySetting(MAPFISH_DATEPLACE, this.pageDatePlace.getValueAsString()));
        settings.setSettings("default", properties);

        collection.addSettings("print-settings", settings);
        updater.update(collection, loc, new AsyncCallback<Collection>() {
            @Override
            public void onFailure(Throwable caught) {
                GWT.log("Could not update collection attributes.");
                SC.warn(MSG.getString(caught.getMessage()));
            }
            @Override
            public void onSuccess(Collection collection) {
                GWT.log("MapPrint: collection attributes updated");
            }
        });
    }
}

http://dive4elements.wald.intevation.org