view flys-client/src/main/java/de/intevation/flys/client/client/ui/DoubleRangePanel.java @ 211:b92281182c6b

Removed the FLYSMessages interface and replaced it with a FLYSConstants interface - this interface has the ability to lookup i18n strings with given keys. flys-client/trunk@1645 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 05 Apr 2011 08:13:48 +0000
parents a3d235c63195
children 137daff2c732
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import java.util.Map;

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

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FloatItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;

import de.intevation.flys.client.client.FLYSConstants;



/**
 * This class creates a DynamicForm with three input fields.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class DoubleRangePanel
extends      DynamicForm
{
    /** The message class that provides i18n strings.*/
    protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);


    /** The constant name of the input field to enter the start of a distance.*/
    public static final String FIELD_FROM = "from";

    /** The constant name of the input field to enter the end of a distance.*/
    public static final String FIELD_TO = "to";

    /** The constant name of the input field to enter the step width of a
     * distance.*/
    public static final String FIELD_WIDTH = "step";


    /**
     * Creates a new form with a single input field that displays an array of
     * double values.
     *
     * @param name The name of the TextItem.
     * @param title The title of the TextItem.
     * @param values The double values that should be displayed initially.
     * @param handler The BlurHandler that is used to valide the input.
     */
    public DoubleRangePanel(
        String titleFrom, String titleTo, String titleStep,
        double from, double to, double step,
        int width,
        BlurHandler handler)
    {
        FloatItem fromItem = new FloatItem(FIELD_FROM);
        FloatItem toItem   = new FloatItem(FIELD_TO);
        FloatItem stepItem = new FloatItem(FIELD_WIDTH);

        fromItem.addBlurHandler(handler);
        toItem.addBlurHandler(handler);
        stepItem.addBlurHandler(handler);

        NumberFormat f = NumberFormat.getDecimalFormat();

        fromItem.setValue(f.format(from));
        toItem.setValue(f.format(to));
        stepItem.setValue(f.format(step));

        StaticTextItem fromText = new StaticTextItem("staticFrom");
        fromText.setValue(titleFrom);
        fromText.setShowTitle(false);
        fromItem.setShowTitle(false);

        StaticTextItem toText = new StaticTextItem("staticTo");
        toText.setValue(titleTo);
        toText.setShowTitle(false);
        toItem.setShowTitle(false);

        StaticTextItem stepText = new StaticTextItem("staticStep");
        stepText.setValue(titleStep);
        stepText.setShowTitle(false);
        stepItem.setShowTitle(false);

        int itemWidth = width / 6;
        fromItem.setWidth(itemWidth);
        fromText.setWidth(itemWidth);
        toItem.setWidth(itemWidth);
        toText.setWidth(itemWidth);
        stepItem.setWidth(itemWidth);
        stepText.setWidth(itemWidth);

        setFields(fromItem, fromText, toItem, toText, stepItem, stepText);
        setFixedColWidths(false);
        setNumCols(6);
        setWidth(width);
        setAlign(Alignment.CENTER);
    }


    /**
     * This method validates the entered text in the input fields. If
     * there are values that doesn't represent a valid float, an error is
     * displayed.
     *
     * @param item The FormItem.
     */
    protected boolean validateForm(FormItem item) {
        if (item instanceof StaticTextItem) {
            return true;
        }

        boolean valid = true;

        String v = (String) item.getValue();

        NumberFormat f = NumberFormat.getDecimalFormat();
        Map errors     = getErrors();

        try {
            double value = f.parse(v);

            errors.remove(item.getFieldName());
        }
        catch (NumberFormatException nfe) {
            errors.put(item.getFieldName(), MESSAGES.wrongFormat());

            item.focusInItem();

            valid = false;
        }

        setErrors(errors, true);

        return valid;
    }


    /**
     * Returns the double value of <i>value</i>.
     *
     * @return the double value of <i>value</i>.
     */
    protected double getDouble(String value) {
        NumberFormat f = NumberFormat.getDecimalFormat();

        return f.parse(value);
    }


    /**
     * Returns the start value.
     *
     * @return the start value.
     */
    public double getFrom() {
        String v = getValueAsString(FIELD_FROM);

        return getDouble(v);
    }


    /**
     * Returns the end value.
     *
     * @return the end value.
     */
    public double getTo() {
        String v = getValueAsString(FIELD_TO);

        return getDouble(v);
    }


    /**
     * Returns the step width.
     *
     * @return the step width.
     */
    public double getStep() {
        String v = getValueAsString(FIELD_WIDTH);

        return getDouble(v);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org