TreeNodeCollection.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. private 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 int Count {
  47. get { return count; }
  48. }
  49. public 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. int res;
  101. TreeView tree_view = null;
  102. if (owner != null)
  103. tree_view = owner.TreeView;
  104. if (tree_view != null && tree_view.Sorted) {
  105. res = AddSorted (node);
  106. } else {
  107. node.parent = owner;
  108. if (count >= nodes.Length)
  109. Grow ();
  110. nodes [count++] = node;
  111. res = count;
  112. }
  113. if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
  114. // XXX: Need to ensure the boxes for the nodes have been created
  115. tree_view.UpdateBelow (owner);
  116. } else if (owner != null && tree_view != null) {
  117. tree_view.UpdateBelow (owner);
  118. }
  119. return res;
  120. }
  121. public virtual void AddRange (TreeNode [] nodes)
  122. {
  123. if (nodes == null)
  124. throw new ArgumentNullException("node");
  125. // We can't just use Array.Copy because the nodes also
  126. // need to have some properties set when they are added.
  127. for (int i = 0; i < nodes.Length; i++)
  128. Add (nodes [i]);
  129. }
  130. public virtual void Clear ()
  131. {
  132. for (int i = 0; i < count; i++)
  133. RemoveAt (i, false);
  134. Array.Clear (nodes, 0, count);
  135. count = 0;
  136. TreeView tree_view = null;
  137. if (owner != null) {
  138. tree_view = owner.TreeView;
  139. if (owner.IsRoot)
  140. tree_view.top_node = null;
  141. if (tree_view != null)
  142. tree_view.UpdateBelow (owner);
  143. }
  144. }
  145. public bool Contains (TreeNode node)
  146. {
  147. return (Array.BinarySearch (nodes, node) > 0);
  148. }
  149. public void CopyTo (Array dest, int index)
  150. {
  151. nodes.CopyTo (dest, index);
  152. }
  153. public IEnumerator GetEnumerator ()
  154. {
  155. return new TreeNodeEnumerator (this);
  156. }
  157. public int IndexOf (TreeNode node)
  158. {
  159. return Array.IndexOf (nodes, node);
  160. }
  161. public virtual void Insert (int index, TreeNode node)
  162. {
  163. node.parent = owner;
  164. if (count >= nodes.Length)
  165. Grow ();
  166. Array.Copy (nodes, index, nodes, index + 1, count - index);
  167. nodes [index] = node;
  168. count++;
  169. }
  170. public void Remove (TreeNode node)
  171. {
  172. int index = IndexOf (node);
  173. if (index > 0)
  174. RemoveAt (index);
  175. }
  176. public virtual void RemoveAt (int index)
  177. {
  178. RemoveAt (index, true);
  179. }
  180. private void RemoveAt (int index, bool update)
  181. {
  182. TreeNode removed = nodes [index];
  183. Array.Copy (nodes, index + 1, nodes, index, count - index);
  184. count--;
  185. if (nodes.Length > OrigSize && nodes.Length > (count * 2))
  186. Shrink ();
  187. TreeView tree_view = null;
  188. if (owner != null)
  189. tree_view = owner.TreeView;
  190. if (tree_view != null && removed == tree_view.top_node) {
  191. OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
  192. if (oe.MoveNext () && oe.MoveNext ())
  193. tree_view.top_node = oe.CurrentNode;
  194. else
  195. tree_view.top_node = null;
  196. }
  197. TreeNode parent = removed.parent;
  198. removed.parent = null;
  199. if (tree_view != null)
  200. tree_view.UpdateBelow (parent);
  201. }
  202. int IList.Add (object node)
  203. {
  204. return Add ((TreeNode) node);
  205. }
  206. bool IList.Contains (object node)
  207. {
  208. return Contains ((TreeNode) node);
  209. }
  210. int IList.IndexOf (object node)
  211. {
  212. return IndexOf ((TreeNode) node);
  213. }
  214. void IList.Insert (int index, object node)
  215. {
  216. Insert (index, (TreeNode) node);
  217. }
  218. void IList.Remove (object node)
  219. {
  220. Remove ((TreeNode) node);
  221. }
  222. private int AddSorted (TreeNode node)
  223. {
  224. if (count >= nodes.Length)
  225. Grow ();
  226. CompareInfo compare = Application.CurrentCulture.CompareInfo;
  227. int pos = 0;
  228. bool found = false;
  229. for (int i = 0; i < count; i++) {
  230. pos = i;
  231. int comp = compare.Compare (node.Text, nodes [i].Text);
  232. if (comp < 0) {
  233. found = true;
  234. break;
  235. }
  236. }
  237. // Stick it at the end
  238. if (!found)
  239. pos = count;
  240. // Move the nodes up and adjust their indices
  241. for (int i = count - 1; i >= pos; i--) {
  242. nodes [i + 1] = nodes [i];
  243. }
  244. count++;
  245. nodes [pos] = node;
  246. node.parent = owner;
  247. return count;
  248. }
  249. // Would be nice to do this without running through the collection twice
  250. internal void Sort () {
  251. Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
  252. for (int i = 0; i < count; i++) {
  253. nodes [i].Nodes.Sort ();
  254. }
  255. }
  256. private void Grow ()
  257. {
  258. TreeNode [] nn = new TreeNode [nodes.Length + 50];
  259. Array.Copy (nodes, nn, nodes.Length);
  260. nodes = nn;
  261. }
  262. private void Shrink ()
  263. {
  264. int len = (count > OrigSize ? count : OrigSize);
  265. TreeNode [] nn = new TreeNode [len];
  266. Array.Copy (nodes, nn, count);
  267. nodes = nn;
  268. }
  269. internal class TreeNodeEnumerator : IEnumerator {
  270. private TreeNodeCollection collection;
  271. private int index = -1;
  272. public TreeNodeEnumerator (TreeNodeCollection collection)
  273. {
  274. this.collection = collection;
  275. }
  276. public object Current {
  277. get { return collection [index]; }
  278. }
  279. public bool MoveNext ()
  280. {
  281. if (index + 1 >= collection.Count)
  282. return false;
  283. index++;
  284. return true;
  285. }
  286. public void Reset ()
  287. {
  288. index = 0;
  289. }
  290. }
  291. private class TreeNodeComparer : IComparer {
  292. private CompareInfo compare;
  293. public TreeNodeComparer (CompareInfo compare)
  294. {
  295. this.compare = compare;
  296. }
  297. public int Compare (object x, object y)
  298. {
  299. TreeNode l = (TreeNode) x;
  300. TreeNode r = (TreeNode) y;
  301. int res = compare.Compare (l.Text, r.Text);
  302. return (res == 0 ? l.Index - r.Index : res);
  303. }
  304. }
  305. }
  306. }