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)
82 foreach (Pair p in (ArrayList)savedState) {
90 itm = (IStateManager) Activator.CreateInstance ((Type) p.Second);
92 itm = (IStateManager) CreateKnownType ((int) p.Second);
94 itm.LoadViewState (p.First);
104 object IStateManager.SaveViewState ()
106 ArrayList saved = new ArrayList ();
107 Type [] knownTypes = GetKnownTypes ();
109 foreach (IStateManager itm in items) {
110 object state = itm.SaveViewState ();
111 if (state == null && !saveEverything) {
116 Pair p = new Pair ();
119 Type t = itm.GetType ();
121 if (knownTypes != null)
122 idx = Array.IndexOf (knownTypes, t);
135 void IStateManager.TrackViewState ()
137 isTrackingViewState = true;
139 foreach (IStateManager i in items)
143 bool isTrackingViewState;
144 bool IStateManager.IsTrackingViewState {
145 get { return isTrackingViewState; }
149 #region ICollection, IList, IEnumerable
155 this.OnClearComplete ();
157 SetSaveEverything ();
160 public int IndexOf (object o)
164 return items.IndexOf (o);
167 public bool Contains (object o)
169 return o != null && items.Contains (o);
172 public IEnumerator GetEnumerator ()
174 return items.GetEnumerator ();
177 void System.Collections.ICollection.CopyTo (Array array, int index)
179 items.CopyTo (array, index);
182 IEnumerator IEnumerable.GetEnumerator ()
184 return GetEnumerator ();
187 int IList.Add (object value)
190 if (isTrackingViewState) {
191 ((IStateManager) value).TrackViewState ();
192 SetDirtyObject (value);
195 OnInsert (-1, value);
197 OnInsertComplete (-1, value);
202 void IList.Insert (int index, object value)
205 if (isTrackingViewState) {
206 ((IStateManager) value).TrackViewState ();
207 SetDirtyObject (value);
210 OnInsert (index, value);
211 items.Insert (index, value);
212 OnInsertComplete(index, value);
214 SetSaveEverything ();
217 void IList.Remove (object value)
222 ((IList)this).RemoveAt (IndexOf (value));
224 void IList.RemoveAt (int index)
226 object o = items [index];
229 items.RemoveAt (index);
230 OnRemoveComplete(index, o);
232 SetSaveEverything ();
240 bool IList.Contains (object value)
246 return Contains (value);
249 int IList.IndexOf (object value)
255 return IndexOf (value);
259 get { return items.Count; }
262 int ICollection.Count {
263 get { return items.Count; }
266 bool ICollection.IsSynchronized {
267 get { return false; }
270 object ICollection.SyncRoot {
274 bool IList.IsFixedSize {
275 get { return false; }
278 bool IList.IsReadOnly {
279 get { return false; }
282 object IList.this [int index] {
283 get { return items [index]; }
285 if (index < 0 || index >= Count)
286 throw new ArgumentOutOfRangeException ("index");
289 if (isTrackingViewState) {
290 ((IStateManager) value).TrackViewState ();
291 SetDirtyObject (value);
294 items [index] = value;
299 ArrayList items = new ArrayList ();
301 bool saveEverything = false;
302 void SetSaveEverything ()
304 if (isTrackingViewState)
305 saveEverything = true;