/*
 * Created on 29.12.2004
 *
 */
package LumiSoft.UI.Controls.Grid.Editors;

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

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

/**
 * Color picker grid editor.
 */
public class WColorPickerEditor extends WBaseEditor
{
	private WColorPicker m_pColorPicker = null;
	
	/**
	 * Default constructor.
	 *
	 */
	public WColorPickerEditor()
	{
		this.setLayout(null);
	//	this.setFocusable(false);
		
		m_pColorPicker = new WColorPicker();
		m_pColorPicker.setLocation(0,0);
		m_pColorPicker.setBorder(null);
	//	m_pTextEdit.setDrawBorders(false);
		m_pColorPicker.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){								
			}
		});
				
		this.add(m_pColorPicker);
	}
	
	
	/**
	 * Paints default editor. This is called foreach visible cell what isn't active editor.
	 * @param g Graphics where to draw.
	 * @param value Cell value.
	 * @param cellBounds Cell bounds.
	 * @param evenrow Specifies if row is even row (2,4,6...).
	 * @param selected Specifies if cell is selected.
	 * @param column Cell owner column.
	 */
	public void PaintDefault(Graphics2D g,Object value,Rectangle cellBounds,boolean evenrow,boolean selected,WGridColumn column)
	{
		g.setColor(ViewStyle.ColorFromArgb(Integer.parseInt(value.toString())));
		g.fillRect(cellBounds.x + 5,cellBounds.y + 5,22,cellBounds.height - 10);
		g.setColor(Color.BLACK);
		g.drawRect(cellBounds.x + 5,cellBounds.y + 5,22,cellBounds.height - 10);
		
		Color color = ViewStyle.ColorFromArgb(Integer.parseInt(value.toString()));
		String s = color.getRed() + ";" + color.getGreen() + ";" + color.getBlue();		
		g.setColor(Color.BLACK);
		g.drawString(s,cellBounds.x + 32,cellBounds.y + 14);
	}
	
	
	/**
	 * 
	 */
	public void StartEdit(WGridTableView view,Object value)
	{				
		m_pColorPicker.setBounds(0,0,this.getSize().width,this.getSize().height);
		m_pColorPicker.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_pColorPicker.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 Integer(0);
		}
		
		if(value.getClass() != Integer.class){
			value = Integer.valueOf(value.toString());
		}
		
		super.setValue(value);
	}
	
	/**
	 * Gets value what is currently visible in editor.
	 */
	public Object getEditValue()
	{
		return new Integer(m_pColorPicker.getColorArgb());
	}
	/**
	 * Sets value what is currently visible in editor.
	 */
	public void setEditValue(Object value)
	{
		m_pColorPicker.setColorArgb(Integer.parseInt(value.toString()));
		
	/*	try{
			if(value != null){
				m_pColorPicker.setArgb(Integer.parseInt(value.toString()));
			}
			else{
				m_pColorPicker.setArgb(0);
			}
		}
		catch(Exception x){
			m_pColorPicker.setArgb(0);
		}*/
	}

}
