/*
 * Created on Mar 22, 2004
 *
 */
package LumiSoft.UI.Controls;

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

/**
 * StatusBar control.
 */
public class WStatusBar extends WControl
{
	private WStatusBarPanelCollection m_pPanels        = null;
	private BufferedImage             m_pPaintBuffer   = null;
	private Graphics2D                m_pPaintGraphics = null;
	
	/**
	 * Default constructoe.
	 *
	 */
	public WStatusBar()
	{
		m_pPanels = new WStatusBarPanelCollection(this);
	}
	
	/**
	 * Clean up any resources being used.
	 */
	public void Dispose()
	{	
		super.Dispose();
		
		if(m_pPaintGraphics != null){
			m_pPaintGraphics.dispose();
			m_pPaintGraphics = null;
		}
		m_pPaintBuffer = null;
	}
	
	
	/**
	 * Is called when ViewStyle has changed.
	 *
	 */
	protected void OnViewStyleChaned()
	{
		if(m_pPaintGraphics != null){
			m_pPaintGraphics.dispose();
			m_pPaintGraphics = null;
		}
		m_pPaintBuffer = null;
		
		repaint();
	}
	
	/**
	 * Is called by system when control need to repaint.
	 */
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		// All painting is done to offscreen image, we just need to paint
		// offscreen image to Graphics.
		
		// This is first time paint or size changed, we need to create new offscreen image
		if(m_pPaintBuffer == null || (m_pPaintBuffer.getWidth() != this.getBounds().width || m_pPaintBuffer.getHeight() != this.getBounds().height)){
			m_pPaintBuffer = (BufferedImage)this.createImage(this.getBounds().width,this.getBounds().height);
		
			// Release old paint graphics, if exists
			if(m_pPaintGraphics != null){
				m_pPaintGraphics.dispose();
				m_pPaintGraphics = null;
			}
						
			Paint(GetPaintGraphics());
		}
		
		// Draw offscreen image
		g.drawImage(m_pPaintBuffer,0,0,this);
	}
	
	/**
	 * Paints status bar to specified Graphics object.
	 * @param g
	 */
	private void Paint(Graphics2D g)
	{
		// Fill background
		g.setColor(getViewStyle().getFormColor());
		g.fillRect(0,0,this.getWidth(),this.getHeight());
			
		int startX = 2;
		boolean autoSizeDone = false;
		for(int i=0;i<getPanels().getCount();i++){
			 WStatusBarPanel panel = getPanels().get(i);
				 
			 //--- Auto size panel ------------------------------------------//
			 int width = panel.getWidth();
			 if(!autoSizeDone && panel.getAutoSize()){
			 	int rightSidePanlesWidth = 0;
			 	for(int a=i+1;a<getPanels().getCount();a++){
			 		rightSidePanlesWidth += getPanels().get(a).getWidth() + 3;
			 	}
			 	width = this.getWidth() - startX - rightSidePanlesWidth - 4;
			 	autoSizeDone = true;
			 }
			 //---------------------------------------------------------------//
				 
			 // Paint icon
			 int textStartX = startX;
			 if(panel.getIcon() != null){
			 	panel.getIcon().paintIcon(this,g,startX,2);
			 	textStartX += panel.getIcon().getIconWidth();
			 }
				 
			 // Paint text
			 g.setColor(Color.BLACK);
			 Paint.DrawText((Graphics2D)g,this.getFont(),panel.getText(),new Rectangle(textStartX + 2,2,width - 4,this.getHeight() - 4),HorizontalAlignment.Left);
			
			 // Paint border
			 g.setColor(ViewStyle.GetColorFromRgb(128,128,128));
			 g.drawRect(startX,3,width,this.getHeight() - 6);
				 
			 startX += width + 3;
		}
	}
	
	/**
	 * Gets Graphics object what must be used for painting.
	 * @return
	 */
	protected Graphics2D GetPaintGraphics()
	{
		if(m_pPaintBuffer != null && m_pPaintGraphics == null){
			m_pPaintGraphics = m_pPaintBuffer.createGraphics();
		}

		return m_pPaintGraphics;
	}
	
	
	/**
	 * Is called from from WStatusBarPanel when some property changed.
	 *
	 */
	protected void UI_Changed()
	{
		if(!this.isVisible()){
			return;
		}
		
		Graphics2D g = GetPaintGraphics();
		if(g != null){
			Paint(g);
		}
		this.repaint();
	}
	
	
	/**
	 * Gets the collection of StatusBar panels contained within the control.
	 * @return
	 */
	public WStatusBarPanelCollection getPanels()
	{
		return m_pPanels;
	}
}
