2006-04-11 Lluis Sanchez <lluis@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                 MenuItem[] originalItems;
42                 ArrayList items = new ArrayList ();
43                 Menu menu;
44                 MenuItem parent;
45                 bool marked;
46                 bool dirty;
47                 
48                 public MenuItemCollection ()
49                 {
50                 }
51                 
52                 public MenuItemCollection (MenuItem owner)
53                 {
54                         this.parent = owner;
55                         this.menu = owner.Menu;
56                 }
57                 
58                 internal MenuItemCollection (Menu menu)
59                 {
60                         this.menu = menu;
61                 }
62                 
63                 internal void SetMenu (Menu menu)
64                 {
65                         this.menu = menu;
66                         foreach (MenuItem item in items)
67                                 item.Menu = menu;
68                 }
69                 
70                 public MenuItem this [int i] {
71                         get { return (MenuItem) items [i]; }
72                 }
73                 
74                 public void Add (MenuItem child)
75                 {
76                         child.Index = items.Add (child);
77                         child.Menu = menu;
78                         child.SetParent (parent);
79                         if (marked) {
80                                 ((IStateManager)child).TrackViewState ();
81                                 child.SetDirty ();
82                                 dirty = true;
83                         }
84                 }
85                 
86                 public void AddAt (int index, MenuItem child)
87                 {
88                         items.Insert (index, child);
89                         child.Index = index;
90                         child.Menu = menu;
91                         child.SetParent (parent);
92                         for (int n=index+1; n<items.Count; n++)
93                                 ((MenuItem)items[n]).Index = n;
94                         if (marked) {
95                                 ((IStateManager)child).TrackViewState ();
96                                 child.SetDirty ();
97                                 dirty = true;
98                         }
99                 }
100                 
101                 public void Clear ()
102                 {
103                         if (menu != null || parent != null) {
104                                 foreach (MenuItem nod in items) {
105                                         nod.Menu = null;
106                                         nod.SetParent (null);
107                                 }
108                         }
109                         items.Clear ();
110                         dirty = true;
111                 }
112                 
113                 public bool Contains (MenuItem child)
114                 {
115                         return items.Contains (child);
116                 }
117                 
118                 public void CopyTo (Array itemArray, int index)
119                 {
120                         items.CopyTo (itemArray, index);
121                 }
122                 
123                 public void CopyTo (MenuItem[] itemArray, int index)
124                 {
125                         items.CopyTo (itemArray, index);
126                 }
127                 
128                 public IEnumerator GetEnumerator ()
129                 {
130                         return items.GetEnumerator ();
131                 }
132                 
133                 public int IndexOf (MenuItem item)
134                 {
135                         return items.IndexOf (item);
136                 }
137                 
138                 public void Remove (MenuItem item)
139                 {
140                         int i = IndexOf (item);
141                         if (i == -1) return;
142                         items.RemoveAt (i);
143                         if (menu != null)
144                                 item.Menu = null;
145                         dirty = true;
146                 }
147                 
148                 public void RemoveAt (int index)
149                 {
150                         MenuItem item = (MenuItem) items [index];
151                         items.RemoveAt (index);
152                         if (menu != null)
153                                 item.Menu = 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                                 MenuItem item;
188                                 if (oi != -1) item = originalItems [oi];
189                                 else item = new MenuItem ();
190                                 if (dirty) Add (item);
191                                 ((IStateManager)item).LoadViewState (pair.Second);
192                         }
193                 }
194                 
195                 object IStateManager.SaveViewState ()
196                 {
197                         object[] state = null;
198                         bool hasData = false;
199                         
200                         if (dirty) {
201                                 state = new object [items.Count + 1];
202                                 state [0] = true;
203                                 for (int n=0; n<items.Count; n++) {
204                                         MenuItem item = items[n] as MenuItem;
205                                         int oi = Array.IndexOf (originalItems, item);
206                                         object ns = ((IStateManager)item).SaveViewState ();
207                                         if (ns != null) hasData = true;
208                                         state [n + 1] = new Pair (oi, ns);
209                                 }
210                         } else {
211                                 ArrayList list = new ArrayList ();
212                                 for (int n=0; n<items.Count; n++) {
213                                         MenuItem item = items[n] as MenuItem;
214                                         object ns = ((IStateManager)item).SaveViewState ();
215                                         if (ns != null) {
216                                                 hasData = true;
217                                                 list.Add (new Pair (n, ns));
218                                         }
219                                 }
220                                 if (hasData) {
221                                         list.Insert (0, false);
222                                         state = list.ToArray ();
223                                 }
224                         }
225                         
226                         if (hasData)
227                                 return state;
228                         else
229                                 return null;
230                 }
231                 
232                 void IStateManager.TrackViewState ()
233                 {
234                         marked = true;
235                         originalItems = new MenuItem [items.Count];
236                         for (int n=0; n<items.Count; n++) {
237                                 originalItems [n] = (MenuItem) items [n];
238                                 ((IStateManager)originalItems [n]).TrackViewState ();
239                         }
240                 }
241                 
242                 bool IStateManager.IsTrackingViewState {
243                         get { return marked; }
244                 }
245         }
246 }
247
248 #endif