TreeNodeCollection.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 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. SetData (node);
  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. SetData (value);
  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. if (owner != null && owner.TreeView != null && owner.TreeView.Sorted)
  99. return AddSorted (node);
  100. SetData (node);
  101. if (count >= nodes.Length)
  102. Grow ();
  103. nodes [count++] = node;
  104. if (owner.TreeView != null && (owner.IsExpanded || owner.IsRoot)) {
  105. // XXX:
  106. // Ideally we could just call TreeView::UpdateNode here, but most
  107. // likely the bounding boxes for the nodes have not been created yet
  108. // so the update wouldn't work well....setting the bounds on
  109. // insert would allow this optimization.
  110. owner.TreeView.Refresh ();
  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. SetData (node);
  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. Array.Copy (nodes, index, nodes, index + 1, count - index);
  162. count--;
  163. if (nodes.Length > OrigSize && nodes.Length > (count * 2))
  164. Shrink ();
  165. }
  166. int IList.Add (object node)
  167. {
  168. return Add ((TreeNode) node);
  169. }
  170. bool IList.Contains (object node)
  171. {
  172. return Contains ((TreeNode) node);
  173. }
  174. int IList.IndexOf (object node)
  175. {
  176. return IndexOf ((TreeNode) node);
  177. }
  178. void IList.Insert (int index, object node)
  179. {
  180. Insert (index, (TreeNode) node);
  181. }
  182. void IList.Remove (object node)
  183. {
  184. Remove ((TreeNode) node);
  185. }
  186. private int AddSorted (TreeNode node)
  187. {
  188. if (count >= nodes.Length)
  189. Grow ();
  190. CompareInfo compare = Application.CurrentCulture.CompareInfo;
  191. int pos = 0;
  192. bool found = false;
  193. for (int i = 0; i < count; i++) {
  194. pos = i;
  195. int comp = compare.Compare (node.Text, nodes [i].Text);
  196. if (comp < 0) {
  197. found = true;
  198. break;
  199. }
  200. }
  201. // Stick it at the end
  202. if (!found)
  203. pos = count;
  204. // Move the nodes up and adjust their indices
  205. for (int i = count - 1; i >= pos; i--) {
  206. nodes [i + 1] = nodes [i];
  207. nodes [i + 1].SetIndex (i + 1);
  208. }
  209. count++;
  210. nodes [pos] = node;
  211. SetData (node, pos);
  212. return count;
  213. }
  214. // Would be nice to do this without running through the collection twice
  215. internal void Sort () {
  216. Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
  217. for (int i = 0; i < count; i++) {
  218. nodes [i].SetIndex (i);
  219. nodes [i].Nodes.Sort ();
  220. }
  221. }
  222. private void SetData (TreeNode node)
  223. {
  224. SetData (node, count);
  225. }
  226. private void SetData (TreeNode node, int pos)
  227. {
  228. node.SetAddedData ((owner != null ? owner.TreeView : null), owner, pos);
  229. }
  230. private void Grow ()
  231. {
  232. TreeNode [] nn = new TreeNode [nodes.Length + 50];
  233. Array.Copy (nodes, nn, nodes.Length);
  234. nodes = nn;
  235. }
  236. private void Shrink ()
  237. {
  238. int len = (count > OrigSize ? count : OrigSize);
  239. TreeNode [] nn = new TreeNode [len];
  240. Array.Copy (nodes, nn, count);
  241. nodes = nn;
  242. }
  243. internal class TreeNodeEnumerator : IEnumerator {
  244. private TreeNodeCollection collection;
  245. private int index = -1;
  246. public TreeNodeEnumerator (TreeNodeCollection collection)
  247. {
  248. this.collection = collection;
  249. }
  250. public object Current {
  251. get { return collection [index]; }
  252. }
  253. public bool MoveNext ()
  254. {
  255. if (index + 1 >= collection.Count)
  256. return false;
  257. index++;
  258. return true;
  259. }
  260. public void Reset ()
  261. {
  262. index = 0;
  263. }
  264. }
  265. private class TreeNodeComparer : IComparer {
  266. private CompareInfo compare;
  267. public TreeNodeComparer (CompareInfo compare)
  268. {
  269. this.compare = compare;
  270. }
  271. public int Compare (object x, object y)
  272. {
  273. TreeNode l = (TreeNode) x;
  274. TreeNode r = (TreeNode) y;
  275. int res = compare.Compare (l.Text, r.Text);
  276. return (res == 0 ? l.Index - r.Index : res);
  277. }
  278. }
  279. }
  280. }