/*
 * Created on May 4, 2005
 *
 */
package LumiSoft.UI.Controls.Grid.Editors;

import java.util.*;
import java.awt.event.*;

import LumiSoft.UI.Controls.*;
import LumiSoft.UI.Controls.Grid.WGridTableView;

/**
 * DateTime grid editor.
 */
public class WDateTimeEditor extends WBaseEditor implements FocusListener
{
	private WDateTime m_pTextEdit = null;
	
	/**
	 * Default constructor.
	 *
	 */
	public WDateTimeEditor()
	{
		m_pTextEdit = new WDateTime();
		m_pTextEdit.setLocation(0,0);
		m_pTextEdit.setBorder(null);
		m_pTextEdit.setDrawBorders(false);
		m_pTextEdit.addKeyListener(new KeyListener(){
			public void keyTyped(KeyEvent e){
			}
			
			public void keyPressed(KeyEvent e){
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					e.consume();
					
					m_pGridView.MoveNext();
				}
				else if(e.getKeyCode() == KeyEvent.VK_DELETE && e.isControlDown()){
					e.consume();
					
					m_pGridView.DeleteActiveRow();
				}
				else if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
					Undo();
				}
				else if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN){
					m_pGridView.Process_keyPressed(e);
					
					e.consume();
				}
			}
			
			public void keyReleased(KeyEvent e){								
			}
		});
/*		m_pTextEdit.addValueChangedListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				RaiseValueChanged();
			}
		});
	*/	
		this.setLayout(null);
		this.addFocusListener(this);
		this.add(m_pTextEdit);
	}
	
	
	/*	Override this if need custom cell painting
	protected void PaintDefault(Graphics2D g,Object value,Rectangle cellBounds,boolean selected,Column column)
	{	
	}
	*/
	
	/**
	 * 
	 */
	public void StartEdit(WGridTableView view,Object value)
	{		
		m_pTextEdit.setBounds(0,0,this.getSize().width,this.getSize().height);
		m_pTextEdit.grabFocus();
				
		super.StartEdit(view,value);
	}
	
	
	public void focusGained(FocusEvent e)
	{
	//	m_pTextEdit.grabFocus();
		
	//	this.repaint();
	}
	
	public void focusLost(FocusEvent e)
	{		
//		this.repaint();
	}
	
	protected void SizeChanged()
	{
		m_pTextEdit.setBounds(0,0,this.getSize().width - 2,this.getSize().height);
	}
	
	
	/**
	 * Selects editor value if editor supports it.
	 *
	 */
	public void SelectAll()
	{
	//	m_pTextEdit.g();
	}
	
	
	/**
	 * Sets value.
	 * @param value
	 */
	public void setValue(Object value)
	{
		if(value == null || value.toString().equals("")){
			GregorianCalendar cal = (GregorianCalendar)GregorianCalendar.getInstance();
			cal.set(GregorianCalendar.HOUR_OF_DAY,0);
			cal.set(GregorianCalendar.MINUTE,0);
			cal.set(GregorianCalendar.SECOND,0);
			cal.set(GregorianCalendar.MILLISECOND,0);
			value = cal.getTime();
		//	value = new Date(GregorianCalendar.getInstance().getTime().getYear(),GregorianCalendar.getInstance().getTime().getMonth(),GregorianCalendar.getInstance().getTime().getDay());
		}
		
		if(value.getClass() != Date.class){
			System.out.println(value.getClass().toString());
			value = Date.parse(value.toString());
		}
		
		super.setValue(value);
	}
	
	/**
	 * Gets value what is currently visible in editor.
	 */
	public Object getEditValue()
	{
		return m_pTextEdit.getValue();
	}
	/**
	 * Sets value what is currently visible in editor.
	 */
	public void setEditValue(Object value)
	{
		try{
			if(value != null){
				m_pTextEdit.setValue((Date)value);				
			}
			else{
				GregorianCalendar cal = (GregorianCalendar)GregorianCalendar.getInstance();
				cal.set(GregorianCalendar.HOUR_OF_DAY,0);
				cal.set(GregorianCalendar.MINUTE,0);
				cal.set(GregorianCalendar.SECOND,0);
				cal.set(GregorianCalendar.MILLISECOND,0);
				
				m_pTextEdit.setValue(cal.getTime());
			}
		}
		catch(Exception x){
			GregorianCalendar cal = (GregorianCalendar)GregorianCalendar.getInstance();
			cal.set(GregorianCalendar.HOUR_OF_DAY,0);
			cal.set(GregorianCalendar.MINUTE,0);
			cal.set(GregorianCalendar.SECOND,0);
			cal.set(GregorianCalendar.MILLISECOND,0);
			
			m_pTextEdit.setValue(cal.getTime());
		}
	}
			
}
