view flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugePanel.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 44c1beb78ad1
children 436eec3be6ff
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;

import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.events.ResizedEvent;
import com.smartgwt.client.widgets.events.ResizedHandler;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.client.services.GaugeOverviewInfoService;
import de.intevation.flys.client.client.services.GaugeOverviewInfoServiceAsync;
import de.intevation.flys.client.shared.model.DataList;
import de.intevation.flys.client.shared.model.RiverInfo;

/**
 * The GaugePanel is intendet to be used within a SectionStackSection
 * It extends the VLayout by two methods to show and hide the
 * section stack section.
 *
 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
 */
public class GaugePanel extends VLayout implements ResizedHandler {

    /** SectionStackSection where this GaugePanel belongs in*/
    private SectionStackSection sectionStack;

    /** Name of the river */
    private String river;

    /** The message class that provides i18n strings.*/
    protected FLYSConstants MSG = GWT.create(FLYSConstants.class);

    protected GaugeOverviewInfoServiceAsync gaugeOverviewInfoService =
        GWT.create(GaugeOverviewInfoService.class);

    protected GaugeTree gaugetree = new GaugeTree();

    protected RiverInfoPanel riverinfopanel;

    /**
     * Creates a new VLayout with a SectionStackSection
     * The GaugePanel's SectionStackSection is hidden by default.
     * @param sectionStack The section stack section to place the VLayout in.
     */
    public GaugePanel(SectionStackSection sectionStack) {
        setOverflow(Overflow.HIDDEN);
        sectionStack.setHidden(true);
        sectionStack.setItems(this);
        this.sectionStack = sectionStack;
        setStyleName("gaugepanel");
        addResizedHandler(this);
    }

    /**
     * Sets and loads the river data if river is not the current set river
     */
    public void setRiver(String river) {
        if (!river.equals(this.river)) {
            this.river = river;
            this.refresh();
        }
    }

    /**
     * Sets the data and closes not corresponding folds in the gauge tree
     */
    public void setData(DataList[] data) {
        gaugetree.setData(data);
    }

    /**
     * Loads the river info and renders it afterwards
     */
    public void refresh() {
        gaugeOverviewInfoService.getRiverInfo(this.river, new AsyncCallback<RiverInfo>() {
            public void onFailure(Throwable e) {
                GWT.log("Could not load the river info." + e);
            }

            public void onSuccess(RiverInfo riverinfo) {
                GWT.log("Loaded river info");
                renderGaugeOverviewInfo(riverinfo);
            }
        });
    }

    public void renderGaugeOverviewInfo(RiverInfo riverinfo) {
        removeMembers(getMembers());

        riverinfopanel = new RiverInfoPanel(riverinfo);
        addMember(riverinfopanel);
        addMember(gaugetree);

        gaugetree.setGauges(riverinfo);
    }

    @Override
    public void onResized(ResizedEvent event) {
        /* this height calculation is only an approximation and doesn't reflect
         * the real height of the the gaugetree. */
        int height = getInnerContentHeight() -
            (RiverInfoPanel.HEIGHT +
            (2 * RiverInfoPanel.BORDER_WIDTH) +
            (2 * RiverInfoPanel.PADDING) +
            (2 * RiverInfoPanel.MARGIN));

        if (height < 0) {
            height = 0;
        }

        gaugetree.setHeight("" + height + "px");
    }


    /**
     * Hide the section stack section.
     */
    public void hide() {
        GWT.log("GaugePanel - hide");
        this.sectionStack.setHidden(true);
    }

    /**
     * Show the section stack section.
     */
    public void show() {
        GWT.log("GaugePanel - show");
        this.sectionStack.setHidden(false);
    }

    class RiverInfoPanel extends HorizontalPanel {

        public final static int HEIGHT = 30;
        public final static int BORDER_WIDTH = 3;
        public final static int PADDING = 8;
        public final static int MARGIN = 10;

        public RiverInfoPanel(RiverInfo riverinfo) {
            setStyleName("riverinfopanel");
            setHeight("" + HEIGHT + "px");
            setVerticalAlignment(ALIGN_MIDDLE);

            NumberFormat nf = NumberFormat.getDecimalFormat();

            addLabel(riverinfo.getName(), false);

            String kmtext = "";
            Double start = riverinfo.getKmStart();
            Double end = riverinfo.getKmEnd();

            if (!riverinfo.isKmUp()) {
                Double tmp = end;
                end = start;
                start = tmp;
            }
            if (end != null) {
                kmtext += nf.format(end);
                kmtext += " - ";
            }
            if (start != null) {
                kmtext += nf.format(start);
            }
            kmtext += " km";

            addLabel(kmtext, false);

            String qtext = "";
            Double qmin = riverinfo.getMinQ();
            Double qmax = riverinfo.getMaxQ();
            if (qmin != null) {
                qtext += nf.format(qmin);
                qtext += " " + MSG.gauge_q_unit();
                qtext += " - ";
            }
            if (qmax != null) {
                qtext += nf.format(qmax);
                qtext += " " + MSG.gauge_q_unit();
            }

            addLabel(qtext, false);

            Long number = riverinfo.getOfficialNumber();
            String url = number != null ?
                MSG.gauge_river_url() + number :
                MSG.gauge_river_url();
            Anchor anchor = new Anchor(MSG.gauge_river_info_link(), url);
            add(anchor);
        }

        private void addLabel(String text, boolean wordwrap) {
            Label label = new Label(text, wordwrap);
            add(label);
            setCellHeight(label, "" + HEIGHT + "px");
        }
    }
}

http://dive4elements.wald.intevation.org