TreeNodeCollection.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jackson Harper ([email protected])
  24. // TODO: Sorting
  25. using System;
  26. using System.Collections;
  27. namespace System.Windows.Forms {
  28. public class TreeNodeCollection : IList, ICollection, IEnumerable {
  29. private static readonly int OrigSize = 50;
  30. private TreeNode owner;
  31. private int count;
  32. private TreeNode [] nodes;
  33. private TreeNodeCollection ()
  34. {
  35. }
  36. internal TreeNodeCollection (TreeNode owner)
  37. {
  38. this.owner = owner;
  39. nodes = new TreeNode [OrigSize];
  40. }
  41. public virtual int Count {
  42. get { return count; }
  43. }
  44. public virtual bool IsReadOnly {
  45. get { return false; }
  46. }
  47. bool ICollection.IsSynchronized {
  48. get { return false; }
  49. }
  50. object ICollection.SyncRoot {
  51. get { return this; }
  52. }
  53. bool IList.IsFixedSize {
  54. get { return false; }
  55. }
  56. object IList.this [int index] {
  57. get {
  58. if (index < 0 || index > Count)
  59. throw new ArgumentOutOfRangeException ("index");
  60. return nodes [index];
  61. }
  62. set {
  63. if (index < 0 || index > Count)
  64. throw new ArgumentOutOfRangeException ("index");
  65. TreeNode node = (TreeNode) value;
  66. SetData (node);
  67. nodes [index] = node;
  68. }
  69. }
  70. public virtual TreeNode this [int index] {
  71. get {
  72. if (index < 0 || index > Count)
  73. throw new ArgumentOutOfRangeException ("index");
  74. return nodes [index];
  75. }
  76. set {
  77. if (index < 0 || index > Count)
  78. throw new ArgumentOutOfRangeException ("index");
  79. SetData (value);
  80. nodes [index] = value;
  81. }
  82. }
  83. public virtual TreeNode Add (string text)
  84. {
  85. TreeNode res = new TreeNode (text);
  86. Add (res);
  87. return res;
  88. }
  89. public virtual int Add (TreeNode node)
  90. {
  91. if (node == null)
  92. throw new ArgumentNullException("node");
  93. if (owner != null && owner.TreeView != null && owner.TreeView.Sorted)
  94. return AddSorted (node);
  95. SetData (node);
  96. if (count >= nodes.Length)
  97. Grow ();
  98. nodes [count++] = node;
  99. if (owner.TreeView != null)
  100. owner.TreeView.Refresh ();
  101. return count;
  102. }
  103. public virtual void AddRange (TreeNode [] nodes)
  104. {
  105. if (nodes == null)
  106. throw new ArgumentNullException("node");
  107. // We can't just use Array.Copy because the nodes also
  108. // need to have some properties set when they are added.
  109. for (int i = 0; i < nodes.Length; i++)
  110. Add (nodes [i]);
  111. }
  112. public virtual void Clear ()
  113. {
  114. Array.Clear (nodes, 0, count);
  115. count = 0;
  116. }
  117. public bool Contains (TreeNode node)
  118. {
  119. return (Array.BinarySearch (nodes, node) > 0);
  120. }
  121. public virtual void CopyTo (Array dest, int index)
  122. {
  123. nodes.CopyTo (dest, index);
  124. }
  125. public virtual IEnumerator GetEnumerator ()
  126. {
  127. return new TreeNodeEnumerator (this);
  128. }
  129. public int IndexOf (TreeNode node)
  130. {
  131. return Array.IndexOf (nodes, node);
  132. }
  133. public virtual void Insert (int index, TreeNode node)
  134. {
  135. SetData (node);
  136. IList list = (IList) nodes;
  137. list.Insert (index, node);
  138. }
  139. public virtual void Remove (TreeNode node)
  140. {
  141. int index = IndexOf (node);
  142. if (index > 0)
  143. RemoveAt (index);
  144. }
  145. public virtual void RemoveAt (int index)
  146. {
  147. Array.Copy (nodes, index, nodes, index - 1, count - index);
  148. count--;
  149. if (nodes.Length > OrigSize && nodes.Length > (count * 2))
  150. Shrink ();
  151. if (owner.TreeView != null)
  152. owner.TreeView.TotalNodeCount--;
  153. }
  154. int IList.Add (object node)
  155. {
  156. return Add ((TreeNode) node);
  157. }
  158. bool IList.Contains (object node)
  159. {
  160. return Contains ((TreeNode) node);
  161. }
  162. int IList.IndexOf (object node)
  163. {
  164. return IndexOf ((TreeNode) node);
  165. }
  166. void IList.Insert (int index, object node)
  167. {
  168. Insert (index, (TreeNode) node);
  169. }
  170. void IList.Remove (object node)
  171. {
  172. Remove ((TreeNode) node);
  173. }
  174. [MonoTODO]
  175. private int AddSorted (TreeNode node)
  176. {
  177. SetData (node);
  178. if (count >= nodes.Length)
  179. Grow ();
  180. nodes [count++] = node;
  181. return count;
  182. }
  183. [MonoTODO]
  184. internal void Sort ()
  185. {
  186. }
  187. private void SetData (TreeNode node)
  188. {
  189. node.SetAddedData ((owner != null ? owner.TreeView : null), owner, count);
  190. }
  191. private void Grow ()
  192. {
  193. TreeNode [] nn = new TreeNode [nodes.Length + 50];
  194. Array.Copy (nodes, nn, nodes.Length);
  195. nodes = nn;
  196. }
  197. private void Shrink ()
  198. {
  199. int len = (count > OrigSize ? count : OrigSize);
  200. TreeNode [] nn = new TreeNode [len];
  201. Array.Copy (nodes, nn, count);
  202. nodes = nn;
  203. }
  204. internal class TreeNodeEnumerator : IEnumerator {
  205. private TreeNodeCollection collection;
  206. private int index = -1;
  207. public TreeNodeEnumerator (TreeNodeCollection collection)
  208. {
  209. this.collection = collection;
  210. }
  211. public object Current {
  212. get { return collection [index]; }
  213. }
  214. public bool MoveNext ()
  215. {
  216. if (index + 1 >= collection.Count)
  217. return false;
  218. index++;
  219. return true;
  220. }
  221. public void Reset ()
  222. {
  223. index = 0;
  224. }
  225. }
  226. }
  227. }