package hibergui.property;

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

import java.text.ParseException;
import java.util.Date;

import org.eclipse.swt.widgets.Composite;

public class TimeEdit1 extends TextEdit<Date> {

    public static DefaultComputation<Date> now = new DefaultComputation<Date>() {
		public Date defaultValue() {
			return new Date();
		}
    };

    public TimeEdit1(Form form, Composite cptParent, int style, String prop, String name) {
		super(form, cptParent, style, prop, name);

		addConstraint(
                new SelfConstraint() {
					public boolean check() {
						String s = getText();
						if (s == null)
							return true;
						return DateFormatConstraint.parseTime(s);
					}
                },
                new VerificationError(
                		I18n.get("constraint.timeformat.0") +
                		getName() +
                		I18n.get("constraint.timeformat.2")
                		)
        );
	}

	@Override
	public void fill() {
		Date d = get();
		setText(d == null ? "" : Util.timeFormat.format(d));
//		setText(Util.ensureString(get()));
	}

	@Override
	public Date extract() {
		try {
			return Util.timeFormat.parse(getText());
		} catch (ParseException e) {
			return null;
		} catch (NullPointerException e) {
			return null;
		}
	}
}
