view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/NaviChartOutputTab.java @ 9099:850ce16034e9

2.3.4.1.10 Berechnung mit Start-km > End-km
author gernotbelger
date Mon, 28 May 2018 13:22:45 +0200
parents 02739b8c010d
children abf14917be32
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.chart;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.dive4elements.river.client.client.Config;
import org.dive4elements.river.client.client.ui.CollectionView;
import org.dive4elements.river.client.shared.model.AbstractFixBunduArtifact;
import org.dive4elements.river.client.shared.model.Artifact;
import org.dive4elements.river.client.shared.model.Collection;
import org.dive4elements.river.client.shared.model.FixFilter;
import org.dive4elements.river.client.shared.model.OutputMode;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;

/**
 * Tab representing and showing one Chart-output with a "navi" thing.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class NaviChartOutputTab extends ChartOutputTab implements TabSelectedHandler {
    protected TextItem currentkm;

    public NaviChartOutputTab(final String title, final Collection collection, final OutputMode mode, final CollectionView collectionView) {
        super(title, collection, mode, collectionView);
        this.right.removeChild(this.chart);
        this.right.addChild(createNaviChart());
        collectionView.registerTabHandler(this);
    }

    protected Canvas createNaviChart() {
        final Artifact art = this.collectionView.getArtifact();
        final VLayout root = new VLayout();
        root.setWidth100();
        root.setHeight100();

        final HLayout layout = new HLayout();
        layout.setAlign(Alignment.CENTER);

        final DynamicForm form = new DynamicForm();
        final Button lower = new Button("<<");
        lower.setWidth(30);
        final Button upper = new Button(">>");
        upper.setWidth(30);
        this.currentkm = new TextItem();
        this.currentkm.setWidth(60);
        this.currentkm.setShowTitle(false);

        form.setFields(this.currentkm);
        form.setWidth(60);

        double fromKm;
        double toKm;

        if (art instanceof AbstractFixBunduArtifact) {
            final AbstractFixBunduArtifact fix = (AbstractFixBunduArtifact) art;
            final FixFilter fixFilter = fix.getFilter();
            final String s = fix.getArtifactDescription().getDataValueAsString("ld_step");
            try {
                final double ds = Double.parseDouble(s);
                this.collectionView.setSteps(ds);
            }
            catch (final NumberFormatException nfe) {
                this.collectionView.setSteps(100d);
            }
            fromKm = fixFilter.getLowerKm();
            toKm = fixFilter.getUpperKm();
        } else {
            // Probably WINFOArtifact kind of artifact.
            final String ld_step = art.getArtifactDescription().getDataValueAsString("ld_step");
            try {
                this.collectionView.setSteps(Double.valueOf(ld_step));
            }
            catch (final Exception e) {
                GWT.log("No ld_steps data or not parsable.");
                return root;
            }

            final double[] kmRange = art.getArtifactDescription().getKMRange();
            if (kmRange == null || kmRange.length == 2) {
                fromKm = kmRange[0];
                toKm = kmRange[1];
            } else {
                GWT.log("No KM range in description found.");
                return root;
            }
        }

        this.collectionView.setMinKm(fromKm);
        this.collectionView.setMaxKm(toKm);

        final NumberFormat nf = NumberFormat.getDecimalFormat();

        // Always jump to the from km when initialized.
        try {
            final double d = Double.valueOf(fromKm);
            this.currentkm.setValue(nf.format(d));
        }
        catch (final NumberFormatException e) {
            this.currentkm.setValue(fromKm);
        }
        this.collectionView.setCurrentKm(fromKm);

        lower.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(final ClickEvent ce) {
                NaviChartOutputTab.this.tbarPanel.deselectControls();
                updateChartDown();
                try {
                    final double d = Double.valueOf(NaviChartOutputTab.this.collectionView.getCurrentKm());
                    NaviChartOutputTab.this.currentkm.setValue(nf.format(d));
                    NaviChartOutputTab.this.tbarPanel.onZoom(null);
                }
                catch (final NumberFormatException e) {
                    NaviChartOutputTab.this.currentkm.setValue(NaviChartOutputTab.this.collectionView.getCurrentKm());
                }
            }
        });

        upper.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(final ClickEvent ce) {
                NaviChartOutputTab.this.tbarPanel.deselectControls();
                updateChartUp();
                try {
                    final double d = Double.valueOf(NaviChartOutputTab.this.collectionView.getCurrentKm());
                    NaviChartOutputTab.this.currentkm.setValue(nf.format(d));
                    NaviChartOutputTab.this.tbarPanel.onZoom(null);
                }
                catch (final NumberFormatException e) {
                    NaviChartOutputTab.this.currentkm.setValue(NaviChartOutputTab.this.collectionView.getCurrentKm());
                }
            }
        });

        this.currentkm.addKeyPressHandler(new KeyPressHandler() {
            @Override
            public void onKeyPress(final KeyPressEvent kpe) {
                if (!kpe.getKeyName().equals("Enter")) {
                    return;
                }
                if (kpe.getItem().getValue() != null) {
                    NaviChartOutputTab.this.tbarPanel.deselectControls();
                    try {
                        final String s = kpe.getItem().getValue().toString();
                        double d;
                        try {
                            d = nf.parse(s);
                            NaviChartOutputTab.this.currentkm.setValue(nf.format(d));
                        }
                        catch (final NumberFormatException e) {
                            d = -1d;
                        }
                        if (d <= NaviChartOutputTab.this.collectionView.getMaxKm() && d >= NaviChartOutputTab.this.collectionView.getMinKm()) {
                            NaviChartOutputTab.this.collectionView.setCurrentKm(d);
                            NaviChartOutputTab.this.tbarPanel.updateLinks();
                            NaviChartOutputTab.this.tbarPanel.onZoom(null);
                            if (NaviChartOutputTab.this.right != null) {
                                updateChartPanel();
                                updateChartInfo();
                            }
                        }
                    }
                    catch (final NumberFormatException nfe) {
                        // Do nothing.
                    }
                }
            }
        });
        layout.addMember(lower);
        layout.addMember(form);
        layout.addMember(upper);

        root.addMember(this.chart);
        root.addMember(layout);
        return root;
    }

    /**
     * Callback when km-up-button is clicked.
     * Increases collectionViews KM and refreshes view.
     */
    protected void updateChartUp() {
        final double currentKm = this.collectionView.getCurrentKm();
        if (currentKm < this.collectionView.getMaxKm()) {
            // Why this math?
            double newVal = currentKm * 100;
            newVal += (this.collectionView.getSteps() / 10);
            this.collectionView.setCurrentKm((double) Math.round(newVal) / 100);
            this.tbarPanel.updateLinks();
            updateChartPanel();
            updateChartInfo();
        }
    }

    /**
     * Callback when km-down-button is clicked.
     * Decreases collectionViews KM and refreshes view.
     */
    protected void updateChartDown() {
        final double currentKm = this.collectionView.getCurrentKm();
        if (currentKm > this.collectionView.getMinKm()) {
            // Why this math?
            double newVal = currentKm * 100;
            newVal -= (this.collectionView.getSteps() / 10);
            this.collectionView.setCurrentKm((double) Math.round(newVal) / 100);
            this.tbarPanel.updateLinks();
            updateChartPanel();
            updateChartInfo();
        }

    }

    /**
     * Returns the existing chart panel.
     *
     * @return the existing chart panel.
     */
    @Override
    public Canvas getChartPanel() {
        return this.chart;
    }

    /**
     * Builds the URL that points to the chart image.
     *
     * @param width
     *            The width of the requested chart.
     * @param height
     *            The height of the requested chart.
     * @param xr
     *            Optional x range (used for zooming).
     * @param yr
     *            Optional y range (used for zooming).
     *
     * @return the URL to the chart image.
     */
    @Override
    protected String getImgUrl(final int width, final int height) {
        final Config config = Config.getInstance();

        String imgUrl = GWT.getModuleBaseURL();
        imgUrl += "chart";
        imgUrl += "?uuid=" + this.collection.identifier();
        imgUrl += "&type=" + this.mode.getName();
        imgUrl += "&locale=" + config.getLocale();
        imgUrl += "&timestamp=" + new Date().getTime();
        imgUrl += "&width=" + Integer.toString(width);
        imgUrl += "&height=" + Integer.toString(height - 40);

        final Number[] zoom = getZoomValues();

        if (zoom != null) {
            if (zoom[0].intValue() != 0 || zoom[1].intValue() != 1) {
                // a zoom range of 0-1 means displaying the whole range. In such
                // case we don't need to zoom.
                imgUrl += "&minx=" + zoom[0];
                imgUrl += "&maxx=" + zoom[1];
            }

            if (zoom[2].intValue() != 0 || zoom[3].intValue() != 1) {
                // a zoom range of 0-1 means displaying the whole range. In such
                // case we don't need to zoom.
                imgUrl += "&miny=" + zoom[2];
                imgUrl += "&maxy=" + zoom[3];
            }
        }

        if (this.collectionView.getArtifact() instanceof AbstractFixBunduArtifact) {
            if (this.collectionView.getCurrentKm() == -1) {
                final AbstractFixBunduArtifact fix = (AbstractFixBunduArtifact) this.collectionView.getArtifact();
                this.collectionView.setCurrentKm(fix.getFilter().getLowerKm());
            }
        } else if (this.collectionView.getCurrentKm() == -1) {
            this.collectionView.setCurrentKm(this.collectionView.getArtifact().getArtifactDescription().getKMRange()[0]);
        }
        if (this.collectionView.getCurrentKm() != -1) {
            imgUrl += "&currentKm=" + this.collectionView.getCurrentKm();
        }

        return imgUrl;
    }

    @Override
    public void onTabSelected(final TabSelectedEvent tse) {
        if (this.equals(tse.getTab())) {
            updateChartPanel();
            updateChartInfo();
            this.currentkm.setValue(this.collectionView.getCurrentKm());
        }
    }

    @Override
    public Map<String, String> getChartAttributes() {
        Map<String, String> attr = new HashMap<String, String>();

        attr = super.getChartAttributes();
        attr.put("km", String.valueOf(this.collectionView.getCurrentKm()));

        return attr;
    }

    /**
     * In contrast to supers implementation, include the currently selected
     * km in the url.
     */
    @Override
    public String getExportUrl(final int width, final int height, final String format) {
        String url = super.getExportUrl(width, height, format);
        if (this.collectionView.getCurrentKm() != -1) {
            url += "&currentKm=" + this.collectionView.getCurrentKm();
        }
        return url;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org