view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/ExportPanel.java @ 9190:0633f963c5be

uinfo.inundationduration workflow
author gernotbelger
date Thu, 28 Jun 2018 15:10:04 +0200
parents fc42d82aa71c
children a4121ec450d6
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.client.client.ui;

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

import org.dive4elements.river.client.client.Config;
import org.dive4elements.river.client.client.FLYSConstants;
import org.dive4elements.river.client.shared.model.Collection;
import org.dive4elements.river.client.shared.model.ExportMode;
import org.dive4elements.river.client.shared.model.Facet;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * A panel that displays an download icon for all available export modes of a
 * Collection.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ExportPanel extends VLayout {

    private static final String LABEL_URL_SEPARATOR = ";";// always sync with server (InundationDurationState)
    /** The message class that provides i18n strings. */
    protected FLYSConstants MSG = GWT.create(FLYSConstants.class);

    protected Collection c;
    protected List<ExportMode> exports;

    /** This layout will store a list of available export types. */
    protected HLayout container;

    public ExportPanel(final Collection c, final List<ExportMode> exports) {
        super();

        this.c = c;
        this.exports = exports;
        this.container = new HLayout();

        final Label title = new Label(this.MSG.dataexport());
        title.setHeight(15);
        title.setStyleName("fontNormalSmallUnderlined");

        addMember(title);
        addMember(createExportItems());
        addMember(createVlayoutExportItems());
        setHeight(45);
        setMembersMargin(5);
    }

    private VLayout createVlayoutExportItems() {
        final VLayout layout = new VLayout();
        final DynamicForm form1 = new DynamicForm();
        form1.setColWidths("10%");
        form1.setWidth(400);
        form1.setAlign(Alignment.LEFT);
        final List<FormItem> items = new ArrayList<FormItem>();
        for (final ExportMode mode : this.exports) {
            final String name = mode.getName();
            final List<Facet> facets = mode.getFacets();

            for (int i = 0; i < facets.size(); i++) {
                final Facet facet = facets.get(i);
                final String facetName = facet.getName();

                if (!facetName.equals("wms_url"))
                    continue;
                if ("wms_url".equals(facetName))
                    items.add(createUrlLine(facet.getDescription(), i));
            }
        }
        form1.setFields(items.toArray(new FormItem[items.size()]));
        layout.addMember(form1);
        return layout;
    }

    private FormItem createUrlLine(final String labelAndUrl, final int count) {

        final String[] labelAndurlSplit = labelAndUrl.split(LABEL_URL_SEPARATOR);
        if (labelAndurlSplit.length == 2) {

            final TextAreaItem label = new TextAreaItem();
            label.setTitle(labelAndurlSplit[0]);

            label.setWrapTitle(true);
            label.setTitleColSpan(3);
            label.setColSpan(7);
            label.setWidth(250);

            label.setTitleAlign(Alignment.LEFT);

            label.setValue(labelAndurlSplit[1]);
            label.setCanEdit(false);

            return label;
        }
        return null;
    }

    /**
     * This method is used to create an item (created by createExportButton) for
     * each facet for each export mode.
     *
     * @return a horizontal list of buttons.
     */
    protected HLayout createExportItems() {
        final HLayout layout = new HLayout();

        for (final ExportMode mode : this.exports) {
            final String name = mode.getName();
            final List<Facet> facets = mode.getFacets();

            for (final Facet facet : facets) {
                final String facetName = facet.getName();

                if (name.equals("fix_wq_curve_at_export"))
                    continue;

                String filename = name;
                if (name.equals("computed_dischargecurve_at_export"))
                    filename = "dischargecurve";

                if ("wms_url".equals(facetName))
                    continue;

                layout.addMember(createExportButton(name, facetName, filename));
            }
        }

        return layout;
    }

    /**
     * This method is used to create a button (with click handler) for a
     * concrete export mode / type.
     *
     * @param name
     *            The name of the export.
     * @param facet
     *            The name of the export type (e.g. CSV, WST).
     *
     * @return an image with click handler.
     */
    protected Canvas createExportButton(final String name, final String facet, final String filename) {
        String url = getExportUrl(name, facet, filename);
        String imgUrl = GWT.getHostPageBaseURL();
        if (facet.equals("pdf")) {
            imgUrl += this.MSG.downloadPDF();
        } else if (facet.equals("at")) {
            imgUrl += this.MSG.downloadAT();
        } else if (facet.equals("wst")) {
            imgUrl += this.MSG.downloadWST();
        } else if (facet.equals("csv")) {
            url += "&encoding=UTF-8";
            imgUrl += this.MSG.downloadCSV();
        } else {
            imgUrl += this.MSG.imageSave();
        }
        final ImgLink link = new ImgLink(imgUrl, url, 30, 30);
        link.setTooltip(getTooltipText(name, facet));

        return link;
    }

    /**
     * Creates the URL used to trigger an export.
     *
     * @param name
     *            The name of the export.
     * @param facet
     *            The name of the export type (e.g. CSV, WST).
     *
     * @return the export URL.
     */
    protected String getExportUrl(final String name, final String facet, final String filename) {
        final Config config = Config.getInstance();

        String url = GWT.getModuleBaseURL();
        url += "export";
        url += "?uuid=" + this.c.identifier();
        url += "&name=" + filename;
        url += "&mode=" + name;
        url += "&type=" + facet;
        url += "&server=" + config.getServerUrl();
        url += "&locale=" + config.getLocale();

        return url;
    }

    /**
     * Creates a text used as tooltip for a specific export and type.
     *
     * @param name
     *            The name of the export.
     * @param facet
     *            The name of the export type (e.g. CSV, WST).
     *
     * @return a tooltip text.
     */
    protected String getTooltipText(final String name, final String facet) {
        try {
            return this.MSG.getString(name) + " | " + this.MSG.getString(facet);
        }
        catch (final MissingResourceException mre) {
            return name + " | " + facet;
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org