view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DistancePanelInputHelper.java @ 9237:972e10522ed6

salix.supraregional ui
author gernotbelger
date Tue, 10 Jul 2018 11:24:12 +0200
parents
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.client.client.ui;

import java.util.LinkedHashMap;

import org.dive4elements.river.client.client.Config;
import org.dive4elements.river.client.client.FLYSConstants;
import org.dive4elements.river.client.client.event.FilterHandler;
import org.dive4elements.river.client.client.event.RangeFilterEvent;
import org.dive4elements.river.client.client.event.StringFilterEvent;
import org.dive4elements.river.client.client.ui.range.DistanceInfoDataSource;
import org.dive4elements.river.client.client.ui.range.LocationsTable;
import org.dive4elements.river.client.client.ui.range.RangeTable;

import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Criterion;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;

/** Panel to allow input of distance for calculation range. */
public class DistancePanelInputHelper implements FilterHandler {

    private RangeTable distancesTable;
    private LocationsTable locationsTable;
    private TabSet tabs;
    private StaticTextItem filterResultCount;
    private TableFilter filterDescription;
    private RangeTableFilter filterRange;
    private ListGrid currentFiltered;
    protected final VLayout helperContainer;
    private final FLYSConstants MSG;
    private final String river;

    public DistancePanelInputHelper(final FLYSConstants MSG, final VLayout helperContainer, final String river) {
        this.MSG = MSG;
        this.helperContainer = helperContainer;
        this.river = river;

        initHelperPanel(); // copy from DistancePanel
    }

    protected void initHelperPanel() {
        this.distancesTable = new RangeTable();
        this.locationsTable = new LocationsTable();

        final Config config = Config.getInstance();
        final String url = config.getServerUrl();
        // final String river = getRiverName();

        this.distancesTable.setAutoFetchData(true);
        this.locationsTable.setAutoFetchData(true);
        this.distancesTable.setDataSource(new DistanceInfoDataSource(url, this.river, "distances"));
        this.locationsTable.setDataSource(new DistanceInfoDataSource(url, this.river, "locations"));

        // recordClickHandlers were here

        this.tabs = new TabSet();
        this.tabs.setWidth100();
        this.tabs.setHeight100();

        final Tab locations = new Tab(this.MSG.locations());
        final Tab distances = new Tab(this.MSG.distance());

        locations.setPane(this.locationsTable);
        distances.setPane(this.distancesTable);

        this.tabs.addTab(locations, 0);
        this.tabs.addTab(distances, 1);

        this.filterResultCount = new StaticTextItem(this.MSG.resultCount());
        this.filterResultCount.setTitleAlign(Alignment.LEFT);
        this.filterResultCount.setTitleStyle("color: #000");

        this.filterDescription = new TableFilter();
        this.filterDescription.setHeight("30px");
        this.filterDescription.addFilterHandler(this);

        this.filterRange = new RangeTableFilter();
        this.filterRange.setHeight("30px");
        this.filterRange.addFilterHandler(this);
        this.filterRange.setVisible(false);

        final SelectItem filterCriteria = new SelectItem();
        filterCriteria.setShowTitle(false);
        filterCriteria.setWidth(100);
        filterCriteria.addChangedHandler(new ChangedHandler() {
            @Override
            public void onChanged(final ChangedEvent e) {
                if (e.getValue().toString().equals("range")) {
                    DistancePanelInputHelper.this.filterRange.setVisible(true);
                    DistancePanelInputHelper.this.filterDescription.setVisible(false);
                    DistancePanelInputHelper.this.filterDescription.clear();
                } else {
                    DistancePanelInputHelper.this.filterRange.setVisible(false);
                    DistancePanelInputHelper.this.filterRange.clear();
                    DistancePanelInputHelper.this.filterDescription.setVisible(true);
                }
            }
        });

        final LinkedHashMap<String, String> filterMap = new LinkedHashMap<String, String>();
        filterMap.put("description", this.MSG.description());
        filterMap.put("range", this.MSG.range());
        filterCriteria.setValueMap(filterMap);
        filterCriteria.setValue("description");

        final DynamicForm form = new DynamicForm();
        form.setFields(filterCriteria);

        final DynamicForm form2 = new DynamicForm();
        form2.setFields(this.filterResultCount);

        final HLayout filterLayout = new HLayout();
        filterLayout.addMember(form);
        filterLayout.addMember(this.filterDescription);
        filterLayout.addMember(this.filterRange);
        filterLayout.setHeight(30);
        this.tabs.addTabSelectedHandler(new TabSelectedHandler() {
            @Override
            public void onTabSelected(final TabSelectedEvent evt) {
                DistancePanelInputHelper.this.filterDescription.clear();
                DistancePanelInputHelper.this.filterRange.clear();
                DistancePanelInputHelper.this.filterResultCount.setValue("");

                final Canvas c = evt.getTabPane();
                if (c instanceof ListGrid) {
                    DistancePanelInputHelper.this.currentFiltered = (ListGrid) c;
                }
            }
        });

        this.helperContainer.addMember(this.tabs);
        this.helperContainer.addMember(filterLayout);
        this.helperContainer.addMember(form2);
    }

