2 // System.Web.UI.WebControls.TreeNodeCollection.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
35 using System.Collections;
37 namespace System.Web.UI.WebControls
39 public sealed class TreeNodeCollection: ICollection, IEnumerable, IStateManager
41 ArrayList items = new ArrayList ();
47 public TreeNodeCollection ()
51 public TreeNodeCollection (TreeNode owner)
54 this.tree = owner.Tree;
57 internal TreeNodeCollection (TreeView tree)
62 internal void SetTree (TreeView tree)
65 foreach (TreeNode node in items)
69 public TreeNode this [int i] {
70 get { return (TreeNode) items [i]; }
73 public void Add (TreeNode child)
78 internal void Add (TreeNode child, bool updateParent)
80 int index = items.Add (child);
86 child.SetParent (parent);
89 ((IStateManager)child).TrackViewState ();
95 public void AddAt (int index, TreeNode child)
97 items.Insert (index, child);
99 child.SetParent (parent);
101 for (int n=index+1; n<items.Count; n++)
102 ((TreeNode)items[n]).Index = n;
104 ((IStateManager)child).TrackViewState ();
110 internal void SetDirty () {
111 for (int n = 0; n < Count; n++)
112 this [n].SetDirty ();
118 if (tree != null || parent != null) {
119 foreach (TreeNode nod in items) {
121 nod.SetParent (null);
130 public bool Contains (TreeNode child)
132 return items.Contains (child);
135 public void CopyTo (TreeNode[] nodeArray, int index)
137 items.CopyTo (nodeArray, index);
140 public IEnumerator GetEnumerator ()
142 return items.GetEnumerator ();
145 public int IndexOf (TreeNode node)
147 return items.IndexOf (node);
150 public void Remove (TreeNode node)
152 int i = IndexOf (node);
162 public void RemoveAt (int index)
164 TreeNode node = (TreeNode) items [index];
165 items.RemoveAt (index);
174 get { return items.Count; }
177 public bool IsSynchronized {
178 get { return false; }
181 public object SyncRoot {
182 get { return items; }
185 void System.Collections.ICollection.CopyTo (Array array, int index)
187 items.CopyTo (array, index);
190 void IStateManager.LoadViewState (object state)
192 if (state == null) return;
193 object[] its = (object[]) state;
195 dirty = (bool)its [0];
200 for (int n=1; n<its.Length; n++) {
201 Pair pair = (Pair) its [n];
202 int oi = (int) pair.First;
205 node = (TreeNode) items [oi];
207 node = new TreeNode ();
208 if (dirty) Add (node);
209 ((IStateManager)node).LoadViewState (pair.Second);
213 object IStateManager.SaveViewState ()
215 object[] state = null;
216 bool hasData = false;
219 state = new object [items.Count + 1];
221 for (int n=0; n<items.Count; n++) {
222 TreeNode node = items[n] as TreeNode;
223 object ns = ((IStateManager)node).SaveViewState ();
224 if (ns != null) hasData = true;
225 state [n + 1] = new Pair (-1, ns);
228 ArrayList list = new ArrayList ();
229 for (int n=0; n<items.Count; n++) {
230 TreeNode node = items[n] as TreeNode;
231 object ns = ((IStateManager)node).SaveViewState ();
234 list.Add (new Pair (n, ns));
238 list.Insert (0, false);
239 state = list.ToArray ();
249 void IStateManager.TrackViewState ()
252 for (int n=0; n<items.Count; n++) {
253 ((IStateManager) items [n]).TrackViewState ();
257 bool IStateManager.IsTrackingViewState {
258 get { return marked; }