/*
 * Created on Aug 17, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package hibergui.property;

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

import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import com.tiff.common.ui.datepicker.DatePickerCombo;


/**
 * @author espakm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DateCombo extends Edit<Date> {
    
    protected DatePickerCombo dpc;
    
    public static DefaultComputation<Date> today = new DefaultComputation<Date>() {
		public Date defaultValue() {
			return new Date();
		}
    };
    
    public DateCombo(Form form, Composite cpt, int style, String prop, String name) {
        super(form, cpt, style, prop, name);

        dpc.addModifyListener(lsn_WIDGET_MOD);
        dpc.addKeyListener(lsn_WIDGET_KEY);
        dpc.addFocusListener(lsn_WIDGET_FOCUS);
        dpc.addTraverseListener(lsn_TRAVERSE);

        addConstraint(
                new SelfConstraint() {
					public boolean check() {
						return DateFormatConstraint.parseDate(dpc.getText());
					}
                },
                new VerificationError(
                		I18n.get("constraint.dateformat.0") +
                		getName() +
                		I18n.get("constraint.dateformat.2")
                		)
        );
    }
    
    @Override
    protected Control createContents(Composite cptParent) {
        return dpc = new DatePickerCombo(cptParent, SWT.BORDER);
    }
    
    @Override
    public void setBackground(Color clr) {
    	dpc.setBackground(clr);
    }
    
	@Override
    public void clear() {
        dpc.setDate(null);
    }
    
    @Override
    public void fill() {
        dpc.setDate(value);
    }
    
    @Override
    public Date extract() {
        return dpc.getDate();
    }
    
    @Override
    public boolean setFocus() {
        return dpc.setFocus();
    }
    
    @Override
    public boolean isEnabled() {
        return dpc.isEnabled();
    }
    
    @Override
    public void setEnabled(boolean b) {
        dpc.setEnabled(b);
    }

	@Override
	public void setEditable(boolean b) {
		dpc.setEnabled(b);
	}
}
