package org.libreplan.web.common;

import static org.libreplan.business.workingday.EffortDuration.zero;
import static org.libreplan.web.I18nHelper._;

import org.libreplan.business.workingday.EffortDuration;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zul.Textbox;

public class EffortDurationBox extends Textbox {

    public static EffortDurationBox notEditable() {
        EffortDurationBox result = new EffortDurationBox();
        result.setDisabled(true);
        return result;
    }

    public EffortDurationBox() {
        setValueDirectly(zero());
    }

    public EffortDurationBox(EffortDuration effortDuration) {
        setValue(effortDuration);
    }

    public void setValue(EffortDuration effortDuration) {
        setText(coerceToString(effortDuration));
    }

    public EffortDuration getEffortDurationValue() {
        return (EffortDuration) getTargetValue();
    }

    @Override
    protected Object marshall(Object value) {
        return coerceToString(value);
    }

    @Override
    protected Object unmarshall(Object value) {
        EffortDuration result = EffortDuration
                .parseFromFormattedString((String) value);
        if (result == null) {
            return EffortDuration.zero();
        }
        return result;
    }

    @Override
    protected Object coerceFromString(String value) throws WrongValueException {
        EffortDuration result = EffortDuration.parseFromFormattedString(value);
        if (result == null) {
            throw new WrongValueException(this,
                    _("Not a valid effort duration"));
        }
        return result;
    }

    @Override
    protected String coerceToString(Object value) {
        if (value instanceof EffortDuration) {
            EffortDuration effort = (EffortDuration) value;
            return effort.toFormattedString();
        }
        return "";
    }

}

@@ -1082,209 +1082,47 @@
     
-EffortDuration result = EffortDuration%0A                .parseFromFormattedString((String) value);%0A        if (result == null) %7B%0A            return EffortDuration.zero();%0A        %7D%0A        return result
+return coerceFromString((String) value)
 ;%0A  

Revert "Revert "[Bug #1253] Constraint for EffortDuration in advanced allocation""