package org.vaadin.viritin.fields;

import org.apache.commons.lang3.StringUtils;

import com.vaadin.event.FieldEvents;

/**
 * An field to edit Double values.
 *
 * The actual field implementation uses HTML5 input type "number" so you'll have
 * up/down arrows available in addition to simply typing the value. The client
 * side also prevent non numeric characters to be typed. Some of these advanced
 * UX helpers may work in modern browsers only, but the field should be
 * perfectly usable with older browsers as well.
 *
 * @author Matti Tahvonen
 */
public class DoubleField extends AbstractNumberField<DoubleField, Double> {

    private static final long serialVersionUID = 377246000306551089L;

    public DoubleField() {
        setSizeUndefined();
    }

    public DoubleField(String caption) {
        setCaption(caption);
    }

    @Override
    protected void userInputToValue(String str) {
        if (StringUtils.isNotBlank(str)) {
            // a hacky support for locales that use comma as a decimal separator
            // some browser do the conversion already on the browser, IE don't
            str = str.replaceAll(",", ".");
            value = Double.parseDouble(str);
        } else {
            value = null;
        }
    }

    @Override
    public DoubleField withBlurListener(FieldEvents.BlurListener listener) {
        return (DoubleField) super.withBlurListener(listener);
    }

    @Override
    public DoubleField withFocusListener(FieldEvents.FocusListener listener) {
        return (DoubleField) super.withFocusListener(listener);
    }

    @Override
    public Double getValue() {
        return value;
    }

    @Override
    protected void configureHtmlElement() {
        s.setProperty("type", getHtmlFieldType());
        // prevent all but numbers or single dot after a number with a simple js
        s.setJavaScriptEventHandler("keypress",
                "function(e) {if(e.metaKey || e.ctrlKey) return true; var c = viritin.getChar(e); if( (c === '.' || c === ',') && this.value != '' && this.value.indexOf('.') == -1 && this.value.indexOf(',') == -1) return true; return c==null || /^[-\\d\\n\\t\\r]+$/.test(c);}");
        }

}

@@ -697,16 +697,76 @@
 1089L;%0A%0A
+%09private String step = %22any%22;%0A%09private String definedStep;%0A%0A
     publ
@@ -856,24 +856,52 @@
  caption) %7B%0A
+        setSizeUndefined();%0A
         setC
@@ -1657,32 +1657,122 @@
 stener);%0A    %7D%0A%0A
+%09public DoubleField withStep(String step) %7B%0A%09%09this.definedStep = step;%0A%09%09return this;%0A%09%7D%0A%0A
     @Override%0A  
@@ -1883,24 +1883,336 @@
 Element() %7B%0A
+%09%09if (StringUtils.isNotBlank(definedStep)) %7B%0A%09%09%09step = definedStep;%0A%0A%09%09%7D else if (getValue() != null) %7B%0A%09%09%09String s = String.valueOf(getValue()).replaceAll(%22%5C%5Cd%22, %220%22).replaceAll(%22(%5C%5Cd%7B1%7D)$%22, %221%22);%0A%09%09%09if (%22any%22.equals(step) %7C%7C s.length() %3E step.length()) %7B%0A%09%09%09%09step = s;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09s.setProperty(%22step%22, step);%0A%0A
         s.se

add step attribute support to DoubleField