| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- //
- // System.Web.UI.StateManagedCollection
- //
- // Authors:
- // Ben Maurer ([email protected])
- //
- // (C) 2003 Ben Maurer
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System.Collections;
- using System.Collections.Specialized;
- using System.Text;
- namespace System.Web.UI {
- public abstract class StateManagedCollection : IList, IStateManager {
-
- protected abstract object CreateKnownType (int index);
- protected abstract void SetDirtyObject (object o);
- protected virtual Type [] GetKnownTypes ()
- {
- return null;
- }
-
- #region OnXXX
- protected virtual void OnClear ()
- {
- }
-
- protected virtual void OnClearComplete ()
- {
- }
-
- protected virtual void OnInsert (int index, object value)
- {
- }
-
- protected virtual void OnInsertComplete (int index, object value)
- {
- }
-
- protected virtual void OnRemove (int index, object value)
- {
- }
-
- protected virtual void OnRemoveComplete (int index, object value)
- {
- }
-
- protected virtual void OnValidate (object value)
- {
- if (value == null)
- throw new ArgumentNullException ("value");
- }
- #endregion
-
- #region IStateManager
- void IStateManager.LoadViewState (object savedState)
- {
- if (savedState == null) return;
- int pos = -1;
- foreach (Pair p in (ArrayList)savedState) {
- pos ++;
-
- if (p == null)
- continue;
- IStateManager itm;
-
- if (p.Second is Type)
- itm = (IStateManager) Activator.CreateInstance ((Type) p.Second);
- else
- itm = (IStateManager) CreateKnownType ((int) p.Second);
-
- if (isTrackingViewState)
- itm.TrackViewState ();
- itm.LoadViewState (p.First);
-
- if (pos >= Count)
- items.Add (itm);
- else
- items [pos] = itm;
-
- }
- }
-
- object IStateManager.SaveViewState ()
- {
- ArrayList saved = new ArrayList ();
- Type [] knownTypes = GetKnownTypes ();
- bool allNull = true;
-
- foreach (IStateManager itm in items) {
- object state = itm.SaveViewState ();
- if (state == null && !saveEverything) {
- saved.Add (null);
- continue;
- }
-
- Pair p = new Pair ();
- p.First = state;
-
- Type t = itm.GetType ();
- int idx = -1;
- if (knownTypes != null)
- idx = Array.IndexOf (knownTypes, t);
-
- if (idx != -1)
- p.Second = idx;
- else
- p.Second = t;
-
- saved.Add (p);
- allNull = false;
- }
-
- if (allNull) return null;
- else return saved;
- }
-
- void IStateManager.TrackViewState ()
- {
- isTrackingViewState = true;
-
- foreach (IStateManager i in items)
- i.TrackViewState ();
- }
-
- bool isTrackingViewState;
- bool IStateManager.IsTrackingViewState {
- get { return isTrackingViewState; }
- }
- #endregion
-
- #region ICollection, IList, IEnumerable
-
- public void Clear ()
- {
- this.OnClear ();
- items.Clear ();
- this.OnClearComplete ();
-
- SetSaveEverything ();
- }
-
- public int IndexOf (object o)
- {
- if (o == null)
- return -1;
- return items.IndexOf (o);
- }
-
- public bool Contains (object o)
- {
- return o != null && items.Contains (o);
- }
-
- public IEnumerator GetEnumerator ()
- {
- return items.GetEnumerator ();
- }
-
- void System.Collections.ICollection.CopyTo (Array array, int index)
- {
- items.CopyTo (array, index);
- }
-
- IEnumerator IEnumerable.GetEnumerator ()
- {
- return GetEnumerator ();
- }
-
- int IList.Add (object value)
- {
- OnValidate(value);
- if (isTrackingViewState) {
- ((IStateManager) value).TrackViewState ();
- SetDirtyObject (value);
- }
-
- OnInsert (-1, value);
- items.Add (value);
- OnInsertComplete (-1, value);
-
- return Count - 1;
- }
-
- void IList.Insert (int index, object value)
- {
- OnValidate(value);
- if (isTrackingViewState) {
- ((IStateManager) value).TrackViewState ();
- SetDirtyObject (value);
- }
-
- OnInsert (index, value);
- items.Insert (index, value);
- OnInsertComplete(index, value);
-
- SetSaveEverything ();
- }
-
- void IList.Remove (object value)
- {
- if (value == null)
- return;
- OnValidate (value);
- ((IList)this).RemoveAt (IndexOf (value));
- }
- void IList.RemoveAt (int index)
- {
- object o = items [index];
-
- OnRemove (index, o);
- items.RemoveAt (index);
- OnRemoveComplete(index, o);
-
- SetSaveEverything ();
- }
-
- void IList.Clear ()
- {
- this.Clear ();
- }
-
- bool IList.Contains (object value)
- {
- if (value == null)
- return false;
-
- OnValidate (value);
- return Contains (value);
- }
-
- int IList.IndexOf (object value)
- {
- if (value == null)
- return -1;
-
- OnValidate (value);
- return IndexOf (value);
- }
- public int Count {
- get { return items.Count; }
- }
-
- int ICollection.Count {
- get { return items.Count; }
- }
-
- bool ICollection.IsSynchronized {
- get { return false; }
- }
-
- object ICollection.SyncRoot {
- get { return this; }
- }
-
- bool IList.IsFixedSize {
- get { return false; }
- }
-
- bool IList.IsReadOnly {
- get { return false; }
- }
-
- object IList.this [int index] {
- get { return items [index]; }
- set {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException ("index");
-
- OnValidate (value);
- if (isTrackingViewState) {
- ((IStateManager) value).TrackViewState ();
- SetDirtyObject (value);
- }
-
- items [index] = value;
- }
- }
- #endregion
- ArrayList items = new ArrayList ();
-
- bool saveEverything = false;
- void SetSaveEverything ()
- {
- if (isTrackingViewState)
- saveEverything = true;
- }
- }
- }
- #endif
|