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

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Label control.
 */
public class WLabel extends WControl
{
	private String m_Text      = "";
	private String m_TextID    = "";
	private int    m_TextAlign = HorizontalAlignment.Center;
	
	/**
	 * Default constructor.
	 */
	public WLabel()
	{
		super();
		
		this.setFont(new Font("SansSerif",Font.PLAIN,12));
		this.setFocusable(false);
	}
			
	/**
	 * Clean up any resources being used.
	 */
	public void Dispose()
	{
		super.Dispose();
	}
	
	
	/**
	 * Is called by system when control need to repaint.
	 */
	public void paintComponent(Graphics g)
	{	
		super.paintComponent(g);
		
		// Create offscreen graphics
		BufferedImage paintBuffer = (BufferedImage)this.createImage(this.getBounds().width,this.getBounds().height);
		Graphics2D g2 = paintBuffer.createGraphics();
		
		// Draw offscreen
		Paint(g2);
		g2.dispose();
		
		// Draw offscreen image
		g.drawImage(paintBuffer,0,0,this);
	}
	
	/**
	 * Paints this control.
	 * @param g
	 */
	private void Paint(Graphics2D g)
	{
		Rectangle txtRect = new Rectangle(0,0,this.getWidth() - 4,this.getHeight() - 2);
		
		// Fill backround
		g.setColor(ViewStyle.getstaticViewStyle().getFormColor());
		g.fillRect(0,0,this.getWidth(),this.getHeight());
		
		// Draw Shadow
		g.setColor(getViewStyle().getTextShadowColor());
		Rectangle sRect = new Rectangle(txtRect.x + 2,txtRect.y + 2,txtRect.width,txtRect.height);
		Paint.DrawText((Graphics2D)g,this.getFont(),this.getText(),sRect,m_TextAlign);
		
		// Draw 3d effect
		g.setColor(getViewStyle().getText3DColor());
		Rectangle dRect = new Rectangle(txtRect.x + 1,txtRect.y + 1,txtRect.width,txtRect.height);
		Paint.DrawText((Graphics2D)g,this.getFont(),this.getText(),dRect,m_TextAlign);
		
		// Draw normal text
		g.setColor(getViewStyle().getTextColor());
		Paint.DrawText((Graphics2D)g,this.getFont(),this.getText(),txtRect,m_TextAlign);
	}
	
	
	/**
	 * Is called when UI language has changed.
	 */
	protected void OnLanguageChanged()
	{
		this.repaint();
	}
	
	
	/**
	 * Sets text info and language controller.
	 * @param text
	 * @param textID
	 * @param wText
	 */
	public void SetTextInfo(String text,String textID,WText wText)
	{
		this.setText(text);
		this.setTextID(textID);
		this.setWText(wText);
	}
	
	
	/** 
	 * Gets label text.
	 */
	public String getText()
	{
		if(getWText() != null && getTextID().length() > 0){
			return getWText().get(getTextID());
		}
		else{
			return m_Text;
		}
	}
	/** 
	 * Sets label text.
	 * @param value Value to set.
	 */
	public void setText(String value)
	{
		if(m_Text != value){
			m_Text = value;
			this.repaint();
		}
	}
	
	/**
	 * Gets textID.
	 * @return
	 */
	public String getTextID()
	{
		return m_TextID;
	}
	/**
	 * Sets textID.
	 * @param value
	 */
	public void setTextID(String value)
	{
		m_TextID = value;
	}
	
	/** 
	 * Gets text alignment.
	 */
	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();
		}
	}	
}
