1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
/**
* Namespace: System.Web.UI.WebControls
* Class: ListItemCollection
*
* Author: Gaurav Vaish
* Maintainer: gvaish@iitk.ac.in
* Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
* Implementation: yes
* Status: 100%
*
* (C) Gaurav Vaish (2001)
*/
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Reflection;
namespace System.Web.UI.WebControls
{
[Editor ("System.Web.UI.Design.WebControls.ListItemsCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager
{
private ArrayList items;
private bool saveAll;
private bool marked;
public ListItemCollection()
{
items = new ArrayList();
saveAll = false;
marked = false;
}
public int Capacity
{
get
{
return items.Capacity;
}
set
{
items.Capacity = value;
}
}
public int Count
{
get
{
return items.Count;
}
}
public bool IsReadOnly
{
get
{
return items.IsReadOnly;
}
}
public bool IsSynchronized
{
get
{
return items.IsSynchronized;
}
}
public ListItem this[int index]
{
get
{
if(index < 0 || index >= Count)
return null;
return (ListItem)(items[index]);
}
}
public object SyncRoot
{
get
{
return this;
}
}
public void Add(ListItem item)
{
items.Add(item);
if(marked)
item.Dirty = true;
}
public void Add(string item)
{
Add(new ListItem(item));
}
public void AddRange(ListItem[] items)
{
foreach(ListItem item in items)
{
if(item!=null)
Add(item);
}
}
public void Clear()
{
items.Clear();
if(marked)
saveAll = true;
}
public bool Contains(ListItem item)
{
return items.Contains(item);
}
public void CopyTo(Array array, int index)
{
items.CopyTo(array, index);
}
public ListItem FindByText(string text)
{
int i=-1;
foreach(object current in items)
{
i++;
if(((ListItem)current).Text == text)
break;
}
return (i==-1 ? null : (ListItem)items[i]);
}
public ListItem FindByValue(string value)
{
foreach(ListItem current in items)
{
if(current.Value == value)
{
return current;
}
}
return null;
}
internal int FindByValueInternal(string value)
{
int i = -1;
foreach(ListItem current in items)
{
i++;
if(current.Value == value)
{
return i;
}
}
return -1;
}
public IEnumerator GetEnumerator()
{
return items.GetEnumerator();
}
public int IndexOf(ListItem item)
{
return items.IndexOf(item);
}
public void Insert(int index, ListItem item)
{
items.Insert(index, item);
if(marked)
saveAll = true;
}
public void Insert(int index, string item)
{
Insert(index, new ListItem(item));
}
public void RemoveAt(int index)
{
if(index < 0 || index >= items.Count)
return;
items.RemoveAt(index);
if(marked)
saveAll = true;
}
public void Remove(ListItem item)
{
RemoveAt(IndexOf(item));
}
public void Remove(string item)
{
RemoveAt(IndexOf(ListItem.FromString(item)));
}
internal object SaveViewState ()
{
int count = Count;
if (saveAll) {
string [] keys = new string [count];
string [] vals = new string [count];
for(int i = 0; i < count; i++) {
keys[i] = this [i].Text;
vals[i] = this [i].Value;
}
return new Triplet (count, keys, vals);
}
ArrayList indices = new ArrayList ();
ArrayList states = new ArrayList ();
object o;
for(int i = 0; i < count; i++) {
o = this [i].SaveViewState ();
if (o == null)
continue;
indices.Add (i);
states.Add (o);
}
if (indices.Count > 0)
return new Pair (indices, states);
return null;
}
internal void LoadViewState (object savedState)
{
if (savedState == null)
return;
int i, end;
if (savedState is Pair) {
Pair pair = (Pair) savedState;
ArrayList indices = (ArrayList) pair.First;
ArrayList states = (ArrayList) pair.Second;
end = indices.Count;
for (i = 0; i < end; i++) {
if ((int) indices [i] < Count ) {
this [i].LoadViewState (states [i]);
} else {
ListItem temp = new ListItem ();
temp.LoadViewState (states [i]);
Add (temp);
}
}
} else if (savedState is Triplet) {
Triplet t = (Triplet) savedState;
items = new ArrayList ((int) t.First);
saveAll = true;
object [] text = (object []) t.Second;
object [] vals = (object []) t.Third;
end = text.Length;
for(i = 0; i < end; i++)
items.Add (new ListItem (text[i].ToString (), vals[i].ToString ()));
}
}
internal void TrackViewState()
{
marked = true;
foreach(ListItem current in items)
current.TrackViewState();
}
bool IList.IsFixedSize
{
get
{
return false;
}
}
object IList.this[int index]
{
get
{
return this[index];
}
set
{
if(index >= 0 && index < Count)
if(value is ListItem)
items[index] = (ListItem) value;
}
}
int IList.Add(object item)
{
int index = (item is ListItem ? items.Add((ListItem)item) : -1);
if(index!=-1 && marked)
((ListItem)item).Dirty = true;
return index;
}
bool IList.Contains(object item)
{
if(item is ListItem)
return Contains((ListItem)item);
return false;
}
int IList.IndexOf(object item)
{
if(item is ListItem)
return IndexOf((ListItem)item);
return -1;
}
void IList.Insert(int index, object item)
{
if(item is ListItem)
Insert(index, (ListItem)item);
}
void IList.Remove(object item)
{
if(item is string)
Remove((string)item);
if(item is ListItem)
Remove((ListItem)item);
}
bool IStateManager.IsTrackingViewState
{
get
{
return marked;
}
}
void IStateManager.LoadViewState(object state)
{
LoadViewState(state);
}
object IStateManager.SaveViewState()
{
return SaveViewState();
}
void IStateManager.TrackViewState()
{
TrackViewState();
}
}
}
|