//
// Authors:
// Ben Maurer (bmaurer@users.sourceforge.net)
+// Sebastien Pouliot <sebastien@ximian.com>
+// Marek Habersack (mhabersack@novell.com)
//
// (C) 2003 Ben Maurer
-//
-
+// Copyright (C) 2005-2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// 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;
+using System.Collections.Generic;
namespace System.Web.UI {
+
public abstract class StateManagedCollection : IList, IStateManager
- {
+ {
ArrayList items = new ArrayList ();
bool saveEverything = false;
- IStateManager[] originalItems;
-
- protected abstract object CreateKnownType (int index);
+
+ protected virtual object CreateKnownType (int index)
+ {
+ return null;
+ }
+
+ public void SetDirty ()
+ {
+ saveEverything = true;
+ for (int i = 0; i < items.Count; i++)
+ SetDirtyObject (items[i]);
+ }
+
protected abstract void SetDirtyObject (object o);
+
protected virtual Type [] GetKnownTypes ()
{
return null;
void IStateManager.LoadViewState (object savedState)
{
if (savedState == null) {
- foreach (IStateManager item in items)
- item.LoadViewState (null);
+ foreach (IStateManager i in items)
+ i.LoadViewState (null);
return;
}
+
+ Triplet state = savedState as Triplet;
+ if (state == null)
+ throw new InvalidOperationException ("Internal error.");
+
+ List <int> indices = state.First as List <int>;
+ List <object> states = state.Second as List <object>;
+ List <object> types = state.Third as List <object>;
+ IList list = this as IList;
+ IStateManager item;
+ object t;
- object[] its = (object[]) savedState;
-
- saveEverything = (bool)its [0];
-
- if (saveEverything)
+ saveEverything = indices == null;
+ if (saveEverything) {
items.Clear ();
- for (int n=1; n<its.Length; n++) {
- int oi;
- object state;
- object type;
-
- Triplet triplet = its [n] as Triplet;
- if (triplet != null) {
- oi = (int) triplet.First;
- state = triplet.Second;
- type = triplet.Third;
- } else {
- Pair pair = (Pair) its [n];
- oi = (int) pair.First;
- state = pair.Second;
- type = null;
- }
-
- IStateManager item;
- if (oi != -1)
- item = originalItems [oi];
- else {
- if (type is Type)
- item = (IStateManager) Activator.CreateInstance ((Type) type);
+ for (int i = 0; i < states.Count; i++) {
+ t = types [i];
+ if (t is Type)
+ item = (IStateManager) Activator.CreateInstance ((Type) t);
+ else if (t is int)
+ item = (IStateManager) CreateKnownType ((int) t);
else
- item = (IStateManager) CreateKnownType ((int) type);
+ continue;
+
+ item.TrackViewState ();
+ item.LoadViewState (states [i]);
+ list.Add (item);
}
-
- if (saveEverything) ((IList)this).Add (item);
-
- item.LoadViewState (state);
+ return;
+ }
+
+ int idx;
+ for (int i = 0; i < indices.Count; i++) {
+ idx = indices [i];
+
+ if (idx < Count) {
+ item = list [idx] as IStateManager;
+ item.TrackViewState ();
+ item.LoadViewState (states [i]);
+ continue;
+ }
+
+ t = types [i];
+
+ if (t is Type)
+ item = (IStateManager) Activator.CreateInstance ((Type) t);
+ else if (t is int)
+ item = (IStateManager) CreateKnownType ((int) t);
+ else
+ continue;
+
+ item.TrackViewState ();
+ item.LoadViewState (states [i]);
+ list.Add (item);
}
}
+ void AddListItem <T> (ref List <T> list, T item)
+ {
+ if (list == null)
+ list = new List <T> ();
+
+ list.Add (item);
+ }
+
object IStateManager.SaveViewState ()
{
- object[] state = null;
- bool hasData = false;
Type[] knownTypes = GetKnownTypes ();
-
- if (saveEverything) {
- state = new object [items.Count + 1];
- state [0] = true;
- for (int n=0; n<items.Count; n++)
- {
- IStateManager item = (IStateManager) items [n];
- int oi = Array.IndexOf (originalItems, item);
- object ns = item.SaveViewState ();
- if (ns != null) hasData = true;
-
- if (oi == -1) {
- Type t = item.GetType ();
- int idx = knownTypes == null ? -1 : Array.IndexOf (knownTypes, t);
- if (idx != -1)
- state [n + 1] = new Triplet (oi, ns, idx);
- else
- state [n + 1] = new Triplet (oi, ns, t);
- }
+ bool haveData = false, haveKnownTypes = knownTypes != null && knownTypes.Length > 0;
+ int count = items.Count;
+ IStateManager item;
+ object itemState;
+ Type type;
+ int idx;
+ List <int> indices = null;
+ List <object> states = null;
+ List <object> types = null;
+
+ for (int i = 0; i < count; i++) {
+ item = items [i] as IStateManager;
+ if (item == null)
+ continue;
+ item.TrackViewState ();
+ itemState = item.SaveViewState ();
+ if (saveEverything || itemState != null) {
+ haveData = true;
+ type = item.GetType ();
+ idx = haveKnownTypes ? Array.IndexOf (knownTypes, type) : -1;
+
+ if (!saveEverything)
+ AddListItem <int> (ref indices, i);
+ AddListItem <object> (ref states, itemState);
+ if (idx == -1)
+ AddListItem <object> (ref types, type);
else
- state [n + 1] = new Pair (oi, ns);
- }
- } else {
- ArrayList list = new ArrayList ();
- for (int n=0; n<items.Count; n++) {
- IStateManager item = (IStateManager) items [n];
- object ns = item.SaveViewState ();
- if (ns != null) {
- hasData = true;
- list.Add (new Pair (n, ns));
- }
- }
- if (hasData) {
- list.Insert (0, false);
- state = list.ToArray ();
+ AddListItem <object> (ref types, idx);
}
}
-
- if (hasData)
- return state;
- else
+
+ if (!haveData)
return null;
- }
+
+ return new Triplet (indices, states, types);
+ }
void IStateManager.TrackViewState ()
{
isTrackingViewState = true;
- originalItems = new IStateManager [items.Count];
- for (int n=0; n<items.Count; n++) {
- originalItems [n] = (IStateManager) items [n];
- originalItems [n].TrackViewState ();
+ if (items != null && items.Count > 0) {
+ IStateManager item;
+ foreach (object o in items) {
+ item = o as IStateManager;
+ if (item == null)
+ continue;
+ item.TrackViewState ();
+ }
}
-
}
bool isTrackingViewState;
this.OnClearComplete ();
if (isTrackingViewState)
- saveEverything = true;
- }
-
- 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);
+ SetDirty ();
}
public IEnumerator GetEnumerator ()
return items.GetEnumerator ();
}
- void System.Collections.ICollection.CopyTo (Array array, int index)
+ public void CopyTo (Array array, int index)
{
items.CopyTo (array, index);
}
if (isTrackingViewState) {
((IStateManager) value).TrackViewState ();
SetDirtyObject (value);
- saveEverything = true;
}
OnInsert (-1, value);
OnValidate(value);
if (isTrackingViewState) {
((IStateManager) value).TrackViewState ();
- SetDirtyObject (value);
- saveEverything = true;
+ SetDirty ();
}
OnInsert (index, value);
if (value == null)
return;
OnValidate (value);
- ((IList)this).RemoveAt (IndexOf (value));
+ IList list = (IList)this;
+ int i = list.IndexOf (value);
+ if (i >= 0)
+ list.RemoveAt (i);
}
+
void IList.RemoveAt (int index)
{
object o = items [index];
OnRemoveComplete(index, o);
if (isTrackingViewState)
- saveEverything = true;
+ SetDirty ();
}
void IList.Clear ()
return false;
OnValidate (value);
- return Contains (value);
+ return items.Contains (value);
}
int IList.IndexOf (object value)
return -1;
OnValidate (value);
- return IndexOf (value);
+ return items.IndexOf (value);
}
public int Count {
OnValidate (value);
if (isTrackingViewState) {
((IStateManager) value).TrackViewState ();
- SetDirtyObject (value);
- saveEverything = true;
+ SetDirty ();
}
items [index] = value;
#endregion
}
}
-#endif