/*
 * 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 de.jaret.util.ui.datechooser.DateChooser;

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

        mdc.addKeyListener(lsn_WIDGET_KEY);
        mdc.addFocusListener(lsn_WIDGET_FOCUS);
        mdc.addTraverseListener(lsn_TRAVERSE);
        
        addConstraint(
                new SelfConstraint() {
					public boolean check() {
						return DateFormatConstraint.parseDate(mdc.getText());
					}
                },
                new VerificationError(
                		I18n.get("constraint.dateformat.0") +
                		getName() +
                		I18n.get("constraint.dateformat.2")
                		)
        );
    }
    
    @Override
    protected Control createContents(Composite cptParent) {
        return mdc = new MyDateChooser(cptParent, SWT.BORDER);
    }
    
    @Override
    public void clear() {
        mdc.setDate(null);
    }
    
    @Override
    public void fill() {
        mdc.setDate(value);
    }
    
    @Override
    public Date extract() {
        return mdc.getDate();
    }
    
    @Override
    public boolean setFocus() {
        return mdc.setFocus();
    }
    
    @Override
    public boolean isEnabled() {
        return mdc.isEnabled();
    }
    
    @Override
    public void setEnabled(boolean b) {
        mdc.setEnabled(b);
    }

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

	@Override
	public void setBackground(Color clr) {
		mdc.setBackground(clr);
	}
	
}
