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)
34 using System.Collections;
36 namespace System.Web.UI.WebControls
38 public sealed class TreeNodeCollection: ICollection, IEnumerable, IStateManager
40 ArrayList items = new ArrayList ();
46 public TreeNodeCollection ()
50 public TreeNodeCollection (TreeNode owner)
53 this.tree = owner.Tree;
56 internal TreeNodeCollection (TreeView tree)
61 internal void SetTree (TreeView tree)
64 foreach (TreeNode node in items)
68 public TreeNode this [int i] {
69 get { return (TreeNode) items [i]; }
72 public void Add (TreeNode child)
77 internal void Add (TreeNode child, bool updateParent)
79 int index = items.Add (child);
82 parent.HadChildrenBeforePopulating = true;
88 child.SetParent (parent);
91 ((IStateManager)child).TrackViewState ();
96 public void AddAt (int index, TreeNode child)
98 items.Insert (index, child);
100 child.SetParent (parent);
102 for (int n=index+1; n<items.Count; n++)
103 ((TreeNode)items[n]).Index = n;
105 ((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 var pair = its [n] as Pair;
203 throw new InvalidOperationException ("Broken view state (item " + n + ")");
206 Type type = pair.First as Type;
209 item = new TreeNode ();
211 item = Activator.CreateInstance (pair.First as Type) as TreeNode;
213 object ns = pair.Second;
215 ((IStateManager) item).LoadViewState (ns);
219 for (int n = 1; n < its.Length; n++) {
220 var pair = its [n] as Pair;
222 throw new InvalidOperationException ("Broken view state " + n + ")");
224 int oi = (int) pair.First;
225 TreeNode node = (TreeNode) items [oi];
226 ((IStateManager) node).LoadViewState (pair.Second);
232 object IStateManager.SaveViewState ()
234 object[] state = null;
235 bool hasData = false;
238 if (items.Count > 0) {
240 state = new object [items.Count + 1];
242 for (int n = 0; n < items.Count; n++) {
243 TreeNode node = items [n] as TreeNode;
244 object ns = ((IStateManager) node).SaveViewState ();
245 Type type = node.GetType ();
246 state [n + 1] = new Pair (type == typeof (TreeNode) ? null : type, ns);
250 ArrayList list = new ArrayList ();
251 for (int n=0; n<items.Count; n++) {
252 TreeNode node = items[n] as TreeNode;
253 object ns = ((IStateManager)node).SaveViewState ();
256 list.Add (new Pair (n, ns));
260 list.Insert (0, false);
261 state = list.ToArray ();
271 void IStateManager.TrackViewState ()
274 for (int n=0; n<items.Count; n++) {
275 ((IStateManager) items [n]).TrackViewState ();
279 bool IStateManager.IsTrackingViewState {
280 get { return marked; }