2 // System.Web.UI.StateManagedCollection
5 // Ben Maurer (bmaurer@users.sourceforge.net)
11 using System.Collections;
12 using System.Collections.Specialized;
15 namespace System.Web.UI {
16 public abstract class StateManagedCollection : IList, IStateManager {
18 protected abstract object CreateKnownType (int index);
19 protected abstract void SetDirtyObject (object o);
20 protected virtual Type [] GetKnownTypes ()
26 protected virtual void OnClear ()
30 protected virtual void OnClearComplete ()
34 protected virtual void OnInsert (int index, object value)
38 protected virtual void OnInsertComplete (int index, object value)
42 protected virtual void OnRemove (int index, object value)
46 protected virtual void OnRemoveComplete (int index, object value)
50 protected virtual void OnValidate (object value)
53 throw new ArgumentNullException ("value");
58 void IStateManager.LoadViewState (object savedState)
61 foreach (Pair p in (ArrayList)savedState) {
69 itm = (IStateManager) Activator.CreateInstance ((Type) p.Second);
71 itm = (IStateManager) CreateKnownType ((int) p.Second);
73 itm.LoadViewState (p.First);
83 object IStateManager.SaveViewState ()
85 ArrayList saved = new ArrayList ();
86 Type [] knownTypes = GetKnownTypes ();
88 foreach (IStateManager itm in items) {
89 object state = itm.SaveViewState ();
90 if (state == null && !saveEverything) {
98 Type t = itm.GetType ();
100 if (knownTypes != null)
101 idx = Array.IndexOf (knownTypes, t);
114 void IStateManager.TrackViewState ()
116 isTrackingViewState = true;
118 foreach (IStateManager i in items)
122 bool isTrackingViewState;
123 bool IStateManager.IsTrackingViewState {
124 get { return isTrackingViewState; }
128 #region ICollection, IList, IEnumerable
134 this.OnClearComplete ();
136 SetSaveEverything ();
139 public int IndexOf (object o)
143 return items.IndexOf (o);
146 public bool Contains (object o)
148 return o != null && items.Contains (o);
151 public IEnumerator GetEnumerator ()
153 return items.GetEnumerator ();
156 void System.Collections.ICollection.CopyTo (Array array, int index)
158 items.CopyTo (array, index);
161 IEnumerator IEnumerable.GetEnumerator ()
163 return GetEnumerator ();
166 int IList.Add (object value)
169 if (isTrackingViewState) {
170 ((IStateManager) value).TrackViewState ();
171 SetDirtyObject (value);
174 OnInsert (-1, value);
176 OnInsertComplete (-1, value);
181 void IList.Insert (int index, object value)
184 if (isTrackingViewState) {
185 ((IStateManager) value).TrackViewState ();
186 SetDirtyObject (value);
189 OnInsert (index, value);
190 items.Insert (index, value);
191 OnInsertComplete(index, value);
193 SetSaveEverything ();
196 void IList.Remove (object value)
201 ((IList)this).RemoveAt (IndexOf (value));
203 void IList.RemoveAt (int index)
205 object o = items [index];
208 items.RemoveAt (index);
209 OnRemoveComplete(index, o);
211 SetSaveEverything ();
219 bool IList.Contains (object value)
225 return Contains (value);
228 int IList.IndexOf (object value)
234 return IndexOf (value);
238 get { return items.Count; }
241 int ICollection.Count {
242 get { return items.Count; }
245 bool ICollection.IsSynchronized {
246 get { return false; }
249 object ICollection.SyncRoot {
253 bool IList.IsFixedSize {
254 get { return false; }
257 bool IList.IsReadOnly {
258 get { return false; }
261 object IList.this [int index] {
262 get { return items [index]; }
264 if (index < 0 || index >= Count)
265 throw new ArgumentOutOfRangeException ("index");
268 if (isTrackingViewState) {
269 ((IStateManager) value).TrackViewState ();
270 SetDirtyObject (value);
273 items [index] = value;
278 ArrayList items = new ArrayList ();
280 bool saveEverything = false;
281 void SetSaveEverything ()
283 if (isTrackingViewState)
284 saveEverything = true;