PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ListViewGroupCollection.cs

https://bitbucket.org/danipen/mono
C# | 332 lines | 238 code | 66 blank | 28 comment | 46 complexity | 1e142cfaac74c6e6306398246a801c8d MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // ListViewGroupCollection.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Daniel Nauck
  24. //
  25. // Author:
  26. // Daniel Nauck (dna(at)mono-project(dot)de)
  27. // Carlos Alberto Cortez <calberto.cortez@gmail.com>
  28. using System;
  29. using System.Text;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.ComponentModel;
  33. namespace System.Windows.Forms
  34. {
  35. [ListBindable(false)]
  36. public class ListViewGroupCollection : IList, ICollection, IEnumerable
  37. {
  38. private List<ListViewGroup> list = null;
  39. private ListView list_view_owner = null;
  40. private ListViewGroup default_group;
  41. ListViewGroupCollection()
  42. {
  43. list = new List<ListViewGroup> ();
  44. default_group = new ListViewGroup ("Default Group");
  45. default_group.IsDefault = true;
  46. }
  47. internal ListViewGroupCollection(ListView listViewOwner) : this()
  48. {
  49. list_view_owner = listViewOwner;
  50. default_group.ListViewOwner = listViewOwner;
  51. }
  52. internal ListView ListViewOwner {
  53. get { return list_view_owner; }
  54. set { list_view_owner = value; }
  55. }
  56. #region IEnumerable Members
  57. public IEnumerator GetEnumerator()
  58. {
  59. return list.GetEnumerator();
  60. }
  61. #endregion
  62. #region ICollection Members
  63. public void CopyTo(Array array, int index)
  64. {
  65. ((ICollection) list).CopyTo(array, index);
  66. }
  67. public int Count {
  68. get { return list.Count; }
  69. }
  70. bool ICollection.IsSynchronized {
  71. get { return true; }
  72. }
  73. object ICollection.SyncRoot {
  74. get { return this; }
  75. }
  76. #endregion
  77. #region IList Members
  78. int IList.Add(object value)
  79. {
  80. if (!(value is ListViewGroup))
  81. throw new ArgumentException("value");
  82. return Add((ListViewGroup)value);
  83. }
  84. public int Add(ListViewGroup group)
  85. {
  86. if (Contains(group))
  87. return -1;
  88. AddGroup (group);
  89. if (this.list_view_owner != null)
  90. list_view_owner.Redraw(true);
  91. return list.Count - 1;
  92. }
  93. public ListViewGroup Add(string key, string headerText)
  94. {
  95. ListViewGroup newGroup = new ListViewGroup(key, headerText);
  96. Add(newGroup);
  97. return newGroup;
  98. }
  99. public void Clear()
  100. {
  101. foreach (ListViewGroup group in list)
  102. group.ListViewOwner = null;
  103. list.Clear ();
  104. if(list_view_owner != null)
  105. list_view_owner.Redraw(true);
  106. }
  107. bool IList.Contains(object value)
  108. {
  109. if (value is ListViewGroup)
  110. return Contains((ListViewGroup)value);
  111. else
  112. return false;
  113. }
  114. public bool Contains(ListViewGroup value)
  115. {
  116. return list.Contains(value);
  117. }
  118. int IList.IndexOf(object value)
  119. {
  120. if (value is ListViewGroup)
  121. return IndexOf((ListViewGroup)value);
  122. else
  123. return -1;
  124. }
  125. public int IndexOf(ListViewGroup value)
  126. {
  127. return list.IndexOf(value);
  128. }
  129. void IList.Insert(int index, object value)
  130. {
  131. if (value is ListViewGroup)
  132. Insert(index, (ListViewGroup)value);
  133. }
  134. public void Insert(int index, ListViewGroup group)
  135. {
  136. if (Contains(group))
  137. return;
  138. CheckListViewItemsInGroup(group);
  139. group.ListViewOwner = list_view_owner;
  140. list.Insert(index, group);
  141. if(list_view_owner != null)
  142. list_view_owner.Redraw(true);
  143. }
  144. bool IList.IsFixedSize {
  145. get { return false; }
  146. }
  147. bool IList.IsReadOnly {
  148. get { return false; }
  149. }
  150. void IList.Remove(object value)
  151. {
  152. Remove((ListViewGroup)value);
  153. }
  154. public void Remove (ListViewGroup group)
  155. {
  156. int idx = list.IndexOf (group);
  157. if (idx != -1)
  158. RemoveAt (idx);
  159. }
  160. public void RemoveAt (int index)
  161. {
  162. if (list.Count <= index || index < 0)
  163. return;
  164. ListViewGroup group = list [index];
  165. group.ListViewOwner = null;
  166. list.RemoveAt (index);
  167. if (list_view_owner != null)
  168. list_view_owner.Redraw (true);
  169. }
  170. object IList.this[int index] {
  171. get { return this[index]; }
  172. set {
  173. if (value is ListViewGroup)
  174. this[index] = (ListViewGroup)value;
  175. }
  176. }
  177. public ListViewGroup this[int index] {
  178. get {
  179. if (list.Count <= index || index < 0)
  180. throw new ArgumentOutOfRangeException("index");
  181. return list [index];
  182. }
  183. set {
  184. if (list.Count <= index || index < 0)
  185. throw new ArgumentOutOfRangeException("index");
  186. if (Contains (value))
  187. return;
  188. if (value != null)
  189. CheckListViewItemsInGroup (value);
  190. list [index] = value;
  191. if (list_view_owner != null)
  192. list_view_owner.Redraw(true);
  193. }
  194. }
  195. public ListViewGroup this [string key] {
  196. get {
  197. int idx = IndexOfKey (key);
  198. if (idx != -1)
  199. return this [idx];
  200. return null;
  201. }
  202. set {
  203. int idx = IndexOfKey (key);
  204. if (idx == -1)
  205. return;
  206. this [idx] = value;
  207. }
  208. }
  209. int IndexOfKey (string key)
  210. {
  211. for (int i = 0; i < list.Count; i++)
  212. if (list [i].Name == key)
  213. return i;
  214. return -1;
  215. }
  216. #endregion
  217. public void AddRange(ListViewGroup[] groups)
  218. {
  219. foreach (ListViewGroup group in groups)
  220. AddGroup (group);
  221. if (list_view_owner != null)
  222. list_view_owner.Redraw (true);
  223. }
  224. public void AddRange(ListViewGroupCollection groups)
  225. {
  226. foreach (ListViewGroup group in groups)
  227. AddGroup (group);
  228. if (list_view_owner != null)
  229. list_view_owner.Redraw (true);
  230. }
  231. internal ListViewGroup GetInternalGroup (int index)
  232. {
  233. if (index == 0)
  234. return default_group;
  235. return list [index - 1];
  236. }
  237. internal int InternalCount {
  238. get {
  239. return list.Count + 1;
  240. }
  241. }
  242. internal ListViewGroup DefaultGroup {
  243. get {
  244. return default_group;
  245. }
  246. }
  247. void AddGroup (ListViewGroup group)
  248. {
  249. if (Contains (group))
  250. return;
  251. CheckListViewItemsInGroup (group);
  252. group.ListViewOwner = list_view_owner;
  253. list.Add (group);
  254. }
  255. private void CheckListViewItemsInGroup(ListViewGroup value)
  256. {
  257. //check for correct ListView
  258. foreach (ListViewItem item in value.Items)
  259. {
  260. if (item.ListView != null && item.ListView != this.list_view_owner)
  261. throw new ArgumentException("ListViewItem belongs to a ListView control other than the one that owns this ListViewGroupCollection.",
  262. "ListViewGroup");
  263. }
  264. }
  265. }
  266. }