/*
 * Created on Jan 14, 2004
 *
 */
package LumiSoft.UI.Controls;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;

/**
 * Context menu.
 */
public class WContextMenu 
{
	private static WContextMenu m_pActiveMainContextMenu = null;
	
	protected WContextMenu m_pParentMenu    = null;
	
	private EventListenerList    m_pListners    = null;
	private ArrayList<WMenuItem> m_pItems       = null;
	private ImageList            m_pImageList   = null;
	private Component            m_pParent      = null;
	private WContextMenuPopup    m_pPopup       = null;
	private WText                m_pWText       = null;
	private JDesktopPane         m_pDesktopPane = null;
	
	/**
	 * Default constructor.
	 * @param parent
	 */
	public WContextMenu(Component parent)
	{
		this(parent,(WText)null);
	}
	/**
	 * Constructs menu with specified language provider.
	 * @param parent
	 */
	public WContextMenu(Component parent,WText wText)
	{
		m_pParent = parent;
		m_pWText  = wText;
		
		m_pListners = new EventListenerList();
		m_pItems    = new ArrayList<WMenuItem>();
	}
	
	/**
	 * Constructs window list. Menu items are taken from desktopPane.
	 * @param parent
	 * @param desktopPane
	 */
	public WContextMenu(Component parent,JDesktopPane desktopPane)
	{
		m_pParent = parent;
		m_pDesktopPane = desktopPane;
		
		m_pListners = new EventListenerList();
		m_pItems    = new ArrayList<WMenuItem>();		
	}
	
	
	/**
	 * Add menu item with secified text to menu item collection.
	 * @param text
	 */
	public void Add(String text)
	{
		Add(text,"");
	}
	
	/**
	 * Add menu item with secified text and textID to menu item collection.
	 * @param text
	 * @param textID
	 */
	public void Add(String text,String textID)
	{
		Add(text,"",null);
	}
	
	/**
	 * Add menu item with secified text,textID and tag to menu item collection.
	 * @param text
	 * @param textID
	 * @param tag
	 */
	public void Add(String text,String textID,Object tag)
	{
		Add(text,textID,-1,tag);
	}
	
	/**
	 * Add menu item with secified text,textID and tag to menu item collection.
	 * @param text
	 * @param textID
	 * @param imageIndex
	 * @param tag
	 */
	public void Add(String text,String textID,int imageIndex,Object tag)
	{
		WMenuItem item = new WMenuItem(this);
		item.setText(text);
		item.setTextID(textID);
		item.setTag(tag);
		item.setImageIndex(imageIndex);
		m_pItems.add(item);
	}
	
	/**
	 * Add separator to menu item collection.
	 * @param isSeparator
	 */
	public void Add(boolean isSeparator)
	{
		WMenuItem item = new WMenuItem(this);
		item.setSeparator(isSeparator);
		m_pItems.add(item);
	}
	
	/**
	 * Gets menu item at specified index.
	 * @param value
	 * @return
	 */
	public WMenuItem get(int value)
	{
		return m_pItems.get(value);
	}
	
	
	/**
	 * Shows menu at specified location.
	 * @param screenX
	 * @param screenY
	 */
	public void Show(int screenX,int screenY)
	{	
		// If there is any open main context menu, close it
		if(m_pActiveMainContextMenu != null && m_pParentMenu == null){
			m_pActiveMainContextMenu.Hide();
			m_pActiveMainContextMenu = null;
		}
		
		// This MDI Window list, construct menu items.
		if(m_pDesktopPane != null){
			JInternalFrame[] mdiChilds = m_pDesktopPane.getAllFrames();
			ArrayList<WMenuItem> menuItems = new ArrayList<WMenuItem>();
			for(int i=0;i<mdiChilds.length;i++){
				WMenuItem item = new WMenuItem(this);
				item.setText(mdiChilds[i].getTitle());
				item.setTag(mdiChilds[i]);
								
				menuItems.add(item);
			}
			
			if(menuItems.size() > 0){
				m_pPopup = new WContextMenuPopup(this,m_pParent,menuItems,m_pImageList);
				m_pPopup.Show(screenX,screenY);
			}
		}
		// Normal context menu, show user specified menuitems.
		else{		
			m_pPopup = new WContextMenuPopup(this,m_pParent,m_pItems,m_pImageList);
			m_pPopup.Show(screenX,screenY);
		}		
		
		if(m_pParentMenu == null){
			m_pActiveMainContextMenu = this;
		}
	}
	
	/**
	 * Hides menu.
	 *
	 */
	public void Hide()
	{
		if(m_pPopup != null){
			m_pPopup.Hide();
			m_pPopup = null;
		}
	}
			
	
	/**
	 * Gets image list to use for menu items.
	 * @return
	 */
	public ImageList getImageList()
	{
		return m_pImageList;
	}
	/**
	 * Sets image list to use for menu items.
	 * @param value
	 */
	public void setImageList(ImageList value)
	{
		if(value != null){
			m_pImageList = value;			
		}		
	}
	
	/**
	 * Gets UI text getting source.
	 * @return
	 */
	public WText getWText()
	{
		return m_pWText;
	}
	/**
	 * Sets UI text getting source.
	 */
	public void setWText(WText value)
	{
		m_pWText = value; 
	}
	
		
	/**
	 * Adds the specified action listener to receive action events
	 * from this button.
	 * @param l the action listener
	 */ 
	public void addContextMenuListener(WContextMenuListener l) 
	{
		m_pListners.add(WContextMenuListener.class,l);
	}

	/**
	 * Removes the specified action listener so it no longer receives
	 * action events from this button.
	 * @param l the action listener
	 */ 
	public void removeContextMenuListener(WContextMenuListener l) 
	{
		m_pListners.remove(WContextMenuListener.class,l);
	}
	
	
	public void fireMenuItemSelected(WMenuItem item)
	{
		// This context menu is MDI Window list, just eat MDI click and just activate thta window.
		if(m_pDesktopPane != null){
			JInternalFrame mdiChild = (JInternalFrame)item.getTag();
			m_pDesktopPane.getDesktopManager().activateFrame(mdiChild);
			return;
		}
		
		// If this isn't main menu, forward this event to next parent menu
		if(m_pParentMenu != null){
			m_pParentMenu.fireMenuItemSelected(item);
		}
		
		Object[] listeners = m_pListners.getListenerList();
		// loop through each listener and pass on the event if needed		
		for(int i=0;i < listeners.length;i++){
			if(listeners[i] == WContextMenuListener.class){
				// pass the event to the listeners event dispatch method
				((WContextMenuListener)listeners[i+1]).MenuItemSelected(item);
			}            
		}
	}
	
	public void fireMenuClosed()
	{	
		m_pPopup = null;
		
		Object[] listeners = m_pListners.getListenerList();
		// loop through each listener and pass on the event if needed		
		for(int i=0;i < listeners.length;i++){
			if(listeners[i] == WContextMenuListener.class){
				// pass the event to the listeners event dispatch method
				((WContextMenuListener)listeners[i+1]).ContextMenuClosed();
			}            
		}
	}	
}
