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

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.SwingUtilities;
import javax.swing.event.EventListenerList;

/**
 * Hyperlink control 
 */
public class WHyperlink extends WControl
{
	private EventListenerList m_pListners = null;
	private String            m_Text      = "";
	private int               m_TextAlign = HorizontalAlignment.Center;
	private boolean           m_MouseIn   = false;
	
	/**
	 * Default constructor.
	 *
	 */
	public WHyperlink()
	{
		m_pListners = new EventListenerList();
	
		this.addMouseListener(new MouseAdapter(){
			public void mouseEntered(MouseEvent e){		
				OnMouseEntered();
			}
			
			public void mouseExited(MouseEvent e){
				OnMouseExited();
			}
			
			public void mouseReleased(MouseEvent e){
				OnMouseReleased();
			}
		});
	}
	
	/**
	 * Is called by system when control need to repaint.
	 */
	public void paintComponent(Graphics g)
	{
		// Create offscreen graphics	
		BufferedImage paintBuffer = (BufferedImage)this.createImage(this.getBounds().width,this.getBounds().height);
		Graphics2D g2 = paintBuffer.createGraphics();
		
		// Draw offscreen
		Paint(g2,m_MouseIn);
		g2.dispose();
		
		// Draw offscreen image
		g.drawImage(paintBuffer,0,0,this);	
	}
	
	/**
	 * Paints this control.
	 * @param g
	 */
	private void Paint(Graphics2D g,boolean hot)
	{
		Rectangle txtRect = new Rectangle(0,0,this.getWidth() - 4,this.getHeight() - 2);
	
		// Fill backround
	//	if(isOpaque()){
			g.setColor(ViewStyle.getstaticViewStyle().getFormColor());
			g.fillRect(0,0,this.getWidth(),this.getHeight());
	//	}
		
		// Draw text
//		if(hot){
//			g.setColor(Color.MAGENTA);
//		}
//		else{
			g.setColor(Color.BLUE);
//		}
		Paint.DrawText((Graphics2D)g,this.getFont(),this.getText(),txtRect,m_TextAlign);
		
	//	g.drawLine(0,getHeight() - 1,getWidth(),getHeight() - 1);
	}
	
	
	/**
	 * Is callled when mouse entered.
	 *
	 */
	private void OnMouseEntered()
	{
		m_MouseIn = true;
		repaint();
		
		SwingUtilities.getRoot(this).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}
	
	/**
	 * Is called when mouse leaved.
	 *
	 */
	private void OnMouseExited()
	{
		m_MouseIn = false;
		repaint();
		
		SwingUtilities.getRoot(this).setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}
	
	/**
	 * Is called when mouse is released.
	 *
	 */
	private void OnMouseReleased()
	{
		RaiseClicked();
	}
	
	
	/**
	 * Gets text.
	 * @return
	 */
	public String getText()
	{
		return m_Text;
	}
	/**
	 * Sets text.
	 * @param value
	 */
	public void setText(String value)
	{
		if(!m_Text.equals(value)){
			m_Text = value;
		}
	}
	
	/** 
	 * Gets text alingment.
	 */
	public int getTextAlign()
	{
		return m_TextAlign;
	}
	/** 
	 * Sets text alignment.
	 * @param value Value to set. 
	 * 
	 * @see LumiSoft.UI.Controls.HorizontalAlignment
	 */
	public void setTextAlign(int value)
	{
		if(m_TextAlign != value){
			m_TextAlign = value;
			this.repaint();
		}
	}
	
	
	/**
	 * Adds the specified action listener to receive action events
	 * from this button.
	 * @param l the action listener
	 */ 
	public void addClickedListener(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 removeClickedListener(ActionListener l) 
	{
		m_pListners.remove(ActionListener.class,l);
	}
	
	/**
	 * Raises clicked event.
	 *
	 */
	protected void RaiseClicked()
	{
		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,""));
			}            
		}
	}

}
