view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DoubleRangePanel.java @ 9270:7337034eb5d5

multiple whitespace input fix
author gernotbelger
date Thu, 19 Jul 2018 13:36:38 +0200
parents 316a9eeb0836
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.Map;

import org.dive4elements.river.client.client.FLYSConstants;

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;

/**
 * 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";

    /** The textboxes */
    protected FloatItem fromItem;
    protected FloatItem toItem;
    protected FloatItem stepItem;

    public DoubleRangePanel() {
    }

    public FloatItem getToItem() {
        return this.toItem;
    }

    /**
     * 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(final String titleFrom, final String titleTo, final String titleStep, final double from, final double to, final double step,
            final int width, final BlurHandler handler) {
        this(titleFrom, titleTo, titleStep, from, to, step, width, handler, "right");
    }

    public DoubleRangePanel(final String titleFrom, final String titleTo, final String titleStep, final double from, final double to, final double step,
            final int width, final BlurHandler handler, final String labelOrientation) {
        this.fromItem = new FloatItem(FIELD_FROM);
        this.toItem = new FloatItem(FIELD_TO);
        this.stepItem = new FloatItem(FIELD_WIDTH);

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

        final NumberFormat f = NumberFormat.getDecimalFormat();

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

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

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

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

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

        if (labelOrientation.equals("right")) {
            setFields(this.fromItem, fromText, this.toItem, toText, this.stepItem, stepText);
        } else {
            setFields(fromText, this.fromItem, toText, this.toItem, stepText, this.stepItem);
        }

        setFixedColWidths(false);
        setNumCols(6);
        setWidth(width);
        setAlign(Alignment.CENTER);
    }

    /**
     * This method takes distances values and sets them to the textboxes
     * visualizied by this widget.
     *
     * @param from
     *            The from value.
     * @param to
     *            The to value.
     * @param steps
     *            The max steps.
     */
    public void setValues(final double from, final double to, final double steps) {
        final NumberFormat f = NumberFormat.getDecimalFormat();

        this.fromItem.setValue(f.format(from));
        this.toItem.setValue(f.format(to));
        this.stepItem.setValue(f.format(steps));
    }

    public boolean validateForm() {
        try {
            return validateForm(this.fromItem) && validateForm(this.toItem) && validateForm(this.stepItem);
        }
        catch (final NumberFormatException nfe) {
            return false;
        }
    }

    /**
     * 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.
     *
     * Also if negativeToAllowed is false, an error is registered if
     * the 'to' field contains a negative value.
     *
     * @param item
     *            The FormItem.
     */
    @SuppressWarnings("unchecked")
    protected boolean validateForm(final FormItem item) {
        if (item instanceof StaticTextItem) {
            return true;
        }

        boolean valid = true;

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

        final NumberFormat f = NumberFormat.getDecimalFormat();
        @SuppressWarnings("rawtypes")
        final Map errors = getErrors();

        try {
            if (v == null) {
                throw new NumberFormatException("empty");
            }

            f.parse(v);

            errors.remove(item.getFieldName());
        }
        catch (final NumberFormatException nfe) {
            errors.put(item.getFieldName(), this.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(final String value) {
        final NumberFormat f = NumberFormat.getDecimalFormat();

        final String[] splitted = StringArrayParseHelper.getArrayFromRawString(value); // Nullpointer?

        return f.parse(splitted[0]);
    }

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

        return getDouble(v);
    }

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

        return getDouble(v);
    }

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

        return getDouble(v);
    }

    /**
     * Sets the value of the field with name <i>fieldname</i>.
     *
     * @param value
     *            The new value.
     * @param fieldname
     *            The name of the field.
     */
    public void setDoubleValue(final double value, final String fieldname) {
        final NumberFormat f = NumberFormat.getDecimalFormat();
        setValue(fieldname, f.format(value));
    }

    /**
     * Sets a new start value.
     *
     * @param value
     *            The new start value.
     */
    public void setFrom(final double value) {
        setDoubleValue(value, FIELD_FROM);
    }

    /**
     * Sets a new end value.
     *
     * @param value
     *            The new end value.
     */
    public void setTo(final double value) {
        setDoubleValue(value, FIELD_TO);
    }

    /**
     * Sets a new step width.
     *
     * @param value
     *            The new step width.
     */
    public void setStep(final double value) {
        setDoubleValue(value, FIELD_WIDTH);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org