TreeNodeCollection.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if (marked) {
  111. dirty = true;
  112. }
  113. }
  114. public bool Contains (TreeNode child)
  115. {
  116. return items.Contains (child);
  117. }
  118. public void CopyTo (TreeNode[] nodeArray, int index)
  119. {
  120. items.CopyTo (nodeArray, index);
  121. }
  122. public IEnumerator GetEnumerator ()
  123. {
  124. return items.GetEnumerator ();
  125. }
  126. public int IndexOf (TreeNode node)
  127. {
  128. return items.IndexOf (node);
  129. }
  130. public void Remove (TreeNode node)
  131. {
  132. int i = IndexOf (node);
  133. if (i == -1) return;
  134. items.RemoveAt (i);
  135. if (tree != null)
  136. node.Tree = null;
  137. if (marked) {
  138. dirty = true;
  139. }
  140. }
  141. public void RemoveAt (int index)
  142. {
  143. TreeNode node = (TreeNode) items [index];
  144. items.RemoveAt (index);
  145. if (tree != null)
  146. node.Tree = null;
  147. if (marked) {
  148. dirty = true;
  149. }
  150. }
  151. public int Count {
  152. get { return items.Count; }
  153. }
  154. public bool IsSynchronized {
  155. get { return false; }
  156. }
  157. public object SyncRoot {
  158. get { return items; }
  159. }
  160. void System.Collections.ICollection.CopyTo (Array array, int index)
  161. {
  162. items.CopyTo (array, index);
  163. }
  164. void IStateManager.LoadViewState (object state)
  165. {
  166. if (state == null) return;
  167. object[] its = (object[]) state;
  168. dirty = (bool)its [0];
  169. if (dirty)
  170. items.Clear ();
  171. for (int n=1; n<its.Length; n++) {
  172. Pair pair = (Pair) its [n];
  173. int oi = (int) pair.First;
  174. TreeNode node;
  175. if (oi != -1)
  176. node = (TreeNode) items [oi];
  177. else
  178. node = new TreeNode ();
  179. if (dirty) Add (node);
  180. ((IStateManager)node).LoadViewState (pair.Second);
  181. }
  182. }
  183. object IStateManager.SaveViewState ()
  184. {
  185. object[] state = null;
  186. bool hasData = false;
  187. if (dirty) {
  188. state = new object [items.Count + 1];
  189. state [0] = true;
  190. for (int n=0; n<items.Count; n++) {
  191. TreeNode node = items[n] as TreeNode;
  192. object ns = ((IStateManager)node).SaveViewState ();
  193. if (ns != null) hasData = true;
  194. state [n + 1] = new Pair (-1, ns);
  195. }
  196. } else {
  197. ArrayList list = new ArrayList ();
  198. for (int n=0; n<items.Count; n++) {
  199. TreeNode node = items[n] as TreeNode;
  200. object ns = ((IStateManager)node).SaveViewState ();
  201. if (ns != null) {
  202. hasData = true;
  203. list.Add (new Pair (n, ns));
  204. }
  205. }
  206. if (hasData) {
  207. list.Insert (0, false);
  208. state = list.ToArray ();
  209. }
  210. }
  211. if (hasData)
  212. return state;
  213. else
  214. return null;
  215. }
  216. void IStateManager.TrackViewState ()
  217. {
  218. marked = true;
  219. for (int n=0; n<items.Count; n++) {
  220. ((IStateManager) items [n]).TrackViewState ();
  221. }
  222. }
  223. bool IStateManager.IsTrackingViewState {
  224. get { return marked; }
  225. }
  226. }
  227. }
  228. #endif