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