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

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

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

/**
 * Grid button edit control.
 * 
 */
public class WButtonEditEditor extends WBaseEditor
{
	private EventListenerList m_pListners = null;
	private WButtonEdit       m_pTextEdit = null;
//	private int               m_MaxLength  = 9999999;
	
	/**
	 * Default constructor.
	 *
	 */
	public WButtonEditEditor()
	{	
		m_pListners = new EventListenerList();
		 
		m_pTextEdit = new WButtonEdit();
		m_pTextEdit.setLocation(0,0);
		m_pTextEdit.setBorder(null);
		m_pTextEdit.setDrawBorders(false);
		m_pTextEdit.addButtonClickedListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				RaiseButtonPressed();
			}
		});
		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();
				}
			}
			
			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)
	{	
	}
*/
	
	/**
	 * 
	 */
	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);
	}
	
	
	/**
	 * Selects editor value if editor supports it.
	 *
	 */
	public void SelectAll()
	{
	//	m_pTextEdit.
	}
	
	
	/**
	 * 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_pTextEdit.getMaxLength();
	}
	/**
	 * Sets the maximum number of characters allowed in the text box.
	 * @param value
	 */
	public void setMaxLength(int value)
	{
		m_pTextEdit.setMaxLength(value);
	}
	
	
	/**
	 * Adds the specified action listener to receive action events
	 * from this button.
	 * @param l the action listener
	 */ 
	public void addButtonClickedListener(ActionListener l) 
	{
		m_pListners.add(ActionListener.class,l);
	}

	/**
	 * Removes the specified action listener so it no longer receives
	 * action events from this button.
	 * @param l the action listener
	 */ 
	public void removeButtonClickedListener(ActionListener l) 
	{
		m_pListners.remove(ActionListener.class,l);
	}
	
	/**
	 * Raises ButtonClicked event.
	 *
	 */
	private void RaiseButtonPressed()
	{
		Object[] listeners = m_pListners.getListenerList();
		// loop through each listener and pass on the event if needed		
		for(int i=0;i < listeners.length;i++){
			if(listeners[i] == ActionListener.class){
				// pass the event to the listeners event dispatch method
				((ActionListener)listeners[i+1]).actionPerformed(new ActionEvent(this,1,""));
			}            
		}
	}
}
