/*
 * Created on Dec 29, 2003
 *
 */
package LumiSoft.UI.Controls;

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import LumiSoft.UI.Controls.Paint;

/**
 * 
 */
public class WButtonEdit_base extends WControl implements MouseListener,MouseMotionListener
{
//	protected boolean m_IsDroppedDwon = false;
			
	private EventListenerList m_pListners      = null;
	private boolean           m_Editable       = true;
	private boolean           m_Selectable     = true;
	private int               m_ButtonAlign    = LeftRight.Right;
	private int               m_ButtonWidth    = 18;
	private Image             m_pButtonImage   = null;
	private boolean           m_DrawBorders    = true;
	private Point             m_MousePos       = new Point(-1,-1);
	private int               m_MouseModifiers = 0;
	private String            m_HittedObject   = "";
	private Dimension         m_Size           = new Dimension(0,0);
	
	/**
	 * Default constructor.
	 *
	 */
	public WButtonEdit_base()
	{
		super();
		
		m_pListners = new EventListenerList();
		
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}
	
	/**
	 * Is called by system when control need to repaint.
	 */
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		if(m_Size != this.getSize()){
			m_Size = this.getSize();
			
			SizeChanged();			
		}
		
		Draw((Graphics2D)g);
	}
	
	protected void SizeChanged()
	{		
	}
	
	private Rectangle getTriangleRect(Rectangle buttonRect)
	{
		return new Rectangle(buttonRect.x + 4,buttonRect.y + 7,buttonRect.width - 7,buttonRect.height - 12);
	}
	
	private void Draw(Graphics2D g)
	{
		Rectangle clientRect = new Rectangle(0,0,this.getSize().width,this.getSize().height);
		boolean hot =  this.isEnabled() && (Utils.ContainsFocus(this) || (clientRect.contains(m_MousePos)));
		
		// Fill backround
		g.setColor(getViewStyle().GetEditColor(!getEditable(),isEnabled(),Utils.ContainsFocus(this)));		
		g.fill(clientRect);
		
		//--- Draw button ------------------------------------------------------------------------------------------
		boolean buttonHot = this.getButtonRect().contains(m_MousePos) && this.isEnabled() && this.getSelectable();
		g.setColor(getViewStyle().GetButtonColor(buttonHot,m_MouseModifiers == MouseEvent.BUTTON1_DOWN_MASK));
		g.fill(this.getButtonRect());
		g.setColor(Color.BLACK);
		
		if(m_pButtonImage == null){
			if(this.isEnabled() && this.getSelectable()){
				g.setColor(Color.BLACK);
			}
			else{
				g.setColor(Color.GRAY);
			}
			Paint.DrawTriangle(g,getTriangleRect(getButtonRect()),Direction.Down);
		}
		else{
			if(this.isEnabled() && this.getSelectable()){
				g.drawImage(m_pButtonImage,this.getButtonRect().x,this.getButtonRect().y,this.getButtonRect().width,this.getButtonRect().height,this);
			}
			else{
				g.drawImage(Paint.CreateGrayedImage(this,m_pButtonImage),this.getButtonRect().x,this.getButtonRect().y,this.getButtonRect().width,this.getButtonRect().height,this);
			}
		}
		//----------------------------------------------------------------------------------------------------------
		
		//--- Draw borders -----------------------------------------------------------------------------------------
		if(m_DrawBorders){
			g.setColor(getViewStyle().GetBorderColor(hot));
			g.drawRect(0,0,this.getWidth() - 1,this.getHeight() - 1);
			
			if(m_ButtonAlign == LeftRight.Left){			
				g.drawLine((int)this.getButtonRect().getMaxX(),0,(int)this.getButtonRect().getMaxX(),this.getHeight());			
			}
			else{
				g.drawLine(this.getButtonRect().x,0,this.getButtonRect().x,this.getHeight());			
			}
		}
		//----------------------------------------------------------------------------------------------------------
	}
	
	
	public void mousePressed(MouseEvent e)
	{
		m_MouseModifiers = e.getModifiersEx();
		
		this.repaint();
	}
	
	public void mouseReleased(MouseEvent e)
	{
		// Reset, we don't need them anymore
		m_MouseModifiers = 0;
		
		if(this.isEnabled() && this.getSelectable() && this.getButtonRect().contains(m_MousePos)){
			RaiseButtonClicked();
		}
		
		this.repaint();
	}
	
	public void mouseEntered(MouseEvent e)
	{
		this.repaint();
	}
	public void mouseExited(MouseEvent e)
	{
		m_MousePos = new Point(-1,-1);
		
		this.repaint();
	}
	
	public void mouseMoved(MouseEvent e)
	{
		m_MousePos = e.getPoint();
		
		String hittedObject = "";
		if(this.getButtonRect().contains(m_MousePos)){
			hittedObject = "Button";
		}
		
		if(m_HittedObject != hittedObject){
			m_HittedObject = hittedObject;
			
			this.repaint();
		}	
	}
	
	public void mouseClicked(MouseEvent e){}	
	public void mouseDragged(MouseEvent e){}
	
	
	private Rectangle getButtonRect()
	{
		if(m_ButtonAlign == LeftRight.Right){
			return new Rectangle(this.getWidth() - m_ButtonWidth,1,m_ButtonWidth - 1,this.getHeight() - 2);
		}
		else{
			return new Rectangle(1,1,m_ButtonWidth - 1,this.getHeight() - 2);			
		}
	}
	
	protected Rectangle getEditRect()
	{
		if(m_ButtonAlign == LeftRight.Right){
			return new Rectangle(2,1,this.getWidth() - this.getButtonRect().width - 4,this.getHeight() - 2);
		}
		else{
			return new Rectangle((int)this.getButtonRect().getMaxX() + 2,1,this.getWidth() - this.getButtonRect().width - 4,this.getHeight() - 2);
		}
	}
	
	
	
	/**
	 * Gets if control is editable.
	 * @return
	 */
	public boolean getEditable()
	{
		return m_Editable;
	}
	/**
	 * Sets if control is editable.
	 * @param value
	 */
	public void setEditable(boolean value)
	{
		if(m_Editable != value){
			m_Editable = value;
		}
	}
	
	/**
	 * Gets if button is enabled.
	 * @return
	 */
	public boolean getSelectable()
	{
		return m_Selectable;
	}
	/**
	 * Sets if button is enabled.
	 * @param value
	 */
	public void setSelectable(boolean value)
	{
		if(m_Selectable != value){
			m_Selectable = value;
			
			repaint();
		}
	}
	
	/**
	 * Gets button image.
	 * @return
	 */
	public Image getButtonImage()
	{
		return m_pButtonImage;
	}
	/**
	 * Sets button image.
	 * @param value
	 */
	public void setButtonImage(Image value)
	{
		if(m_pButtonImage != value){
			m_pButtonImage = value;
		}
	}
	
	/**
	 * Sets if to draw borders.
	 * @param value
	 */
	public void setDrawBorders(boolean value)
	{
		m_DrawBorders = 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.
	 *
	 */
	protected void RaiseButtonClicked()
	{
		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,""));
			}            
		}
	}
}
