ListItemCollection.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: ListItemCollection
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.Web;
  16. using System.Web.UI;
  17. using System.ComponentModel;
  18. using System.Reflection;
  19. namespace System.Web.UI.WebControls
  20. {
  21. //[Editor("??", typeof(Design.WebControls.ListItemCollectionEditor))]
  22. public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager
  23. {
  24. private ArrayList items;
  25. private bool saveAll;
  26. private bool marked;
  27. public ListItemCollection()
  28. {
  29. items = new ArrayList();
  30. saveAll = false;
  31. marked = false;
  32. }
  33. public int Capacity
  34. {
  35. get
  36. {
  37. return items.Capacity;
  38. }
  39. set
  40. {
  41. items.Capacity = value;
  42. }
  43. }
  44. public int Count
  45. {
  46. get
  47. {
  48. return items.Count;
  49. }
  50. }
  51. public bool IsReadOnly
  52. {
  53. get
  54. {
  55. return items.IsReadOnly;
  56. }
  57. }
  58. public bool IsSynchronized
  59. {
  60. get
  61. {
  62. return items.IsSynchronized;
  63. }
  64. }
  65. public ListItem this[int index]
  66. {
  67. get
  68. {
  69. if(index < 0 || index >= Count)
  70. return null;
  71. return (ListItem)(items[index]);
  72. }
  73. }
  74. public object SyncRoot
  75. {
  76. get
  77. {
  78. return this;
  79. }
  80. }
  81. public void Add(ListItem item)
  82. {
  83. items.Add(item);
  84. if(marked)
  85. item.Dirty = true;
  86. }
  87. public void Add(string item)
  88. {
  89. Add(new ListItem(item));
  90. }
  91. public void AddRange(ListItem[] items)
  92. {
  93. foreach(ListItem item in items)
  94. {
  95. if(item!=null)
  96. Add(item);
  97. }
  98. }
  99. public void Clear()
  100. {
  101. items.Clear();
  102. if(marked)
  103. saveAll = true;
  104. }
  105. public bool Contains(ListItem item)
  106. {
  107. return items.Contains(item);
  108. }
  109. public void CopyTo(Array array, int index)
  110. {
  111. items.CopyTo(array, index);
  112. }
  113. public ListItem FindByText(string text)
  114. {
  115. int i=-1;
  116. foreach(object current in items)
  117. {
  118. i++;
  119. if(((ListItem)current).Text == text)
  120. break;
  121. }
  122. return (i==-1 ? null : (ListItem)items[i]);
  123. }
  124. public ListItem FindByValue(string value)
  125. {
  126. foreach(ListItem current in items)
  127. {
  128. if(current.Value == value)
  129. {
  130. return current;
  131. }
  132. }
  133. return null;
  134. }
  135. internal int FindByValueInternal(string value)
  136. {
  137. int i = -1;
  138. foreach(ListItem current in items)
  139. {
  140. i++;
  141. if(current.Value == value)
  142. {
  143. return i;
  144. }
  145. }
  146. return -1;
  147. }
  148. public IEnumerator GetEnumerator()
  149. {
  150. return items.GetEnumerator();
  151. }
  152. public int IndexOf(ListItem item)
  153. {
  154. return items.IndexOf(item);
  155. }
  156. public void Insert(int index, ListItem item)
  157. {
  158. items.Insert(index, item);
  159. if(marked)
  160. saveAll = true;
  161. }
  162. public void Insert(int index, string item)
  163. {
  164. Insert(index, new ListItem(item));
  165. }
  166. public void RemoveAt(int index)
  167. {
  168. if(index < 0 || index >= items.Count)
  169. return;
  170. items.RemoveAt(index);
  171. if(marked)
  172. saveAll = true;
  173. }
  174. public void Remove(ListItem item)
  175. {
  176. RemoveAt(IndexOf(item));
  177. }
  178. public void Remove(string item)
  179. {
  180. RemoveAt(IndexOf(ListItem.FromString(item)));
  181. }
  182. internal object SaveViewState ()
  183. {
  184. int count = Count;
  185. if (saveAll) {
  186. string [] keys = new string [count];
  187. string [] vals = new string [count];
  188. for(int i = 0; i < count; i++) {
  189. keys[i] = this [i].Text;
  190. vals[i] = this [i].Value;
  191. }
  192. return new Triplet (count, keys, vals);
  193. }
  194. ArrayList indices = new ArrayList ();
  195. ArrayList states = new ArrayList ();
  196. object o;
  197. for(int i = 0; i < count; i++) {
  198. o = this [i].SaveViewState ();
  199. if (o == null)
  200. continue;
  201. indices.Add (i);
  202. states.Add (o);
  203. }
  204. if (indices.Count > 0)
  205. return new Pair (indices, states);
  206. return null;
  207. }
  208. internal void LoadViewState (object savedState)
  209. {
  210. if (savedState == null)
  211. return;
  212. int i, end;
  213. if (savedState is Pair) {
  214. Pair pair = (Pair) savedState;
  215. ArrayList indices = (ArrayList) pair.First;
  216. ArrayList states = (ArrayList) pair.Second;
  217. end = indices.Count;
  218. for (i = 0; i < end; i++) {
  219. if ((int) indices [i] < Count ) {
  220. this [i].LoadViewState (states [i]);
  221. } else {
  222. ListItem temp = new ListItem ();
  223. temp.LoadViewState (states [i]);
  224. Add (temp);
  225. }
  226. }
  227. } else if (savedState is Triplet) {
  228. Triplet t = (Triplet) savedState;
  229. items = new ArrayList ((int) t.First);
  230. saveAll = true;
  231. object [] text = (object []) t.Second;
  232. object [] vals = (object []) t.Third;
  233. end = text.Length;
  234. for(i = 0; i < end; i++)
  235. items.Add (new ListItem (text[i].ToString (), vals[i].ToString ()));
  236. }
  237. }
  238. internal void TrackViewState()
  239. {
  240. marked = true;
  241. foreach(ListItem current in items)
  242. current.TrackViewState();
  243. }
  244. bool IList.IsFixedSize
  245. {
  246. get
  247. {
  248. return false;
  249. }
  250. }
  251. object IList.this[int index]
  252. {
  253. get
  254. {
  255. return this[index];
  256. }
  257. set
  258. {
  259. if(index >= 0 && index < Count)
  260. if(value is ListItem)
  261. items[index] = (ListItem) value;
  262. }
  263. }
  264. int IList.Add(object item)
  265. {
  266. int index = (item is ListItem ? items.Add((ListItem)item) : -1);
  267. if(index!=-1 && marked)
  268. ((ListItem)item).Dirty = true;
  269. return index;
  270. }
  271. bool IList.Contains(object item)
  272. {
  273. if(item is ListItem)
  274. return Contains((ListItem)item);
  275. return false;
  276. }
  277. int IList.IndexOf(object item)
  278. {
  279. if(item is ListItem)
  280. return IndexOf((ListItem)item);
  281. return -1;
  282. }
  283. void IList.Insert(int index, object item)
  284. {
  285. if(item is ListItem)
  286. Insert(index, (ListItem)item);
  287. }
  288. void IList.Remove(object item)
  289. {
  290. if(item is string)
  291. Remove((string)item);
  292. if(item is ListItem)
  293. Remove((ListItem)item);
  294. }
  295. bool IStateManager.IsTrackingViewState
  296. {
  297. get
  298. {
  299. return marked;
  300. }
  301. }
  302. void IStateManager.LoadViewState(object state)
  303. {
  304. LoadViewState(state);
  305. }
  306. object IStateManager.SaveViewState()
  307. {
  308. return SaveViewState();
  309. }
  310. void IStateManager.TrackViewState()
  311. {
  312. TrackViewState();
  313. }
  314. }
  315. }