(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Web / System.Web.UI / StateManagedCollection.cs
blobfc520176ee898e2bdb683200885eb3647e89a9fd
1 //
2 // System.Web.UI.StateManagedCollection
3 //
4 // Authors:
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
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.
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Text;
36 namespace System.Web.UI {
37 public abstract class StateManagedCollection : IList, IStateManager {
39 protected abstract object CreateKnownType (int index);
40 protected abstract void SetDirtyObject (object o);
41 protected virtual Type [] GetKnownTypes ()
43 return null;
46 #region OnXXX
47 protected virtual void OnClear ()
51 protected virtual void OnClearComplete ()
55 protected virtual void OnInsert (int index, object value)
59 protected virtual void OnInsertComplete (int index, object value)
63 protected virtual void OnRemove (int index, object value)
67 protected virtual void OnRemoveComplete (int index, object value)
71 protected virtual void OnValidate (object value)
73 if (value == null)
74 throw new ArgumentNullException ("value");
76 #endregion
78 #region IStateManager
79 void IStateManager.LoadViewState (object savedState)
81 int pos = -1;
82 foreach (Pair p in (ArrayList)savedState) {
83 pos ++;
85 if (p == null)
86 continue;
87 IStateManager itm;
89 if (p.Second is Type)
90 itm = (IStateManager) Activator.CreateInstance ((Type) p.Second);
91 else
92 itm = (IStateManager) CreateKnownType ((int) p.Second);
94 itm.LoadViewState (p.First);
96 if (pos >= Count)
97 items.Add (itm);
98 else
99 items [pos] = itm;
104 object IStateManager.SaveViewState ()
106 ArrayList saved = new ArrayList ();
107 Type [] knownTypes = GetKnownTypes ();
109 foreach (IStateManager itm in items) {
110 object state = itm.SaveViewState ();
111 if (state == null && !saveEverything) {
112 saved.Add (null);
113 continue;
116 Pair p = new Pair ();
117 p.First = state;
119 Type t = itm.GetType ();
120 int idx = -1;
121 if (knownTypes != null)
122 idx = Array.IndexOf (knownTypes, t);
124 if (idx != -1)
125 p.Second = idx;
126 else
127 p.Second = t;
129 saved.Add (p);
132 return saved;
135 void IStateManager.TrackViewState ()
137 isTrackingViewState = true;
139 foreach (IStateManager i in items)
140 i.TrackViewState ();
143 bool isTrackingViewState;
144 bool IStateManager.IsTrackingViewState {
145 get { return isTrackingViewState; }
147 #endregion
149 #region ICollection, IList, IEnumerable
151 public void Clear ()
153 this.OnClear ();
154 items.Clear ();
155 this.OnClearComplete ();
157 SetSaveEverything ();
160 public int IndexOf (object o)
162 if (o == null)
163 return -1;
164 return items.IndexOf (o);
167 public bool Contains (object o)
169 return o != null && items.Contains (o);
172 public IEnumerator GetEnumerator ()
174 return items.GetEnumerator ();
177 void System.Collections.ICollection.CopyTo (Array array, int index)
179 items.CopyTo (array, index);
182 IEnumerator IEnumerable.GetEnumerator ()
184 return GetEnumerator ();
187 int IList.Add (object value)
189 OnValidate(value);
190 if (isTrackingViewState) {
191 ((IStateManager) value).TrackViewState ();
192 SetDirtyObject (value);
195 OnInsert (-1, value);
196 items.Add (value);
197 OnInsertComplete (-1, value);
199 return Count - 1;
202 void IList.Insert (int index, object value)
204 OnValidate(value);
205 if (isTrackingViewState) {
206 ((IStateManager) value).TrackViewState ();
207 SetDirtyObject (value);
210 OnInsert (index, value);
211 items.Insert (index, value);
212 OnInsertComplete(index, value);
214 SetSaveEverything ();
217 void IList.Remove (object value)
219 if (value == null)
220 return;
221 OnValidate (value);
222 ((IList)this).RemoveAt (IndexOf (value));
224 void IList.RemoveAt (int index)
226 object o = items [index];
228 OnRemove (index, o);
229 items.RemoveAt (index);
230 OnRemoveComplete(index, o);
232 SetSaveEverything ();
235 void IList.Clear ()
237 this.Clear ();
240 bool IList.Contains (object value)
242 if (value == null)
243 return false;
245 OnValidate (value);
246 return Contains (value);
249 int IList.IndexOf (object value)
251 if (value == null)
252 return -1;
254 OnValidate (value);
255 return IndexOf (value);
258 public int Count {
259 get { return items.Count; }
262 int ICollection.Count {
263 get { return items.Count; }
266 bool ICollection.IsSynchronized {
267 get { return false; }
270 object ICollection.SyncRoot {
271 get { return this; }
274 bool IList.IsFixedSize {
275 get { return false; }
278 bool IList.IsReadOnly {
279 get { return false; }
282 object IList.this [int index] {
283 get { return items [index]; }
284 set {
285 if (index < 0 || index >= Count)
286 throw new ArgumentOutOfRangeException ("index");
288 OnValidate (value);
289 if (isTrackingViewState) {
290 ((IStateManager) value).TrackViewState ();
291 SetDirtyObject (value);
294 items [index] = value;
297 #endregion
299 ArrayList items = new ArrayList ();
301 bool saveEverything = false;
302 void SetSaveEverything ()
304 if (isTrackingViewState)
305 saveEverything = true;
309 #endif