TreeNodeCollection.cs: fixed: ViewState saved and restored correct
[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                         child.Index = items.Add (child);
76                         child.Tree = tree;
77                         child.SetParent (parent);
78                         if (marked) {
79                                 ((IStateManager)child).TrackViewState ();
80                                 child.SetDirty ();
81                                 dirty = true;
82                         }
83                 }
84                 
85                 public void AddAt (int index, TreeNode child)
86                 {
87                         items.Insert (index, child);
88                         child.Index = index;
89                         child.Tree = tree;
90                         child.SetParent (parent);
91                         for (int n=index+1; n<items.Count; n++)
92                                 ((TreeNode)items[n]).Index = n;
93                         if (marked) {
94                                 ((IStateManager)child).TrackViewState ();
95                                 child.SetDirty ();
96                                 dirty = true;
97                         }
98                 }
99
100                 internal void SetDirty () {
101                         for (int n = 0; n < Count; n++)
102                                 this [n].SetDirty ();
103                         dirty = true;
104                 }
105                 
106                 public void Clear ()
107                 {
108                         if (tree != null || parent != null) {
109                                 foreach (TreeNode nod in items) {
110                                         nod.Tree = null;
111                                         nod.SetParent (null);
112                                 }
113                         }
114                         items.Clear ();
115                         dirty = true;
116                 }
117                 
118                 public bool Contains (TreeNode child)
119                 {
120                         return items.Contains (child);
121                 }
122                 
123                 public void CopyTo (TreeNode[] nodeArray, int index)
124                 {
125                         items.CopyTo (nodeArray, index);
126                 }
127                 
128                 public IEnumerator GetEnumerator ()
129                 {
130                         return items.GetEnumerator ();
131                 }
132                 
133                 public int IndexOf (TreeNode node)
134                 {
135                         return items.IndexOf (node);
136                 }
137                 
138                 public void Remove (TreeNode node)
139                 {
140                         int i = IndexOf (node);
141                         if (i == -1) return;
142                         items.RemoveAt (i);
143                         if (tree != null)
144                                 node.Tree = null;
145                         dirty = true;
146                 }
147                 
148                 public void RemoveAt (int index)
149                 {
150                         TreeNode node = (TreeNode) items [index];
151                         items.RemoveAt (index);
152                         if (tree != null)
153                                 node.Tree = null;
154                         dirty = true;
155                 }
156                 
157                 public int Count {
158                         get { return items.Count; }
159                 }
160                 
161                 public bool IsSynchronized {
162                         get { return false; }
163                 }
164                 
165                 public object SyncRoot {
166                         get { return items; }
167                 }
168                 
169                 void System.Collections.ICollection.CopyTo (Array array, int index)
170                 {
171                         items.CopyTo (array, index);
172                 }
173
174                 void IStateManager.LoadViewState (object state)
175                 {
176                         if (state == null) return;
177                         object[] its = (object[]) state;
178                         
179                         dirty = (bool)its [0];
180                         
181                         if (dirty)
182                                 items.Clear ();
183
184                         for (int n=1; n<its.Length; n++) {
185                                 Pair pair = (Pair) its [n];
186                                 int oi = (int) pair.First;
187                                 TreeNode node;
188                                 if (oi != -1)
189                                         node = (TreeNode) items [oi];
190                                 else
191                                         node = new TreeNode ();
192                                 if (dirty) Add (node);
193                                 ((IStateManager)node).LoadViewState (pair.Second);
194                         }
195                 }
196                 
197                 object IStateManager.SaveViewState ()
198                 {
199                         object[] state = null;
200                         bool hasData = false;
201                         
202                         if (dirty) {
203                                 state = new object [items.Count + 1];
204                                 state [0] = true;
205                                 for (int n=0; n<items.Count; n++) {
206                                         TreeNode node = items[n] as TreeNode;
207                                         object ns = ((IStateManager)node).SaveViewState ();
208                                         if (ns != null) hasData = true;
209                                         state [n + 1] = new Pair (-1, ns);
210                                 }
211                         } else {
212                                 ArrayList list = new ArrayList ();
213                                 for (int n=0; n<items.Count; n++) {
214                                         TreeNode node = items[n] as TreeNode;
215                                         object ns = ((IStateManager)node).SaveViewState ();
216                                         if (ns != null) {
217                                                 hasData = true;
218                                                 list.Add (new Pair (n, ns));
219                                         }
220                                 }
221                                 if (hasData) {
222                                         list.Insert (0, false);
223                                         state = list.ToArray ();
224                                 }
225                         }
226                         
227                         if (hasData)
228                                 return state;
229                         else
230                                 return null;
231                 }
232                 
233                 void IStateManager.TrackViewState ()
234                 {
235                         marked = true;
236                         for (int n=0; n<items.Count; n++) {
237                                 ((IStateManager) items [n]).TrackViewState ();
238                         }
239                 }
240                 
241                 bool IStateManager.IsTrackingViewState {
242                         get { return marked; }
243                 }
244         }
245 }
246
247 #endif