view flys-client/src/main/java/de/intevation/flys/client/client/ui/stationinfo/GaugeListGrid.java @ 5462:f2371f3aaf03

Show top level folder icons only if node has no factory If you have an empty folder the folder icon is still shown. This makes it possible to add functional "Top Level" entries in the Datacage
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 26 Mar 2013 18:29:13 +0100
parents 0e6e44e6725f
children 76d40d257c8c
line wrap: on
line source
package de.intevation.flys.client.client.ui.stationinfo;

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

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.WidgetCanvas;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;

import de.intevation.flys.client.client.FLYS;
import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.DataList;
import de.intevation.flys.client.shared.model.GaugeInfo;
import de.intevation.flys.client.shared.model.RiverInfo;


/**
 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
 */
public class GaugeListGrid extends InfoListGrid implements RecordClickHandler {

    private static final int ABFLUSSTAFEL_COLUMN = 6;

    public GaugeListGrid(FLYS flys) {
        super(flys);
        ListGridField nfield = new ListGridField("name", "Pegel");
        ListGridField sfield = new ListGridField("kmstart", "Start [km]", 60);
        ListGridField efield = new ListGridField("kmend", "Ende [km]", 60);
        ListGridField stfield = new ListGridField("station", "Station [km]");
        ListGridField lfield = new ListGridField("link", "Info");
        lfield.setType(ListGridFieldType.LINK);
        ListGridField cfield = new ListGridField("curvelink", "Link");
        cfield.addRecordClickHandler(this);

        this.setFields(nfield, sfield, efield, stfield, lfield, cfield);
    }

    public void setRiverInfo(RiverInfo riverinfo) {
        List<GaugeInfo> gauges = riverinfo.getGauges();

        if (gauges != null && !gauges.isEmpty()) {

            ArrayList<GaugeInfo> emptygauges = new ArrayList<GaugeInfo>();

            if (!riverinfo.isKmUp()) {
                for (GaugeInfo gauge : gauges) {
                    addGauge(gauge, emptygauges);
                }
            }
            else {
                for (int i = gauges.size()-1; i >= 0; i--) {
                    GaugeInfo gauge = gauges.get(i);
                    addGauge(gauge, emptygauges);
                }
            }

            // put empty gauges to the end
            for (GaugeInfo gauge : emptygauges) {
                addGauge(gauge);
            }
        }
    }

    private void addGauge(GaugeInfo gauge, List<GaugeInfo> empty) {
        if (gauge.getKmStart() != null && gauge.getKmEnd() != null) {
            addGauge(gauge);
        }
        else {
            empty.add(gauge);
        }
    }

    private void addGauge(GaugeInfo gauge) {
        this.addData(new GaugeRecord(gauge));
    }

