//######################################################################################### //★★★★★★★ 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 Row objects /// public class RowCollection : CollectionBase { #region Class Data /// /// The TableModel that owns the RowCollection /// private TableModel owner; #endregion #region Constructor /// /// Initializes a new instance of the RowCollection class /// that belongs to the specified TableModel /// /// A TableModel representing the tableModel that owns /// the RowCollection public RowCollection(TableModel owner) : base() { if (owner == null) { throw new ArgumentNullException("owner"); } this.owner = owner; } #endregion #region Methods /// /// Adds the specified Row to the end of the collection /// /// The Row to add public int Add(Row row) { if (row == null) { throw new System.ArgumentNullException("Row is null"); } int index = this.List.Add(row); this.OnRowAdded(new TableModelEventArgs(this.owner, row, index, index)); return index; } /// /// Adds an array of Row objects to the collection /// /// An array of Row objects to add /// to the collection public void AddRange(Row[] rows) { if (rows == null) { throw new System.ArgumentNullException("Row[] is null"); } for (int i=0; i /// Removes the specified Row from the model /// /// The Row to remove public void Remove(Row row) { int rowIndex = this.IndexOf(row); if (rowIndex != -1) { this.RemoveAt(rowIndex); } } /// /// Removes an array of Row objects from the collection /// /// An array of Row objects to remove /// from the collection public void RemoveRange(Row[] rows) { if (rows == null) { throw new System.ArgumentNullException("Row[] is null"); } for (int i=0; i /// Removes the Row at the specified index from the collection /// /// The index of the Row to remove public new void RemoveAt(int index) { if (index >= 0 && index < this.Count) { Row row = this[index]; this.List.RemoveAt(index); this.OnRowRemoved(new TableModelEventArgs(this.owner, row, index, index)); } } /// /// Removes all Rows from the collection /// public new void Clear() { if (this.Count == 0) { return; } for (int i=0; i /// Inserts a Row into the collection at the specified index /// /// The zero-based index at which the Row /// should be inserted /// The Row to insert public void Insert(int index, Row row) { if (row == null) { return; } if (index < 0) { throw new IndexOutOfRangeException(); } if (index >= this.Count) { this.Add(row); } else { base.List.Insert(index, row); this.owner.OnRowAdded(new TableModelEventArgs(this.owner, row, index, index)); } } /// /// Inserts an array of Rows into the collection at the specified /// index /// /// The zero-based index at which the rows /// should be inserted /// The array of Rows to be inserted into /// the collection public void InsertRange(int index, Row[] rows) { if (rows == null) { throw new System.ArgumentNullException("Row[] is null"); } if (index < 0) { throw new IndexOutOfRangeException(); } if (index >= this.Count) { this.AddRange(rows); } else { for (int i=rows.Length-1; i>=0; i--) { this.Insert(index, rows[i]); } } } /// /// Returns the index of the specified Row in the model /// /// The Row to look for /// The index of the specified Row in the model public int IndexOf(Row row) { for (int i=0; i /// Gets the Row at the specified index /// public Row this[int index] { get { if (index < 0 || index >= this.Count) { return null; } return this.List[index] as Row; } } /// /// Replaces the Row at the specified index to the specified Row /// /// The index of the Row to be replaced /// The Row to be placed at the specified index internal void SetRow(int index, Row row) { if (index < 0 || index >= this.Count) { throw new ArgumentOutOfRangeException("value"); } if (row == null) { throw new ArgumentNullException("row cannot be null"); } this.List[index] = row; row.InternalIndex = index; } #endregion #region Events /// /// Raises the RowAdded event /// /// A TableModelEventArgs that contains the event data protected virtual void OnRowAdded(TableModelEventArgs e) { this.owner.OnRowAdded(e); } /// /// Raises the RowRemoved event /// /// A TableModelEventArgs that contains the event data protected virtual void OnRowRemoved(TableModelEventArgs e) { this.owner.OnRowRemoved(e); } #endregion } }