Merge pull request #1695 from gregoryyoung/master
[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
32 using System;
33 using System.Web.UI;
34 using System.Collections;
35
36 namespace System.Web.UI.WebControls
37 {
38         public sealed class TreeNodeCollection: ICollection, IEnumerable, IStateManager
39         {
40                 ArrayList items = new ArrayList ();
41                 TreeView tree;
42                 TreeNode parent;
43                 bool marked;
44                 bool dirty;
45                 
46                 public TreeNodeCollection ()
47                 {
48                 }
49                 
50                 public TreeNodeCollection (TreeNode owner)
51                 {
52                         this.parent = owner;
53                         this.tree = owner.Tree;
54                 }
55                 
56                 internal TreeNodeCollection (TreeView tree)
57                 {
58                         this.tree = tree;
59                 }
60                 
61                 internal void SetTree (TreeView tree)
62                 {
63                         this.tree = tree;
64                         foreach (TreeNode node in items)
65                                 node.Tree = tree;
66                 }
67                 
68                 public TreeNode this [int i] {
69                         get { return (TreeNode) items [i]; }
70                 }
71                 
72                 public void Add (TreeNode child)
73                 {
74                         Add (child, true);
75                 }
76                 
77                 internal void Add (TreeNode child, bool updateParent)
78                 {
79                         int index = items.Add (child);
80
81                         if (parent != null)
82                                 parent.HadChildrenBeforePopulating = true;
83                         
84                         if (!updateParent)
85                                 return;
86
87                         child.Index = index;
88                         child.SetParent (parent);
89                         child.Tree = tree;
90                         if (marked) {
91                                 ((IStateManager)child).TrackViewState ();
92                                 SetDirty ();
93                         }
94                 }
95                 
96                 public void AddAt (int index, TreeNode child)
97                 {
98                         items.Insert (index, child);
99                         child.Index = index;
100                         child.SetParent (parent);
101                         child.Tree = tree;
102                         for (int n=index+1; n<items.Count; n++)
103                                 ((TreeNode)items[n]).Index = n;
104                         if (marked) {
105                                 ((IStateManager)child).TrackViewState ();
106                                 SetDirty ();
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                                 SetDirty ();
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                                 SetDirty ();
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                                         var pair = its [n] as Pair;
202                                         if (pair == null)
203                                                 throw new InvalidOperationException ("Broken view state (item " + n + ")");
204                                         
205                                         TreeNode item;
206                                         Type type = pair.First as Type;
207
208                                         if (type == null)
209                                                 item = new TreeNode ();
210                                         else
211                                                 item = Activator.CreateInstance (pair.First as Type) as TreeNode;
212                                         Add (item);
213                                         object ns = pair.Second;
214                                         if (ns != null)
215                                                 ((IStateManager) item).LoadViewState (ns);
216                                 }
217                         }
218                         else {
219                                 for (int n = 1; n < its.Length; n++) {
220                                         var pair = its [n] as Pair;
221                                         if (pair  == null)
222                                                 throw new InvalidOperationException ("Broken view state " + n + ")");
223                                         
224                                         int oi = (int) pair.First;
225                                         TreeNode node = (TreeNode) items [oi];
226                                         ((IStateManager) node).LoadViewState (pair.Second);
227                                 }
228                         }
229
230                 }
231                 
232                 object IStateManager.SaveViewState ()
233                 {
234                         object[] state = null;
235                         bool hasData = false;
236                         
237                         if (dirty) {
238                                 if (items.Count > 0) {
239                                         hasData = true;
240                                         state = new object [items.Count + 1];
241                                         state [0] = true;
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);
247                                         }
248                                 }
249                         } else {
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 ();
254                                         if (ns != null) {
255                                                 hasData = true;
256                                                 list.Add (new Pair (n, ns));
257                                         }
258                                 }
259                                 if (hasData) {
260                                         list.Insert (0, false);
261                                         state = list.ToArray ();
262                                 }
263                         }
264                         
265                         if (hasData)
266                                 return state;
267                         else
268                                 return null;
269                 }
270                 
271                 void IStateManager.TrackViewState ()
272                 {
273                         marked = true;
274                         for (int n=0; n<items.Count; n++) {
275                                 ((IStateManager) items [n]).TrackViewState ();
276                         }
277                 }
278                 
279                 bool IStateManager.IsTrackingViewState {
280                         get { return marked; }
281                 }
282         }
283 }
284