//#########################################################################################
//★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
//★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
//#########################################################################################
/*
* Copyright ?2005, Mathew Hall
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
using System;
using System.Collections;
using XPTable.Events;
namespace XPTable.Models
{
///
/// Represents a collection of Column objects
///
public class ColumnCollection : CollectionBase
{
#region Class Data
///
/// The ColumnModel that owns the CollumnCollection
///
private ColumnModel owner;
///
/// A local cache of the combined width of all columns
///
private int totalColumnWidth;
///
/// A local cache of the combined width of all visible columns
///
private int visibleColumnsWidth;
///
/// A local cache of the number of visible columns
///
private int visibleColumnCount;
///
/// A local cache of the last visible column in the collection
///
private int lastVisibleColumn;
#endregion
#region Constructor
///
/// Initializes a new instance of the ColumnModel.ColumnCollection class
/// that belongs to the specified ColumnModel
///
/// A ColumnModel representing the columnModel that owns
/// the Column collection
public ColumnCollection(ColumnModel owner) : base()
{
if (owner == null)
{
throw new ArgumentNullException("owner");
}
this.owner = owner;
this.totalColumnWidth = 0;
this.visibleColumnsWidth = 0;
this.visibleColumnCount = 0;
this.lastVisibleColumn = -1;
}
#endregion
#region Methods
///
/// Adds the specified Column to the end of the collection
///
/// The Column to add
public int Add(Column column)
{
if (column == null)
{
throw new System.ArgumentNullException("Column is null");
}
int index = this.List.Add(column);
this.RecalcWidthCache();
this.OnColumnAdded(new ColumnModelEventArgs(this.owner, column, index, index));
return index;
}
///
/// Adds an array of Column objects to the collection
///
/// An array of Column objects to add
/// to the collection
public void AddRange(Column[] columns)
{
if (columns == null)
{
throw new System.ArgumentNullException("Column[] is null");
}
for (int i=0; i
/// Removes the specified Column from the model
///
/// The Column to remove
public void Remove(Column column)
{
int columnIndex = this.IndexOf(column);
if (columnIndex != -1)
{
this.RemoveAt(columnIndex);
}
}
///
/// Removes an array of Column objects from the collection
///
/// An array of Column objects to remove
/// from the collection
public void RemoveRange(Column[] columns)
{
if (columns == null)
{
throw new System.ArgumentNullException("Column[] is null");
}
for (int i=0; i
/// Removes the Column at the specified index from the collection
///
/// The index of the Column to remove
public new void RemoveAt(int index)
{
if (index >= 0 && index < this.Count)
{
Column column = this[index];
this.List.RemoveAt(index);
this.RecalcWidthCache();
this.OnColumnRemoved(new ColumnModelEventArgs(this.owner, column, index, index));
}
}
///
/// Removes all Columns from the collection
///
public new void Clear()
{
if (this.Count == 0)
{
return;
}
for (int i=0; i
/// Returns the index of the specified Column in the model
///
/// The Column to look for
/// The index of the specified Column in the model
public int IndexOf(Column column)
{
for (int i=0; i
/// Recalculates the total combined width of all columns
///
protected internal void RecalcWidthCache()
{
int total = 0;
int visibleWidth = 0;
int visibleCount = 0;
int lastVisible = -1;
for (int i=0; i
/// Gets the Column at the specified index
///
public Column this[int index]
{
get
{
if (index < 0 || index >= this.Count)
{
return null;
}
return this.List[index] as Column;
}
}
///
/// Gets the ColumnModel that owns this ColumnCollection
///
public ColumnModel ColumnModel
{
get
{
return this.owner;
}
}
///
/// Returns the total width of all the Columns in the model
///
internal int TotalColumnWidth
{
get
{
return this.totalColumnWidth;
}
}
///
/// Returns the total width of all the visible Columns in the model
///
internal int VisibleColumnsWidth
{
get
{
return this.visibleColumnsWidth;
}
}
///
/// Returns the number of visible Columns in the model
///
internal int VisibleColumnCount
{
get
{
return this.visibleColumnCount;
}
}
///
/// Returns the index of the last visible Column in the model
///
internal int LastVisibleColumn
{
get
{
return this.lastVisibleColumn;
}
}
#endregion
#region Events
///
/// Raises the ColumnAdded event
///
/// A ColumnModelEventArgs that contains the event data
protected virtual void OnColumnAdded(ColumnModelEventArgs e)
{
this.owner.OnColumnAdded(e);
}
///
/// Raises the ColumnRemoved event
///
/// A ColumnModelEventArgs that contains the event data
protected virtual void OnColumnRemoved(ColumnModelEventArgs e)
{
this.owner.OnColumnRemoved(e);
}
#endregion
}
}