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

import java.util.*;

import LumiSoft.UI.Controls.*;

/**
 * Grid columns collection.
 */
public class WGridColumns implements Iterable<WGridColumn>
{		
	private WGridTableView         m_pView    = null;
	private ArrayList<WGridColumn> m_pColumns = null; 
	
	/**
	 * Default constructor.
	 * @param parentView View that owns these grid columns.
	 */
	protected WGridColumns(WGridTableView parentView)
	{	
		m_pView    = parentView;
		m_pColumns = new ArrayList<WGridColumn>();
	}
	
	
	/**
	 * Adds new column with specified text to columns collection. 
	 * @param text Column text.
	 * @return Returns added column.
	 */
	public WGridColumn AddColumn(String text) throws Exception
	{
		return AddColumn(text,20);
	}
	
	/**
	 * Adds new column with specified text and width to columns collection. 
	 * @param text Column text.
	 * @param width Column width.
	 * @return Returns added column.
	 */
	public WGridColumn AddColumn(String text,int width) throws Exception
	{
		return AddColumn(text,width,"");
	}
	
	/**
	 * Adds new column with specified text,width and DataColumn mapping name to columns collection. 
	 * @param text Column text.
	 * @param width Column width.
	 * @param mappingName Column datasource DataColumn mapping name.
	 * @return Returns added column.
	 */
	public WGridColumn AddColumn(String text,int width,String mappingName) throws Exception
	{
		return AddColumn(text,width,mappingName,HorizontalAlignment.Left);
	}
	
	/**
	 * Adds new column with specified text,width and DataColumn mapping name to columns collection. 
	 * @param text Column text.
	 * @param textID
	 * @param width Column width.
	 * @param mappingName Column datasource DataColumn mapping name.
	 * @return Returns added column.
	 */
	public WGridColumn AddColumn(String text,String textID,int width,String mappingName) throws Exception
	{
		return AddColumn(text,textID,width,mappingName,HorizontalAlignment.Left);
	}
	
	/**
	 * Adds new column with specified name,text,width and DataColumn mapping name to columns collection. 
	 * @param name
	 * @param text
	 * @param textID
	 * @param width
	 * @param mappingName
	 * @return
	 * @throws Exception
	 */
	public WGridColumn AddColumn(String name,String text,String textID,int width,String mappingName) throws Exception
	{
		return AddColumn(name,text,textID,width,mappingName,HorizontalAlignment.Left,"");
	}
	
	/**
	 * Adds new column with specified text,width,DataColumn mapping name and cells text alignment to columns collection. 
	 * @param text
	 * @param width
	 * @param mappingName
	 * @param cellTextHzAlign
	 * @return Returns added column.
	 */
	public WGridColumn AddColumn(String text,int width,String mappingName,int cellTextHzAlign) throws Exception
	{
		return AddColumn(text,"",width,mappingName,cellTextHzAlign,"");
	}
	
	/**
	 * Adds new column with specified text,width,DataColumn mapping name and cells text alignment to columns collection. 
	 * @param text
	 * @param textID
	 * @param width
	 * @param mappingName
	 * @param cellTextHzAlign
	 * @return Returns added column.
	 */
	public WGridColumn AddColumn(String text,String textID,int width,String mappingName,int cellTextHzAlign) throws Exception
	{
		return AddColumn(text,textID,width,mappingName,cellTextHzAlign,"");
	}
	
	/**
	 * Adds new column with specified name,text,width,DataColumn mapping name and cells text alignment to columns collection. 
	 * @param name
	 * @param text
	 * @param textID
	 * @param width
	 * @param mappingName
	 * @param cellTextHzAlign
	 * @return
	 * @throws Exception
	 */	
	public WGridColumn AddColumn(String name,String text,String textID,int width,String mappingName,int cellTextHzAlign) throws Exception
	{
		return AddColumn(name,text,textID,width,mappingName,cellTextHzAlign,"");
	}
	
	/**
	 * Adds new column with specified text,textID,width,DataColumn mapping name,cells text alignment and cell text format to columns collection. 
	 * @param text
	 * @param width
	 * @param mappingName
	 * @param cellTextHzAlign
	 * @param cellTextFormat
	 * @return Returns added column.
	 */
	public WGridColumn AddColumn(String text,int width,String mappingName,int cellTextHzAlign,String cellTextFormat) throws Exception
	{
		return AddColumn(text,"",width,mappingName,cellTextHzAlign,cellTextFormat);
	}
	
