package hibergui.property;

import hibergui.Edit;
import hibergui.Form;
import hibergui.swt.DateField;

import java.text.DateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

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

public class TimeEdit extends Edit<Date> {
	
	DateField df;
	Text txt;
	
	GregorianCalendar calendar = new GregorianCalendar();

	public TimeEdit(Form<?> form, Composite cptParent, int style, String prop, String name) {
		super(form, cptParent, style, prop, name);
		
		txt.addModifyListener(lsn_WIDGET_MOD);
		txt.addKeyListener(lsn_WIDGET_KEY);
		txt.addFocusListener(lsn_WIDGET_FOCUS);
		txt.addTraverseListener(lsn_TRAVERSE);
	}

	@Override
	protected Control createContents(Composite cptParent) {
		df = new DateField(DateFormat.getTimeInstance(DateFormat.SHORT));
		return txt = df.create(cptParent);
	}

	@Override
	public void clear() {
		txt.setText("");
		df.setCalendar(null);
	}

	@Override
	public void fill() {
		calendar.setTime(get());
		df.setCalendar(calendar);
	}

	@Override
	public Date extract() {
		return df.getCalendar().getTime();
	}

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

	@Override
	public boolean setFocus() {
		return txt.setFocus();
	}

	@Override
	public boolean isEnabled() {
		return txt.isEnabled();
	}

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

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