RowCollection.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 Row objects
  37. /// </summary>
  38. public class RowCollection : CollectionBase
  39. {
  40. #region Class Data
  41. /// <summary>
  42. /// The TableModel that owns the RowCollection
  43. /// </summary>
  44. private TableModel owner;
  45. #endregion
  46. #region Constructor
  47. /// <summary>
  48. /// Initializes a new instance of the RowCollection class
  49. /// that belongs to the specified TableModel
  50. /// </summary>
  51. /// <param name="owner">A TableModel representing the tableModel that owns
  52. /// the RowCollection</param>
  53. public RowCollection(TableModel 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 Row to the end of the collection
  65. /// </summary>
  66. /// <param name="row">The Row to add</param>
  67. public int Add(Row row)
  68. {
  69. if (row == null)
  70. {
  71. throw new System.ArgumentNullException("Row is null");
  72. }
  73. int index = this.List.Add(row);
  74. this.OnRowAdded(new TableModelEventArgs(this.owner, row, index, index));
  75. return index;
  76. }
  77. /// <summary>
  78. /// Adds an array of Row objects to the collection
  79. /// </summary>
  80. /// <param name="rows">An array of Row objects to add
  81. /// to the collection</param>
  82. public void AddRange(Row[] rows)
  83. {
  84. if (rows == null)
  85. {
  86. throw new System.ArgumentNullException("Row[] is null");
  87. }
  88. for (int i=0; i<rows.Length; i++)
  89. {
  90. this.Add(rows[i]);
  91. }
  92. }
  93. /// <summary>
  94. /// Removes the specified Row from the model
  95. /// </summary>
  96. /// <param name="row">The Row to remove</param>
  97. public void Remove(Row row)
  98. {
  99. int rowIndex = this.IndexOf(row);
  100. if (rowIndex != -1)
  101. {
  102. this.RemoveAt(rowIndex);
  103. }
  104. }
  105. /// <summary>
  106. /// Removes an array of Row objects from the collection
  107. /// </summary>
  108. /// <param name="rows">An array of Row objects to remove
  109. /// from the collection</param>
  110. public void RemoveRange(Row[] rows)
  111. {
  112. if (rows == null)
  113. {
  114. throw new System.ArgumentNullException("Row[] is null");
  115. }
  116. for (int i=0; i<rows.Length; i++)
  117. {
  118. this.Remove(rows[i]);
  119. }
  120. }
  121. /// <summary>
  122. /// Removes the Row at the specified index from the collection
  123. /// </summary>
  124. /// <param name="index">The index of the Row to remove</param>
  125. public new void RemoveAt(int index)
  126. {
  127. if (index >= 0 && index < this.Count)
  128. {
  129. Row row = this[index];
  130. this.List.RemoveAt(index);
  131. this.OnRowRemoved(new TableModelEventArgs(this.owner, row, index, index));
  132. }
  133. }
  134. /// <summary>
  135. /// Removes all Rows 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].InternalTableModel = null;
  146. }
  147. base.Clear();
  148. this.InnerList.Capacity = 0;
  149. this.owner.OnRowRemoved(new TableModelEventArgs(this.owner, null, -1, -1));
  150. }
  151. /// <summary>
  152. /// Inserts a Row into the collection at the specified index
  153. /// </summary>
  154. /// <param name="index">The zero-based index at which the Row
  155. /// should be inserted</param>
  156. /// <param name="row">The Row to insert</param>
  157. public void Insert(int index, Row row)
  158. {
  159. if (row == null)
  160. {
  161. return;
  162. }
  163. if (index < 0)
  164. {
  165. throw new IndexOutOfRangeException();
  166. }
  167. if (index >= this.Count)
  168. {
  169. this.Add(row);
  170. }
  171. else
  172. {
  173. base.List.Insert(index, row);
  174. this.owner.OnRowAdded(new TableModelEventArgs(this.owner, row, index, index));
  175. }
  176. }
  177. /// <summary>
  178. /// Inserts an array of Rows into the collection at the specified
  179. /// index
  180. /// </summary>
  181. /// <param name="index">The zero-based index at which the rows
  182. /// should be inserted</param>
  183. /// <param name="rows">The array of Rows to be inserted into
  184. /// the collection</param>
  185. public void InsertRange(int index, Row[] rows)
  186. {
  187. if (rows == null)
  188. {
  189. throw new System.ArgumentNullException("Row[] is null");
  190. }
  191. if (index < 0)
  192. {
  193. throw new IndexOutOfRangeException();
  194. }
  195. if (index >= this.Count)
  196. {
  197. this.AddRange(rows);
  198. }
  199. else
  200. {
  201. for (int i=rows.Length-1; i>=0; i--)
  202. {
  203. this.Insert(index, rows[i]);
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// Returns the index of the specified Row in the model
  209. /// </summary>
  210. /// <param name="row">The Row to look for</param>
  211. /// <returns>The index of the specified Row in the model</returns>
  212. public int IndexOf(Row row)
  213. {
  214. for (int i=0; i<this.Count; i++)
  215. {
  216. if (this[i] == row)
  217. {
  218. return i;
  219. }
  220. }
  221. return -1;
  222. }
  223. #endregion
  224. #region Properties
  225. /// <summary>
  226. /// Gets the Row at the specified index
  227. /// </summary>
  228. public Row this[int index]
  229. {
  230. get
  231. {
  232. if (index < 0 || index >= this.Count)
  233. {
  234. return null;
  235. }
  236. return this.List[index] as Row;
  237. }
  238. }
  239. /// <summary>
  240. /// Replaces the Row at the specified index to the specified Row
  241. /// </summary>
  242. /// <param name="index">The index of the Row to be replaced</param>
  243. /// <param name="row">The Row to be placed at the specified index</param>
  244. internal void SetRow(int index, Row row)
  245. {
  246. if (index < 0 || index >= this.Count)
  247. {
  248. throw new ArgumentOutOfRangeException("value");
  249. }
  250. if (row == null)
  251. {
  252. throw new ArgumentNullException("row cannot be null");
  253. }
  254. this.List[index] = row;
  255. row.InternalIndex = index;
  256. }
  257. #endregion
  258. #region Events
  259. /// <summary>
  260. /// Raises the RowAdded event
  261. /// </summary>
  262. /// <param name="e">A TableModelEventArgs that contains the event data</param>
  263. protected virtual void OnRowAdded(TableModelEventArgs e)
  264. {
  265. this.owner.OnRowAdded(e);
  266. }
  267. /// <summary>
  268. /// Raises the RowRemoved event
  269. /// </summary>
  270. /// <param name="e">A TableModelEventArgs that contains the event data</param>
  271. protected virtual void OnRowRemoved(TableModelEventArgs e)
  272. {
  273. this.owner.OnRowRemoved(e);
  274. }
  275. #endregion
  276. }
  277. }