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 TreeNode[] originalItems;
42 ArrayList items = new ArrayList ();
48 public TreeNodeCollection ()
52 public TreeNodeCollection (TreeNode owner)
55 this.tree = owner.Tree;
58 internal TreeNodeCollection (TreeView tree)
63 internal void SetTree (TreeView tree)
66 foreach (TreeNode node in items)
70 public TreeNode this [int i] {
71 get { return (TreeNode) items [i]; }
74 public void Add (TreeNode child)
76 child.Index = items.Add (child);
78 child.SetParent (parent);
80 child.TrackViewState ();
86 public void AddAt (int index, TreeNode child)
88 items.Insert (index, child);
91 child.SetParent (parent);
92 for (int n=index+1; n<items.Count; n++)
93 ((TreeNode)items[n]).Index = n;
95 child.TrackViewState ();
103 if (tree != null || parent != null) {
104 foreach (TreeNode nod in items) {
106 nod.SetParent (null);
113 public bool Contains (TreeNode child)
115 return items.Contains (child);
118 public void CopyTo (TreeNode[] nodeArray, int index)
120 items.CopyTo (nodeArray, index);
123 public IEnumerator GetEnumerator ()
125 return items.GetEnumerator ();
128 public int IndexOf (TreeNode node)
130 return items.IndexOf (node);
133 public void Remove (TreeNode node)
135 int i = IndexOf (node);
143 public void RemoveAt (int index)
145 TreeNode node = (TreeNode) items [index];
146 items.RemoveAt (index);
153 get { return items.Count; }
156 public bool IsSynchronized {
157 get { return false; }
160 public object SyncRoot {
161 get { return items; }
164 void System.Collections.ICollection.CopyTo (Array array, int index)
166 items.CopyTo (array, index);
169 void IStateManager.LoadViewState (object state)
171 if (state == null) return;
172 object[] its = (object[]) state;
174 dirty = (bool)its [0];
179 for (int n=1; n<its.Length; n++) {
180 Pair pair = (Pair) its [n];
181 int oi = (int) pair.First;
183 if (oi != -1) node = originalItems [oi];
184 else node = new TreeNode ();
185 if (dirty) Add (node);
186 node.LoadViewState (pair.Second);
190 object IStateManager.SaveViewState ()
192 object[] state = null;
193 bool hasData = false;
196 state = new object [items.Count + 1];
198 for (int n=0; n<items.Count; n++) {
199 TreeNode node = items[n] as TreeNode;
200 int oi = Array.IndexOf (originalItems, node);
201 object ns = node.SaveViewState ();
202 if (ns != null) hasData = true;
203 state [n + 1] = new Pair (oi, ns);
206 ArrayList list = new ArrayList ();
207 for (int n=0; n<items.Count; n++) {
208 TreeNode node = items[n] as TreeNode;
209 object ns = node.SaveViewState ();
212 list.Add (new Pair (n, ns));
216 list.Insert (0, false);
217 state = list.ToArray ();
227 void IStateManager.TrackViewState ()
230 originalItems = new TreeNode [items.Count];
231 for (int n=0; n<items.Count; n++) {
232 originalItems [n] = (TreeNode) items [n];
233 originalItems [n].TrackViewState ();
237 bool IStateManager.IsTrackingViewState {
238 get { return marked; }