    @Override
    public void onFilterCriteriaChanged(final StringFilterEvent event) {
        final String search = event.getFilter();

        if (search != null && search.length() > 0) {
            final Criteria c = new Criteria("description", search);
            this.locationsTable.filterData(c);
            this.distancesTable.filterData(c);
            this.filterResultCount.setValue(this.currentFiltered.getRecords().length);
        } else {
            this.locationsTable.clearCriteria();
            this.distancesTable.clearCriteria();
            this.filterResultCount.setValue("");
        }
    }

    @Override
    public void onFilterCriteriaChanged(final RangeFilterEvent event) {
        final Float from = event.getFrom() - 0.001f;
        final Float to = event.getTo() + 0.001f;

        Criterion combinedFilter = null;
        Criterion locationFilter = null;

        if (from.equals(Float.NaN) && to.equals(Float.NaN)) {
            this.locationsTable.clearCriteria();
            this.distancesTable.clearCriteria();
            this.filterResultCount.setValue("");
            return;
        }

        if (from.equals(Float.NaN)) {
            combinedFilter = new Criterion("to", OperatorId.LESS_OR_EQUAL, to);

            locationFilter = new Criterion("from", OperatorId.LESS_OR_EQUAL, to);

            this.locationsTable.filterData(locationFilter);
            this.distancesTable.filterData(combinedFilter);
            this.filterResultCount.setValue(this.currentFiltered.getRecords().length);
            return;
        }

        if (to.equals(Float.NaN)) {
            combinedFilter = new Criterion("from", OperatorId.GREATER_OR_EQUAL, from);
        } else {
            final AdvancedCriteria c1 = new AdvancedCriteria(OperatorId.AND,
                    new Criterion[] { new Criterion("from", OperatorId.GREATER_OR_EQUAL, from), new Criterion("from", OperatorId.LESS_OR_EQUAL, to) });

            final AdvancedCriteria c2 = new AdvancedCriteria(OperatorId.AND,
                    new Criterion[] { new Criterion("to", OperatorId.GREATER_OR_EQUAL, from), new Criterion("to", OperatorId.LESS_OR_EQUAL, to) });

            final AdvancedCriteria c3 = new AdvancedCriteria(OperatorId.AND,
                    new Criterion[] { new Criterion("from", OperatorId.LESS_OR_EQUAL, to), new Criterion("to", OperatorId.GREATER_OR_EQUAL, from) });

            combinedFilter = new AdvancedCriteria(OperatorId.OR, new Criterion[] { c1, c2, c3 });
        }
        this.locationsTable.filterData(combinedFilter);
        this.distancesTable.filterData(combinedFilter);
        this.filterResultCount.setValue(this.currentFiltered.getRecords().length);

    }

    public ListGrid getDistancesTable() {
        return this.distancesTable;
    }

    public ListGrid getLocationsTable() {
        return this.locationsTable;
    }

}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org