MenuItemCollection.cs 5.6 KB

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