New test.
[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                 #endregion      // Fields
44
45                 #region Public Constructors
46                 public ListItemCollection() {
47                         items = new ArrayList();
48                 }
49                 #endregion      // Public Constructors
50
51                 #region Public Instance Properties
52                 public int Capacity {
53                         get {
54                                 return items.Capacity;
55                         }
56
57                         set {
58                                 items.Capacity = value;
59                         }
60                 }
61
62                 public int Count {
63                         get {
64                                 return items.Count;
65                         }
66                 }
67
68                 public bool IsReadOnly {
69                         get {
70                                 return items.IsReadOnly;
71                         }
72                 }
73
74                 public bool IsSynchronized {
75                         get {
76                                 return items.IsSynchronized;
77                         }
78                 }
79
80                 public object SyncRoot {
81                         get {
82                                 return items.SyncRoot;
83                         }
84                 }
85
86                 public ListItem this[int index] {
87                         get {
88                                 return (ListItem)items[index];
89                         }
90                 }
91                 #endregion      // Public Instance Properties
92
93                 #region Public Instance Methods
94                 public void Add(ListItem item) {
95                         items.Add(item);
96                 }
97
98                 public void Add(string item) {
99                         items.Add(new ListItem(item));
100                 }
101
102                 public void AddRange(ListItem[] items) {
103                         for (int i = 0; i < items.Length; i++) {
104                                 Add(items[i]);
105                         }
106                 }
107
108                 public void Clear() {
109                         items.Clear();
110                 }
111
112                 public bool Contains(ListItem item) {
113                         return items.Contains(item);
114                 }
115
116                 public void CopyTo(Array array, int index) {
117                         items.CopyTo(array, index);
118                 }
119
120                 public ListItem FindByText (string text)
121                 {
122                         for (int i = 0; i < items.Count; i++)
123                                 if (text == this [i].Text)
124                                         return this [i];
125                         
126                         return null;
127                 }
128
129                 public ListItem FindByValue (string value)
130                 {
131                         for (int i = 0; i < items.Count; i++)
132                                 if (value == this [i].Value)
133                                         return this [i];
134                         
135                         return null;
136                 }
137
138                 public IEnumerator GetEnumerator() {
139                         return items.GetEnumerator();
140                 }
141
142                 public int IndexOf(ListItem item) {
143                         return items.IndexOf(item);
144                 }
145
146                 internal int IndexOf(string value) {
147                         for (int i = 0; i < items.Count; i++)
148                                 if (value == this [i].Value)
149                                         return i;
150                         return -1;
151                 }
152
153                 public void Insert(int index, ListItem item) {
154                         items.Insert(index, item);
155                 }
156
157                 public void Insert(int index, string item) {
158                         items.Insert(index, new ListItem(item));
159                 }
160
161                 public void Remove(ListItem item) {
162                         items.Remove(item);
163                 }
164
165                 public void Remove (string item)
166                 {
167                         for (int i = 0; i < items.Count; i++)
168                                 if (item == this [i].Value)
169                                         items.RemoveAt (i);
170                 }
171
172                 public void RemoveAt(int index) {
173                         items.RemoveAt(index);
174                 }
175                 #endregion      // Public Instance Methods
176
177                 #region Interface methods
178                 bool IList.IsFixedSize {
179                         get {
180                                 return items.IsFixedSize;
181                         }
182                 }
183
184                 object IList.this[int index] {
185                         get {
186                                 return this[index];
187                         }
188
189                         set {
190                                 if ((index >= 0) && (index < items.Count)) {
191                                         items[index] = (ListItem)value;
192                                 }
193                         }
194                 }
195
196                 int IList.Add(object value) {
197                         return items.Add((ListItem)value);
198                 }
199
200                 bool IList.Contains(object value) {
201                         return Contains((ListItem)value);
202                 }
203
204                 int IList.IndexOf(object value) {
205                         return IndexOf((ListItem)value);
206                 }
207
208                 void IList.Insert(int index, object value) {
209                         Insert(index, (ListItem)value);
210                 }
211
212                 void IList.Remove(object value) {
213                         Remove((ListItem)value);
214                 }
215
216                 bool IStateManager.IsTrackingViewState {
217                         get {
218                                 return tracking;
219                         }
220                 }
221
222                 void IStateManager.LoadViewState(object state) {
223                         int     count;
224                         string[] text;
225                         string[] value;
226 #if NET_2_0
227                         bool [] enabled;
228 #endif
229
230                         if (state == null) {
231                                 return;
232                         }
233 #if NET_2_0
234                         Triplet stateObj;
235                         stateObj = (Triplet) state;
236 #else
237                         Pair stateObj;
238                         stateObj = (Pair) state;
239 #endif
240
241                         text = (string []) stateObj.First;
242                         value = (string []) stateObj.Second;
243 #if NET_2_0
244                         enabled = (bool []) stateObj.Third;
245 #endif
246
247                         count = text.Length;
248
249                         items = new ArrayList(count);
250                         for (int i = 0; i < count; i++) {
251 #if NET_2_0
252                                 items.Add(new ListItem(text[i], value[i], enabled[i]));
253 #else
254                                 items.Add(new ListItem(text[i], value[i]));
255 #endif
256                         }
257                 }
258
259                 object IStateManager.SaveViewState() {
260                         int count;
261                         string[] text;
262                         string[] value;
263 #if NET_2_0
264                         bool [] enabled;
265 #endif
266
267                         count = items.Count;
268                         text = new string[count];
269                         value = new string[count];
270 #if NET_2_0
271                         enabled = new bool [count];
272 #endif
273
274                         for (int i = 0; i < count; i++) {
275                                 text[i] = ((ListItem)items[i]).Text;
276                                 value[i] = ((ListItem)items[i]).Value;
277 #if NET_2_0
278                                 enabled [i] = ((ListItem) items [i]).Enabled;
279 #endif
280                         }
281
282 #if NET_2_0
283                         return new Triplet(text, value, enabled);
284 #else
285                         return new Pair(text, value);
286 #endif
287                 }
288
289                 void IStateManager.TrackViewState() {
290                         tracking = true;
291
292                         for (int i = 0; i < items.Count; i++) {
293                                 ((ListItem)items[i]).TrackViewState();
294                         }
295                 }
296                 #endregion      // Interface methods
297         }
298 }