/*
 * Created on 21.12.2004
 *
 */
package LumiSoft.UI.Controls;

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

/**
 * Color picker control.
 */
public class WColorPicker extends WButtonEdit_base implements FocusListener
{
	private Color m_Color = Color.BLUE;
	
	private JTextField m_pTextbox = null;
	
	/**
	 * Default constructor.
	 *
	 */
	public WColorPicker()
	{				
		m_pTextbox = new JTextField();
		m_pTextbox.setBorder(null);
		m_pTextbox.setDisabledTextColor(Color.LIGHT_GRAY);
		m_pTextbox.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				On_m_pTextbox_KeyPressed(e);
			}
			
			public void keyTyped(KeyEvent e){
				On_m_pTextbox_KeyTyped(e);
			}
		});
		m_pTextbox.addFocusListener(this);
		
		this.addFocusListener(this);
		this.add(m_pTextbox);
		
		SetColor();
	}
	
	
	/**
	 * Is called by system when control need to repaint.
	 */
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		g.setColor(m_Color);
		g.fillRect(5,5,22,getHeight() - 10);
		g.setColor(Color.BLACK);
		g.drawRect(5,5,22,getHeight() - 10);
	}
	
	/**
	 * Is called when control size has changed.
	 */
	protected void SizeChanged()
	{	
		m_pTextbox.setLocation(getEditRect().getLocation().x + 30,getEditRect().getLocation().y);		
		m_pTextbox.setSize(getEditRect().getSize().width - 30,getEditRect().getSize().height);
	}
	
	protected void RaiseButtonClicked()
	{
		super.RaiseButtonClicked();
		
		Color c = JColorChooser.showDialog(this,"",getColor());
		if(c != null){
			setColor(c);
		}		
	}
	
	public void focusGained(FocusEvent e)
	{
		if(!m_pTextbox.hasFocus()){
			m_pTextbox.grabFocus();
		}
		m_pTextbox.selectAll();
		m_pTextbox.setBackground(getViewStyle().GetEditColor(false,this.isEnabled(),true));
		
		this.repaint();
	}
	
	public void focusLost(FocusEvent e)
	{
		m_pTextbox.setBackground(getViewStyle().GetEditColor(false,this.isEnabled(),false));
		
		this.repaint();
	}
	
	/**
	 * Is called when key has pressed down.
	 * @param e
	 */
	public void On_m_pTextbox_KeyPressed(KeyEvent e)
	{	
		if(e.getKeyChar() == '\n'){
			SetColor();
		}
		
		KeyListener[] listeners = this.getKeyListeners();
		for(int i=0;i < listeners.length;i++){
			// pass the event to the listeners event dispatch method
			listeners[i].keyPressed(e);			            
		}	
	}
	
	/**
	 * Is called when key has typed down.
	 * @param e
	 */
	private void On_m_pTextbox_KeyTyped(KeyEvent e)
	{
		KeyListener[] listeners = this.getKeyListeners();
		for(int i=0;i < listeners.length;i++){
			// pass the event to the listeners event dispatch method
			listeners[i].keyReleased(e);			            
		}
		
		if(e.getKeyChar() == ';'){			
		}
		else if(e.getKeyChar() == '+'){
			RaiseButtonClicked();
			e.consume();
		}			
		else if(Character.isDigit(e.getKeyChar())){			
		}
	//	else if(e.getKeyChar() == '\n'){
	//		SetColor();
	//	}
		else{
			e.consume();
		}
	}
	
	
	private void SetColor()
	{
		try{
			String[] rgb = m_pTextbox.getText().split(";");
			m_Color = ViewStyle.GetColorFromRgb(Integer.parseInt(rgb[0]),Integer.parseInt(rgb[1]),Integer.parseInt(rgb[2]));
			repaint();
		}
		catch(Exception x){
			m_pTextbox.setText(m_Color.getRed() + ";" + m_Color.getGreen() + ";" + m_Color.getBlue());
		}		
	}
	
	
	/**
	 * Gets current color.
	 * @return
	 */
	public Color getColor()
	{
		return m_Color;
	}
	/**
	 * Sets current color.
	 * @param value
	 */
	public void setColor(Color value)
	{
		if(m_Color != value){
			m_Color = value;
			
			m_pTextbox.setText(m_Color.getRed() + ";" + m_Color.getGreen() + ";" + m_Color.getBlue());
		}
	}
	
	
	/**
	 * Gets current color in 32-bit argb format.
	 * @return
	 */
	public int getColorArgb()
	{
		return ViewStyle.ColorToArgb(m_Color);
	}
	/**
	 * Sets current color in 32-bit argb format.
	 * @param value
	 */
	public void setColorArgb(int value)
	{
		setColor(ViewStyle.ColorFromArgb(value));
	}

}
