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
49 #region Public Constructors
50 public ListItemCollection() {
51 items = new ArrayList();
53 #endregion // Public Constructors
55 internal bool ItemsEnabled {
56 get { return itemsEnabled; }
57 set { itemsEnabled = value; }
60 #region Public Instance Properties
63 return items.Capacity;
67 items.Capacity = value;
77 public bool IsReadOnly {
79 return items.IsReadOnly;
83 public bool IsSynchronized {
85 return items.IsSynchronized;
89 public object SyncRoot {
91 return items.SyncRoot;
95 public ListItem this[int index] {
97 return (ListItem)items[index];
100 #endregion // Public Instance Properties
102 #region Public Instance Methods
103 public void Add(ListItem item) {
106 item.TrackViewState ();
111 public void Add(string item) {
112 ListItem listItem = new ListItem (item);
113 items.Add (listItem);
116 listItem.TrackViewState ();
121 public void AddRange(ListItem[] items) {
122 for (int i = 0; i < items.Length; i++) {
126 items [i].TrackViewState ();
132 public void Clear() {
139 public bool Contains(ListItem item) {
140 return items.Contains(item);
143 public void CopyTo(Array array, int index) {
144 items.CopyTo(array, index);
147 public ListItem FindByText (string text)
149 for (int i = 0; i < items.Count; i++)
150 if (text == this [i].Text)
156 public ListItem FindByValue (string value)
158 for (int i = 0; i < items.Count; i++)
159 if (value == this [i].Value)
165 public IEnumerator GetEnumerator() {
166 return items.GetEnumerator();
169 public int IndexOf(ListItem item) {
170 return items.IndexOf(item);
173 internal int IndexOf(string value) {
174 for (int i = 0; i < items.Count; i++)
175 if (value == this [i].Value)
180 public void Insert(int index, ListItem item) {
181 items.Insert(index, item);
184 item.TrackViewState ();
190 public void Insert(int index, string item) {
191 ListItem listItem = new ListItem(item);
192 items.Insert (index, listItem);
195 listItem.TrackViewState ();
201 public void Remove(ListItem item) {
208 public void Remove (string item)
210 for (int i = 0; i < items.Count; i++)
211 if (item == this [i].Value) {
219 public void RemoveAt(int index) {
220 items.RemoveAt(index);
225 #endregion // Public Instance Methods
227 #region Interface methods
228 bool IList.IsFixedSize {
230 return items.IsFixedSize;
234 object IList.this[int index] {
240 if ((index >= 0) && (index < items.Count)) {
241 items[index] = (ListItem)value;
244 ((ListItem) value).TrackViewState ();
249 int IList.Add(object value) {
250 int i = items.Add ((ListItem) value);
253 ((IStateManager) value).TrackViewState ();
259 bool IList.Contains(object value) {
260 return Contains((ListItem)value);
263 int IList.IndexOf(object value) {
264 return IndexOf((ListItem)value);
267 void IList.Insert(int index, object value) {
268 Insert(index, (ListItem)value);
271 void IList.Remove(object value) {
272 Remove((ListItem)value);
275 bool IStateManager.IsTrackingViewState {
281 void IStateManager.LoadViewState (object savedState) {
282 Pair pair = savedState as Pair;
286 bool newCollection = (bool) pair.First;
287 object [] itemsArray = (object []) pair.Second;
288 int count = itemsArray==null ? 0 : itemsArray.Length;
292 items = new ArrayList(count);
294 items = new ArrayList();
296 for (int i = 0; i < count; i++) {
297 ListItem item = new ListItem ();
300 item.LoadViewState (itemsArray [i]);
305 if (itemsArray [i] != null){
306 item.LoadViewState (itemsArray [i]);
314 object IStateManager.SaveViewState() {
316 bool itemsDirty = false;
319 if (count == 0 && !dirty)
322 object [] itemsState = null;
324 itemsState = new object [count];
327 bool enabled = ItemsEnabled;
329 for (int i = 0; i < count; i++) {
331 li = items [i] as ListItem;
332 if (li != null && li.Enabled != enabled)
333 li.Enabled = enabled;
336 itemsState [i] = ((IStateManager) items [i]).SaveViewState ();
337 if (itemsState [i] != null)
341 if (!dirty && !itemsDirty)
344 return new Pair (dirty, itemsState);
347 void IStateManager.TrackViewState() {
350 for (int i = 0; i < items.Count; i++) {
351 ((ListItem)items[i]).TrackViewState();
354 #endregion // Interface methods
359 for (int i = lastDirty; i < items.Count; i++)
360 ((ListItem) items [i]).SetDirty ();
362 lastDirty = items.Count - 1;