2 // System.Web.UI.StateManagedCollection
5 // Ben Maurer (bmaurer@users.sourceforge.net)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Collections.Specialized;
36 namespace System.Web.UI {
37 public abstract class StateManagedCollection : IList, IStateManager {
39 protected abstract object CreateKnownType (int index);
40 protected abstract void SetDirtyObject (object o);
41 protected virtual Type [] GetKnownTypes ()
47 protected virtual void OnClear ()
51 protected virtual void OnClearComplete ()
55 protected virtual void OnInsert (int index, object value)
59 protected virtual void OnInsertComplete (int index, object value)
63 protected virtual void OnRemove (int index, object value)
67 protected virtual void OnRemoveComplete (int index, object value)
71 protected virtual void OnValidate (object value)
74 throw new ArgumentNullException ("value");
79 void IStateManager.LoadViewState (object savedState)
81 if (savedState == null) return;
84 foreach (Pair p in (ArrayList)savedState) {
92 itm = (IStateManager) Activator.CreateInstance ((Type) p.Second);
94 itm = (IStateManager) CreateKnownType ((int) p.Second);
96 if (isTrackingViewState)
97 itm.TrackViewState ();
99 itm.LoadViewState (p.First);
109 object IStateManager.SaveViewState ()
111 ArrayList saved = new ArrayList ();
112 Type [] knownTypes = GetKnownTypes ();
115 foreach (IStateManager itm in items) {
116 object state = itm.SaveViewState ();
117 if (state == null && !saveEverything) {
122 Pair p = new Pair ();
125 Type t = itm.GetType ();
127 if (knownTypes != null)
128 idx = Array.IndexOf (knownTypes, t);
139 if (allNull) return null;
143 void IStateManager.TrackViewState ()
145 isTrackingViewState = true;
147 foreach (IStateManager i in items)
151 bool isTrackingViewState;
152 bool IStateManager.IsTrackingViewState {
153 get { return isTrackingViewState; }
157 #region ICollection, IList, IEnumerable
163 this.OnClearComplete ();
165 SetSaveEverything ();
168 public int IndexOf (object o)
172 return items.IndexOf (o);
175 public bool Contains (object o)
177 return o != null && items.Contains (o);
180 public IEnumerator GetEnumerator ()
182 return items.GetEnumerator ();
185 void System.Collections.ICollection.CopyTo (Array array, int index)
187 items.CopyTo (array, index);
190 IEnumerator IEnumerable.GetEnumerator ()
192 return GetEnumerator ();
195 int IList.Add (object value)
198 if (isTrackingViewState) {
199 ((IStateManager) value).TrackViewState ();
200 SetDirtyObject (value);
203 OnInsert (-1, value);
205 OnInsertComplete (-1, value);
210 void IList.Insert (int index, object value)
213 if (isTrackingViewState) {
214 ((IStateManager) value).TrackViewState ();
215 SetDirtyObject (value);
218 OnInsert (index, value);
219 items.Insert (index, value);
220 OnInsertComplete(index, value);
222 SetSaveEverything ();
225 void IList.Remove (object value)
230 ((IList)this).RemoveAt (IndexOf (value));
232 void IList.RemoveAt (int index)
234 object o = items [index];
237 items.RemoveAt (index);
238 OnRemoveComplete(index, o);
240 SetSaveEverything ();
248 bool IList.Contains (object value)
254 return Contains (value);
257 int IList.IndexOf (object value)
263 return IndexOf (value);
267 get { return items.Count; }
270 int ICollection.Count {
271 get { return items.Count; }
274 bool ICollection.IsSynchronized {
275 get { return false; }
278 object ICollection.SyncRoot {
282 bool IList.IsFixedSize {
283 get { return false; }
286 bool IList.IsReadOnly {
287 get { return false; }
290 object IList.this [int index] {
291 get { return items [index]; }
293 if (index < 0 || index >= Count)
294 throw new ArgumentOutOfRangeException ("index");
297 if (isTrackingViewState) {
298 ((IStateManager) value).TrackViewState ();
299 SetDirtyObject (value);
302 items [index] = value;
307 ArrayList items = new ArrayList ();
309 bool saveEverything = false;
310 void SetSaveEverything ()
312 if (isTrackingViewState)
313 saveEverything = true;