view flys-client/src/main/java/de/intevation/flys/client/client/ui/WQInputPanel.java @ 47:45ae03d9ca2b

Implemented the getData() method of the WQInputPanel. flys-client/trunk@1495 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 17 Mar 2011 10:54:41 +0000
parents f99c5f8e4672
children 6e191588a295
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import java.util.LinkedHashMap;

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

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.layout.VLayout;

import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.DefaultData;
import de.intevation.flys.client.shared.model.DefaultDataItem;
import de.intevation.flys.client.client.FLYSMessages;


/**
 * This UIProvider creates a widget to enter W or Q data.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class WQInputPanel
extends      AbstractUIProvider
implements   ChangeHandler, BlurHandler
{
    /** The message class that provides i18n strings.*/
    protected FLYSMessages MSG = GWT.create(FLYSMessages.class);


    /** The constant field name for choosing w or q mode.*/
    public static final String FIELD_WQ = "wq";

    /** The constant field value for W input mode.*/
    public static final String FIELD_WQ_W = "W";

    /** The constant field value for Q input mode.*/
    public static final String FIELD_WQ_Q = "Q";

    /** The constant field name for choosing single values or range.*/
    public static final String FIELD_MODE = "mode";

    /** The constant field value for single input mode.*/
    public static final String FIELD_MODE_SINGLE = "single";

    /** The constant field value for range input mode.*/
    public static final String FIELD_MODE_RANGE = "range";

    /** The constant value that determines the width of the left panel.*/
    public static final int WIDTH_LEFT = 200;


    /** The container that manages the w and q panels.*/
    protected VLayout container;

    /** The RadioGroupItem that determines the w/q input mode.*/
    protected DynamicForm modes;

    /** The 'from' value entered in the range W mode.*/
    protected double fromW;

    /** The 'to' value entered in the range W mode.*/
    protected double toW;

    /** The 'step' value entered in the range W mode.*/
    protected double stepW;

    /** The values entered in the single W mode.*/
    protected double[] valuesW;

    /** The 'from' value entered in the range Q mode.*/
    protected double fromQ;

    /** The 'to' value entered in the range Q mode.*/
    protected double toQ;

    /** The 'step' value entered in the range Q mode.*/
    protected double stepQ;

    /** The values entered in the single Q mode.*/
    protected double[] valuesQ;


    /**
     * Creates a new WQInputPanel instance.
     */
    public WQInputPanel() {
    }


    /**
     * This method calls createWidget and puts a 'next' button to the bottom.
     *
     * @param data The data that is displayed.
     *
     * @return the widget.
     */
    public Canvas create(Data data) {
        Canvas  widget = createWidget(data);
        IButton submit = new IButton(MSG.next(), this);
        Label   label  = new Label(MSG.wqTitle());

        label.setHeight(25);

        VLayout layout = new VLayout();
        layout.addMember(label);
        layout.addMember(widget);
        layout.addMember(submit);

        return layout;
    }


    /**
     * This method creates the whole widget. There is a panel on the left, that
     * allows the user to enter values manually by keyboard. On the right, there
     * is a table that allows the user to enter values by mouse click.
     *
     * @param data The data that is displayed in the table on the right.
     *
     * @return the widget.
     */
    protected Canvas createWidget(Data data) {
        VLayout layout  = new VLayout();
        container       = new VLayout();
        Canvas modeForm = createModePanel();

        layout.addMember(modeForm);
        layout.addMember(container);

        // TODO add a table on the right to select Q values

        return layout;
    }


    /**
     * This method creates the mode panel. It contains two radio button panels
     * that allows the user to switch the input mode between w/q and
     * single/range input.
     *
     * @return a panel.
     */
    protected Canvas createModePanel() {
        RadioGroupItem wq = new RadioGroupItem(FIELD_WQ);
        wq.setShowTitle(false);
        wq.setVertical(false);
        wq.setWidth(WIDTH_LEFT);

        RadioGroupItem mode = new RadioGroupItem(FIELD_MODE);
        mode.setShowTitle(false);
        mode.setVertical(false);
        mode.setWidth(WIDTH_LEFT);

        LinkedHashMap wqValues = new LinkedHashMap();
        wqValues.put(FIELD_WQ_W, MSG.wqW());
        wqValues.put(FIELD_WQ_Q, MSG.wqQ());

        LinkedHashMap modeValues = new LinkedHashMap();
        modeValues.put(FIELD_MODE_SINGLE, MSG.wqSingle());
        modeValues.put(FIELD_MODE_RANGE, MSG.wqRange());

        wq.setValueMap(wqValues);
        mode.setValueMap(modeValues);

        wq.addChangeHandler(this);
        mode.addChangeHandler(this);

        modes = new DynamicForm();
        modes.setFields(wq, mode);
        modes.setWidth(WIDTH_LEFT);
        modes.setNumCols(1);

        LinkedHashMap initial = new LinkedHashMap();
        initial.put(FIELD_WQ, FIELD_WQ_W);
        initial.put(FIELD_MODE, FIELD_MODE_SINGLE);
        modes.setValues(initial);

        return modes;
    }


    /**
     * This method returns the selected data.
     *
     * @return the selected/inserted data.
     */
    public Data[] getData() {
        return new Data[] {
            getDataMode(),
            getDataFrom(),
            getDataTo(),
            getDataStep() };
    }


    /**
     * Returns the Data object for the 'mode' attribute.
     *
     * @return the Data object for the 'mode' attribute.
     */
    protected Data getDataMode() {
        String wqMode = modes.getValueAsString(FIELD_WQ);
        DataItem item = new DefaultDataItem("wq_mode", "wq_mode", wqMode);
        return new DefaultData(
            "wq_mode", null, null, new DataItem[] { item }, null);
    }


    /**
     * Returns the Data object for the 'from' attribute.
     *
     * @return the Data object for the 'from' attribute.
     */
    protected Data getDataFrom() {
        String value  = Double.valueOf(getFinalFrom()).toString();
        DataItem item = new DefaultDataItem("wq_from", "wq_from", value);
        return new DefaultData(
            "wq_from", null, null, new DataItem[] { item }, null);
    }


    /**
     * Returns the Data object for the 'to' attribute.
     *
     * @return the Data object for the 'to' attribute.
     */
    protected Data getDataTo() {
        String value  = Double.valueOf(getFinalTo()).toString();
        DataItem item = new DefaultDataItem("wq_to", "wq_to", value);
        return new DefaultData(
            "wq_to", null, null, new DataItem[] { item }, null);
    }


    /**
     * Returns the Data object for the 'step' attribute.
     *
     * @return the Data object for the 'step' attribute.
     */
    protected Data getDataStep() {
        String value  = Double.valueOf(getFinalStep()).toString();
        DataItem item = new DefaultDataItem("wq_step","wq_step", value);
        return new DefaultData(
            "wq_step", null, null, new DataItem[] { item }, null);
    }


    /**
     * Returns the value of 'from' depending on the selected input mode.
     *
     * @return the value of 'from' depending on the selected input mode.
     */
    protected double getFinalFrom() {
        boolean wMode     = isWMode();
        boolean rangeMode = isRangeMode();

        if (rangeMode) {
            return wMode ? getFromW() : getFromQ();

        }
        else {
            double[] values = wMode ? getSingleW() : getSingleQ();
            double   value  = Double.MAX_VALUE;

            for (double v: values) {
                value = value < v ? value : v;
            }

            return value;
        }
    }


    /**
     * Returns the value of 'to' depending on the selected input mode.
     *
     * @return the value of 'to' depending on the selected input mode.
     */
    protected double getFinalTo() {
        boolean wMode     = isWMode();
        boolean rangeMode = isRangeMode();

        if (rangeMode) {
            return wMode ? getToW() : getToQ();

        }
        else {
            double[] values = wMode ? getSingleW() : getSingleQ();
            double   value  = Double.MIN_VALUE;

            for (double v: values) {
                value = value > v ? value : v;
            }

            return value;
        }
    }


    /**
     * Returns the value of 'step' depending on the selected input mode.
     *
     * @return the value of 'step' depending on the selected input mode.
     */
    protected double getFinalStep() {
        boolean wMode     = isWMode();
        boolean rangeMode = isRangeMode();

        if (rangeMode) {
            // we have no field to enter the 'step' attribute in the
            // range mode
            return 0.0;
        }
        else {
            if (wMode) {
                return getStepW();
            }
            else {
                // we have no field to enter the 'step' attribute in the
                // range mode
                return getStepQ();
            }
        }
    }


    /**
     * Determines the range/single mode.
     *
     * @return true if the range mode is activated.
     */
    public boolean isRangeMode() {
        String rMode = modes.getValueAsString(FIELD_MODE);

        return rMode.equals(FIELD_MODE_RANGE) ? true : false;
    }


    /**
     * Determines the w/q mode.
     *
     * @return true, if the W mode is activated.
     */
    public boolean isWMode() {
        String wq = modes.getValueAsString(FIELD_WQ);

        return wq.equals(FIELD_WQ_W) ? true : false;
    }


    /**
     * This method changes the lower panel with the input fields depending on
     * the combination of the two radio button panels.
     *
     * @param event The ChangeEvent.
     */
    public void onChange(ChangeEvent event) {
        DynamicForm form = event.getForm();
        FormItem    item = event.getItem();

        String wqMode    = null;
        String inputMode = null;

        if (item.getFieldName().equals(FIELD_MODE)) {
            wqMode    = form.getValueAsString(FIELD_WQ);
            inputMode = (String) event.getValue();
        }
        else {
            wqMode    = (String) event.getValue();
            inputMode = form.getValueAsString(FIELD_MODE);
        }

        container.removeMembers(container.getMembers());

        Canvas newPanel = null;

        if (wqMode.equals(FIELD_WQ_W)) {
            if (inputMode.equals(FIELD_MODE_SINGLE)) {
                // Single W mode
                double[] values = getSingleW();

                newPanel = new DoubleArrayPanel(
                    MSG.unitWSingle(), values, this);
            }
            else {
                // Range W mode
                double from = getFromW();
                double to   = getToW();
                double step = getStepW();

                newPanel = new DoubleRangePanel(
                    MSG.unitWFrom(), MSG.unitWTo(), MSG.unitWStep(),
                    from, to, step,
                    250,
                    this);
            }
        }
        else {
            if (inputMode.equals(FIELD_MODE_SINGLE)) {
                // Single Q mode
                double[] values = getSingleQ();

                newPanel = new DoubleArrayPanel(
                    MSG.unitQSingle(), values, this);
            }
            else {
                // Range Q mode
                double from = getFromQ();
                double to   = getToQ();
                double step = getStepQ();

                newPanel = new DoubleRangePanel(
                    MSG.unitQFrom(), MSG.unitQTo(), MSG.unitQStep(),
                    from, to, step,
                    250,
                    this);
            }
        }

        container.addMember(newPanel);
    }


    /**
     * This method is called if the value of one of the input fields might have
     * changed. The entered values are validated and stored.
     *
     * @param event The BlurEvent.
     */
    public void onBlur(BlurEvent event) {
        DynamicForm form = event.getForm();
        FormItem    item = event.getItem();

        String wqMode    = (String) modes.getValue(FIELD_WQ);
        String inputMode = (String) modes.getValue(FIELD_MODE);

        if (wqMode.equals(FIELD_WQ_W)) {
            if (inputMode.equals(FIELD_MODE_SINGLE)) {
                DoubleArrayPanel p = (DoubleArrayPanel) form;

                if (p.validateForm(item)) {
                    setSingleW(p.getInputValues(item));
                }
            }
            else {
                DoubleRangePanel p = (DoubleRangePanel) form;

                if (p.validateForm(item)) {
                    setFromW(p.getFrom());
                    setToW(p.getTo());
                    setStepW(p.getStep());
                }
            }
        }
        else {
            if (inputMode.equals(FIELD_MODE_SINGLE)) {
                DoubleArrayPanel p = (DoubleArrayPanel) form;

                if (p.validateForm(item)) {
                    setSingleQ(p.getInputValues(item));
                }
            }
            else {
                DoubleRangePanel p = (DoubleRangePanel) form;

                if (p.validateForm(item)) {
                    setFromQ(p.getFrom());
                    setToQ(p.getTo());
                    setStepQ(p.getStep());
                }
            }
        }
    }


    protected double[] getSingleQ() {
        return valuesQ;
    }


    protected void setSingleQ(double[] values) {
        valuesQ = values;
    }


    protected double getFromQ() {
        return fromQ;
    }


    protected void setFromQ(double fromQ) {
        this.fromQ = fromQ;
    }


    protected double getToQ() {
        return toQ;
    }


    protected void setToQ(double toQ) {
        this.toQ = toQ;
    }


    protected double getStepQ() {
        return stepQ;
    }


    protected void setStepQ(double stepQ) {
        this.stepQ = stepQ;
    }


    protected double[] getSingleW() {
        return valuesW;
    }


    protected void setSingleW(double[] values) {
        valuesW = values;
    }


    protected double getFromW() {
        return fromW;
    }


    protected void setFromW(double fromW) {
        this.fromW = fromW;
    }


    protected double getToW() {
        return toW;
    }


    protected void setToW(double toW) {
        this.toW = toW;
    }


    protected double getStepW() {
        return stepW;
    }


    protected void setStepW(double stepW) {
        this.stepW = stepW;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org