/*
 * Created on Jan 19, 2004
 *
 */
package LumiSoft.UI.Controls.Grid.Editors;

import java.awt.event.*;
import javax.swing.*;

import LumiSoft.UI.Controls.Grid.*;

/**
 * Grid text edit control.
 * 
 */
public class WTextEditor extends WBaseEditor
{			
	private JTextField m_pTextEdit = null;
	private int        m_MaxLength = 9999999;
	
	/**
	 * Default constructor.
	 *
	 */
	public WTextEditor()
	{	
		m_pTextEdit = new JTextField();
		m_pTextEdit.setLocation(0,0);
		m_pTextEdit.setBorder(null);
		m_pTextEdit.addKeyListener(new KeyListener(){
			public void keyTyped(KeyEvent e){
				// Maximum allowed text length exceeded
				if((m_pTextEdit.getText().length() - Math.abs(m_pTextEdit.getSelectionEnd() - m_pTextEdit.getSelectionStart())) >= getMaxLength()){
					e.consume();
				}
			}
			
			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){								
			}
		});
		
		this.setLayout(null);
		this.add(m_pTextEdit);
	}
	
/*	Override this if need custom cell painting
	protected void PaintDefault(Graphics2D g,Object value,Rectangle cellBounds,boolean selected,Column column)
	{	
	}
*/
		
	protected void SizeChanged()
	{
		m_pTextEdit.setBounds(0,0,this.getSize().width - 2,this.getSize().height);
	}
	
	/**
	 * 
	 */
	public void StartEdit(WGridTableView view,Object value)
	{		
	//	m_pTextEdit.setBounds(0,0,this.getSize().width,this.getSize().height);
		m_pTextEdit.requestFocus();
				
		super.StartEdit(view,value);
	}
	
	
/*	public void setEnabled(boolean value)
	{
		super.setEnabled(value);
		m_pTextEdit.setEnabled(value);
	}*/
	
	/**
	 * Selects editor value if editor supports it.
	 *
	 */
	public void SelectAll()
	{
		m_pTextEdit.selectAll();
	}
	
			
	/**
	 * Sets value.
	 * @param value
	 */
	public void setValue(Object value)
	{
		if(value == null){
			value = "";
		}
		
		super.setValue(value);
	}
	
	/**
	 * Gets value what is currently visible in editor.
	 */
	public Object getEditValue()
	{
		return m_pTextEdit.getText();
	}
	/**
	 * Sets value what is currently visible in editor.
	 */
	public void setEditValue(Object value)
	{
		if(value != null){
			m_pTextEdit.setText(value.toString());
		}
		else{
			m_pTextEdit.setText("");
		}
	}
	
	/**
	 * Gets the maximum number of characters allowed in the text box.
	 * @return
	 */
	public int getMaxLength()
	{
		return m_MaxLength;
	}
	/**
	 * Sets the maximum number of characters allowed in the text box.
	 * @param value
	 */
	public void setMaxLength(int value)
	{
		if(m_MaxLength != value){
			m_MaxLength = value;
		}
	}

}
