view flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugeTimeRangePanel.java @ 1602:f30919997e57

Added helper input table to state 'timerange' input in hist. discharge curves. flys-client/trunk@3940 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Mon, 06 Feb 2012 16:11:10 +0000
parents
children 6a65694bdcc2
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import com.google.gwt.core.client.GWT;

import com.smartgwt.client.types.ListGridFieldType;

import com.smartgwt.client.data.Record;

import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;

import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.client.Config;

import de.intevation.flys.client.shared.model.DataList;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.ArtifactDescription;
import de.intevation.flys.client.shared.model.Data;

import de.intevation.flys.client.client.ui.range.DischargeInfoDataSource;

/**
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class GaugeTimeRangePanel extends IntegerRangePanel {

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

    protected ListGrid yearTable;

    public GaugeTimeRangePanel() {
        GWT.log("Creating YearInputPanel");
        yearTable = new ListGrid();
        yearTable.setAutoFetchData(true);
        yearTable.setShowHeaderContextMenu(false);

    }

    public Canvas create(DataList data) {
        setDataName(data);

        VLayout root = new VLayout();

        root.addMember(createLabel(data));
        root.addMember(createForm(data));
        root.addMember(getNextButton());

        initDefaults(data);


        initYearTable();

        long gauge = getGaugeNumber();

        Config config = Config.getInstance();
        String url = config.getServerUrl();
        yearTable.setDataSource(new DischargeInfoDataSource(url, gauge));

        helperContainer.addMember(yearTable);
        return root;
    }


    protected ListGrid initYearTable() {
        String baseUrl = GWT.getHostPageBaseURL();

        yearTable.setWidth100();
        yearTable.setHeight100();
        yearTable.setShowRecordComponents(true);
        yearTable.setShowRecordComponentsByCell(true);
        yearTable.setEmptyMessage(MESSAGES.empty_filter());
        yearTable.setCanReorderFields(false);

        CellFormatter cf = new CellFormatter() {
            public String format(
                Object value,
                ListGridRecord record,
                int rowNum, int colNum) {
                    if (value == null) return null;
                    if (value.toString().equals("-1")) {
                        return "";
                    }
                    return value.toString();
            }
        };


        ListGridField addstart = new ListGridField ("", "");
        addstart.setType (ListGridFieldType.ICON);
        addstart.setWidth (20);
        addstart.setCellIcon(baseUrl + MESSAGES.markerGreen());
        addstart.addRecordClickHandler(new RecordClickHandler() {
            public void onRecordClick(RecordClickEvent e) {
                Record r = e.getRecord();
                if (r.getAttribute("start").equals("-1")) {
                    return;
                }
                else {
                    setLower(r.getAttribute("start"));
                }
            }
        });

        ListGridField addend = new ListGridField ("", "");
        addend.setType (ListGridFieldType.ICON);
        addend.setWidth (20);
        addend.setCellIcon(baseUrl + MESSAGES.markerRed());
        addend.addRecordClickHandler(new RecordClickHandler() {
            public void onRecordClick(RecordClickEvent e) {
                Record r = e.getRecord();
                if (r.getAttribute("end").equals("-1")) {
                    return;
                }
                else {
                    setUpper(r.getAttribute("end"));
                }
            }
        });

        ListGridField desc =
            new ListGridField("description", MESSAGES.description());
        desc.setType(ListGridFieldType.TEXT);
        desc.setWidth("*");

        ListGridField start =
            new ListGridField("start", MESSAGES.start_year());
        start.setType(ListGridFieldType.INTEGER);
        start.setWidth(50);
        start.setCellFormatter(cf);

        ListGridField end =
            new ListGridField("end", MESSAGES.end_year());
        end.setType(ListGridFieldType.INTEGER);
        end.setWidth(50);
        end.setCellFormatter(cf);

        yearTable.setFields(addstart, addend, desc, start, end);

        return yearTable;
    }


    protected long getGaugeNumber() {
        ArtifactDescription adescr = artifact.getArtifactDescription();
        DataList[] data = adescr.getOldData();

        String gauge = "";
        if (data != null && data.length > 0) {
            for (int i = 0; i < data.length; i++) {
                DataList dl = data[i];
                if (dl.getState().equals("state.winfo.historicalq.reference_gauge")) {
                    for (int j = 0; j < dl.size(); j++) {
                        Data d = dl.get(j);
                        DataItem[] di = d.getItems();
                        if (di != null && di.length == 1) {
                           gauge = d.getItems()[0].getStringValue();
                        }
                    }
                }
            }
        }
        try {
            return Long.parseLong(gauge);
        }
        catch (NumberFormatException nfe) {
            GWT.log("Error parsing gauge.");
            return 0;
        }
    }
}

http://dive4elements.wald.intevation.org