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)
35 using System.Collections;
37 namespace System.Web.UI.WebControls
39 public sealed class MenuItemCollection: ICollection, IEnumerable, IStateManager
41 ArrayList items = new ArrayList ();
47 public MenuItemCollection ()
51 public MenuItemCollection (MenuItem owner)
54 this.menu = owner.Menu;
57 internal MenuItemCollection (Menu menu)
62 internal void SetMenu (Menu menu)
65 foreach (MenuItem item in items)
69 public MenuItem this [int i] {
70 get { return (MenuItem) items [i]; }
73 public void Add (MenuItem child)
75 child.Index = items.Add (child);
77 child.SetParent (parent);
79 ((IStateManager)child).TrackViewState ();
85 internal void SetDirty () {
86 for (int n = 0; n < Count; n++)
91 public void AddAt (int index, MenuItem child)
93 items.Insert (index, child);
96 child.SetParent (parent);
97 for (int n=index+1; n<items.Count; n++)
98 ((MenuItem)items[n]).Index = n;
100 ((IStateManager)child).TrackViewState ();
108 if (menu != null || parent != null) {
109 foreach (MenuItem nod in items) {
111 nod.SetParent (null);
118 public bool Contains (MenuItem child)
120 return items.Contains (child);
123 public void CopyTo (Array itemArray, int index)
125 items.CopyTo (itemArray, index);
128 public void CopyTo (MenuItem[] itemArray, int index)
130 items.CopyTo (itemArray, index);
133 public IEnumerator GetEnumerator ()
135 return items.GetEnumerator ();
138 public int IndexOf (MenuItem item)
140 return items.IndexOf (item);
143 public void Remove (MenuItem item)
145 int i = IndexOf (item);
153 public void RemoveAt (int index)
155 MenuItem item = (MenuItem) items [index];
156 items.RemoveAt (index);
163 get { return items.Count; }
166 public bool IsSynchronized {
167 get { return false; }
170 public object SyncRoot {
171 get { return items; }
174 void System.Collections.ICollection.CopyTo (Array array, int index)
176 items.CopyTo (array, index);
179 void IStateManager.LoadViewState (object state)
181 if (state == null) return;
182 object[] its = (object[]) state;
184 dirty = (bool)its [0];
189 for (int n = 1; n < its.Length; n++) {
190 MenuItem item = new MenuItem ();
194 ((IStateManager) item).LoadViewState (ns);
199 object IStateManager.SaveViewState ()
201 object[] state = null;
202 bool hasData = false;
205 state = new object [items.Count + 1];
207 for (int n=0; n<items.Count; n++) {
208 MenuItem item = items[n] as MenuItem;
209 object ns = ((IStateManager)item).SaveViewState ();
216 ArrayList list = new ArrayList ();
217 for (int n=0; n<items.Count; n++) {
218 MenuItem item = items[n] as MenuItem;
219 object ns = ((IStateManager)item).SaveViewState ();
222 list.Add (new Pair (n, ns));
226 list.Insert (0, false);
227 state = list.ToArray ();
237 void IStateManager.TrackViewState ()
240 for (int n=0; n<items.Count; n++)
241 ((IStateManager) items [n]).TrackViewState ();
244 bool IStateManager.IsTrackingViewState {
245 get { return marked; }