CellCollection.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.Collections;
  32. using XPTable.Events;
  33. namespace XPTable.Models
  34. {
  35. /// <summary>
  36. /// Represents a collection of Cell objects
  37. /// </summary>
  38. public class CellCollection : CollectionBase
  39. {
  40. #region Class Data
  41. /// <summary>
  42. /// The Row that owns the CellCollection
  43. /// </summary>
  44. private Row owner;
  45. #endregion
  46. #region Constructor
  47. /// <summary>
  48. /// Initializes a new instance of the CellCollection class
  49. /// that belongs to the specified Row
  50. /// </summary>
  51. /// <param name="owner">A Row representing the row that owns
  52. /// the Cell collection</param>
  53. public CellCollection(Row owner) : base()
  54. {
  55. if (owner == null)
  56. {
  57. throw new ArgumentNullException("owner");
  58. }
  59. this.owner = owner;
  60. }
  61. #endregion
  62. #region Methods
  63. /// <summary>
  64. /// Adds the specified Cell to the end of the collection
  65. /// </summary>
  66. /// <param name="cell">The Cell to add</param>
  67. public int Add(Cell cell)
  68. {
  69. if (cell == null)
  70. {
  71. throw new System.ArgumentNullException("Cell is null");
  72. }
  73. int index = this.List.Add(cell);
  74. this.OnCellAdded(new RowEventArgs(this.owner, cell, index, index));
  75. return index;
  76. }
  77. /// <summary>
  78. /// Adds an array of Cell objects to the collection
  79. /// </summary>
  80. /// <param name="cells">An array of Cell objects to add
  81. /// to the collection</param>
  82. public void AddRange(Cell[] cells)
  83. {
  84. if (cells == null)
  85. {
  86. throw new System.ArgumentNullException("Cell[] is null");
  87. }
  88. for (int i=0; i<cells.Length; i++)
  89. {
  90. this.Add(cells[i]);
  91. }
  92. }
  93. /// <summary>
  94. /// Removes the specified Cell from the model
  95. /// </summary>
  96. /// <param name="cell">The Cell to remove</param>
  97. public void Remove(Cell cell)
  98. {
  99. int cellIndex = this.IndexOf(cell);
  100. if (cellIndex != -1)
  101. {
  102. this.RemoveAt(cellIndex);
  103. }
  104. }
  105. /// <summary>
  106. /// Removes an array of Cell objects from the collection
  107. /// </summary>
  108. /// <param name="cells">An array of Cell objects to remove
  109. /// from the collection</param>
  110. public void RemoveRange(Cell[] cells)
  111. {
  112. if (cells == null)
  113. {
  114. throw new System.ArgumentNullException("Cell[] is null");
  115. }
  116. for (int i=0; i<cells.Length; i++)
  117. {
  118. this.Remove(cells[i]);
  119. }
  120. }
  121. /// <summary>
  122. /// Removes the Cell at the specified index from the collection
  123. /// </summary>
  124. /// <param name="index">The index of the Cell to remove</param>
  125. public new void RemoveAt(int index)
  126. {
  127. if (index >= 0 && index < this.Count)
  128. {
  129. Cell cell = this[index];
  130. this.List.RemoveAt(index);
  131. this.OnCellRemoved(new RowEventArgs(this.owner, cell, index, index));
  132. }
  133. }
  134. /// <summary>
  135. /// Removes all Cells from the collection
  136. /// </summary>
  137. public new void Clear()
  138. {
  139. if (this.Count == 0)
  140. {
  141. return;
  142. }
  143. for (int i=0; i<this.Count; i++)
  144. {
  145. this[i].InternalRow = null;
  146. }
  147. base.Clear();
  148. this.InnerList.Capacity = 0;
  149. this.OnCellRemoved(new RowEventArgs(this.owner, null, -1, -1));
  150. }
  151. /// <summary>
  152. /// Inserts a Cell into the collection at the specified index
  153. /// </summary>
  154. /// <param name="index">The zero-based index at which the Cell
  155. /// should be inserted</param>
  156. /// <param name="cell">The Cell to insert</param>
  157. public void Insert(int index, Cell cell)
  158. {
  159. if (cell == null)
  160. {
  161. return;
  162. }
  163. if (index < 0)
  164. {
  165. throw new IndexOutOfRangeException();
  166. }
  167. if (index >= this.Count)
  168. {
  169. this.Add(cell);
  170. }
  171. else
  172. {
  173. base.List.Insert(index, cell);
  174. this.OnCellAdded(new RowEventArgs(this.owner, cell, index, index));
  175. }
  176. }
  177. /// <summary>
  178. /// Inserts an array of Cells into the collection at the specified index
  179. /// </summary>
  180. /// <param name="index">The zero-based index at which the cells should be inserted</param>
  181. /// <param name="cells">An array of Cells to be inserted into the collection</param>
  182. public void InsertRange(int index, Cell[] cells)
  183. {
  184. if (cells == null)
  185. {
  186. throw new System.ArgumentNullException("Cell[] is null");
  187. }
  188. if (index < 0)
  189. {
  190. throw new IndexOutOfRangeException();
  191. }
  192. if (index >= this.Count)
  193. {
  194. this.AddRange(cells);
  195. }
  196. else
  197. {
  198. for (int i=cells.Length-1; i>=0; i--)
  199. {
  200. this.Insert(index, cells[i]);
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// Returns the index of the specified Cell in the model
  206. /// </summary>
  207. /// <param name="cell">The Cell to look for</param>
  208. /// <returns>The index of the specified Cell in the model</returns>
  209. public int IndexOf(Cell cell)
  210. {
  211. for (int i=0; i<this.Count; i++)
  212. {
  213. if (this[i] == cell)
  214. {
  215. return i;
  216. }
  217. }
  218. return -1;
  219. }
  220. #endregion
  221. #region Properties
  222. /// <summary>
  223. /// Gets the Cell at the specified index
  224. /// </summary>
  225. public Cell this[int index]
  226. {
  227. get
  228. {
  229. if (index < 0 || index >= this.Count)
  230. {
  231. return null;
  232. }
  233. return this.List[index] as Cell;
  234. }
  235. }
  236. #endregion
  237. #region Events
  238. /// <summary>
  239. /// Raises the CellAdded event
  240. /// </summary>
  241. /// <param name="e">A RowEventArgs that contains the event data</param>
  242. protected virtual void OnCellAdded(RowEventArgs e)
  243. {
  244. this.owner.OnCellAdded(e);
  245. }
  246. /// <summary>
  247. /// Raises the CellRemoved event
  248. /// </summary>
  249. /// <param name="e">A RowEventArgs that contains the event data</param>
  250. protected virtual void OnCellRemoved(RowEventArgs e)
  251. {
  252. this.owner.OnCellRemoved(e);
  253. }
  254. #endregion
  255. }
  256. }