package hibergui.property;

import hibergui.Form;
import hibergui.I18n;
import hibergui.verify.SelfConstraint;
import hibergui.verify.VerificationError;

import org.eclipse.swt.widgets.Composite;

public class IntegerEdit extends TextEdit<Integer> {

    public IntegerEdit(Form form, Composite cptParent, int style, String prop, String name) {
        super(form, cptParent, style, prop, name);
        addConstraint(
                new SelfConstraint() {
                    public boolean check() {
                    	String t = getText();
                    	if (t == null)
                    		return true;
                        try {
                            Integer.parseInt(t);
                        } catch (NumberFormatException e) {
                            return false;
                        }
                        return true;
                    }
                },
                new VerificationError(
                		I18n.get("constraint.integerformat.0") +
                		getName() +
                		I18n.get("constraint.integerformat.2")
                		)
        );
    }
    
    @Override
    public void fill() {
        setText(value == null ? "" : value.toString());
    }

    @Override
    public Integer extract() {
    	String t = getText();
        return t == null ? null : new Integer(t);        
    }

}
