TreeNodeCollection.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. TreeNode[] originalItems;
  39. ArrayList items = new ArrayList ();
  40. TreeView tree;
  41. TreeNode parent;
  42. bool marked;
  43. bool dirty;
  44. public TreeNodeCollection ()
  45. {
  46. }
  47. public TreeNodeCollection (TreeNode owner)
  48. {
  49. this.parent = owner;
  50. this.tree = owner.Tree;
  51. }
  52. internal TreeNodeCollection (TreeView tree)
  53. {
  54. this.tree = tree;
  55. }
  56. internal void SetTree (TreeView tree)
  57. {
  58. this.tree = tree;
  59. foreach (TreeNode node in items)
  60. node.Tree = tree;
  61. }
  62. public TreeNode this [int i] {
  63. get { return (TreeNode) items [i]; }
  64. }
  65. public void Add (TreeNode child)
  66. {
  67. child.Index = items.Add (child);
  68. child.Tree = tree;
  69. child.SetParent (parent);
  70. if (marked) {
  71. ((IStateManager)child).TrackViewState ();
  72. child.SetDirty ();
  73. dirty = true;
  74. }
  75. }
  76. public void AddAt (int index, TreeNode child)
  77. {
  78. items.Insert (index, child);
  79. child.Index = index;
  80. child.Tree = tree;
  81. child.SetParent (parent);
  82. for (int n=index+1; n<items.Count; n++)
  83. ((TreeNode)items[n]).Index = n;
  84. if (marked) {
  85. ((IStateManager)child).TrackViewState ();
  86. child.SetDirty ();
  87. dirty = true;
  88. }
  89. }
  90. public void Clear ()
  91. {
  92. if (tree != null || parent != null) {
  93. foreach (TreeNode nod in items) {
  94. nod.Tree = null;
  95. nod.SetParent (null);
  96. }
  97. }
  98. items.Clear ();
  99. dirty = true;
  100. }
  101. public bool Contains (TreeNode child)
  102. {
  103. return items.Contains (child);
  104. }
  105. public void CopyTo (TreeNode[] nodeArray, int index)
  106. {
  107. items.CopyTo (nodeArray, index);
  108. }
  109. public IEnumerator GetEnumerator ()
  110. {
  111. return items.GetEnumerator ();
  112. }
  113. public int IndexOf (TreeNode node)
  114. {
  115. return items.IndexOf (node);
  116. }
  117. public void Remove (TreeNode node)
  118. {
  119. int i = IndexOf (node);
  120. if (i == -1) return;
  121. items.RemoveAt (i);
  122. if (tree != null)
  123. node.Tree = null;
  124. dirty = true;
  125. }
  126. public void RemoveAt (int index)
  127. {
  128. TreeNode node = (TreeNode) items [index];
  129. items.RemoveAt (index);
  130. if (tree != null)
  131. node.Tree = null;
  132. dirty = true;
  133. }
  134. public int Count {
  135. get { return items.Count; }
  136. }
  137. public bool IsSynchronized {
  138. get { return false; }
  139. }
  140. public object SyncRoot {
  141. get { return items; }
  142. }
  143. void System.Collections.ICollection.CopyTo (Array array, int index)
  144. {
  145. items.CopyTo (array, index);
  146. }
  147. void IStateManager.LoadViewState (object state)
  148. {
  149. if (state == null) return;
  150. object[] its = (object[]) state;
  151. dirty = (bool)its [0];
  152. if (dirty)
  153. items.Clear ();
  154. for (int n=1; n<its.Length; n++) {
  155. Pair pair = (Pair) its [n];
  156. int oi = (int) pair.First;
  157. TreeNode node;
  158. if (oi != -1) node = originalItems [oi];
  159. else node = new TreeNode ();
  160. if (dirty) Add (node);
  161. ((IStateManager)node).LoadViewState (pair.Second);
  162. }
  163. }
  164. object IStateManager.SaveViewState ()
  165. {
  166. object[] state = null;
  167. bool hasData = false;
  168. if (dirty) {
  169. state = new object [items.Count + 1];
  170. state [0] = true;
  171. for (int n=0; n<items.Count; n++) {
  172. TreeNode node = items[n] as TreeNode;
  173. int oi = Array.IndexOf (originalItems, node);
  174. object ns = ((IStateManager)node).SaveViewState ();
  175. if (ns != null) hasData = true;
  176. state [n + 1] = new Pair (oi, ns);
  177. }
  178. } else {
  179. ArrayList list = new ArrayList ();
  180. for (int n=0; n<items.Count; n++) {
  181. TreeNode node = items[n] as TreeNode;
  182. object ns = ((IStateManager)node).SaveViewState ();
  183. if (ns != null) {
  184. hasData = true;
  185. list.Add (new Pair (n, ns));
  186. }
  187. }
  188. if (hasData) {
  189. list.Insert (0, false);
  190. state = list.ToArray ();
  191. }
  192. }
  193. if (hasData)
  194. return state;
  195. else
  196. return null;
  197. }
  198. void IStateManager.TrackViewState ()
  199. {
  200. marked = true;
  201. originalItems = new TreeNode [items.Count];
  202. for (int n=0; n<items.Count; n++) {
  203. originalItems [n] = (TreeNode) items [n];
  204. ((IStateManager)originalItems [n]).TrackViewState ();
  205. }
  206. }
  207. bool IStateManager.IsTrackingViewState {
  208. get { return marked; }
  209. }
  210. }
  211. }
  212. #endif