using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using FastReport.Utils;
namespace FastReport.Data
{
///
/// Represents the collection of objects.
///
public class ColumnCollection : FRCollectionBase
{
///
/// Gets or sets a column.
///
/// The index of a column in this collection.
/// The column with specified index.
public Column this[int index]
{
get { return List[index] as Column; }
set { List[index] = value; }
}
///
/// Finds a column by its name.
///
/// The name of a column.
/// The object if found; otherwise null.
public Column FindByName(string name)
{
foreach (Column c in this)
{
if (String.Compare(c.Name, name, true) == 0)
return c;
}
return null;
}
///
/// Finds a column by its alias.
///
/// The alias of a column.
/// The object if found; otherwise null.
public Column FindByAlias(string alias)
{
foreach (Column c in this)
{
if (String.Compare(c.Alias, alias, true) == 0)
return c;
}
return null;
}
///
/// Returns an unique column name based on given name.
///
/// The base name.
/// The unique name.
public string CreateUniqueName(string name)
{
string baseName = name;
int i = 1;
while (FindByName(name) != null)
{
name = baseName + i.ToString();
i++;
}
return name;
}
///
/// Returns an unique column alias based on given alias.
///
/// The base alias.
/// The unique alias.
public string CreateUniqueAlias(string alias)
{
string baseAlias = alias;
int i = 1;
while (FindByAlias(alias) != null)
{
alias = baseAlias + i.ToString();
i++;
}
return alias;
}
///
/// Sorts the collection of columns.
///
public void Sort()
{
InnerList.Sort(new ColumnComparer());
}
///
/// Initializes a new instance of the class with default settings.
///
/// The owner of this collection.
public ColumnCollection(Base owner) : base(owner)
{
}
}
///
/// Represents the comparer class that used for sorting the collection of columns.
///
public class ColumnComparer : IComparer
{
///
public int Compare(object x, object y)
{
object xValue = x.GetType().GetProperty("Name").GetValue(x, null);
object yValue = y.GetType().GetProperty("Name").GetValue(y, null);
IComparable comp = xValue as IComparable;
return comp.CompareTo(yValue);
}
}
}