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;
47 #region Public Constructors
48 public ListItemCollection() {
49 items = new ArrayList();
51 #endregion // Public Constructors
53 #region Public Instance Properties
56 return items.Capacity;
60 items.Capacity = value;
70 public bool IsReadOnly {
72 return items.IsReadOnly;
76 public bool IsSynchronized {
78 return items.IsSynchronized;
82 public object SyncRoot {
84 return items.SyncRoot;
88 public ListItem this[int index] {
90 return (ListItem)items[index];
93 #endregion // Public Instance Properties
95 #region Public Instance Methods
96 public void Add(ListItem item) {
99 item.TrackViewState ();
104 public void Add(string item) {
105 ListItem listItem = new ListItem (item);
106 items.Add (listItem);
109 listItem.TrackViewState ();
114 public void AddRange(ListItem[] items) {
115 for (int i = 0; i < items.Length; i++) {
119 items [i].TrackViewState ();
125 public void Clear() {
132 public bool Contains(ListItem item) {
133 return items.Contains(item);
136 public void CopyTo(Array array, int index) {
137 items.CopyTo(array, index);
140 public ListItem FindByText (string text)
142 for (int i = 0; i < items.Count; i++)
143 if (text == this [i].Text)
149 public ListItem FindByValue (string value)
151 for (int i = 0; i < items.Count; i++)
152 if (value == this [i].Value)
158 public IEnumerator GetEnumerator() {
159 return items.GetEnumerator();
162 public int IndexOf(ListItem item) {
163 return items.IndexOf(item);
166 internal int IndexOf(string value) {
167 for (int i = 0; i < items.Count; i++)
168 if (value == this [i].Value)
173 public void Insert(int index, ListItem item) {
174 items.Insert(index, item);
177 item.TrackViewState ();
183 public void Insert(int index, string item) {
184 ListItem listItem = new ListItem(item);
185 items.Insert (index, listItem);
188 listItem.TrackViewState ();
194 public void Remove(ListItem item) {
201 public void Remove (string item)
203 for (int i = 0; i < items.Count; i++)
204 if (item == this [i].Value) {
212 public void RemoveAt(int index) {
213 items.RemoveAt(index);
218 #endregion // Public Instance Methods
220 #region Interface methods
221 bool IList.IsFixedSize {
223 return items.IsFixedSize;
227 object IList.this[int index] {
233 if ((index >= 0) && (index < items.Count)) {
234 items[index] = (ListItem)value;
237 ((ListItem) value).TrackViewState ();
242 int IList.Add(object value) {
243 int i = items.Add ((ListItem) value);
246 ((IStateManager) value).TrackViewState ();
252 bool IList.Contains(object value) {
253 return Contains((ListItem)value);
256 int IList.IndexOf(object value) {
257 return IndexOf((ListItem)value);
260 void IList.Insert(int index, object value) {
261 Insert(index, (ListItem)value);
264 void IList.Remove(object value) {
265 Remove((ListItem)value);
268 bool IStateManager.IsTrackingViewState {
274 void IStateManager.LoadViewState (object savedState) {
275 Pair pair = savedState as Pair;
279 bool newCollection = (bool) pair.First;
280 object [] itemsArray = (object []) pair.Second;
281 int count = itemsArray.Length;
284 items = new ArrayList(count);
286 for (int i = 0; i < count; i++) {
287 ListItem item = new ListItem ();
290 item.LoadViewState (itemsArray [i]);
294 if (itemsArray [i] != null){
295 item.LoadViewState (itemsArray [i]);
303 object IStateManager.SaveViewState() {
305 bool itemsDirty = false;
311 object [] itemsState = new object [count];
312 for (int i = 0; i < count; i++) {
313 itemsState [i] = ((IStateManager) items [i]).SaveViewState ();
314 if (itemsState [i] != null)
318 if (!dirty && !itemsDirty)
321 return new Pair (dirty, itemsState);
324 void IStateManager.TrackViewState() {
327 for (int i = 0; i < items.Count; i++) {
328 ((ListItem)items[i]).TrackViewState();
331 #endregion // Interface methods
333 private void SetDirty ()
336 for (int i = lastDirty; i < items.Count; i++)
337 ((ListItem) items [i]).SetDirty ();
339 lastDirty = items.Count;