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

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

/**
 * Tab control.
 */
public class WTabControl extends JPanel implements IDisposable
{
	private WTabBar          m_pTabBar  = null;	
	private JPanel           m_pTabArea = null;
	private WTabControlPages m_pTabs    = null;

	/**
	 * Default constructor.
	 *
	 */
	public WTabControl()
	{			
		m_pTabBar  = new WTabBar();
		m_pTabs    = new WTabControlPages(this,m_pTabBar);
		
		m_pTabArea = new JPanel();
		m_pTabArea.setLayout(new BorderLayout());
		m_pTabArea.setBorder(BorderFactory.createLoweredBevelBorder());
		
		m_pTabBar.addTabChangedListener(new WTabListener(){
			public void TabChanged(Object sender,WTabBarItem newTab,WTabBarItem oldTab){
				OnTabChanged(sender,newTab,oldTab);
			}
		});
				
		this.add(m_pTabBar);
		this.add(m_pTabArea);
			
		this.setLayout(null);
	}
	
	/**
	 * Clean up any resources being used.
	 */
	public void Dispose()
	{
		for(int i=0;i<m_pTabs.getCount();i++){
			WTabControlPage tabPage = m_pTabs.get(i);
			if(tabPage.getTabPage() instanceof IDisposable){
				((IDisposable)tabPage.getTabPage()).Dispose();
			}
		}
		
		m_pTabBar.Dispose();
	}
	
	/**
	 * Is called by system when control need to repaint.
	 */
	public void paintComponent(Graphics g)
	{	
		super.paintComponent(g);
		
		m_pTabBar.setLocation(0,0);
		m_pTabBar.setSize(this.getWidth(),24);
		m_pTabArea.setLocation(0,25);
		m_pTabArea.setSize(this.getWidth(),this.getHeight() - 25);		
	}
	
	
	private void OnTabChanged(Object sender,WTabBarItem newTab,WTabBarItem oldTab)
	{
		if(oldTab != null){
			Component c = ((WTabControlPage)oldTab.getTag()).getTabPage();
			c.setVisible(false);
			m_pTabArea.remove(c);
		}

		if(newTab != null){
			WTabControlPage tabPage = (WTabControlPage)newTab.getTag();
			Component c = tabPage.getTabPage();
			c.setSize(m_pTabArea.getSize());
			c.setVisible(true);
			m_pTabArea.add(c,BorderLayout.CENTER);
			m_pTabArea.doLayout();
		}
	}
	
	
	/**
	 * Makes first tab visible
	 *
	 */
	public void SelectFirstTab()
	{
		m_pTabBar.SelectFirstTab();
	}
	
	
	/**
	 * Gets UI text getting source.
	 * @return
	 */
	public WText getWText()
	{
		return m_pTabBar.getWText();
	}
	/**
	 * Sets UI text getting source.
	 * @param value
	 */
	public void setWText(WText value)
	{
		m_pTabBar.setWText(value);
	}
	
	/**
	 * Gets tab pages collection.
	 * @return
	 */
	public WTabControlPages getTabPages()
	{
		return m_pTabs;
	}

}