    public void open() {
        ArrayList<Double> locations = new ArrayList<Double>();

        if (data != null && data.length > 0) {
            for (int i = 0; i < data.length; i++) {
                DataList dl = data[i];
                String state = dl.getState();
                GWT.log("GaugeListGrid - open " + state);
                if (state.equals("state.winfo.location_distance")) {
                    Double ldfrom = null;
                    Double ldto = null;

                    for (int j = dl.size()-1; j >= 0; --j) {
                        Data d = dl.get(j);
                        String label = d.getLabel();
                        GWT.log("GaugeListGrid - setData - label " + label + " " + d.getStringValue());
                        if (label.equals("ld_from")) {
                            ldfrom = getDoubleValue(d);
                        }
                        else if (label.equals("ld_to")) {
                            ldto = getDoubleValue(d);
                        }
                        else if (label.equals("ld_locations")) {
                            getLocationsFromData(locations, d);
                            openOnLocations(locations);
                            return;
                        }
                    }
                    if (ldfrom != null) {
                        openOnDistance(ldfrom, ldto);
                        return;
                    }
                }
                else if(state.equals("state.winfo.distance_only") ||
                        state.equals("state.winfo.distance")) {
                    Double ldfrom = null;
                    Double ldto = null;

                    for (int j = dl.size()-1; j >= 0; --j) {
                        Data d = dl.get(j);
                        String label = d.getLabel();
                        GWT.log("GaugeListGrid - setData - label " + label + " " + d.getStringValue());
                        if (label.equals("ld_from")) {
                            ldfrom = getDoubleValue(d);
                        }
                        else if (label.equals("ld_to")) {
                            ldto = getDoubleValue(d);
                        }
                    }

                    if (ldfrom != null) {
                        openOnDistance(ldfrom, ldto);
                        return;
                    }
                }
                else if (state.equals("state.winfo.location")) {
                    getLocations("ld_locations", locations, dl);
                    openOnLocations(locations);
                    return;
                }
                else if (state.equals("state.winfo.reference.curve.input.start")) {
                    getLocations("reference_startpoint", locations, dl);
                }
                else if (state.equals("state.winfo.reference.curve.input.end")) {
                    getLocations("reference_endpoint", locations, dl);
                }
                else if (state.equals("state.winfo.historicalq.reference_gauge")) {
                    for (int j = dl.size()-1; j >= 0; --j) {
                        Data d = dl.get(j);
                        String label = d.getLabel();
                        if (label.equals("reference_gauge")) {
                            String tmp = d.getStringValue();
                            if (tmp != null) {
                                Long gaugereference = Long.valueOf(tmp);
                                if (gaugereference != null) {
                                    openOnReference(gaugereference);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!locations.isEmpty()) {
            openOnLocations(locations);
        }
        else {
            openAll();
        }
    }

    void getLocations(String labelname, List<Double> locations, DataList dl) {
        for (int j = dl.size()-1; j >= 0; --j) {
            Data d = dl.get(j);
            String label = d.getLabel();
            if (label.equals(labelname)) {
                getLocationsFromData(locations, d);
            }
        }
    }

    void getLocationsFromData(List<Double> locations, Data data) {
        DataItem[] items = data.getItems();
        for (int k = 0; k < items.length; k++) {
            String tmp = items[k].getStringValue();
            GWT.log("GaugeListGrid - getLocationsFromData " + tmp);
            if (tmp != null) {
                if (tmp.contains(" ")) {
                    // string contains several values ...
                    String[] values = tmp.split(" ");
                    for(int i=0; i < values.length; i++) {
                        Double value = Double.valueOf(values[i]);
                        if (value != null) {
                            locations.add(value);
                        }
                    }
                }
                else {
                    Double value = Double.valueOf(tmp);
                    if (value != null) {
                        locations.add(value);
                    }
                }
            }
        }
    }

    public void openOnReference(Long number) {
        GWT.log("GaugeListGrid - openOnReference " + number);
        for (ListGridRecord record: this.getRecords()) {
            GaugeRecord item = (GaugeRecord)record;
            if (item.getOfficialNumber().equals(number)) {
                expandRecord(item);
            }
            else {
                collapseRecord(item);
            }
        }
    }

    public void openOnDistance(Double start, Double end) {
        GWT.log("GaugeListGrid - openOnDistance " + start + " " + end);

        for (ListGridRecord record: this.getRecords()) {
            GaugeRecord item = (GaugeRecord)record;
            if (end == null && item.getKmStart() != null) {
                if (item.getKmStart() >= start) {
                    expandRecord(item);
                }
                else {
                    collapseRecord(item);
                }
            }
            else if (item.getKmStart() != null && item.getKmEnd() != null) {
                // as getStart()/getEnd() return Double objects, they can be null and
                // can cause NPEs when comparing with double... strange...
                GWT.log("GaugeListGrid - openOnDistance item " + item.getKmStart() + " " + item.getKmEnd());
                if ((start >= item.getKmStart() && start <= item.getKmEnd()) ||
                      (end >= item.getKmStart() &&   end <= item.getKmEnd()) ||
                    (start <= item.getKmStart() &&   end >= item.getKmEnd())) {
                    expandRecord(item);
                }
                else {
                    collapseRecord(item);
                }
            }
            else {
                collapseRecord(item);
            }
        }
    }

    /**
     * Open Gauge entry if a location fits to the gauge.
     */
    public void openOnLocations(List<Double> locations) {
        GWT.log("GaugeListGrid - openOnLocations " + locations);

        if (locations == null || locations.isEmpty()) {
            return;
        }

        for (ListGridRecord record: this.getRecords()) {
            GaugeRecord item = (GaugeRecord)record;
            boolean isset = false;
            for (Double location: locations) {
                if (locations == null) {
                    continue;
                }

                Double start = item.getKmStart();
                Double end   = item.getKmEnd();
                if (start == null || end == null) {
                    // should not occur but avoid NullPointerException
                    continue;
                }

                if (location >= start && location <= end) {
                    isset = true;
                    break;
                }
            }
            if (isset) {
                expandRecord(item);
            }
            else {
                collapseRecord(item);
            }
        }
    }

    @Override
    protected Canvas getExpandPanel(ListGridRecord record) {
        GaugeRecord item = (GaugeRecord)record;
        return new WidgetCanvas(new GaugeInfoPanel(item, flys));
    }

    @Override
    public void onRecordClick(RecordClickEvent event) {
        GaugeRecord gauge = (GaugeRecord)event.getRecord();
        flys.newGaugeDischargeCurve(gauge.getRiverName(),
                gauge.getOfficialNumber());
    }

    @Override
    public String getCellCSSText(ListGridRecord record, int rowNum,
            int colNum) {
        if (colNum == ABFLUSSTAFEL_COLUMN) {
            // display the ablfusstafel cell like a link
            return "text-decoration: underline; color: #0000EE; cursor: pointer;";
        }
        else {
            return super.getCellCSSText(record, rowNum, colNum);
        }
    }
}

http://dive4elements.wald.intevation.org