/*
 * Created on Apr 2, 2004
 *
 */
package LumiSoft.UI.Controls.Grid.Editors;

import java.awt.event.*;

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

/**
 * Numeric edit grid editor.
 */
public class WNumericEditEditor extends WBaseEditor implements FocusListener
{
	private WNumericEdit m_pTextEdit = null;
	
	/**
	 * Default constructor.
	 *
	 */
	public WNumericEditEditor()
	{	
		m_pTextEdit = new WNumericEdit();
		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("")){
			value = new Double(0);
		}
		
		if(value.getClass() != Double.class){
			value = Double.valueOf(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(Double.parseDouble(value.toString()));				
			}
			else{
				m_pTextEdit.setValue(0);
			}
		}
		catch(Exception x){
			m_pTextEdit.setValue(0);
		}
	}
	
	/**
	 * Gets if spinners visible.
	 * @return
	 */
	public boolean getSpinnersVisible()
	{
		return m_pTextEdit.getSpinnersVisible();
	}
	/**
	 * Sets if spinners visible.
	 * @param value
	 */
	public void setSpinnersVisible(boolean value)
	{
		m_pTextEdit.setSpinnersVisible(value);
	}
	
	/**
	 * Gets text alignment.
	 * @return
	 */
	public int getTextAlign()
	{
		return m_pTextEdit.getTextAlign();
	}
	/**
	 * Sets text alignment.
	 * @param value
	 */
	public void setTextAlign(int value)
	{
		m_pTextEdit.setTextAlign(value);
	}
	
	/**
	 * Gets number of decimal places.
	 * @return
	 */
	public int getDecimalPlaces()
	{
		return m_pTextEdit.getDecimalPlaces();
	}
	/**
	 * Sets number of decimal places.
	 * @param value
	 */
	public void setDecimalPlaces(int value)
	{
		m_pTextEdit.setDecimalPlaces(value);
	}
	
}
