/*
 * Created on Apr 26, 2004
 *
 */
package LumiSoft.UI.Controls;

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

import javax.swing.JPanel;

/**
 * Base Control what supports IDisposable,ViewStyle,WText and Anchouring. 
 */
public class WControl extends JPanel implements IDisposable
{
	private int            m_AnchorStyle           = AnchorStyles.Top | AnchorStyles.Left;
	private Rectangle      m_pOriginalParentBounds = null;
	private Rectangle      m_pOriginalBounds       = null;
	private ViewStyle      m_ViewStyle             = null;
	private ActionListener m_pViewStyleListener    = null;
	private WText          m_pWText                = null;
	private ActionListener m_pLanguageListener     = null;
	
	/**
	 * Default constructor.
	 *
	 */
	public WControl()
	{	
		setViewStyle(ViewStyle.getstaticViewStyle());
		
		this.addHierarchyBoundsListener(new HierarchyBoundsAdapter(){
			public void ancestorResized(HierarchyEvent e){
				DoAnchor();
			}
		});
		this.addHierarchyListener(new HierarchyListener(){
			public void hierarchyChanged(HierarchyEvent e){					
				if(m_pOriginalParentBounds == null && e.getChangeFlags() == HierarchyEvent.PARENT_CHANGED){
					m_pOriginalParentBounds = e.getChangedParent().getBounds();
					m_pOriginalBounds       = getBounds();
				}
			}
		});	
	}
	
	/**
	 * Clean up any resources being used.
	 */
	public void Dispose()
	{
		// Remove old ViewStyle change listener
		if(m_ViewStyle != null){
			m_ViewStyle.removeViewStyleChanged(m_pViewStyleListener);
			m_pViewStyleListener = null;
		}
		
		// Remove old language change listener
		if(m_pWText != null && m_pLanguageListener != null){
			m_pWText.removeLanguageChangedListener(m_pLanguageListener);
			m_pWText = null;
			m_pLanguageListener = null;			
		}
		
		// Dispose child controls
		for(int i=0;i<this.getComponentCount();i++){
			if(this.getComponent(i) instanceof IDisposable){
				((IDisposable)this.getComponent(i)).Dispose();
			}
		}
	}
	
	
	private void DoAnchor()
	{
		if(m_AnchorStyle == AnchorStyles.None){
			return;
		}
		
		if(this.getParent().getBounds().width < 1){
			return;
		}
		
		int topSpacing    = m_pOriginalBounds.y;
		int leftSpacing   = m_pOriginalBounds.x;
		int rightSpacing  = (int)(m_pOriginalParentBounds.width - m_pOriginalBounds.getMaxX());
		int bottomSpacing = (int)(m_pOriginalParentBounds.height - m_pOriginalBounds.getMaxY());
	
		int x = -1;
		int y = -1;
		int w = m_pOriginalBounds.width;
		int h = m_pOriginalBounds.height;
		
		if((AnchorStyles.Left & m_AnchorStyle) != 0){
			x = m_pOriginalBounds.x;
		}
		if((AnchorStyles.Top & m_AnchorStyle) != 0){
			y = m_pOriginalBounds.y;
		}
		if((AnchorStyles.Right & m_AnchorStyle) != 0){
			if(x != -1){
				w = (int)this.getParent().getBounds().width - rightSpacing - x;
			}
			else{
				w = m_pOriginalBounds.width;
				x = (int)this.getParent().getBounds().width - rightSpacing - w;
			}
		}
		if((AnchorStyles.Bottom & m_AnchorStyle) != 0){
			if(y != -1){
				h = (int)this.getParent().getBounds().height - bottomSpacing - y;
			}
			else{
				h = m_pOriginalBounds.height;
				y = (int)this.getParent().getBounds().height - bottomSpacing - h;
			}
		}

		if(w < 0){
			w = 0;
		}
		if(h < 0){
			h = 0;
		}
				
		super.setBounds(x,y,w,h);
	}
	
	
	/**
	 * Is called when ViewStyle has changed.
	 *
	 */
	protected void OnViewStyleChaned()
	{
		repaint();
	}
	
	/**
	 * Is called when Language has changed.
	 *
	 */
	protected void OnLanguageChanged()
	{
		repaint();
	}
	
	
	/**
	 * Flashes control.
	 *
	 */
	public void FlashControl()
	{
		
	}
	
	
	/**
	 * Sets control bounds.
	 */
	public void setBounds(int x,int y,int w,int h)
	{
		super.setBounds(x,y,w,h);
		
		if(this.isVisible() && this.getParent() != null){
			m_pOriginalParentBounds = this.getParent().getBounds();
			m_pOriginalBounds       = this.getBounds();
		}
	}
	
	/**
	 * Gets which edges of the control are anchored to the edges of its container.
	 * @return
	 */
	public int getAnchorStyle()
	{
		return m_AnchorStyle;
	}
	/**
	 * Sets which edges of the control are anchored to the edges of its container.
	 * @param value
	 */
	public void setAnchorStyle(int value)
	{
		if(m_AnchorStyle != value){
			m_AnchorStyle = value;
		}
	}
	
	/**
	 * Gets ViewStyle.
	 * @return
	 */
	public ViewStyle getViewStyle()
	{
		return m_ViewStyle;
	}
	/**
	 * Sets ViewStyle.
	 * @param value
	 */
	public void setViewStyle(ViewStyle value)
	{
		if(m_ViewStyle != value){
			// Remove old ViewStyle change listener
			if(m_ViewStyle != null){
				m_ViewStyle.removeViewStyleChanged(m_pViewStyleListener);
			}
			
			m_ViewStyle = value;
			
			// Add new ViewStyle change listener
			if(m_ViewStyle != null){
				m_pViewStyleListener = new ActionListener(){
					public void actionPerformed(ActionEvent e){
						OnViewStyleChaned();				
					}
				};
				m_ViewStyle.addViewStyleChanged(m_pViewStyleListener);
			}			
		}
	}
	
	/**
	 * Gets UI text getting source.
	 * @return
	 */
	public WText getWText()
	{
		return m_pWText;
	}
	/**
	 * Sets UI text getting source.
	 * @param value
	 */
	public void setWText(WText value)
	{
		// Remove old language listener, if any.
		if(m_pWText != null){
			m_pWText.removeLanguageChangedListener(m_pLanguageListener);
		}
		
		m_pWText = value;
		if(value != null){
			m_pLanguageListener = new ActionListener(){
				public void actionPerformed(ActionEvent e){
					OnLanguageChanged();
				}
			};
			m_pWText.addLanguageChangedListener(m_pLanguageListener);
		}
	}
}
