MenuItemCollection.cs 5.4 KB

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