2 // System.Web.UI.WebControls.MenuItemCollection.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
34 using System.Collections;
36 namespace System.Web.UI.WebControls
38 public sealed class MenuItemCollection: ICollection, IEnumerable, IStateManager
40 ArrayList items = new ArrayList ();
46 public MenuItemCollection ()
50 public MenuItemCollection (MenuItem owner)
53 this.menu = owner.Menu;
56 internal MenuItemCollection (Menu menu)
61 internal void SetMenu (Menu menu)
64 foreach (MenuItem item in items)
68 public MenuItem this [int index] {
69 get { return (MenuItem) items [index]; }
72 public void Add (MenuItem child)
74 child.Index = items.Add (child);
76 child.SetParent (parent);
78 ((IStateManager)child).TrackViewState ();
83 internal void SetDirty () {
84 for (int n = 0; n < Count; n++)
89 public void AddAt (int index, MenuItem child)
91 items.Insert (index, child);
94 child.SetParent (parent);
95 for (int n=index+1; n<items.Count; n++)
96 ((MenuItem)items[n]).Index = n;
98 ((IStateManager)child).TrackViewState ();
105 if (menu != null || parent != null) {
106 foreach (MenuItem nod in items) {
108 nod.SetParent (null);
117 public bool Contains (MenuItem c)
119 return items.Contains (c);
122 public void CopyTo (Array array, int index)
124 items.CopyTo (array, index);
127 public void CopyTo (MenuItem[] array, int index)
129 items.CopyTo (array, index);
132 public IEnumerator GetEnumerator ()
134 return items.GetEnumerator ();
137 public int IndexOf (MenuItem value)
139 return items.IndexOf (value);
142 public void Remove (MenuItem value)
144 int i = IndexOf (value);
154 public void RemoveAt (int index)
156 MenuItem item = (MenuItem) items [index];
157 items.RemoveAt (index);
166 get { return items.Count; }
169 public bool IsSynchronized {
170 get { return false; }
173 public object SyncRoot {
174 get { return items; }
177 void System.Collections.ICollection.CopyTo (Array array, int index)
179 items.CopyTo (array, index);
182 void IStateManager.LoadViewState (object state)
184 if (state == null) return;
185 object[] its = (object[]) state;
187 dirty = (bool)its [0];
192 for (int n = 1; n < its.Length; n++) {
193 MenuItem item = new MenuItem ();
197 ((IStateManager) item).LoadViewState (ns);
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);
210 object IStateManager.SaveViewState ()
212 object[] state = null;
213 bool hasData = false;
216 if (items.Count > 0) {
218 state = new object [items.Count + 1];
220 for (int n = 0; n < items.Count; n++) {
221 MenuItem item = items [n] as MenuItem;
222 object ns = ((IStateManager) item).SaveViewState ();
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 ();
233 list.Add (new Pair (n, ns));
237 list.Insert (0, false);
238 state = list.ToArray ();
248 void IStateManager.TrackViewState ()
251 for (int n=0; n<items.Count; n++)
252 ((IStateManager) items [n]).TrackViewState ();
255 bool IStateManager.IsTrackingViewState {
256 get { return marked; }