undo
[mono.git] / mcs / class / System.Web / System.Web.UI / StateManagedCollection.cs
index ac3e868c837d7ce1325690aa20edc72d90176aa1..8d2f6249c9318dea98b1ecddeb1e6c1105745291 100644 (file)
@@ -3,10 +3,11 @@
 //
 // 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 {
-               
-               protected abstract object CreateKnownType (int index);
+
+       public abstract class StateManagedCollection : IList, IStateManager
+       {               
+               ArrayList items = new ArrayList ();
+               bool saveEverything = false;
+
+               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;
@@ -78,69 +93,129 @@ namespace System.Web.UI {
                #region IStateManager
                void IStateManager.LoadViewState (object savedState)
                {
-                       int pos = -1;
-                       foreach (Pair p in (ArrayList)savedState) {
-                               pos ++;
-                               
-                               if (p == null)
+                       if (savedState == 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;
+                       
+                       saveEverything = indices == null;
+                       if (saveEverything) {
+                               items.Clear ();
+
+                               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
+                                               continue;
+
+                                       item.TrackViewState ();
+                                       item.LoadViewState (states [i]);
+                                       list.Add (item);
+                               }
+                               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;
-                               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);
+                               t = types [i];
+
+                               if (t is Type)
+                                       item = (IStateManager) Activator.CreateInstance ((Type) t);
+                               else if (t is int)
+                                       item = (IStateManager) CreateKnownType ((int) t);
                                else
-                                       items [pos] = itm;
-                               
+                                       continue;
+
+                               item.TrackViewState ();
+                               item.LoadViewState (states [i]);
+                               list.Add (item);
                        }
                }
                
-               object IStateManager.SaveViewState ()
+               void AddListItem <T> (ref List <T> list, T item)
                {
-                       ArrayList saved = new ArrayList ();
-                       Type [] knownTypes = GetKnownTypes ();
+                       if (list == null)
+                               list = new List <T> ();
+
+                       list.Add (item);
+               }
                        
-                       foreach (IStateManager itm in items) {
-                               object state = itm.SaveViewState ();
-                               if (state == null && !saveEverything) {
-                                       saved.Add (null);
+               object IStateManager.SaveViewState ()
+               {
+                       Type[] knownTypes = GetKnownTypes ();
+                       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
+                                               AddListItem <object> (ref types, idx);
                                }
-                               
-                               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);
                        }
-                       
-                       return saved;
-               }
+
+                       if (!haveData)
+                               return null;
+
+                       return new Triplet (indices, states, types);
+               }               
                
                void IStateManager.TrackViewState ()
                {
                        isTrackingViewState = true;
-                       
-                       foreach (IStateManager i in items)
-                               i.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;
@@ -157,19 +232,8 @@ namespace System.Web.UI {
                        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);
+                       if (isTrackingViewState)
+                               SetDirty ();
                }
                
                public IEnumerator GetEnumerator ()
@@ -177,7 +241,7 @@ namespace System.Web.UI {
                        return items.GetEnumerator ();
                }
                
-               void System.Collections.ICollection.CopyTo (Array array, int index)
+               public void CopyTo (Array array, int index)
                {
                        items.CopyTo (array, index);
                }
@@ -207,14 +271,12 @@ namespace System.Web.UI {
                        OnValidate(value);
                        if (isTrackingViewState) {
                                ((IStateManager) value).TrackViewState ();
-                               SetDirtyObject (value);
+                               SetDirty ();
                        }
                        
                        OnInsert (index, value);
                        items.Insert (index, value);
                        OnInsertComplete(index, value);
-                       
-                       SetSaveEverything ();
                }
                
                void IList.Remove (object value)
@@ -222,8 +284,12 @@ namespace System.Web.UI {
                        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];
@@ -232,7 +298,8 @@ namespace System.Web.UI {
                        items.RemoveAt (index);
                        OnRemoveComplete(index, o);
                        
-                       SetSaveEverything ();
+                       if (isTrackingViewState)
+                               SetDirty ();
                }
                        
                void IList.Clear ()
@@ -246,7 +313,7 @@ namespace System.Web.UI {
                                return false;
                        
                        OnValidate (value);
-                       return Contains (value);
+                       return items.Contains (value);
                }
                
                int IList.IndexOf (object value)
@@ -255,7 +322,7 @@ namespace System.Web.UI {
                                return -1;
                        
                        OnValidate (value);
-                       return IndexOf (value);
+                       return items.IndexOf (value);
                }
 
                public int Count {
@@ -291,23 +358,13 @@ namespace System.Web.UI {
                                OnValidate (value);
                                if (isTrackingViewState) {
                                        ((IStateManager) value).TrackViewState ();
-                                       SetDirtyObject (value);
+                                       SetDirty ();
                                }
                                
                                items [index] = value;
                        }
                }
                #endregion
-
-               ArrayList items = new ArrayList ();
-                               
-               bool saveEverything = false;
-               void SetSaveEverything ()
-               {
-                       if (isTrackingViewState)
-                               saveEverything = true;
-               }
        }
 }
-#endif