view flys-client/src/main/java/de/intevation/flys/client/client/ui/stationinfo/GaugeListGrid.java @ 4961:27b4d5d20dc8

Render text in Abflusstafel/-kurve cell like a "normal" html link
author Björn Ricks <bjoern.ricks@intevation.de>
date Mon, 04 Feb 2013 13:49:15 +0100
parents a3a59055ed5e
children 6f6461e07854
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.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Grid;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
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 com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

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));
    }


    class GaugeInfoHead extends HLayout {

        public GaugeInfoHead(FLYS flys, GaugeInfo gauge) {
            setStyleName("gaugeinfohead");
            setAutoHeight();
            setAutoWidth();

            NumberFormat nf = NumberFormat.getDecimalFormat();

            Label label = new Label(gauge.getName());
            addMember(label);

            Double start;
            Double end;

            if (!gauge.isKmUp()) {
                start = gauge.getKmStart();
                end   = gauge.getKmEnd();
            }
            else {
                start = gauge.getKmEnd();
                end   = gauge.getKmStart();
            }

            String kmtext = "";
            if (start != null) {
                kmtext += nf.format(start);
                kmtext += " - ";
            }
            if (end != null) {
                kmtext += nf.format(end);
            }
            if (start != null || end != null) {
                kmtext += " km";
            }

            label = new Label(kmtext);

            addMember(label);

            Double station = gauge.getStation();
            if (station != null) {
                String stext = nf.format(station);
                stext += " km";
                label = new Label(stext);
                addMember(label);
            }

            Long number = gauge.getOfficialNumber();
            String url = number != null ?
                MSG.gauge_url() + number :
                MSG.gauge_url();
            Anchor anchor = new Anchor(MSG.gauge_info_link(), url, "_blank");
            addMember(anchor);

            addMember(new GaugeCurveAnchor(flys, gauge));
        }
    }

    class GaugeCurveAnchor extends Anchor implements ClickHandler {

        private FLYS flys;
        private GaugeInfo gauge;

        public GaugeCurveAnchor(FLYS flys, GaugeInfo gauge) {
            super(MSG.gauge_curve_link());
            this.flys = flys;
            this.gauge = gauge;

            addClickHandler(this);
        }

        @Override
        public void onClick(ClickEvent ev) {
            GWT.log("GaugeCurveAnchor - onClick " + gauge.getRiverName() +
                    " " + gauge.getOfficialNumber());
            flys.newGaugeDischargeCurve(gauge.getRiverName(),
                    gauge.getOfficialNumber());
        }
    }

    class GaugeInfoPanel extends VLayout {

        public GaugeInfoPanel(GaugeInfo gauge) {
            setStyleName("gaugeinfopanel");
            setWidth100();

            Grid grid = new Grid(4, 2);

            NumberFormat nf = NumberFormat.getDecimalFormat();

            Double minw = gauge.getMinW();
            Double maxw = gauge.getMaxW();
            if (minw != null && maxw != null) {
                grid.setText(0, 0, MSG.wq_value_q());
                grid.setText(0, 1, nf.format(minw) +
                        " - " + nf.format(maxw));
            }

            Double minq = gauge.getMinQ();
            Double maxq = gauge.getMaxQ();
            if (minq != null && maxq != null) {
                grid.setText(1, 0, MSG.wq_value_w());
                grid.setText(1, 1, nf.format(minq) +
                        " - " + nf.format(maxq));
            }

            Double aeo = gauge.getAeo();
            if (aeo != null) {
                grid.setText(2, 0, "AEO [km²]");
                grid.setText(2, 1, nf.format(aeo));
            }

            Double datum = gauge.getDatum();
            if (datum != null) {
                grid.setText(3, 0, MSG.gauge_zero() + " [" +
                        gauge.getWstUnit() + "]");
                grid.setText(3, 1, nf.format(datum));
            }

            addMember(grid);
        }
    }

    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));
    }

    @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