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

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/**
 * Frame control.
 */
public class WFrame extends WControl implements HierarchyBoundsListener,IDisposable
{
	private JSplitPane m_pSplitter    = null;
	private JPanel     m_pContolPane  = null;
	private JPanel     m_pFormPane    = null;
	private JPanel     m_pToolbarPane = null;
	private Component  m_pPaneControl = null;
	private Component  m_pPaneToolbar = null;
	private Component  m_pPaneForm    = null;
	private WButton    m_pToggleBtn   = null;
	
	private int        m_ControlPaneWidth = 160;
	
	/**
	 * Default constructor.
	 *
	 */
	public WFrame()
	{
		this.setLayout(null);
		this.setSize(200,400);
		this.setBackground(ViewStyle.getstaticViewStyle().getFormColor());
		
		m_pContolPane = new JPanel();
		m_pContolPane.setLayout(new BorderLayout());
		m_pContolPane.setLocation(0,25);
		m_pContolPane.setBackground(ViewStyle.getstaticViewStyle().getFormColor());
		
		m_pFormPane = new JPanel();
		m_pFormPane.setLayout(new BorderLayout());
		m_pFormPane.setLocation(0,25);
		m_pFormPane.setBackground(ViewStyle.getstaticViewStyle().getFormColor());
		
		m_pToolbarPane = new JPanel();
		m_pToolbarPane.setLayout(new BorderLayout());
		m_pToolbarPane.setLocation(0,0);
		m_pToolbarPane.setBackground(ViewStyle.getstaticViewStyle().getFormColor());
		
		m_pToggleBtn = new WButton();		
		m_pToggleBtn.setText("<<");
		m_pToggleBtn.setSize(30,16);
		m_pToggleBtn.addClickedListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				OnToggleButtonPressed();
			}
		});
				
		m_pSplitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,m_pContolPane,m_pFormPane);
		m_pSplitter.setLocation(0,25);
	//	m_pSplitter.setBackground(ViewStyle.getstaticViewStyle().getFormColor());
		
		this.add(m_pToolbarPane);
		this.add(m_pSplitter);
		this.add(m_pToggleBtn);
		this.addHierarchyBoundsListener(this);
					
		ShowBar();
	}
	
	/**
	 * Clean up any resources being used.
	 */
	public void Dispose()
	{
		if(m_pPaneForm != null && m_pPaneForm instanceof IDisposable){
			m_pPaneForm.setVisible(false);
			((IDisposable)m_pPaneForm).Dispose();
		}
		if(m_pPaneToolbar != null && m_pPaneToolbar instanceof IDisposable){
			((IDisposable)m_pPaneToolbar).Dispose();
		}
		m_pPaneForm = null;
	}
	
		
	private void OnToggleButtonPressed()
	{
		if(m_pToggleBtn.getText() == "<<"){
			HideBar();
		}
		else{
			ShowBar();
		}		
	}
	
	public void ancestorResized(HierarchyEvent e)
	{	
		Layout();
	}
	
	public void ancestorMoved(HierarchyEvent e){}
		
	private void Layout()
	{
		if(m_pSplitter.getDividerSize() > 0){
			m_pToggleBtn.setLocation(m_pSplitter.getDividerLocation() - m_pToggleBtn.getWidth(),5);
			
			m_pToolbarPane.setLocation(m_pSplitter.getDividerLocation() + m_pSplitter.getDividerSize(),0);
			m_pToolbarPane.setSize(this.getWidth() - m_pSplitter.getDividerLocation(),22);
			
			m_ControlPaneWidth = m_pSplitter.getDividerLocation();
		}
		else{
			m_pToggleBtn.setLocation(5,5);
			m_pToolbarPane.setLocation((int)m_pToggleBtn.getBounds().getMaxX() + 10,0);
			m_pToolbarPane.setSize(this.getWidth() - m_pToolbarPane.getBounds().x,25);
		}
		m_pSplitter.setSize(this.getWidth(),this.getHeight() - 25);
		
		m_pSplitter.doLayout();
		m_pContolPane.doLayout();
		m_pFormPane.doLayout();
		m_pToolbarPane.doLayout();
	}
	
	public void ShowBar()
	{
	//	if(m_pSplitter.getDividerSize() == 0){
			m_pToggleBtn.setText("<<");
			m_pSplitter.setDividerSize(6);
			m_pSplitter.setDividerLocation(m_ControlPaneWidth);
			
			Layout();
	//	}
	}
	
	public void HideBar()
	{
		int x = (int)m_pToggleBtn.getBounds().getMaxX();
		if(m_pSplitter.getDividerSize() > 0){			
			m_pToggleBtn.setText(">>");
			m_pSplitter.setDividerSize(0);
			m_pSplitter.setDividerLocation(0);
			
			Layout();
		}
	}
	
	
	public void setControlPaneWidth(int value)
	{
		if(m_ControlPaneWidth != value){
			m_ControlPaneWidth = value;
			
			m_pSplitter.setDividerLocation(m_ControlPaneWidth);
		}
	}
	
	/**
	 * Gets if splitter locked.
	 * @return
	 */
	public boolean getSplitterLocked()
	{
		return m_pSplitter.isEnabled();
	}	
	/**
	 * Sets if splitter locked.
	 * @param value
	 */
	public void setSplitterLocked(boolean value)
	{
		m_pSplitter.setEnabled(value);
	}
	
	/**
	 * Gets splitter width.
	 * @return
	 */
	public int getSplitterWidth()
	{
		return m_pSplitter.getDividerSize();
	}
	/**
	 * Sets splitter width.
	 * @param value
	 */
	public void setSplitterWidth(int value)
	{
		m_pSplitter.setDividerSize(value);
	}
	
	public void setToolBar(Component value)
	{
		// Remove old form
		if(m_pPaneToolbar != null){
			m_pToolbarPane.remove(m_pPaneToolbar);
			m_pPaneToolbar.setVisible(false);
			if(m_pPaneToolbar instanceof IDisposable){
				((IDisposable)m_pPaneToolbar).Dispose();
			}
		}
		
		m_pPaneToolbar = value;
		m_pPaneToolbar.setLocation(0,0);
		m_pToolbarPane.add(value,BorderLayout.CENTER);
		
		Layout();
	}
	
	public void setForm(Component value)
	{
		// Remove old form
		if(m_pPaneForm != null){
			m_pFormPane.remove(m_pPaneForm);
			m_pPaneForm.setVisible(false);
			if(m_pPaneForm instanceof IDisposable){
				((IDisposable)m_pPaneForm).Dispose();
			}
			m_pPaneForm = null;
		}
		
		m_pPaneForm = value;
	//	m_pPaneForm.setLocation(0,0);
		m_pPaneForm.setSize(1,1);
		m_pPaneForm.setVisible(true);
		m_pFormPane.add(value,BorderLayout.CENTER);
			
		Layout();
	}
	
	public void setControl(Component value)
	{		
		// Remove old control
		if(m_pPaneControl != null){
			this.remove(m_pPaneControl);
			
			m_pPaneControl.removeHierarchyBoundsListener(this);
		}
		
		m_pPaneControl = value;
		m_pPaneControl.setLocation(0,0);
		m_pContolPane.add(value,BorderLayout.CENTER);
		
		m_pPaneControl.addHierarchyBoundsListener(this);
		
		Layout();
	}

}
