StateManagedCollection.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // System.Web.UI.StateManagedCollection
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Ben Maurer
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System.Collections;
  32. namespace System.Web.UI {
  33. public abstract class StateManagedCollection : IList, IStateManager {
  34. ArrayList items = new ArrayList ();
  35. bool saveEverything = false;
  36. IStateManager[] originalItems;
  37. protected virtual object CreateKnownType (int index)
  38. {
  39. return null;
  40. }
  41. public void SetDirty ()
  42. {
  43. saveEverything = true;
  44. }
  45. protected abstract void SetDirtyObject (object o);
  46. protected virtual Type [] GetKnownTypes ()
  47. {
  48. return null;
  49. }
  50. #region OnXXX
  51. protected virtual void OnClear ()
  52. {
  53. }
  54. protected virtual void OnClearComplete ()
  55. {
  56. }
  57. protected virtual void OnInsert (int index, object value)
  58. {
  59. }
  60. protected virtual void OnInsertComplete (int index, object value)
  61. {
  62. }
  63. protected virtual void OnRemove (int index, object value)
  64. {
  65. }
  66. protected virtual void OnRemoveComplete (int index, object value)
  67. {
  68. }
  69. protected virtual void OnValidate (object value)
  70. {
  71. if (value == null)
  72. throw new ArgumentNullException ("value");
  73. }
  74. #endregion
  75. #region IStateManager
  76. void IStateManager.LoadViewState (object savedState)
  77. {
  78. if (savedState == null) {
  79. foreach (IStateManager item in items)
  80. item.LoadViewState (null);
  81. return;
  82. }
  83. object[] its = (object[]) savedState;
  84. saveEverything = (bool)its [0];
  85. if (saveEverything)
  86. items.Clear ();
  87. for (int n=1; n<its.Length; n++) {
  88. int oi;
  89. object state;
  90. object type;
  91. Triplet triplet = its [n] as Triplet;
  92. if (triplet != null) {
  93. oi = (int) triplet.First;
  94. state = triplet.Second;
  95. type = triplet.Third;
  96. } else {
  97. Pair pair = (Pair) its [n];
  98. oi = (int) pair.First;
  99. state = pair.Second;
  100. type = null;
  101. }
  102. IStateManager item;
  103. if (oi != -1)
  104. item = originalItems [oi];
  105. else {
  106. if (type is Type)
  107. item = (IStateManager) Activator.CreateInstance ((Type) type);
  108. else
  109. item = (IStateManager) CreateKnownType ((int) type);
  110. }
  111. if (saveEverything) ((IList)this).Add (item);
  112. item.LoadViewState (state);
  113. }
  114. }
  115. object IStateManager.SaveViewState ()
  116. {
  117. object[] state = null;
  118. bool hasData = false;
  119. Type[] knownTypes = GetKnownTypes ();
  120. if (saveEverything) {
  121. state = new object [items.Count + 1];
  122. state [0] = true;
  123. for (int n=0; n<items.Count; n++)
  124. {
  125. IStateManager item = (IStateManager) items [n];
  126. int oi = Array.IndexOf (originalItems, item);
  127. object ns = item.SaveViewState ();
  128. if (ns != null) hasData = true;
  129. if (oi == -1) {
  130. Type t = item.GetType ();
  131. int idx = knownTypes == null ? -1 : Array.IndexOf (knownTypes, t);
  132. if (idx != -1)
  133. state [n + 1] = new Triplet (oi, ns, idx);
  134. else
  135. state [n + 1] = new Triplet (oi, ns, t);
  136. }
  137. else
  138. state [n + 1] = new Pair (oi, ns);
  139. }
  140. } else {
  141. ArrayList list = new ArrayList ();
  142. for (int n=0; n<items.Count; n++) {
  143. IStateManager item = (IStateManager) items [n];
  144. object ns = item.SaveViewState ();
  145. if (ns != null) {
  146. hasData = true;
  147. list.Add (new Pair (n, ns));
  148. }
  149. }
  150. if (hasData) {
  151. list.Insert (0, false);
  152. state = list.ToArray ();
  153. }
  154. }
  155. if (hasData)
  156. return state;
  157. else
  158. return null;
  159. }
  160. void IStateManager.TrackViewState ()
  161. {
  162. isTrackingViewState = true;
  163. originalItems = new IStateManager [items.Count];
  164. for (int n=0; n<items.Count; n++) {
  165. originalItems [n] = (IStateManager) items [n];
  166. originalItems [n].TrackViewState ();
  167. }
  168. }
  169. bool isTrackingViewState;
  170. bool IStateManager.IsTrackingViewState {
  171. get { return isTrackingViewState; }
  172. }
  173. #endregion
  174. #region ICollection, IList, IEnumerable
  175. public void Clear ()
  176. {
  177. this.OnClear ();
  178. items.Clear ();
  179. this.OnClearComplete ();
  180. if (isTrackingViewState)
  181. saveEverything = true;
  182. }
  183. public IEnumerator GetEnumerator ()
  184. {
  185. return items.GetEnumerator ();
  186. }
  187. public void CopyTo (Array array, int index)
  188. {
  189. items.CopyTo (array, index);
  190. }
  191. IEnumerator IEnumerable.GetEnumerator ()
  192. {
  193. return GetEnumerator ();
  194. }
  195. int IList.Add (object value)
  196. {
  197. OnValidate(value);
  198. if (isTrackingViewState) {
  199. ((IStateManager) value).TrackViewState ();
  200. SetDirtyObject (value);
  201. saveEverything = true;
  202. }
  203. OnInsert (-1, value);
  204. items.Add (value);
  205. OnInsertComplete (-1, value);
  206. return Count - 1;
  207. }
  208. void IList.Insert (int index, object value)
  209. {
  210. OnValidate(value);
  211. if (isTrackingViewState) {
  212. ((IStateManager) value).TrackViewState ();
  213. SetDirtyObject (value);
  214. saveEverything = true;
  215. }
  216. OnInsert (index, value);
  217. items.Insert (index, value);
  218. OnInsertComplete(index, value);
  219. }
  220. void IList.Remove (object value)
  221. {
  222. if (value == null)
  223. return;
  224. OnValidate (value);
  225. int i = items.IndexOf (value);
  226. if (i >= 0)
  227. ((IList)this).RemoveAt (i);
  228. }
  229. void IList.RemoveAt (int index)
  230. {
  231. object o = items [index];
  232. OnRemove (index, o);
  233. items.RemoveAt (index);
  234. OnRemoveComplete(index, o);
  235. if (isTrackingViewState)
  236. saveEverything = true;
  237. }
  238. void IList.Clear ()
  239. {
  240. this.Clear ();
  241. }
  242. bool IList.Contains (object value)
  243. {
  244. if (value == null)
  245. return false;
  246. OnValidate (value);
  247. return items.Contains (value);
  248. }
  249. int IList.IndexOf (object value)
  250. {
  251. if (value == null)
  252. return -1;
  253. OnValidate (value);
  254. return items.IndexOf (value);
  255. }
  256. public int Count {
  257. get { return items.Count; }
  258. }
  259. int ICollection.Count {
  260. get { return items.Count; }
  261. }
  262. bool ICollection.IsSynchronized {
  263. get { return false; }
  264. }
  265. object ICollection.SyncRoot {
  266. get { return this; }
  267. }
  268. bool IList.IsFixedSize {
  269. get { return false; }
  270. }
  271. bool IList.IsReadOnly {
  272. get { return false; }
  273. }
  274. object IList.this [int index] {
  275. get { return items [index]; }
  276. set {
  277. if (index < 0 || index >= Count)
  278. throw new ArgumentOutOfRangeException ("index");
  279. OnValidate (value);
  280. if (isTrackingViewState) {
  281. ((IStateManager) value).TrackViewState ();
  282. SetDirtyObject (value);
  283. saveEverything = true;
  284. }
  285. items [index] = value;
  286. }
  287. }
  288. #endregion
  289. }
  290. }
  291. #endif