view flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugePanel.java @ 4204:9f9843491ac4

Only create the GaugeTree and RiverInfoPanel once This fixes resizing issues when changing the river. SmartGWT is not able to recalculate the height and width if a GWT widget is inserted later. Every attempt to call a recalculation manually did fail. Fix for flys/issue925 (W-INFO / Pegel-Info-Modul / Zeile Gewässer-INFOS)
author Björn Ricks <bjoern.ricks@intevation.de>
date Mon, 22 Oct 2012 15:29:56 +0200
parents de795c13dcfb
children 0dd8963cec9c
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.Canvas;
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.FLYS;
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 intended 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;
    protected Canvas gaugetreecanvas;

    protected RiverInfoPanel riverinfopanel;

    /**
     * Creates a new VLayout with a SectionStackSection
     * The GaugePanel's SectionStackSection is hidden by default.
     *
     * @param flys The FLYS object
     * @param sectionStack The section stack section to place the VLayout in.
     */
    public GaugePanel(FLYS flys, SectionStackSection sectionStack) {
        gaugetree = new GaugeTree(flys);
        gaugetreecanvas = new Canvas();
        gaugetreecanvas.addChild(gaugetree);

        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>() {
            @Override
            public void onFailure(Throwable e) {
                GWT.log("Could not load the river info." + e);
            }

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

    public void renderGaugeOverviewInfo(RiverInfo riverinfo) {
        gaugetree.setGauges(riverinfo);

        if (riverinfopanel == null) {
            removeMembers(getMembers());
            riverinfopanel = new RiverInfoPanel(riverinfo);

            gaugetreecanvas.setWidth("100%");

            addMember(riverinfopanel);
            addMember(gaugetreecanvas);
        }
        else {
            riverinfopanel.setRiverInfo(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.getStaticHeight();

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

        GWT.log("GaugePanel - onResize " + height);

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

        for (Canvas canvas : getMembers()) {
            GWT.log("GaugePanel - member height " + canvas.getHeight());
        }
    }


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

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

http://dive4elements.wald.intevation.org