/*
 * Created on Feb 27, 2004
 *
 */

package LumiSoft.UI.Controls;

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

/**
 * ButtonEdit control.
 */
public class WButtonEdit extends WButtonEdit_base implements FocusListener
{
	private JTextField m_pTextbox     = null;
	private int        m_MaxLength    = 9999999;
	private Timer      m_pTimer       = null;
	private int        m_FlashCounter = 0;	
	
	/**
	 * Default constructor.
	 *
	 */
	public WButtonEdit()
	{
		m_pTextbox = new JTextField();
		m_pTextbox.setBorder(null);
		m_pTextbox.setDisabledTextColor(getViewStyle().getEditDisabledTextColor());
		m_pTextbox.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				On_m_pTextbox_KeyPressed(e);
			}
			
			public void keyReleased(KeyEvent e){
				On_m_pTextbox_KeyReleased(e);
			}
			
			public void keyTyped(KeyEvent e){
				On_m_pTextbox_KeyTyped(e);
			}
		});
		m_pTextbox.addFocusListener(this);
		m_pTextbox.addMouseListener(this);
		m_pTextbox.addMouseMotionListener(this);
		
		m_pTimer = new Timer(200,new ActionListener(){
			public void actionPerformed(ActionEvent evt){
		         On_m_pTimer_Tick();
		    }
		});
		
		this.addFocusListener(this);
		this.add(m_pTextbox);
	}
	
	
	/**
	 * Is called when size has changed.
	 */
	protected void SizeChanged()
	{	
		m_pTextbox.setLocation(this.getEditRect().getLocation());		
		m_pTextbox.setSize(this.getEditRect().getSize());
	}
	
	/**
	 * Is called when ViewStyle has changed.
	 *
	 */
	protected void OnViewStyleChaned()
	{
		super.OnViewStyleChaned();
		
		m_pTextbox.setBackground(getViewStyle().GetEditColor(!this.getEditable(),this.isEnabled(),m_pTextbox.hasFocus()));
		m_pTextbox.setDisabledTextColor(getViewStyle().getEditDisabledTextColor());
	}
		
	/**
	 * Is called when key has pressed down.
	 * @param e
	 */
	private void On_m_pTextbox_KeyPressed(KeyEvent e)
	{
		// Maximum allowed text length exceeded
		if(getText().length() >= getMaxLength()){
			e.consume();
		}
		
		// If read only, allow only arrows to be used
		if(!this.getEditable()){
			if(!ReadOnlyAllowedKey(e)){
				e.consume();				
			}
		}
		
		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 released.
	 * @param e
	 */
	private void On_m_pTextbox_KeyReleased(KeyEvent e)
	{
		// If read only, allow only arrows to be used
		if(!this.getEditable()){
			if(!ReadOnlyAllowedKey(e)){
				e.consume();				
			}
		}
		
		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);			            
		}
	}
	
	/**
	 * Is called when key has typed down.
	 * @param e
	 */
	private void On_m_pTextbox_KeyTyped(KeyEvent e)
	{
		// If read only, allow only arrows to be used
		if(!this.getEditable()){
			if(!ReadOnlyAllowedKey(e)){
				e.consume();				
			}
		}
		
		if(e.getKeyChar() == '+'){
			RaiseButtonClicked();
			e.consume();
		}
		
		KeyListener[] listeners = this.getKeyListeners();
		for(int i=0;i < listeners.length;i++){
			// pass the event to the listeners event dispatch method
			listeners[i].keyTyped(e);			            
		}
	}
	
	public void focusGained(FocusEvent e)
	{				
		if(!m_pTextbox.hasFocus()){
			m_pTextbox.grabFocus();
		}
		m_pTextbox.setBackground(getViewStyle().GetEditColor(!getEditable(),this.isEnabled(),true));
	
		this.repaint();
	}
	
	public void focusLost(FocusEvent e)
	{
		m_pTextbox.setBackground(getViewStyle().GetEditColor(!getEditable(),this.isEnabled(),false));
		
		this.repaint();
	}
	
	/**
	 * Is called when timer elapsed.
	 *
	 */
	private void On_m_pTimer_Tick()
	{
		if(m_pTextbox.getBackground() == getViewStyle().getFlashColor()){
			m_pTextbox.setBackground(getViewStyle().GetEditColor(!getEditable(),this.isEnabled(),hasFocus()));
		}
		else{
			m_pTextbox.setBackground(getViewStyle().getFlashColor());
		}
		
		m_FlashCounter++;
		
		if(m_FlashCounter > 5){
			m_pTimer.stop();
		}
	}
	
	
	/**
	 * Flashes control.
	 */
	public void FlashControl()
	{	
		if(!m_pTimer.isRunning()){
			m_FlashCounter = 0;
			m_pTimer.start();
		}
	}
	
		
	/**
	 * Checks if specified key is allowed when this is readonly.
	 * @param e
	 * @return Returns true if specified key is allowed.
	 */
	private boolean ReadOnlyAllowedKey(KeyEvent e)
	{
		if(e.getKeyCode() == KeyEvent.VK_LEFT){
			return true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
			return true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP){
			return true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_DOWN){
			return true;
		}
		
		return false;
	}
	
	
	/**
	 * Sets if control is enabled.
	 */
	public void setEnabled(boolean value)
	{
		if(this.isEnabled() != value){
			super.setEnabled(value);
			m_pTextbox.setEnabled(value);
			m_pTextbox.setBackground(getViewStyle().GetEditColor(false,value));
			
			this.repaint();
		}		
	}
	
	/**
	 * Gets if control is editable.
	 */
	public void setEditable(boolean value)
	{
		m_pTextbox.setBackground(getViewStyle().GetEditColor(!value,this.isEnabled(),this.hasFocus()));
		super.setEditable(value);
	}
	
	/**
	 * Gets text.
	 * @return
	 */
	public String getText()
	{
		return m_pTextbox.getText();
	}
	/**
	 * Sets text.
	 * @param value
	 */
	public void setText(String value)
	{
		m_pTextbox.setText(value);
	}
	
	/**
	 * Gets font.
	 */
	public Font getFont()
	{
		if(m_pTextbox != null){
			return m_pTextbox.getFont();
		}
		else{
			return super.getFont();
		}
	}
	/**
	 * Sets font.
	 */
	public void setFont(Font value)
	{
		if(m_pTextbox != null){
			m_pTextbox.setFont(value);
		}
	}
	
	/**
	 * 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;
		}
	}
}
