2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ListItemCollection.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    (pbartok@novell.com)
24 //
25 //
26
27 using System.Collections;
28 using System.ComponentModel;
29 using System.Globalization;
30 using System.Reflection;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.WebControls {
34
35         // CAS - no inheritance demand required because the class is sealed
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         // attributes
38         [Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
39         public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager {
40                 #region Fields
41                 private ArrayList       items;
42                 private bool            tracking;
43                 private bool            dirty;
44                 int lastDirty = 0;
45                 #endregion      // Fields
46
47                 #region Public Constructors
48                 public ListItemCollection() {
49                         items = new ArrayList();
50                 }
51                 #endregion      // Public Constructors
52
53                 #region Public Instance Properties
54                 public int Capacity {
55                         get {
56                                 return items.Capacity;
57                         }
58
59                         set {
60                                 items.Capacity = value;
61                         }
62                 }
63
64                 public int Count {
65                         get {
66                                 return items.Count;
67                         }
68                 }
69
70                 public bool IsReadOnly {
71                         get {
72                                 return items.IsReadOnly;
73                         }
74                 }
75
76                 public bool IsSynchronized {
77                         get {
78                                 return items.IsSynchronized;
79                         }
80                 }
81
82                 public object SyncRoot {
83                         get {
84                                 return items.SyncRoot;
85                         }
86                 }
87
88                 public ListItem this[int index] {
89                         get {
90                                 return (ListItem)items[index];
91                         }
92                 }
93                 #endregion      // Public Instance Properties
94
95                 #region Public Instance Methods
96                 public void Add(ListItem item) {
97                         items.Add(item);
98                         if (tracking) {
99                                 item.TrackViewState ();
100                                 SetDirty ();
101                         }
102                 }
103
104                 public void Add(string item) {
105                         ListItem listItem = new ListItem (item);
106                         items.Add (listItem);
107
108                         if (tracking) {
109                                 listItem.TrackViewState ();
110                                 SetDirty ();
111                         }
112                 }
113
114                 public void AddRange(ListItem[] items) {
115                         for (int i = 0; i < items.Length; i++) {
116                                 Add(items[i]);
117
118                                 if (tracking) {
119                                         items [i].TrackViewState ();
120                                         SetDirty ();
121                                 }
122                         }
123                 }
124
125                 public void Clear() {
126                         items.Clear();
127
128                         if (tracking)
129                                 SetDirty ();
130                 }
131
132                 public bool Contains(ListItem item) {
133                         return items.Contains(item);
134                 }
135
136                 public void CopyTo(Array array, int index) {
137                         items.CopyTo(array, index);
138                 }
139
140                 public ListItem FindByText (string text)
141                 {
142                         for (int i = 0; i < items.Count; i++)
143                                 if (text == this [i].Text)
144                                         return this [i];
145                         
146                         return null;
147                 }
148
149                 public ListItem FindByValue (string value)
150                 {
151                         for (int i = 0; i < items.Count; i++)
152                                 if (value == this [i].Value)
153                                         return this [i];
154                         
155                         return null;
156                 }
157
158                 public IEnumerator GetEnumerator() {
159                         return items.GetEnumerator();
160                 }
161
162                 public int IndexOf(ListItem item) {
163                         return items.IndexOf(item);
164                 }
165
166                 internal int IndexOf(string value) {
167                         for (int i = 0; i < items.Count; i++)
168                                 if (value == this [i].Value)
169                                         return i;
170                         return -1;
171                 }
172
173                 public void Insert(int index, ListItem item) {
174                         items.Insert(index, item);
175
176                         if (tracking) {
177                                 item.TrackViewState ();
178                                 lastDirty = index;
179                                 SetDirty ();
180                         }
181                 }
182
183                 public void Insert(int index, string item) {
184                         ListItem listItem = new ListItem(item);
185                         items.Insert (index, listItem);
186
187                         if (tracking) {
188                                 listItem.TrackViewState ();
189                                 lastDirty = index;
190                                 SetDirty ();
191                         }
192                 }
193
194                 public void Remove(ListItem item) {
195                         items.Remove(item);
196                         
197                         if (tracking)
198                                 SetDirty ();
199                 }
200
201                 public void Remove (string item)
202                 {
203                         for (int i = 0; i < items.Count; i++)
204                                 if (item == this [i].Value) {
205                                         items.RemoveAt (i);
206
207                                         if (tracking)
208                                                 SetDirty ();
209                                 }
210                 }
211
212                 public void RemoveAt(int index) {
213                         items.RemoveAt(index);
214
215                         if (tracking)
216                                 SetDirty ();
217                 }
218                 #endregion      // Public Instance Methods
219
220                 #region Interface methods
221                 bool IList.IsFixedSize {
222                         get {
223                                 return items.IsFixedSize;
224                         }
225                 }
226
227                 object IList.this[int index] {
228                         get {
229                                 return this[index];
230                         }
231
232                         set {
233                                 if ((index >= 0) && (index < items.Count)) {
234                                         items[index] = (ListItem)value;
235
236                                         if (tracking)
237                                                 ((ListItem) value).TrackViewState ();
238                                 }
239                         }
240                 }
241
242                 int IList.Add(object value) {
243                         int i = items.Add ((ListItem) value);
244
245                         if (tracking) {
246                                 ((IStateManager) value).TrackViewState ();
247                                 SetDirty ();
248                         }
249                         return i;
250                 }
251
252                 bool IList.Contains(object value) {
253                         return Contains((ListItem)value);
254                 }
255
256                 int IList.IndexOf(object value) {
257                         return IndexOf((ListItem)value);
258                 }
259
260                 void IList.Insert(int index, object value) {
261                         Insert(index, (ListItem)value);
262                 }
263
264                 void IList.Remove(object value) {
265                         Remove((ListItem)value);
266                 }
267
268                 bool IStateManager.IsTrackingViewState {
269                         get {
270                                 return tracking;
271                         }
272                 }
273
274                 void IStateManager.LoadViewState (object savedState) {
275                         Pair pair = savedState as Pair;
276                         if (pair == null)
277                                 return;
278
279                         bool newCollection = (bool) pair.First;
280                         object [] itemsArray = (object []) pair.Second;
281                         int count = itemsArray.Length;
282
283                         if (newCollection)
284                                 items = new ArrayList(count);
285
286                         for (int i = 0; i < count; i++) {
287                                 ListItem item = new ListItem ();
288                                 
289                                 if (newCollection) {
290                                         item.LoadViewState (itemsArray [i]);
291                                         Add (item);
292                                 }
293                                 else{
294                                         if (itemsArray [i] != null){
295                                                 item.LoadViewState (itemsArray [i]);
296                                                 item.SetDirty ();
297                                                 items [i] = item;
298                                         }
299                                 }
300                         }
301                 }
302
303                 object IStateManager.SaveViewState() {
304                         int count;
305                         bool itemsDirty = false;
306
307                         count = items.Count;
308                         if (count == 0)
309                                 return null;
310
311                         object [] itemsState = new object [count];
312                         for (int i = 0; i < count; i++) {
313                                 itemsState [i] = ((IStateManager) items [i]).SaveViewState ();
314                                 if (itemsState [i] != null)
315                                         itemsDirty = true;
316                         }
317
318                         if (!dirty && !itemsDirty)
319                                 return null;
320
321                         return new Pair (dirty, itemsState);
322                 }
323
324                 void IStateManager.TrackViewState() {
325                         tracking = true;
326
327                         for (int i = 0; i < items.Count; i++) {
328                                 ((ListItem)items[i]).TrackViewState();
329                         }
330                 }
331                 #endregion      // Interface methods
332
333                 private void SetDirty ()
334                 {
335                         dirty = true;
336                         for (int i = lastDirty; i < items.Count; i++)
337                                 ((ListItem) items [i]).SetDirty ();
338
339                         lastDirty = items.Count;
340                 }
341         }
342 }