/*
 * Created on 17.12.2003
 *
 */

package LumiSoft.UI.Controls.WOutlookBar;

import java.util.*;

/**
 * Outlookbar bar items collection.
 */
public class Items implements Iterable<Item>
{
	private Bar             m_pBar   = null;
	private ArrayList<Item> m_pItems = null;
	
	/**
	 * Default constructor.
	 * @param bar
	 */
	protected Items(Bar bar)
	{
		m_pBar = bar;
		m_pItems = new ArrayList<Item>();
	}
	
	/**
	 * Adds new item Bar items collection.
	 * @param text Item text.
	 * @param imageIndex Item image index.
	 */
	public void Add(String text,int imageIndex)
	{
		Add(text,"",imageIndex,null);
	}
	/**
	 * Adds new item Bar items collection.
	 * @param text Item text.
	 * @param textID
	 * @param imageIndex Item image index.
	 */
	public void Add(String text,String textID,int imageIndex)
	{
		Add(text,textID,imageIndex,null);
	}
	/**
	 * 
	 * @param text
	 * @param imageIndex
	 * @param tag
	 */
	public void Add(String text,int imageIndex,Object tag)
	{
		Add(text,"",imageIndex,tag);
	}
	/**
	 * 
	 * @param text
	 * @param textID
	 * @param imageIndex
	 * @param tag
	 */
	public void Add(String text,String textID,int imageIndex,Object tag)
	{
		Item item = new Item(this);
		item.setTag(tag);
		item.setText(text);
		item.setImageIndex(imageIndex);
		item.setTextID(textID);

		m_pItems.add(item);
		
		getBar().OnBarNeedsUpdate();
	}
	
	/**
	 * Gets if items collection contains specified item.
	 * @param item
	 * @return
	 */
	public boolean Contains(Item item)
	{
		return m_pItems.contains(item);
	}
	
	/**
	 * Gets item index in collection or -1 if item won't belong to this collection.
	 * @param item
	 * @return
	 */
	public int IndexOf(Item item)
	{
		return m_pItems.indexOf(item);
	}
	
	/**
	 * Gets item from specified index. Index is zero based.
	 * @param index
	 * @return
	 */
	public Item get(int index)
	{
		return m_pItems.get(index);
	}
	
	/**
	 * Gets number of items in collection.
	 * @return
	 */
	public int getCount()
	{
		return m_pItems.size();
	}
	
	/**
	 * Gets visible items.
	 * @return
	 */
	public Item[] getVisibleItems()
	{
		ArrayList<Item> visibleItems = new ArrayList<Item>();
		for(Item item : this){
			if(item.getVisible()){
				visibleItems.add(item);
			}
		}
		
		Item[] retVal = new Item[visibleItems.size()];
		visibleItems.toArray(retVal);
		
		return retVal;
		
		// Won't work  for some reason
	//	return (Column[])visibleColumns.toArray();
	}

	/**
	 * Gets items owner bar.
	 * @return
	 */
	public Bar getBar()
	{
		return m_pBar;
	}
	
	
	/**
	 * Gets iterator.
	 */
	public Iterator<Item> iterator()
	{
		return m_pItems.iterator();
	}
}
