TreeNodeCollection.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // System.Web.UI.WebControls.TreeNodeCollection.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.Web.UI;
  33. using System.Collections;
  34. namespace System.Web.UI.WebControls
  35. {
  36. public sealed class TreeNodeCollection: ICollection, IEnumerable, IStateManager
  37. {
  38. ArrayList items = new ArrayList ();
  39. TreeView tree;
  40. TreeNode parent;
  41. bool marked;
  42. bool dirty;
  43. public TreeNodeCollection ()
  44. {
  45. }
  46. public TreeNodeCollection (TreeNode owner)
  47. {
  48. this.parent = owner;
  49. this.tree = owner.Tree;
  50. }
  51. internal TreeNodeCollection (TreeView tree)
  52. {
  53. this.tree = tree;
  54. }
  55. internal void SetTree (TreeView tree)
  56. {
  57. this.tree = tree;
  58. foreach (TreeNode node in items)
  59. node.Tree = tree;
  60. }
  61. public TreeNode this [int i] {
  62. get { return (TreeNode) items [i]; }
  63. }
  64. public void Add (TreeNode child)
  65. {
  66. Add (child, true);
  67. }
  68. internal void Add (TreeNode child, bool updateParent)
  69. {
  70. int index = items.Add (child);
  71. if (!updateParent)
  72. return;
  73. child.Index = index;
  74. child.SetParent (parent);
  75. child.Tree = tree;
  76. if (marked) {
  77. ((IStateManager)child).TrackViewState ();
  78. child.SetDirty ();
  79. dirty = true;
  80. }
  81. }
  82. public void AddAt (int index, TreeNode child)
  83. {
  84. items.Insert (index, child);
  85. child.Index = index;
  86. child.SetParent (parent);
  87. child.Tree = tree;
  88. for (int n=index+1; n<items.Count; n++)
  89. ((TreeNode)items[n]).Index = n;
  90. if (marked) {
  91. ((IStateManager)child).TrackViewState ();
  92. child.SetDirty ();
  93. dirty = true;
  94. }
  95. }
  96. internal void SetDirty () {
  97. for (int n = 0; n < Count; n++)
  98. this [n].SetDirty ();
  99. dirty = true;
  100. }
  101. public void Clear ()
  102. {
  103. if (tree != null || parent != null) {
  104. foreach (TreeNode nod in items) {
  105. nod.Tree = null;
  106. nod.SetParent (null);
  107. }
  108. }
  109. items.Clear ();
  110. dirty = true;
  111. }
  112. public bool Contains (TreeNode child)
  113. {
  114. return items.Contains (child);
  115. }
  116. public void CopyTo (TreeNode[] nodeArray, int index)
  117. {
  118. items.CopyTo (nodeArray, index);
  119. }
  120. public IEnumerator GetEnumerator ()
  121. {
  122. return items.GetEnumerator ();
  123. }
  124. public int IndexOf (TreeNode node)
  125. {
  126. return items.IndexOf (node);
  127. }
  128. public void Remove (TreeNode node)
  129. {
  130. int i = IndexOf (node);
  131. if (i == -1) return;
  132. items.RemoveAt (i);
  133. if (tree != null)
  134. node.Tree = null;
  135. dirty = true;
  136. }
  137. public void RemoveAt (int index)
  138. {
  139. TreeNode node = (TreeNode) items [index];
  140. items.RemoveAt (index);
  141. if (tree != null)
  142. node.Tree = null;
  143. dirty = true;
  144. }
  145. public int Count {
  146. get { return items.Count; }
  147. }
  148. public bool IsSynchronized {
  149. get { return false; }
  150. }
  151. public object SyncRoot {
  152. get { return items; }
  153. }
  154. void System.Collections.ICollection.CopyTo (Array array, int index)
  155. {
  156. items.CopyTo (array, index);
  157. }
  158. void IStateManager.LoadViewState (object state)
  159. {
  160. if (state == null) return;
  161. object[] its = (object[]) state;
  162. dirty = (bool)its [0];
  163. if (dirty)
  164. items.Clear ();
  165. for (int n=1; n<its.Length; n++) {
  166. Pair pair = (Pair) its [n];
  167. int oi = (int) pair.First;
  168. TreeNode node;
  169. if (oi != -1)
  170. node = (TreeNode) items [oi];
  171. else
  172. node = new TreeNode ();
  173. if (dirty) Add (node);
  174. ((IStateManager)node).LoadViewState (pair.Second);
  175. }
  176. }
  177. object IStateManager.SaveViewState ()
  178. {
  179. object[] state = null;
  180. bool hasData = false;
  181. if (dirty) {
  182. state = new object [items.Count + 1];
  183. state [0] = true;
  184. for (int n=0; n<items.Count; n++) {
  185. TreeNode node = items[n] as TreeNode;
  186. object ns = ((IStateManager)node).SaveViewState ();
  187. if (ns != null) hasData = true;
  188. state [n + 1] = new Pair (-1, ns);
  189. }
  190. } else {
  191. ArrayList list = new ArrayList ();
  192. for (int n=0; n<items.Count; n++) {
  193. TreeNode node = items[n] as TreeNode;
  194. object ns = ((IStateManager)node).SaveViewState ();
  195. if (ns != null) {
  196. hasData = true;
  197. list.Add (new Pair (n, ns));
  198. }
  199. }
  200. if (hasData) {
  201. list.Insert (0, false);
  202. state = list.ToArray ();
  203. }
  204. }
  205. if (hasData)
  206. return state;
  207. else
  208. return null;
  209. }
  210. void IStateManager.TrackViewState ()
  211. {
  212. marked = true;
  213. for (int n=0; n<items.Count; n++) {
  214. ((IStateManager) items [n]).TrackViewState ();
  215. }
  216. }
  217. bool IStateManager.IsTrackingViewState {
  218. get { return marked; }
  219. }
  220. }
  221. }
  222. #endif