PagedDataSource.cs 7.3 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));
  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. {
  185. int total = DataSourceCount;
  186. if(!IsPagingEnabled)
  187. {
  188. return total;
  189. }
  190. return (total + pageSize - 1)/pageSize;
  191. }
  192. return 0;
  193. }
  194. }
  195. public int PageSize
  196. {
  197. get
  198. {
  199. return pageSize;
  200. }
  201. set
  202. {
  203. pageSize = value;
  204. }
  205. }
  206. public object SyncRoot
  207. {
  208. get
  209. {
  210. return this;
  211. }
  212. }
  213. public int VirtualCount
  214. {
  215. get
  216. {
  217. return virtualCount;
  218. }
  219. set
  220. {
  221. virtualCount = value;
  222. }
  223. }
  224. public void CopyTo(Array array, int index)
  225. {
  226. foreach(object current in this)
  227. {
  228. array.SetValue(array, index++);
  229. }
  230. }
  231. public IEnumerator GetEnumerator()
  232. {
  233. int fInd = FirstIndexInPage;
  234. int count = -1;
  235. if(dataSource is ICollection)
  236. {
  237. count = Count;
  238. }
  239. if(dataSource is IList)
  240. {
  241. return (new PrivateListEnumerator((IList)dataSource, fInd, count));
  242. }
  243. if(dataSource is Array)
  244. {
  245. return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
  246. }
  247. if(dataSource is ICollection)
  248. {
  249. return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
  250. }
  251. if(allowCustomPaging)
  252. {
  253. return (new PrivateIEnumeratorEnumerator(dataSource.GetEnumerator(), Count));
  254. }
  255. return dataSource.GetEnumerator();
  256. }
  257. class PrivateIEnumeratorEnumerator : IEnumerator
  258. {
  259. private int index;
  260. private int max;
  261. private IEnumerator enumerator;
  262. public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
  263. {
  264. this.enumerator = enumerator;
  265. index = -1;
  266. max = count;
  267. }
  268. public bool MoveNext()
  269. {
  270. enumerator.MoveNext();
  271. index++;
  272. return (index < max);
  273. }
  274. public void Reset()
  275. {
  276. index = -1;
  277. enumerator.Reset();
  278. }
  279. public object Current
  280. {
  281. get
  282. {
  283. return enumerator.Current;
  284. }
  285. }
  286. }
  287. class PrivateICollectionEnumerator : IEnumerator
  288. {
  289. private int index;
  290. private int start;
  291. private int max;
  292. private ICollection collection;
  293. private IEnumerator collEnum;
  294. public PrivateICollectionEnumerator(ICollection collection, int start, int count)
  295. {
  296. this.collection = collection;
  297. this.start = start;
  298. index = -1;
  299. max = start + count;
  300. if(max > collection.Count)
  301. {
  302. max = collection.Count;
  303. }
  304. }
  305. public bool MoveNext()
  306. {
  307. if(collEnum == null)
  308. {
  309. int cIndex = 0;
  310. collEnum = collection.GetEnumerator();
  311. while(cIndex < start)
  312. {
  313. collEnum.MoveNext();
  314. }
  315. }
  316. collEnum.MoveNext();
  317. index++;
  318. return (start + index < max);
  319. }
  320. public void Reset()
  321. {
  322. index = -1;
  323. collEnum = null;
  324. }
  325. public object Current
  326. {
  327. get
  328. {
  329. return collEnum.Current;
  330. }
  331. }
  332. }
  333. class PrivateArrayEnumerator : IEnumerator
  334. {
  335. private int index;
  336. private int start;
  337. private int max;
  338. private object[] values;
  339. public PrivateArrayEnumerator(object[] values, int start, int count)
  340. {
  341. this.values = values;
  342. this.start = start;
  343. index = -1;
  344. max = start + count;
  345. if(max > this.values.Length)
  346. {
  347. max = this.values.Length;
  348. }
  349. }
  350. public bool MoveNext()
  351. {
  352. index++;
  353. return (index + start < max);
  354. }
  355. public void Reset()
  356. {
  357. index = -1;
  358. }
  359. public object Current
  360. {
  361. get
  362. {
  363. if(index >= 0)
  364. {
  365. return values[index + start];
  366. }
  367. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  368. }
  369. }
  370. }
  371. class PrivateListEnumerator : IEnumerator
  372. {
  373. private int index;
  374. private int start;
  375. private int max;
  376. private IList collection;
  377. public PrivateListEnumerator(IList list, int start, int count)
  378. {
  379. collection = list;
  380. this.start = start;
  381. index = -1;
  382. max = start + count;
  383. if(max > list.Count)
  384. {
  385. max = list.Count;
  386. }
  387. }
  388. public bool MoveNext()
  389. {
  390. index++;
  391. return (index + start < max);
  392. }
  393. public void Reset()
  394. {
  395. index = -1;
  396. }
  397. public object Current
  398. {
  399. get
  400. {
  401. if(index >= 0)
  402. {
  403. return collection[index + start];
  404. }
  405. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  406. }
  407. }
  408. }
  409. public string GetListName(PropertyDescriptor[] listAccessors)
  410. {
  411. return String.Empty;
  412. }
  413. public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
  414. {
  415. if(dataSource != null)
  416. {
  417. if(dataSource is ITypedList)
  418. {
  419. return ((ITypedList)dataSource).GetItemProperties(listAccessors);
  420. }
  421. }
  422. return null;
  423. }
  424. }
  425. }