ColumnCollection.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 Column objects
  37. /// </summary>
  38. public class ColumnCollection : CollectionBase
  39. {
  40. #region Class Data
  41. /// <summary>
  42. /// The ColumnModel that owns the CollumnCollection
  43. /// </summary>
  44. private ColumnModel owner;
  45. /// <summary>
  46. /// A local cache of the combined width of all columns
  47. /// </summary>
  48. private int totalColumnWidth;
  49. /// <summary>
  50. /// A local cache of the combined width of all visible columns
  51. /// </summary>
  52. private int visibleColumnsWidth;
  53. /// <summary>
  54. /// A local cache of the number of visible columns
  55. /// </summary>
  56. private int visibleColumnCount;
  57. /// <summary>
  58. /// A local cache of the last visible column in the collection
  59. /// </summary>
  60. private int lastVisibleColumn;
  61. #endregion
  62. #region Constructor
  63. /// <summary>
  64. /// Initializes a new instance of the ColumnModel.ColumnCollection class
  65. /// that belongs to the specified ColumnModel
  66. /// </summary>
  67. /// <param name="owner">A ColumnModel representing the columnModel that owns
  68. /// the Column collection</param>
  69. public ColumnCollection(ColumnModel owner) : base()
  70. {
  71. if (owner == null)
  72. {
  73. throw new ArgumentNullException("owner");
  74. }
  75. this.owner = owner;
  76. this.totalColumnWidth = 0;
  77. this.visibleColumnsWidth = 0;
  78. this.visibleColumnCount = 0;
  79. this.lastVisibleColumn = -1;
  80. }
  81. #endregion
  82. #region Methods
  83. /// <summary>
  84. /// Adds the specified Column to the end of the collection
  85. /// </summary>
  86. /// <param name="column">The Column to add</param>
  87. public int Add(Column column)
  88. {
  89. if (column == null)
  90. {
  91. throw new System.ArgumentNullException("Column is null");
  92. }
  93. int index = this.List.Add(column);
  94. this.RecalcWidthCache();
  95. this.OnColumnAdded(new ColumnModelEventArgs(this.owner, column, index, index));
  96. return index;
  97. }
  98. /// <summary>
  99. /// Adds an array of Column objects to the collection
  100. /// </summary>
  101. /// <param name="columns">An array of Column objects to add
  102. /// to the collection</param>
  103. public void AddRange(Column[] columns)
  104. {
  105. if (columns == null)
  106. {
  107. throw new System.ArgumentNullException("Column[] is null");
  108. }
  109. for (int i=0; i<columns.Length; i++)
  110. {
  111. this.Add(columns[i]);
  112. }
  113. }
  114. /// <summary>
  115. /// Removes the specified Column from the model
  116. /// </summary>
  117. /// <param name="column">The Column to remove</param>
  118. public void Remove(Column column)
  119. {
  120. int columnIndex = this.IndexOf(column);
  121. if (columnIndex != -1)
  122. {
  123. this.RemoveAt(columnIndex);
  124. }
  125. }
  126. /// <summary>
  127. /// Removes an array of Column objects from the collection
  128. /// </summary>
  129. /// <param name="columns">An array of Column objects to remove
  130. /// from the collection</param>
  131. public void RemoveRange(Column[] columns)
  132. {
  133. if (columns == null)
  134. {
  135. throw new System.ArgumentNullException("Column[] is null");
  136. }
  137. for (int i=0; i<columns.Length; i++)
  138. {
  139. this.Remove(columns[i]);
  140. }
  141. }
  142. /// <summary>
  143. /// Removes the Column at the specified index from the collection
  144. /// </summary>
  145. /// <param name="index">The index of the Column to remove</param>
  146. public new void RemoveAt(int index)
  147. {
  148. if (index >= 0 && index < this.Count)
  149. {
  150. Column column = this[index];
  151. this.List.RemoveAt(index);
  152. this.RecalcWidthCache();
  153. this.OnColumnRemoved(new ColumnModelEventArgs(this.owner, column, index, index));
  154. }
  155. }
  156. /// <summary>
  157. /// Removes all Columns from the collection
  158. /// </summary>
  159. public new void Clear()
  160. {
  161. if (this.Count == 0)
  162. {
  163. return;
  164. }
  165. for (int i=0; i<this.Count; i++)
  166. {
  167. this[i].ColumnModel = null;
  168. }
  169. base.Clear();
  170. this.InnerList.Capacity = 0;
  171. this.RecalcWidthCache();
  172. this.OnColumnRemoved(new ColumnModelEventArgs(this.owner, null, -1, -1));
  173. }
  174. /// <summary>
  175. /// Returns the index of the specified Column in the model
  176. /// </summary>
  177. /// <param name="column">The Column to look for</param>
  178. /// <returns>The index of the specified Column in the model</returns>
  179. public int IndexOf(Column column)
  180. {
  181. for (int i=0; i<this.Count; i++)
  182. {
  183. if (this[i] == column)
  184. {
  185. return i;
  186. }
  187. }
  188. return -1;
  189. }
  190. /// <summary>
  191. /// Recalculates the total combined width of all columns
  192. /// </summary>
  193. protected internal void RecalcWidthCache()
  194. {
  195. int total = 0;
  196. int visibleWidth = 0;
  197. int visibleCount = 0;
  198. int lastVisible = -1;
  199. for (int i=0; i<this.Count; i++)
  200. {
  201. total += this[i].Width;
  202. if (this[i].Visible)
  203. {
  204. this[i].X = visibleWidth;
  205. visibleWidth += this[i].Width;
  206. visibleCount++;
  207. lastVisible = i;
  208. }
  209. }
  210. this.totalColumnWidth = total;
  211. this.visibleColumnsWidth = visibleWidth;
  212. this.visibleColumnCount = visibleCount;
  213. }
  214. #endregion
  215. #region Properties
  216. /// <summary>
  217. /// Gets the Column at the specified index
  218. /// </summary>
  219. public Column this[int index]
  220. {
  221. get
  222. {
  223. if (index < 0 || index >= this.Count)
  224. {
  225. return null;
  226. }
  227. return this.List[index] as Column;
  228. }
  229. }
  230. /// <summary>
  231. /// Gets the ColumnModel that owns this ColumnCollection
  232. /// </summary>
  233. public ColumnModel ColumnModel
  234. {
  235. get
  236. {
  237. return this.owner;
  238. }
  239. }
  240. /// <summary>
  241. /// Returns the total width of all the Columns in the model
  242. /// </summary>
  243. internal int TotalColumnWidth
  244. {
  245. get
  246. {
  247. return this.totalColumnWidth;
  248. }
  249. }
  250. /// <summary>
  251. /// Returns the total width of all the visible Columns in the model
  252. /// </summary>
  253. internal int VisibleColumnsWidth
  254. {
  255. get
  256. {
  257. return this.visibleColumnsWidth;
  258. }
  259. }
  260. /// <summary>
  261. /// Returns the number of visible Columns in the model
  262. /// </summary>
  263. internal int VisibleColumnCount
  264. {
  265. get
  266. {
  267. return this.visibleColumnCount;
  268. }
  269. }
  270. /// <summary>
  271. /// Returns the index of the last visible Column in the model
  272. /// </summary>
  273. internal int LastVisibleColumn
  274. {
  275. get
  276. {
  277. return this.lastVisibleColumn;
  278. }
  279. }
  280. #endregion
  281. #region Events
  282. /// <summary>
  283. /// Raises the ColumnAdded event
  284. /// </summary>
  285. /// <param name="e">A ColumnModelEventArgs that contains the event data</param>
  286. protected virtual void OnColumnAdded(ColumnModelEventArgs e)
  287. {
  288. this.owner.OnColumnAdded(e);
  289. }
  290. /// <summary>
  291. /// Raises the ColumnRemoved event
  292. /// </summary>
  293. /// <param name="e">A ColumnModelEventArgs that contains the event data</param>
  294. protected virtual void OnColumnRemoved(ColumnModelEventArgs e)
  295. {
  296. this.owner.OnColumnRemoved(e);
  297. }
  298. #endregion
  299. }
  300. }