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
48 #region Public Constructors
49 public ListItemCollection() {
50 items = new ArrayList();
52 #endregion // Public Constructors
54 #region Public Instance Properties
57 return items.Capacity;
61 items.Capacity = value;
71 public bool IsReadOnly {
73 return items.IsReadOnly;
77 public bool IsSynchronized {
79 return items.IsSynchronized;
83 public object SyncRoot {
85 return items.SyncRoot;
89 public ListItem this[int index] {
91 return (ListItem)items[index];
94 #endregion // Public Instance Properties
96 #region Public Instance Methods
97 public void Add(ListItem item) {
100 item.TrackViewState ();
105 public void Add(string item) {
106 ListItem listItem = new ListItem (item);
107 items.Add (listItem);
110 listItem.TrackViewState ();
115 public void AddRange(ListItem[] items) {
116 for (int i = 0; i < items.Length; i++) {
120 items [i].TrackViewState ();
126 public void Clear() {
133 public bool Contains(ListItem item) {
134 return items.Contains(item);
137 public void CopyTo(Array array, int index) {
138 items.CopyTo(array, index);
141 public ListItem FindByText (string text)
143 for (int i = 0; i < items.Count; i++)
144 if (text == this [i].Text)
150 public ListItem FindByValue (string value)
152 for (int i = 0; i < items.Count; i++)
153 if (value == this [i].Value)
159 public IEnumerator GetEnumerator() {
160 return items.GetEnumerator();
163 public int IndexOf(ListItem item) {
164 return items.IndexOf(item);
167 internal int IndexOf(string value) {
168 for (int i = 0; i < items.Count; i++)
169 if (value == this [i].Value)
174 public void Insert(int index, ListItem item) {
175 items.Insert(index, item);
178 item.TrackViewState ();
184 public void Insert(int index, string item) {
185 ListItem listItem = new ListItem(item);
186 items.Insert (index, listItem);
189 listItem.TrackViewState ();
195 public void Remove(ListItem item) {
202 public void Remove (string item)
204 for (int i = 0; i < items.Count; i++)
205 if (item == this [i].Value) {
213 public void RemoveAt(int index) {
214 items.RemoveAt(index);
219 #endregion // Public Instance Methods
221 #region Interface methods
222 bool IList.IsFixedSize {
224 return items.IsFixedSize;
228 object IList.this[int index] {
234 if ((index >= 0) && (index < items.Count)) {
235 items[index] = (ListItem)value;
238 ((ListItem) value).TrackViewState ();
243 int IList.Add(object value) {
244 int i = items.Add ((ListItem) value);
247 ((IStateManager) value).TrackViewState ();
253 bool IList.Contains(object value) {
254 return Contains((ListItem)value);
257 int IList.IndexOf(object value) {
258 return IndexOf((ListItem)value);
261 void IList.Insert(int index, object value) {
262 Insert(index, (ListItem)value);
265 void IList.Remove(object value) {
266 Remove((ListItem)value);
269 bool IStateManager.IsTrackingViewState {
275 void IStateManager.LoadViewState (object savedState)
277 Pair pair = savedState as Pair;
281 bool newCollection = (bool) pair.First;
282 object [] itemsArray = (object []) pair.Second;
283 int count = itemsArray==null ? 0 : itemsArray.Length;
287 items = new ArrayList(count);
289 items = new ArrayList();
291 for (int i = 0; i < count; i++) {
292 ListItem item = new ListItem ();
295 item.LoadViewState (itemsArray [i]);
300 if (itemsArray [i] != null){
301 item.LoadViewState (itemsArray [i]);
309 object IStateManager.SaveViewState() {
311 bool itemsDirty = false;
314 if (count == 0 && !dirty)
317 object [] itemsState = null;
319 itemsState = new object [count];
321 for (int i = 0; i < count; i++) {
322 itemsState [i] = ((IStateManager) items [i]).SaveViewState ();
323 if (itemsState [i] != null)
327 if (!dirty && !itemsDirty)
330 return new Pair (dirty, itemsState);
333 void IStateManager.TrackViewState() {
336 for (int i = 0; i < items.Count; i++) {
337 ((ListItem)items[i]).TrackViewState();
340 #endregion // Interface methods
345 for (int i = lastDirty; i < items.Count; i++)
346 ((ListItem) items [i]).SetDirty ();
348 lastDirty = items.Count - 1;