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