2008-09-04 Jb Evain <jbevain@novell.com>
[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 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Ben Maurer
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if NET_2_0
32
33 using System.Collections;
34 using System.Collections.Generic;
35
36 namespace System.Web.UI {
37
38         public abstract class StateManagedCollection : IList, IStateManager {
39
40                 ArrayList items = new ArrayList ();
41                 bool saveEverything = false;
42
43                 protected virtual object CreateKnownType (int index)
44                 {
45                         return null;
46                 }
47
48                 public void SetDirty ()
49                 {
50                         saveEverything = true;
51                         for (int i = 0; i < items.Count; i++)
52                                 SetDirtyObject (items[i]);
53                 }
54
55                 protected abstract void SetDirtyObject (object o);
56
57                 protected virtual Type [] GetKnownTypes ()
58                 {
59                         return null;
60                 }
61                 
62                 #region OnXXX
63                 protected virtual void OnClear ()
64                 {
65                 }
66                 
67                 protected virtual void OnClearComplete ()
68                 {
69                 }
70                 
71                 protected virtual void OnInsert (int index, object value)
72                 {
73                 }
74                 
75                 protected virtual void OnInsertComplete (int index, object value)
76                 {
77                 }
78                 
79                 protected virtual void OnRemove (int index, object value)
80                 {
81                 }
82                 
83                 protected virtual void OnRemoveComplete (int index, object value)
84                 {
85                 }
86                 
87                 protected virtual void OnValidate (object value)
88                 {
89                         if (value == null)
90                                 throw new ArgumentNullException ("value");
91                 }
92                 #endregion
93                 
94                 #region IStateManager
95                 void IStateManager.LoadViewState (object savedState)
96                 {
97                         if (savedState == null) {
98                                 foreach (IStateManager i in items)
99                                         i.LoadViewState (null);
100                                 return;
101                         }
102                         
103                         Triplet[] state = savedState as Triplet[];
104                         saveEverything = (bool)(state [0].First);
105                         if (saveEverything)
106                                 items.Clear ();
107
108                         object itemState;
109                         object type;
110                         Triplet triplet;
111                         IStateManager item;
112                         
113                         for (int i = 1; i < state.Length; i++) {
114                                 triplet = state [i];
115                                 if (triplet == null)
116                                         continue;
117
118                                 itemState = triplet.Second;
119                                 type = triplet.Third;
120                                 if (type is Type)
121                                         item = (IStateManager) Activator.CreateInstance ((Type) type);
122                                 else if (type is int)
123                                         item = (IStateManager) CreateKnownType ((int) type);
124                                 else
125                                         continue;
126
127                                 if (saveEverything)
128                                         ((IList)this).Add (item);
129                                 else
130                                         item.TrackViewState ();
131                                 
132                                 item.LoadViewState (itemState);
133                         }
134                 }
135                 
136                 object IStateManager.SaveViewState ()
137                 {
138                         bool hasData = false;
139                         Type[] knownTypes = GetKnownTypes ();
140                         bool haveKnownTypes = knownTypes != null;
141                         List <Triplet> state = new List <Triplet> ();
142                         int count = items.Count;
143                         IStateManager item;
144                         object itemState;
145                         Type type;
146                         int idx;
147
148                         for (int i = 0; i < count; i++) {
149                                 item = items [i] as IStateManager;
150                                 if (item == null)
151                                         continue;
152                                 item.TrackViewState ();
153                                 itemState = item.SaveViewState ();
154                                 if (saveEverything || itemState != null) {
155                                         hasData = true;
156                                         type = item.GetType ();
157                                         idx = haveKnownTypes ? Array.IndexOf (knownTypes, type) : -1;
158                                         if (idx == -1)
159                                                 state.Add (new Triplet (i, itemState, type));
160                                         else
161                                                 state.Add (new Triplet (i, itemState, idx));
162                                 }
163                         }
164
165                         if (hasData) {
166                                 state.Insert (0, new Triplet (saveEverything, null, null));
167                                 return state.ToArray ();
168                         } else
169                                 return null;
170                 }
171                 
172                 void IStateManager.TrackViewState ()
173                 {
174                         isTrackingViewState = true;
175                         if (items != null && items.Count > 0) {
176                                 IStateManager item;
177                                 foreach (object o in items) {
178                                         item = o as IStateManager;
179                                         if (item == null)
180                                                 continue;
181                                         item.TrackViewState ();
182                                 }
183                         }
184                 }
185                 
186                 bool isTrackingViewState;
187                 bool IStateManager.IsTrackingViewState {
188                         get { return isTrackingViewState; }
189                 }
190                 #endregion
191                 
192                 #region ICollection, IList, IEnumerable
193                 
194                 public void Clear ()
195                 {
196                         this.OnClear ();
197                         items.Clear ();
198                         this.OnClearComplete ();
199                         
200                         if (isTrackingViewState)
201                                 SetDirty ();
202                 }
203                 
204                 public IEnumerator GetEnumerator ()
205                 {
206                         return items.GetEnumerator ();
207                 }
208                 
209                 public void CopyTo (Array array, int index)
210                 {
211                         items.CopyTo (array, index);
212                 }
213                 
214                 IEnumerator IEnumerable.GetEnumerator ()
215                 {
216                         return GetEnumerator ();
217                 }
218                 
219                 int IList.Add (object value)
220                 {
221                         OnValidate(value);
222                         if (isTrackingViewState) {
223                                 ((IStateManager) value).TrackViewState ();
224                                 SetDirtyObject (value);
225                                 SetDirty ();
226                         }
227                         
228                         OnInsert (-1, value);
229                         items.Add (value);
230                         OnInsertComplete (-1, value);
231                         
232                         return Count - 1;
233                 }
234                 
235                 void IList.Insert (int index, object value)
236                 {
237                         OnValidate(value);
238                         if (isTrackingViewState) {
239                                 ((IStateManager) value).TrackViewState ();
240                                 SetDirtyObject (value);
241                                 SetDirty ();
242                         }
243                         
244                         OnInsert (index, value);
245                         items.Insert (index, value);
246                         OnInsertComplete(index, value);
247                 }
248                 
249                 void IList.Remove (object value)
250                 {
251                         if (value == null)
252                                 return;
253                         OnValidate (value);
254                         int i = items.IndexOf (value);
255                         if (i >= 0)
256                                 ((IList)this).RemoveAt (i);
257                 }
258
259                 void IList.RemoveAt (int index)
260                 {
261                         object o = items [index];
262                         
263                         OnRemove (index, o);
264                         items.RemoveAt (index);
265                         OnRemoveComplete(index, o);
266                         
267                         if (isTrackingViewState)
268                                 SetDirty ();
269                 }
270                         
271                 void IList.Clear ()
272                 {
273                         this.Clear ();
274                 }
275                 
276                 bool IList.Contains (object value)
277                 {
278                         if (value == null)
279                                 return false;
280                         
281                         OnValidate (value);
282                         return items.Contains (value);
283                 }
284                 
285                 int IList.IndexOf (object value)
286                 {
287                         if (value == null)
288                                 return -1;
289                         
290                         OnValidate (value);
291                         return items.IndexOf (value);
292                 }
293
294                 public int Count {
295                         get { return items.Count; }
296                 }
297                 
298                 int ICollection.Count {
299                         get { return items.Count; }
300                 }
301                 
302                 bool ICollection.IsSynchronized {
303                         get { return false; }
304                 }
305                 
306                 object ICollection.SyncRoot {
307                         get { return this; }
308                 }
309                 
310                 bool IList.IsFixedSize {
311                         get { return false; }
312                 }
313                 
314                 bool IList.IsReadOnly {
315                         get { return false; }
316                 }
317                 
318                 object IList.this [int index] {
319                         get { return items [index]; }
320                         set {
321                                 if (index < 0 || index >= Count)
322                                         throw new ArgumentOutOfRangeException ("index");
323                                 
324                                 OnValidate (value);
325                                 if (isTrackingViewState) {
326                                         ((IStateManager) value).TrackViewState ();
327                                         SetDirtyObject (value);
328                                         SetDirty ();
329                                 }
330                                 
331                                 items [index] = value;
332                         }
333                 }
334                 #endregion
335         }
336 }
337 #endif
338