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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
27 using System.Collections;
28 using System.ComponentModel;
29 using System.Globalization;
30 using System.Reflection;
31 using System.Security.Permissions;
33 namespace System.Web.UI.WebControls {
35 // CAS - no inheritance demand required because the class is sealed
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
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 {
41 private ArrayList items;
42 private bool tracking;
45 #region Public Constructors
46 public ListItemCollection() {
47 items = new ArrayList();
49 #endregion // Public Constructors
51 #region Public Instance Properties
54 return items.Capacity;
58 items.Capacity = value;
68 public bool IsReadOnly {
70 return items.IsReadOnly;
74 public bool IsSynchronized {
76 return items.IsSynchronized;
80 public object SyncRoot {
82 return items.SyncRoot;
86 public ListItem this[int index] {
88 return (ListItem)items[index];
91 #endregion // Public Instance Properties
93 #region Public Instance Methods
94 public void Add(ListItem item) {
98 public void Add(string item) {
99 items.Add(new ListItem(item));
102 public void AddRange(ListItem[] items) {
103 for (int i = 0; i < items.Length; i++) {
108 public void Clear() {
112 public bool Contains(ListItem item) {
113 return items.Contains(item);
116 public void CopyTo(Array array, int index) {
117 items.CopyTo(array, index);
120 public ListItem FindByText (string text)
122 for (int i = 0; i < items.Count; i++)
123 if (text == this [i].Text)
129 public ListItem FindByValue (string value)
131 for (int i = 0; i < items.Count; i++)
132 if (value == this [i].Value)
138 public IEnumerator GetEnumerator() {
139 return items.GetEnumerator();
142 public int IndexOf(ListItem item) {
143 return items.IndexOf(item);
146 internal int IndexOf(string value) {
147 for (int i = 0; i < items.Count; i++)
148 if (value == this [i].Value)
153 public void Insert(int index, ListItem item) {
154 items.Insert(index, item);
157 public void Insert(int index, string item) {
158 items.Insert(index, new ListItem(item));
161 public void Remove(ListItem item) {
165 public void Remove (string item)
167 for (int i = 0; i < items.Count; i++)
168 if (item == this [i].Value)
172 public void RemoveAt(int index) {
173 items.RemoveAt(index);
175 #endregion // Public Instance Methods
177 #region Interface methods
178 bool IList.IsFixedSize {
180 return items.IsFixedSize;
184 object IList.this[int index] {
190 if ((index >= 0) && (index < items.Count)) {
191 items[index] = (ListItem)value;
196 int IList.Add(object value) {
197 return items.Add((ListItem)value);
200 bool IList.Contains(object value) {
201 return Contains((ListItem)value);
204 int IList.IndexOf(object value) {
205 return IndexOf((ListItem)value);
208 void IList.Insert(int index, object value) {
209 Insert(index, (ListItem)value);
212 void IList.Remove(object value) {
213 Remove((ListItem)value);
216 bool IStateManager.IsTrackingViewState {
222 void IStateManager.LoadViewState(object state) {
235 stateObj = (Triplet) state;
238 stateObj = (Pair) state;
241 text = (string []) stateObj.First;
242 value = (string []) stateObj.Second;
244 enabled = (bool []) stateObj.Third;
249 items = new ArrayList(count);
250 for (int i = 0; i < count; i++) {
252 items.Add(new ListItem(text[i], value[i], enabled[i]));
254 items.Add(new ListItem(text[i], value[i]));
259 object IStateManager.SaveViewState() {
268 text = new string[count];
269 value = new string[count];
271 enabled = new bool [count];
274 for (int i = 0; i < count; i++) {
275 text[i] = ((ListItem)items[i]).Text;
276 value[i] = ((ListItem)items[i]).Value;
278 enabled [i] = ((ListItem) items [i]).Enabled;
283 return new Triplet(text, value, enabled);
285 return new Pair(text, value);
289 void IStateManager.TrackViewState() {
292 for (int i = 0; i < items.Count; i++) {
293 ((ListItem)items[i]).TrackViewState();
296 #endregion // Interface methods