diff flys-client/src/main/java/de/intevation/flys/client/client/ui/WQInputPanel.java @ 42:ba7df4a24ae0

Added a new widget to enter w/Q values in single and range mode. flys-client/trunk@1483 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 16 Mar 2011 15:52:13 +0000
parents
children f99c5f8e4672
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/WQInputPanel.java	Wed Mar 16 15:52:13 2011 +0000
@@ -0,0 +1,404 @@
+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.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());
+        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() {
+        // TODO Implement me
+
+        return null;
+    }
+
+
+    /**
+     * 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