fixed tests
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TreeNodeCollection.cs
1 //
2 // System.Web.UI.WebControls.TreeNodeCollection.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
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
31 #if NET_2_0
32
33 using System;
34 using System.Web.UI;
35 using System.Collections;
36
37 namespace System.Web.UI.WebControls
38 {
39         public sealed class TreeNodeCollection: ICollection, IEnumerable, IStateManager
40         {
41                 ArrayList items = new ArrayList ();
42                 TreeView tree;
43                 TreeNode parent;
44                 bool marked;
45                 bool dirty;
46                 
47                 public TreeNodeCollection ()
48                 {
49                 }
50                 
51                 public TreeNodeCollection (TreeNode owner)
52                 {
53                         this.parent = owner;
54                         this.tree = owner.Tree;
55                 }
56                 
57                 internal TreeNodeCollection (TreeView tree)
58                 {
59                         this.tree = tree;
60                 }
61                 
62                 internal void SetTree (TreeView tree)
63                 {
64                         this.tree = tree;
65                         foreach (TreeNode node in items)
66                                 node.Tree = tree;
67                 }
68                 
69                 public TreeNode this [int i] {
70                         get { return (TreeNode) items [i]; }
71                 }
72                 
73                 public void Add (TreeNode child)
74                 {
75                         Add (child, true);
76                 }
77                 
78                 internal void Add (TreeNode child, bool updateParent)
79                 {
80                         int index = items.Add (child);
81                         
82                         if (!updateParent)
83                                 return;
84
85                         child.Index = index;
86                         child.SetParent (parent);
87                         child.Tree = tree;
88                         if (marked) {
89                                 ((IStateManager)child).TrackViewState ();
90                                 child.SetDirty ();
91                                 dirty = true;
92                         }
93                 }
94                 
95                 public void AddAt (int index, TreeNode child)
96                 {
97                         items.Insert (index, child);
98                         child.Index = index;
99                         child.SetParent (parent);
100                         child.Tree = tree;
101                         for (int n=index+1; n<items.Count; n++)
102                                 ((TreeNode)items[n]).Index = n;
103                         if (marked) {
104                                 ((IStateManager)child).TrackViewState ();
105                                 child.SetDirty ();
106                                 dirty = true;
107                         }
108                 }
109
110                 internal void SetDirty () {
111                         for (int n = 0; n < Count; n++)
112                                 this [n].SetDirty ();
113                         dirty = true;
114                 }
115                 
116                 public void Clear ()
117                 {
118                         if (tree != null || parent != null) {
119                                 foreach (TreeNode nod in items) {
120                                         nod.Tree = null;
121                                         nod.SetParent (null);
122                                 }
123                         }
124                         items.Clear ();
125                         if (marked) {
126                                 dirty = true;
127                         }
128                 }
129                 
130                 public bool Contains (TreeNode child)
131                 {
132                         return items.Contains (child);
133                 }
134                 
135                 public void CopyTo (TreeNode[] nodeArray, int index)
136                 {
137                         items.CopyTo (nodeArray, index);
138                 }
139                 
140                 public IEnumerator GetEnumerator ()
141                 {
142                         return items.GetEnumerator ();
143                 }
144                 
145                 public int IndexOf (TreeNode node)
146                 {
147                         return items.IndexOf (node);
148                 }
149                 
150                 public void Remove (TreeNode node)
151                 {
152                         int i = IndexOf (node);
153                         if (i == -1) return;
154                         items.RemoveAt (i);
155                         if (tree != null)
156                                 node.Tree = null;
157                         if (marked) {
158                                 dirty = true;
159                         }
160                 }
161                 
162                 public void RemoveAt (int index)
163                 {
164                         TreeNode node = (TreeNode) items [index];
165                         items.RemoveAt (index);
166                         if (tree != null)
167                                 node.Tree = null;
168                         if (marked) {
169                                 dirty = true;
170                         }
171                 }
172                 
173                 public int Count {
174                         get { return items.Count; }
175                 }
176                 
177                 public bool IsSynchronized {
178                         get { return false; }
179                 }
180                 
181                 public object SyncRoot {
182                         get { return items; }
183                 }
184                 
185                 void System.Collections.ICollection.CopyTo (Array array, int index)
186                 {
187                         items.CopyTo (array, index);
188                 }
189
190                 void IStateManager.LoadViewState (object state)
191                 {
192                         if (state == null) return;
193                         object[] its = (object[]) state;
194                         
195                         dirty = (bool)its [0];
196                         
197                         if (dirty)
198                                 items.Clear ();
199
200                         for (int n=1; n<its.Length; n++) {
201                                 Pair pair = (Pair) its [n];
202                                 int oi = (int) pair.First;
203                                 TreeNode node;
204                                 if (oi != -1)
205                                         node = (TreeNode) items [oi];
206                                 else
207                                         node = new TreeNode ();
208                                 if (dirty) Add (node);
209                                 ((IStateManager)node).LoadViewState (pair.Second);
210                         }
211                 }
212                 
213                 object IStateManager.SaveViewState ()
214                 {
215                         object[] state = null;
216                         bool hasData = false;
217                         
218                         if (dirty) {
219                                 state = new object [items.Count + 1];
220                                 state [0] = true;
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);
226                                 }
227                         } else {
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 ();
232                                         if (ns != null) {
233                                                 hasData = true;
234                                                 list.Add (new Pair (n, ns));
235                                         }
236                                 }
237                                 if (hasData) {
238                                         list.Insert (0, false);
239                                         state = list.ToArray ();
240                                 }
241                         }
242                         
243                         if (hasData)
244                                 return state;
245                         else
246                                 return null;
247                 }
248                 
249                 void IStateManager.TrackViewState ()
250                 {
251                         marked = true;
252                         for (int n=0; n<items.Count; n++) {
253                                 ((IStateManager) items [n]).TrackViewState ();
254                         }
255                 }
256                 
257                 bool IStateManager.IsTrackingViewState {
258                         get { return marked; }
259                 }
260         }
261 }
262
263 #endif