view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DoubleArrayPanel.java @ 9550:66476c93882f

Alignment test 2
author gernotbelger
date Mon, 22 Oct 2018 11:51:53 +0200
parents c86ae5562e68
children 342c122e3dfe
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.TitleOrientation;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
import com.smartgwt.client.widgets.form.fields.events.FocusHandler;

public class DoubleArrayPanel extends DynamicForm {
    /** The message class that provides i18n strings. */
    protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);

    protected TextItem ti;

    private final StaticTextItem errorItem = new StaticTextItem("error");

    private String title;

    /** The constant input field name. */
    public static final String FIELD_NAME = "doublearray";

    public DoubleArrayPanel(final String title, final double[] values, final BlurHandler handler) {
        this(title, values, handler, null, TitleOrientation.RIGHT);
    }

    /**
     * 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 blurHandler
     *            The BlurHandler that is used to valide the input.
     * @param focusHandler
     *            The FocueHandler that is used to valide the input.
     */

    public DoubleArrayPanel(final String title, final double[] values, final BlurHandler blurHandler, final FocusHandler focusHandler,
            final TitleOrientation titleOrientation, final String errorMsg) {

        this.title = title;
        this.ti = new TextItem(FIELD_NAME);
        final StaticTextItem sti = new StaticTextItem("staticarray");

        this.errorItem.setTitle("");
        if (errorMsg != null)
            this.errorItem.setValue(errorMsg);

        this.errorItem.setShowTitle(false);
        this.errorItem.setVisible(true);
        // this.errorItem.setColSpan();
        this.errorItem.setWidth("*"); // das ErrorItem soll den Rest ausfüllen
        sti.setShowTitle(false);
        sti.setValue(title);
        // sti.setColSpan(1);
        sti.setWidth(60); // hat irgendwie keine Auswirkung

        this.ti.setWidth(120); // Das Eingabefeld - Änderungen an der width funktionieren
        this.ti.setShowTitle(false);
        // this.ti.setColSpan(1);

        this.ti.addBlurHandler(blurHandler);
        if (focusHandler != null) {
            this.ti.addFocusHandler(focusHandler);
        }

        if (titleOrientation == TitleOrientation.RIGHT) {
            setFields(this.ti, sti, this.errorItem);
        } else {
            setFields(sti, this.ti, this.errorItem);
        }

        setTitleOrientation(titleOrientation);
        setNumCols(3); // default (Pegelname, Eingabefeld, ErrorMsg)

        if (values == null) {
            return;
        }

        final NumberFormat f = NumberFormat.getDecimalFormat();

        final StringBuilder text = new StringBuilder();
        boolean firstItem = true;

        for (final double val : values) {
            if (!firstItem) {
                text.append(" ");
            }

            text.append(f.format(val));

            firstItem = false;
        }

        this.ti.setValue(text.toString());

    }

    public DoubleArrayPanel(final String title, final double[] values, final BlurHandler blurHandler, final FocusHandler focusHandler,
            final TitleOrientation titleOrientation) {
        this(title, values, blurHandler, focusHandler, titleOrientation, null);
    }

    /**
     * This method takes the double array to set the values to the textbox.
     *
     * @param values
     *            The double values.
     */
    public void setValues(final double[] values) {
        final NumberFormat f = NumberFormat.getDecimalFormat();

        if (values == null || values.length == 0) {
            this.ti.clearValue();
            return;
        }
        final StringBuilder text = new StringBuilder();
        boolean firstItem = true;
        if (values != null) {
            for (final double val : values) {
                if (!firstItem) {
                    text.append(" ");
                }

                text.append(f.format(val));

                firstItem = false;
            }
        }

        this.ti.clearValue();
        this.ti.setValue(text.toString());
    }

    /**
     * This method appends a double value to the current list of values.
     *
     * @param value
     *            A new value.
     */
    public void addValue(final double value) {
        final NumberFormat f = NumberFormat.getDecimalFormat();

        String current = this.ti.getValueAsString();

        if (current == null || current.length() == 0) {
            current = f.format(value);
        } else {
            current += " " + f.format(value);
        }

        this.ti.setValue(current);
    }

    public void setError(final String error) {
        this.errorItem.setValue(error);
    }

    public boolean validateForm() {
        return validateForm(this.ti);
    }

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

        boolean valid = true;
        final String value = (String) item.getValue();

        if (value == null) {
            return valid;
        }

        final String[] parts = value.split("\\s+");

        if (parts == null) {
            return valid;
        }

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

        try {
            for (final String part : parts) {

                if (part.length() == 0) {
                    continue;
                }

                nf.parse(part);
            }

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

            valid = false;
        }

        setErrors(errors, true);

        return valid;
    }

    /**
     * This method returns the double array that has been entered in
     * <i>item</i>.
     *
     * @param item
     *            The item that contains the desired values.
     *
     * @return the values as double array.
     */
    public double[] getInputValues(final FormItem item) {
        final String value = (String) item.getValue();

        if (value == null) {
            return null;
        }

        final String[] parts = value.split("\\s+");

        if (parts == null) {
            return null;
        }

        final NumberFormat f = NumberFormat.getDecimalFormat();

        final double[] values = new double[parts.length];

        int i = 0;
        OUTER: for (final String part : parts) {
            if (part.length() == 0) {
                continue;
            }

            try {
                final double x = f.parse(part);
                for (int j = 0; j < i; ++j) {
                    if (values[j] == x) {
                        continue OUTER;
                    }
                }
                values[i++] = x;
            }
            catch (final NumberFormatException nfe) {
                // do nothing
            }
        }

        final double[] out = new double[i];
        System.arraycopy(values, 0, out, 0, i);

        return out;
    }

    /**
     * Returns the double values of this panel.
     *
     * @return the double values of this panel.
     */
    public double[] getInputValues() {
        return getInputValues(this.ti);
    }

    public String getItemTitle() {
        return this.title;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org