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