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

import java.util.*;

/**
 * Toolbar item collection.
 */
public class WToolBarItems implements Iterable<WToolBarItem>
{
	private WToolBar                m_pToolBar = null;
	private ArrayList<WToolBarItem> m_pItems   = null;
	
	/**
	 * Default constructor.
	 * @param toolbar
	 */
	protected WToolBarItems(WToolBar toolbar)
	{
		m_pToolBar = toolbar;
		m_pItems   = new ArrayList<WToolBarItem>();
	}
	
	
	/**
	 * Adds toobar item to items collection.
	 * @param imageIndex Toolbar image index in ToolBar.ImageList.
	 * @return Returns added item.
	 * @throws Exception
	 */
	public WToolBarItem Add(int imageIndex) throws Exception
	{
		return Add(GetFreeToolbarItemName(),imageIndex,null,"");
	}
	
	/**
	 * Adds toobar item to items collection.
	 * @param separator Specifies if item is separator.
	 * @return Returns added item.
	 * @throws Exception
	 */
	public WToolBarItem Add(boolean separator) throws Exception
	{		
		return Add(GetFreeToolbarItemName(),true,-1,null,"");
	}
	
	/**
	 * Adds toobar item to items collection.
	 * @param name Toolbar item name.
	 * @param imageIndex Toolbar image index in ToolBar.ImageList.
	 * @param tag
	 * @return Returns added item.
	 * @throws Exception
	 */
	public WToolBarItem Add(String name,int imageIndex,Object tag) throws Exception
	{
		return Add(name,imageIndex,null,tag);
	}
	
	/**
	 * Adds toobar item to items collection.
	 * @param name Toolbar item name.
	 * @param imageIndex Toolbar image index in ToolBar.ImageList.
	 * @param menu Item dropdown menu.
	 * @param tag Item tag.
	 * @return Returns added item.
	 * @throws Exception
	 */
	public WToolBarItem Add(String name,int imageIndex,WContextMenu menu,Object tag) throws Exception
	{
		return Add(name,false,imageIndex,menu,tag);
	}
	
	/**
	 * Adds toobar item to items collection.
	 * @param name Toolbar item name.
	 * @param separator Specifies if item is separator.
	 * @param imageIndex Toolbar image index in ToolBar.ImageList.
	 * @param menu Item dropdown menu.
	 * @param tag Item tag.
	 * @return Returns added item.
	 * @throws Exception
	 */
	public WToolBarItem Add(String name,boolean separator,int imageIndex,WContextMenu menu,Object tag) throws Exception
	{
		// Don't allow duplicate column names
		for(int i=0;i<m_pItems.size();i++){
			WToolBarItem item = (WToolBarItem)m_pItems.get(i);
			if(item.getName().toLowerCase().equals(name.toLowerCase())){
				throw new Exception("Item '" + name + "' with specified name already exists in columns collection !");
			}
		}
		
		WToolBarItem item = new WToolBarItem(m_pToolBar,name);
		item.setImageIndex(imageIndex);
		item.setMenu(menu);
		item.setTag(tag);
		item.setSeparator(separator);
		
		m_pItems.add(item);
		
		return item;
	}	
	
	/**
	 * Removes item from specified index.
	 * @param index
	 */
	public void Remove(int index)
	{
		m_pItems.remove(index);
	}
	
		
	/**
	 * Gets toolbar item from specified index.
	 * @param index
	 * @return
	 */
	public WToolBarItem get(int index)
	{
		return (WToolBarItem)m_pItems.get(index);
	}
	
	/**
	 * Gets toolbar item with specified name.
	 * @param name
	 * @return
	 */
	public WToolBarItem get(String name)
	{
		for(int i=0;i<m_pItems.size();i++){
			WToolBarItem item = (WToolBarItem)m_pItems.get(i);
			if(item.getName().toLowerCase().equals(name.toLowerCase())){
				return item;
			}
		}
		
		return null;
	}
	
	/**
	 * Gets number of items are in items collection.
	 * @return
	 */
	public int getCount()
	{
		return m_pItems.size();
	}
	
	/**
	 * Returns visible items (item.getVisible = true).
	 * @return
	 */
	public WToolBarItem[] getVisibleItems()
	{
		ArrayList<WToolBarItem> visibleColumns = new ArrayList<WToolBarItem>();
		for(WToolBarItem item : this){
			if(item.getVisible()){
				visibleColumns.add(item);
			}
		}
		
		WToolBarItem[] retVal = new WToolBarItem[visibleColumns.size()];
		visibleColumns.toArray(retVal);
		
		return retVal;
		
		// Won't work  for some reason
	//	return (Column[])visibleColumns.toArray();
	}
	
		
	private String GetFreeToolbarItemName()
	{
		//---- Find toolbar item name which doesn't exist ---------//
		String name = "Item " + this.getCount() + 1;
		boolean columnExists = false;
		while(columnExists){
			columnExists = false;
			
			for(int i=0;i<m_pItems.size();i++){
				WToolBarItem col = (WToolBarItem)m_pItems.get(i);
				if(col.getName().toLowerCase().equals(name.toLowerCase())){
					columnExists = true;
				}
			}
		}
		//----------------------------------------------------//
		
		return name;
	}
	

//	public WToolBar getWToolBar()
//	{
//		return m_pToolBar;
//	}
	
	
	/**
	 * Gets iterator.
	 */
	public Iterator<WToolBarItem> iterator()
	{
		return m_pItems.iterator();
	}
}
