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