2
0

TreeNodeCollection.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jackson Harper ([email protected])
  24. // TODO: Sorting
  25. using System;
  26. using System.Collections;
  27. using System.ComponentModel;
  28. using System.Globalization;
  29. namespace System.Windows.Forms {
  30. [Editor("System.Windows.Forms.Design.TreeNodeCollectionEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  31. public class TreeNodeCollection : IList, ICollection, IEnumerable {
  32. private static readonly int OrigSize = 50;
  33. public TreeNode owner;
  34. private int count;
  35. private TreeNode [] nodes;
  36. private TreeNodeCollection ()
  37. {
  38. }
  39. internal TreeNodeCollection (TreeNode owner)
  40. {
  41. this.owner = owner;
  42. nodes = new TreeNode [OrigSize];
  43. }
  44. [Browsable(false)]
  45. [EditorBrowsable(EditorBrowsableState.Advanced)]
  46. public virtual int Count {
  47. get { return count; }
  48. }
  49. public virtual bool IsReadOnly {
  50. get { return false; }
  51. }
  52. bool ICollection.IsSynchronized {
  53. get { return false; }
  54. }
  55. object ICollection.SyncRoot {
  56. get { return this; }
  57. }
  58. bool IList.IsFixedSize {
  59. get { return false; }
  60. }
  61. object IList.this [int index] {
  62. get {
  63. if (index < 0 || index >= Count)
  64. throw new ArgumentOutOfRangeException ("index");
  65. return nodes [index];
  66. }
  67. set {
  68. if (index < 0 || index >= Count)
  69. throw new ArgumentOutOfRangeException ("index");
  70. TreeNode node = (TreeNode) value;
  71. node.parent = owner;
  72. nodes [index] = node;
  73. }
  74. }
  75. public virtual TreeNode this [int index] {
  76. get {
  77. if (index < 0 || index >= Count)
  78. throw new ArgumentOutOfRangeException ("index");
  79. return nodes [index];
  80. }
  81. set {
  82. if (index < 0 || index >= Count)
  83. throw new ArgumentOutOfRangeException ("index");
  84. value.parent = owner;
  85. nodes [index] = value;
  86. }
  87. }
  88. public virtual TreeNode Add (string text)
  89. {
  90. TreeNode res = new TreeNode (text);
  91. Add (res);
  92. return res;
  93. }
  94. public virtual int Add (TreeNode node)
  95. {
  96. if (node == null)
  97. throw new ArgumentNullException("node");
  98. // Remove it from any old parents
  99. node.Remove ();
  100. if (owner != null && owner.TreeView != null && owner.TreeView.Sorted)
  101. return AddSorted (node);
  102. node.parent = owner;
  103. if (count >= nodes.Length)
  104. Grow ();
  105. nodes [count++] = node;
  106. if (owner.TreeView != null && (owner.IsExpanded || owner.IsRoot)) {
  107. // XXX: Need to ensure the boxes for the nodes have been created
  108. owner.TreeView.UpdateNode (owner);
  109. } else if (owner.TreeView != null) {
  110. owner.TreeView.UpdateNodePlusMinus (owner);
  111. }
  112. return count;
  113. }
  114. public virtual void AddRange (TreeNode [] nodes)
  115. {
  116. if (nodes == null)
  117. throw new ArgumentNullException("node");
  118. // We can't just use Array.Copy because the nodes also
  119. // need to have some properties set when they are added.
  120. for (int i = 0; i < nodes.Length; i++)
  121. Add (nodes [i]);
  122. }
  123. public virtual void Clear ()
  124. {
  125. Array.Clear (nodes, 0, count);
  126. count = 0;
  127. }
  128. public bool Contains (TreeNode node)
  129. {
  130. return (Array.BinarySearch (nodes, node) > 0);
  131. }
  132. public virtual void CopyTo (Array dest, int index)
  133. {
  134. nodes.CopyTo (dest, index);
  135. }
  136. public virtual IEnumerator GetEnumerator ()
  137. {
  138. return new TreeNodeEnumerator (this);
  139. }
  140. public int IndexOf (TreeNode node)
  141. {
  142. return Array.IndexOf (nodes, node);
  143. }
  144. public virtual void Insert (int index, TreeNode node)
  145. {
  146. node.parent = owner;
  147. if (count >= nodes.Length)
  148. Grow ();
  149. Array.Copy (nodes, index, nodes, index + 1, count - index);
  150. nodes [index] = node;
  151. count++;
  152. }
  153. public virtual void Remove (TreeNode node)
  154. {
  155. int index = IndexOf (node);
  156. if (index > 0)
  157. RemoveAt (index);
  158. }
  159. public virtual void RemoveAt (int index)
  160. {
  161. TreeNode removed = nodes [index];
  162. TreeNode parent = removed.parent;
  163. removed.parent = null;
  164. Array.Copy (nodes, index + 1, nodes, index, count - index);
  165. count--;
  166. if (nodes.Length > OrigSize && nodes.Length > (count * 2))
  167. Shrink ();
  168. if (owner.TreeView != null)
  169. owner.TreeView.UpdateBelow (parent);
  170. }
  171. int IList.Add (object node)
  172. {
  173. return Add ((TreeNode) node);
  174. }
  175. bool IList.Contains (object node)
  176. {
  177. return Contains ((TreeNode) node);
  178. }
  179. int IList.IndexOf (object node)
  180. {
  181. return IndexOf ((TreeNode) node);
  182. }
  183. void IList.Insert (int index, object node)
  184. {
  185. Insert (index, (TreeNode) node);
  186. }
  187. void IList.Remove (object node)
  188. {
  189. Remove ((TreeNode) node);
  190. }
  191. private int AddSorted (TreeNode node)
  192. {
  193. if (count >= nodes.Length)
  194. Grow ();
  195. CompareInfo compare = Application.CurrentCulture.CompareInfo;
  196. int pos = 0;
  197. bool found = false;
  198. for (int i = 0; i < count; i++) {
  199. pos = i;
  200. int comp = compare.Compare (node.Text, nodes [i].Text);
  201. if (comp < 0) {
  202. found = true;
  203. break;
  204. }
  205. }
  206. // Stick it at the end
  207. if (!found)
  208. pos = count;
  209. // Move the nodes up and adjust their indices
  210. for (int i = count - 1; i >= pos; i--) {
  211. nodes [i + 1] = nodes [i];
  212. }
  213. count++;
  214. nodes [pos] = node;
  215. node.parent = owner;
  216. return count;
  217. }
  218. // Would be nice to do this without running through the collection twice
  219. internal void Sort () {
  220. Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
  221. for (int i = 0; i < count; i++) {
  222. nodes [i].Nodes.Sort ();
  223. }
  224. }
  225. private void Grow ()
  226. {
  227. TreeNode [] nn = new TreeNode [nodes.Length + 50];
  228. Array.Copy (nodes, nn, nodes.Length);
  229. nodes = nn;
  230. }
  231. private void Shrink ()
  232. {
  233. int len = (count > OrigSize ? count : OrigSize);
  234. TreeNode [] nn = new TreeNode [len];
  235. Array.Copy (nodes, nn, count);
  236. nodes = nn;
  237. }
  238. internal class TreeNodeEnumerator : IEnumerator {
  239. private TreeNodeCollection collection;
  240. private int index = -1;
  241. public TreeNodeEnumerator (TreeNodeCollection collection)
  242. {
  243. this.collection = collection;
  244. }
  245. public object Current {
  246. get { return collection [index]; }
  247. }
  248. public bool MoveNext ()
  249. {
  250. if (index + 1 >= collection.Count)
  251. return false;
  252. index++;
  253. return true;
  254. }
  255. public void Reset ()
  256. {
  257. index = 0;
  258. }
  259. }
  260. private class TreeNodeComparer : IComparer {
  261. private CompareInfo compare;
  262. public TreeNodeComparer (CompareInfo compare)
  263. {
  264. this.compare = compare;
  265. }
  266. public int Compare (object x, object y)
  267. {
  268. TreeNode l = (TreeNode) x;
  269. TreeNode r = (TreeNode) y;
  270. int res = compare.Compare (l.Text, r.Text);
  271. return (res == 0 ? l.Index - r.Index : res);
  272. }
  273. }
  274. }
  275. }