view flys-client/src/main/java/de/intevation/flys/client/client/utils/DoubleValidator.java @ 1506:339f8aa641b5

Issue 358. Convert strings for double values. flys-client/trunk@3638 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Mon, 09 Jan 2012 18:06:01 +0000
parents 02a9104c0451
children c21d14e48040
line wrap: on
line source
package de.intevation.flys.client.client.utils;

import java.util.Map;

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

import com.smartgwt.client.widgets.form.fields.FormItem;

/**
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class DoubleValidator extends Validator {

    /**
     *
     */
    @Override
    protected boolean validate(FormItem item, Map errors) {
        boolean valid = true;

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

        NumberFormat f = NumberFormat.getDecimalFormat();

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

            double value = f.parse(v);

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

            item.focusInItem();

            valid = false;
        }
        return valid;
    }


    public String toProtocolString(FormItem item, Map errors) {
        if(validate(item, errors)) {
            return item.getValue().toString().replaceAll(",", ".");
        }
        else {
            return null;
        }
    }
}

http://dive4elements.wald.intevation.org