view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DatacagePairWidget.java @ 9416:05405292a7ca

Navigationtheme panel now shows themes of dWt and WQ charts grayed out, if the current station is outside the valid range of the theme.
author gernotbelger
date Thu, 16 Aug 2018 16:28:03 +0200
parents 84397da33d17
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 org.dive4elements.river.client.client.FLYSConstants;
import org.dive4elements.river.client.shared.model.ToLoad;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Widget showing two Datacages and a add-this-button.
 * Insert a record into a listgrid when add-this-button clicked.
 */
public class DatacagePairWidget extends VLayout {

    /**
     * Allows for advanced controlling of the behavior of this pair widget.
     */
    public static interface IDatacagePairControler {
        void setup(DatacageWidget leftWidget, DatacageWidget rightWidget, HLayout toolbarLayout);
    }

    /**
     * {@link IDatacagePairControler} implementation that does nothing.
     */
    public static final IDatacagePairControler NIL_CONTROLER = new IDatacagePairControler() {
        @Override
        public void setup(final DatacageWidget leftWidget, final DatacageWidget rightWidget, final HLayout toolbarLayout) {
            // does nothing
        }
    };

    /** i18n resource. */
    private static final FLYSConstants MSG = GWT.create(FLYSConstants.class);

    /** The "remote" ListGrid to insert data to when add-button is clicked. */
    private final ListGrid grid;

    /** First (upper) DataCage Grid. */
    private final DatacageWidget firstDatacageWidget;

    /** Second (lower) DataCage Grid. */
    private final DatacageWidget secondDatacageWidget;

    /** Max number of recommendation pairs to be added */
    private final int maxCount;

    /** errorMsg maxCount */
    private final String msgMaxCount;

    public DatacagePairWidget(final IDatacagePairControler controler, final DatacageWidgetData leftData, final DatacageWidgetData rightData,
            final ListGrid grid, final int maxCount, final String msgMaxCount) {

        this.msgMaxCount = msgMaxCount;
        this.maxCount = maxCount;
        this.grid = grid;

        final HLayout hLayout = new HLayout();

        this.firstDatacageWidget = new DatacageWidget(leftData);
        this.secondDatacageWidget = new DatacageWidget(rightData);

        this.firstDatacageWidget.setIsMutliSelectable(false);
        this.secondDatacageWidget.setIsMutliSelectable(false);

        hLayout.addMember(this.firstDatacageWidget);
        hLayout.addMember(this.secondDatacageWidget);

        final HLayout toolbarLayout = new HLayout();
        toolbarLayout.setAutoHeight();

        final Button plusBtn = new Button(MSG.datacage_add_pair());
        plusBtn.setAutoFit(true);
        plusBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(final ClickEvent event) {
                plusClicked();
            }
        });
        toolbarLayout.addMember(plusBtn);

        addMember(hLayout);
        addMember(toolbarLayout);

        controler.setup(this.firstDatacageWidget, this.secondDatacageWidget, toolbarLayout);
    }

    /**
     * Callback for add-button.
     * Fires to load for every selected element and handler.
     */
    protected final void plusClicked() {
        final ToLoad toLoad1 = this.firstDatacageWidget.getSelection();
        final ToLoad toLoad2 = this.secondDatacageWidget.getSelection();

        // FIXME: allows to select folders... but it should not

        if (toLoad1 == null || toLoad2 == null || toLoad1.toRecommendations().isEmpty() || toLoad2.toRecommendations().isEmpty()) {
            SC.say(MSG.warning_select_two_values());
            return;
        }

        if (this.grid.getTotalRows() >= this.maxCount) {
            SC.say(this.msgMaxCount);
            return;
        }

        this.grid.addData(new RecommendationPairRecord(toLoad1.toRecommendations().get(0), toLoad2.toRecommendations().get(0)));
    }
}

http://dive4elements.wald.intevation.org