//######################################################################################### //★★★★★★★ 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 Cell objects /// public class CellCollection : CollectionBase { #region Class Data /// /// The Row that owns the CellCollection /// private Row owner; #endregion #region Constructor /// /// Initializes a new instance of the CellCollection class /// that belongs to the specified Row /// /// A Row representing the row that owns /// the Cell collection public CellCollection(Row owner) : base() { if (owner == null) { throw new ArgumentNullException("owner"); } this.owner = owner; } #endregion #region Methods /// /// Adds the specified Cell to the end of the collection /// /// The Cell to add public int Add(Cell cell) { if (cell == null) { throw new System.ArgumentNullException("Cell is null"); } int index = this.List.Add(cell); this.OnCellAdded(new RowEventArgs(this.owner, cell, index, index)); return index; } /// /// Adds an array of Cell objects to the collection /// /// An array of Cell objects to add /// to the collection public void AddRange(Cell[] cells) { if (cells == null) { throw new System.ArgumentNullException("Cell[] is null"); } for (int i=0; i /// Removes the specified Cell from the model /// /// The Cell to remove public void Remove(Cell cell) { int cellIndex = this.IndexOf(cell); if (cellIndex != -1) { this.RemoveAt(cellIndex); } } /// /// Removes an array of Cell objects from the collection /// /// An array of Cell objects to remove /// from the collection public void RemoveRange(Cell[] cells) { if (cells == null) { throw new System.ArgumentNullException("Cell[] is null"); } for (int i=0; i /// Removes the Cell at the specified index from the collection /// /// The index of the Cell to remove public new void RemoveAt(int index) { if (index >= 0 && index < this.Count) { Cell cell = this[index]; this.List.RemoveAt(index); this.OnCellRemoved(new RowEventArgs(this.owner, cell, index, index)); } } /// /// Removes all Cells from the collection /// public new void Clear() { if (this.Count == 0) { return; } for (int i=0; i /// Inserts a Cell into the collection at the specified index /// /// The zero-based index at which the Cell /// should be inserted /// The Cell to insert public void Insert(int index, Cell cell) { if (cell == null) { return; } if (index < 0) { throw new IndexOutOfRangeException(); } if (index >= this.Count) { this.Add(cell); } else { base.List.Insert(index, cell); this.OnCellAdded(new RowEventArgs(this.owner, cell, index, index)); } } /// /// Inserts an array of Cells into the collection at the specified index /// /// The zero-based index at which the cells should be inserted /// An array of Cells to be inserted into the collection public void InsertRange(int index, Cell[] cells) { if (cells == null) { throw new System.ArgumentNullException("Cell[] is null"); } if (index < 0) { throw new IndexOutOfRangeException(); } if (index >= this.Count) { this.AddRange(cells); } else { for (int i=cells.Length-1; i>=0; i--) { this.Insert(index, cells[i]); } } } /// /// Returns the index of the specified Cell in the model /// /// The Cell to look for /// The index of the specified Cell in the model public int IndexOf(Cell cell) { for (int i=0; i /// Gets the Cell at the specified index /// public Cell this[int index] { get { if (index < 0 || index >= this.Count) { return null; } return this.List[index] as Cell; } } #endregion #region Events /// /// Raises the CellAdded event /// /// A RowEventArgs that contains the event data protected virtual void OnCellAdded(RowEventArgs e) { this.owner.OnCellAdded(e); } /// /// Raises the CellRemoved event /// /// A RowEventArgs that contains the event data protected virtual void OnCellRemoved(RowEventArgs e) { this.owner.OnCellRemoved(e); } #endregion } }