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