style and ^Ms in last patch from Alon
[mono.git] / mcs / class / System.Web / System.Web.UI / StateManagedCollection.cs
1 //
2 // System.Web.UI.StateManagedCollection
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
9
10 #if NET_2_0
11 using System.Collections;
12 using System.Collections.Specialized;
13 using System.Text;
14
15 namespace System.Web.UI {
16         public abstract class StateManagedCollection : IList, IStateManager {
17                 
18                 protected abstract object CreateKnownType (int index);
19                 protected abstract void SetDirtyObject (object o);
20                 protected virtual Type [] GetKnownTypes ()
21                 {
22                         return null;
23                 }
24                 
25                 #region OnXXX
26                 protected virtual void OnClear ()
27                 {
28                 }
29                 
30                 protected virtual void OnClearComplete ()
31                 {
32                 }
33                 
34                 protected virtual void OnInsert (int index, object value)
35                 {
36                 }
37                 
38                 protected virtual void OnInsertComplete (int index, object value)
39                 {
40                 }
41                 
42                 protected virtual void OnRemove (int index, object value)
43                 {
44                 }
45                 
46                 protected virtual void OnRemoveComplete (int index, object value)
47                 {
48                 }
49                 
50                 protected virtual void OnValidate (object value)
51                 {
52                         if (value == null)
53                                 throw new ArgumentNullException ("value");
54                 }
55                 #endregion
56                 
57                 #region IStateManager
58                 void IStateManager.LoadViewState (object savedState)
59                 {
60                         int pos = -1;
61                         foreach (Pair p in (ArrayList)savedState) {
62                                 pos ++;
63                                 
64                                 if (p == null)
65                                         continue;
66                                 IStateManager itm;
67                                 
68                                 if (p.Second is Type)
69                                         itm = (IStateManager) Activator.CreateInstance ((Type) p.Second);
70                                 else
71                                         itm = (IStateManager) CreateKnownType ((int) p.Second);
72                                 
73                                 itm.LoadViewState (p.First);
74                                 
75                                 if (pos >= Count)
76                                         items.Add (itm);
77                                 else
78                                         items [pos] = itm;
79                                 
80                         }
81                 }
82                 
83                 object IStateManager.SaveViewState ()
84                 {
85                         ArrayList saved = new ArrayList ();
86                         Type [] knownTypes = GetKnownTypes ();
87                         
88                         foreach (IStateManager itm in items) {
89                                 object state = itm.SaveViewState ();
90                                 if (state == null && !saveEverything) {
91                                         saved.Add (null);
92                                         continue;
93                                 }
94                                 
95                                 Pair p = new Pair ();
96                                 p.First = state;
97                                 
98                                 Type t = itm.GetType ();
99                                 int idx = -1;
100                                 if (knownTypes != null)
101                                         idx = Array.IndexOf (knownTypes, t);
102                                 
103                                 if (idx != -1)
104                                         p.Second = idx;
105                                 else
106                                         p.Second = t;
107                                 
108                                 saved.Add (p);
109                         }
110                         
111                         return saved;
112                 }
113                 
114                 void IStateManager.TrackViewState ()
115                 {
116                         isTrackingViewState = true;
117                         
118                         foreach (IStateManager i in items)
119                                 i.TrackViewState ();
120                 }
121                 
122                 bool isTrackingViewState;
123                 bool IStateManager.IsTrackingViewState {
124                         get { return isTrackingViewState; }
125                 }
126                 #endregion
127                 
128                 #region ICollection, IList, IEnumerable
129                 
130                 public void Clear ()
131                 {
132                         this.OnClear ();
133                         items.Clear ();
134                         this.OnClearComplete ();
135                         
136                         SetSaveEverything ();
137                 }
138                 
139                 public int IndexOf (object o)
140                 {
141                         if (o == null)
142                                 return -1;
143                         return items.IndexOf (o);
144                 }
145                 
146                 public bool Contains (object o)
147                 {
148                         return o != null && items.Contains (o);
149                 }
150                 
151                 public IEnumerator GetEnumerator ()
152                 {
153                         return items.GetEnumerator ();
154                 }
155                 
156                 void System.Collections.ICollection.CopyTo (Array array, int index)
157                 {
158                         items.CopyTo (array, index);
159                 }
160                 
161                 IEnumerator IEnumerable.GetEnumerator ()
162                 {
163                         return GetEnumerator ();
164                 }
165                 
166                 int IList.Add (object value)
167                 {
168                         OnValidate(value);
169                         if (isTrackingViewState) {
170                                 ((IStateManager) value).TrackViewState ();
171                                 SetDirtyObject (value);
172                         }
173                         
174                         OnInsert (-1, value);
175                         items.Add (value);
176                         OnInsertComplete (-1, value);
177                         
178                         return Count - 1;
179                 }
180                 
181                 void IList.Insert (int index, object value)
182                 {
183                         OnValidate(value);
184                         if (isTrackingViewState) {
185                                 ((IStateManager) value).TrackViewState ();
186                                 SetDirtyObject (value);
187                         }
188                         
189                         OnInsert (index, value);
190                         items.Insert (index, value);
191                         OnInsertComplete(index, value);
192                         
193                         SetSaveEverything ();
194                 }
195                 
196                 void IList.Remove (object value)
197                 {
198                         if (value == null)
199                                 return;
200                         OnValidate (value);
201                         ((IList)this).RemoveAt (IndexOf (value));
202                 }
203                 void IList.RemoveAt (int index)
204                 {
205                         object o = items [index];
206                         
207                         OnRemove (index, o);
208                         items.RemoveAt (index);
209                         OnRemoveComplete(index, o);
210                         
211                         SetSaveEverything ();
212                 }
213                         
214                 void IList.Clear ()
215                 {
216                         this.Clear ();
217                 }
218                 
219                 bool IList.Contains (object value)
220                 {
221                         if (value == null)
222                                 return false;
223                         
224                         OnValidate (value);
225                         return Contains (value);
226                 }
227                 
228                 int IList.IndexOf (object value)
229                 {
230                         if (value == null)
231                                 return -1;
232                         
233                         OnValidate (value);
234                         return IndexOf (value);
235                 }
236
237                 public int Count {
238                         get { return items.Count; }
239                 }
240                 
241                 int ICollection.Count {
242                         get { return items.Count; }
243                 }
244                 
245                 bool ICollection.IsSynchronized {
246                         get { return false; }
247                 }
248                 
249                 object ICollection.SyncRoot {
250                         get { return this; }
251                 }
252                 
253                 bool IList.IsFixedSize {
254                         get { return false; }
255                 }
256                 
257                 bool IList.IsReadOnly {
258                         get { return false; }
259                 }
260                 
261                 object IList.this [int index] {
262                         get { return items [index]; }
263                         set {
264                                 if (index < 0 || index >= Count)
265                                         throw new ArgumentOutOfRangeException ("index");
266                                 
267                                 OnValidate (value);
268                                 if (isTrackingViewState) {
269                                         ((IStateManager) value).TrackViewState ();
270                                         SetDirtyObject (value);
271                                 }
272                                 
273                                 items [index] = value;
274                         }
275                 }
276                 #endregion
277
278                 ArrayList items = new ArrayList ();
279                                 
280                 bool saveEverything = false;
281                 void SetSaveEverything ()
282                 {
283                         if (isTrackingViewState)
284                                 saveEverything = true;
285                 }
286         }
287 }
288 #endif
289