PagedDataSource.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: PagedDataSource
  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 (2002)
  12. */
  13. using System;
  14. using System.ComponentModel;
  15. using System.Collections;
  16. using System.Web;
  17. using System.Web.UI;
  18. namespace System.Web.UI.WebControls
  19. {
  20. public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
  21. {
  22. private int pageSize;
  23. private bool allowPaging;
  24. private int currentPageIndex;
  25. private bool allowCustomPaging;
  26. private int virtualCount;
  27. private IEnumerable dataSource;
  28. public PagedDataSource()
  29. {
  30. Initialize();
  31. }
  32. private void Initialize()
  33. {
  34. pageSize = 10;
  35. allowPaging = false;
  36. currentPageIndex = 0;
  37. allowCustomPaging = false;
  38. virtualCount = 0;
  39. }
  40. public bool AllowCustomPaging
  41. {
  42. get
  43. {
  44. return allowCustomPaging;
  45. }
  46. set
  47. {
  48. allowCustomPaging = value;
  49. }
  50. }
  51. public bool AllowPaging
  52. {
  53. get
  54. {
  55. return allowPaging;
  56. }
  57. set
  58. {
  59. allowPaging = value;
  60. }
  61. }
  62. public int Count
  63. {
  64. get
  65. {
  66. if(dataSource != null)
  67. {
  68. if(!IsPagingEnabled)
  69. {
  70. return DataSourceCount;
  71. }
  72. if(IsCustomPagingEnabled)
  73. {
  74. return pageSize;
  75. }
  76. if(IsLastPage)
  77. {
  78. return (DataSourceCount - FirstIndexInPage);
  79. }
  80. return pageSize;
  81. }
  82. return 0;
  83. }
  84. }
  85. public int CurrentPageIndex
  86. {
  87. get
  88. {
  89. return currentPageIndex;
  90. }
  91. set
  92. {
  93. currentPageIndex = value;
  94. }
  95. }
  96. public IEnumerable DataSource
  97. {
  98. get
  99. {
  100. return dataSource;
  101. }
  102. set
  103. {
  104. dataSource = value;
  105. }
  106. }
  107. public int DataSourceCount
  108. {
  109. get
  110. {
  111. if(dataSource != null)
  112. {
  113. if(IsCustomPagingEnabled)
  114. {
  115. return virtualCount;
  116. }
  117. if(dataSource is ICollection)
  118. {
  119. return ((ICollection)dataSource).Count;
  120. }
  121. throw new HttpException(HttpRuntime.FormatResourceString("PagedDataSource_Cannot_Get_Count"));
  122. }
  123. return 0;
  124. }
  125. }
  126. public int FirstIndexInPage
  127. {
  128. get
  129. {
  130. if(dataSource != null && IsPagingEnabled && !IsCustomPagingEnabled)
  131. {
  132. return (currentPageIndex * pageSize);
  133. }
  134. return 0;
  135. }
  136. }
  137. public bool IsCustomPagingEnabled
  138. {
  139. get
  140. {
  141. return (IsPagingEnabled && allowCustomPaging);
  142. }
  143. }
  144. public bool IsFirstPage
  145. {
  146. get
  147. {
  148. return (!IsPagingEnabled || (CurrentPageIndex == 0));
  149. }
  150. }
  151. public bool IsLastPage
  152. {
  153. get
  154. {
  155. return (!IsPagingEnabled || (CurrentPageIndex == (PageCount - 1)));
  156. }
  157. }
  158. public bool IsPagingEnabled
  159. {
  160. get
  161. {
  162. return (allowPaging && pageSize != 0);
  163. }
  164. }
  165. public bool IsReadOnly
  166. {
  167. get
  168. {
  169. return false;
  170. }
  171. }
  172. public bool IsSynchronized
  173. {
  174. get
  175. {
  176. return false;
  177. }
  178. }
  179. public int PageCount
  180. {
  181. get
  182. {
  183. if(dataSource != null) {
  184. if(!IsPagingEnabled)
  185. return 1;
  186. int total = DataSourceCount;
  187. return (total + pageSize - 1)/pageSize;
  188. }
  189. return 0;
  190. }
  191. }
  192. public int PageSize
  193. {
  194. get
  195. {
  196. return pageSize;
  197. }
  198. set
  199. {
  200. pageSize = value;
  201. }
  202. }
  203. public object SyncRoot
  204. {
  205. get
  206. {
  207. return this;
  208. }
  209. }
  210. public int VirtualCount
  211. {
  212. get
  213. {
  214. return virtualCount;
  215. }
  216. set
  217. {
  218. virtualCount = value;
  219. }
  220. }
  221. public void CopyTo(Array array, int index)
  222. {
  223. IEnumerator enumerator = this.GetEnumerator();
  224. if(enumerator == null) return;
  225. while(enumerator.MoveNext())
  226. array.SetValue(enumerator.Current, index++);
  227. }
  228. public IEnumerator GetEnumerator()
  229. {
  230. int fInd = FirstIndexInPage;
  231. int count = -1;
  232. if(dataSource is ICollection)
  233. {
  234. count = Count;
  235. }
  236. if(dataSource is IList)
  237. {
  238. return (new PrivateListEnumerator((IList)dataSource, fInd, count));
  239. }
  240. if(dataSource is Array)
  241. {
  242. return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
  243. }
  244. if(dataSource is ICollection)
  245. {
  246. return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
  247. }
  248. if(allowCustomPaging)
  249. {
  250. return (new PrivateIEnumeratorEnumerator(dataSource.GetEnumerator(), Count));
  251. }
  252. return dataSource.GetEnumerator();
  253. }
  254. class PrivateIEnumeratorEnumerator : IEnumerator
  255. {
  256. private int index;
  257. private int max;
  258. private IEnumerator enumerator;
  259. public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
  260. {
  261. this.enumerator = enumerator;
  262. index = -1;
  263. max = count;
  264. }
  265. public bool MoveNext()
  266. {
  267. enumerator.MoveNext();
  268. index++;
  269. return (index < max);
  270. }
  271. public void Reset()
  272. {
  273. index = -1;
  274. enumerator.Reset();
  275. }
  276. public object Current
  277. {
  278. get
  279. {
  280. return enumerator.Current;
  281. }
  282. }
  283. }
  284. class PrivateICollectionEnumerator : IEnumerator
  285. {
  286. private int index;
  287. private int start;
  288. private int max;
  289. private ICollection collection;
  290. private IEnumerator collEnum;
  291. public PrivateICollectionEnumerator(ICollection collection, int start, int count)
  292. {
  293. this.collection = collection;
  294. this.start = start;
  295. index = -1;
  296. max = start + count;
  297. if(max > collection.Count)
  298. {
  299. max = collection.Count;
  300. }
  301. }
  302. public bool MoveNext()
  303. {
  304. if(collEnum == null)
  305. {
  306. int cIndex = 0;
  307. collEnum = collection.GetEnumerator();
  308. while(cIndex < start)
  309. {
  310. collEnum.MoveNext();
  311. cIndex++;
  312. }
  313. }
  314. collEnum.MoveNext();
  315. index++;
  316. return (start + index < max);
  317. }
  318. public void Reset()
  319. {
  320. index = -1;
  321. collEnum = null;
  322. }
  323. public object Current
  324. {
  325. get
  326. {
  327. return collEnum.Current;
  328. }
  329. }
  330. }
  331. class PrivateArrayEnumerator : IEnumerator
  332. {
  333. private int index;
  334. private int start;
  335. private int max;
  336. private object[] values;
  337. public PrivateArrayEnumerator(object[] values, int start, int count)
  338. {
  339. this.values = values;
  340. this.start = start;
  341. index = -1;
  342. max = start + count;
  343. if(max > this.values.Length)
  344. {
  345. max = this.values.Length;
  346. }
  347. }
  348. public bool MoveNext()
  349. {
  350. index++;
  351. return (index + start < max);
  352. }
  353. public void Reset()
  354. {
  355. index = -1;
  356. }
  357. public object Current
  358. {
  359. get
  360. {
  361. if(index >= 0)
  362. {
  363. return values[index + start];
  364. }
  365. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  366. }
  367. }
  368. }
  369. class PrivateListEnumerator : IEnumerator
  370. {
  371. private int index;
  372. private int start;
  373. private int max;
  374. private IList collection;
  375. public PrivateListEnumerator(IList list, int start, int count)
  376. {
  377. collection = list;
  378. this.start = start;
  379. index = -1;
  380. max = start + count;
  381. if(max > list.Count)
  382. {
  383. max = list.Count;
  384. }
  385. }
  386. public bool MoveNext()
  387. {
  388. index++;
  389. return (index + start < max);
  390. }
  391. public void Reset()
  392. {
  393. index = -1;
  394. }
  395. public object Current
  396. {
  397. get
  398. {
  399. if(index >= 0)
  400. {
  401. return collection[index + start];
  402. }
  403. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  404. }
  405. }
  406. }
  407. public string GetListName(PropertyDescriptor[] listAccessors)
  408. {
  409. return String.Empty;
  410. }
  411. public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
  412. {
  413. if(dataSource != null)
  414. {
  415. if(dataSource is ITypedList)
  416. {
  417. return ((ITypedList)dataSource).GetItemProperties(listAccessors);
  418. }
  419. }
  420. return null;
  421. }
  422. }
  423. }