	/**
	 * Adds new column with specified text,textID,width,DataColumn mapping name,cells text alignment and cell text format to columns collection. 
	 * @param text
	 * @param textID
	 * @param width
	 * @param mappingName
	 * @param cellTextHzAlign
	 * @param cellTextFormat
	 * @return Returns added column.
	 */
	public WGridColumn AddColumn(String text,String textID,int width,String mappingName,int cellTextHzAlign,String cellTextFormat) throws Exception
	{
		//---- Find column name which doesn't exist ---------//
		String columnName = "Column " + this.getCount() + 1;
		boolean columnExists = false;
		while(columnExists){
			columnExists = false;
			
			for(WGridColumn col : m_pColumns){
				if(col.getColumnName().toLowerCase().equals(columnName.toLowerCase())){
					columnExists = true;
				}
			}
		}
		//----------------------------------------------------//
		
		return AddColumn(columnName,text,textID,width,mappingName,cellTextHzAlign,cellTextFormat);
	}
	
	/**
	 * Adds new column with specified name,text,textID,width,DataColumn mapping name,cells text alignment and cell text format to columns collection. 
	 * @param name
	 * @param text
	 * @param textID
	 * @param width
	 * @param mappingName
	 * @param cellTextHzAlign
	 * @param cellTextFormat
	 * @return Returns added column.
	 */
	public WGridColumn AddColumn(String name,String text,String textID,int width,String mappingName,int cellTextHzAlign,String cellTextFormat) throws Exception
	{
		// Don't allow duplicate column names
		for(WGridColumn col : m_pColumns){
			if(col.getColumnName().toLowerCase().equals(name.toLowerCase())){
				throw new Exception("Column '" + name + "' with specified name already exists in columns collection !");
			}
		}
		
		WGridColumn col = new WGridColumn(this,name);
		col.setText(text);
		col.setTextID(textID);
		col.setWidth(width);
		col.setMappingName(mappingName);
		col.setCellsTextAlign(cellTextHzAlign);
		col.setCellTextFormat(cellTextFormat);
		
		m_pColumns.add(col);
		
		return col;
	}
	
	/**
	 * Gets specified column index in columns collection. Index is zero based.
	 * @param column
	 * @return Returns specified column index in columns collection.
	 */
	public int indexOf(WGridColumn column)
	{
		return m_pColumns.indexOf(column);
	}
	
	/**
	 * Removes specified column from columns collection.
	 * @param column
	 */
	public void remove(WGridColumn column)
	{
		m_pColumns.remove(column);
	}
	
	/**
	 * Clears the collection of any relations.
	 *
	 */
	public void Clear()
	{
		m_pColumns.clear();
	}
	
	/**
	 *  Moves column to specified index.
	 * @param column
	 * @param index
	 */
	public void MoveColumn(WGridColumn column,int index)
	{
		m_pColumns.remove(column);
		m_pColumns.add(index,column);
	}
	
	
	/**
	 * Gets columns count in columns collection.
	 * @return Returns columns count in columns collection.
	 */
	public int getCount()
	{
		return m_pColumns.size();
	}
	
	/**
	 * Gets column from specified index. Index is zero based.
	 * @param index
	 * @return Returns column from specified index.
	 */
	public WGridColumn get(int index)
	{
		return m_pColumns.get(index);
	}
	
	/**
	 * Gets column with specified column name.
	 * @param name
	 * @return Returns column with specified column name.
	 */
	public WGridColumn get(String name)
	{
		for(WGridColumn col : m_pColumns){
			if(col.getColumnName().toLowerCase().equals(name.toLowerCase())){
				return col;
			}
		}
		
		return null;
	}
	
	/**
	 * Returns visible columns (Column.getVisible = true).
	 * @return
	 */
	public WGridColumn[] getVisibleColumns()
	{
		ArrayList<WGridColumn> visibleColumns = new ArrayList<WGridColumn>();
		for(WGridColumn col : this){
			if(col.getVisible()){
				visibleColumns.add(col);
			}
		}
		
		WGridColumn[] retVal = new WGridColumn[visibleColumns.size()];
		visibleColumns.toArray(retVal);
		
		return retVal;
		
		// Won't work  for some reason
	//	return (Column[])visibleColumns.toArray();
	}
	
	/**
	 * Gets columns owner view.
	 * @return
	 */
	public WGridTableView getView()
	{
		return m_pView;
	}
	
	
	/**
	 * Gets iterator.
	 */
	public Iterator<WGridColumn> iterator()
	{
		return m_pColumns.iterator();
	}

}
