PagedDataSource.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI.WebControls
  23. * Class: PagedDataSource
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: [email protected]
  27. * Contact: <[email protected]>, <[email protected]>
  28. * Implementation: yes
  29. * Status: 100%
  30. *
  31. * (C) Gaurav Vaish (2002)
  32. */
  33. using System;
  34. using System.ComponentModel;
  35. using System.Collections;
  36. using System.Web;
  37. using System.Web.UI;
  38. namespace System.Web.UI.WebControls
  39. {
  40. public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
  41. {
  42. private int pageSize;
  43. private bool allowPaging;
  44. private int currentPageIndex;
  45. private bool allowCustomPaging;
  46. private int virtualCount;
  47. private IEnumerable dataSource;
  48. public PagedDataSource()
  49. {
  50. Initialize();
  51. }
  52. private void Initialize()
  53. {
  54. pageSize = 10;
  55. allowPaging = false;
  56. currentPageIndex = 0;
  57. allowCustomPaging = false;
  58. virtualCount = 0;
  59. }
  60. public bool AllowCustomPaging
  61. {
  62. get
  63. {
  64. return allowCustomPaging;
  65. }
  66. set
  67. {
  68. allowCustomPaging = value;
  69. }
  70. }
  71. public bool AllowPaging
  72. {
  73. get
  74. {
  75. return allowPaging;
  76. }
  77. set
  78. {
  79. allowPaging = value;
  80. }
  81. }
  82. public int Count
  83. {
  84. get
  85. {
  86. if(dataSource != null)
  87. {
  88. if(!IsPagingEnabled)
  89. {
  90. return DataSourceCount;
  91. }
  92. if(IsCustomPagingEnabled)
  93. {
  94. return pageSize;
  95. }
  96. if(IsLastPage)
  97. {
  98. return (DataSourceCount - FirstIndexInPage);
  99. }
  100. return pageSize;
  101. }
  102. return 0;
  103. }
  104. }
  105. public int CurrentPageIndex
  106. {
  107. get
  108. {
  109. return currentPageIndex;
  110. }
  111. set
  112. {
  113. currentPageIndex = value;
  114. }
  115. }
  116. public IEnumerable DataSource
  117. {
  118. get
  119. {
  120. return dataSource;
  121. }
  122. set
  123. {
  124. dataSource = value;
  125. }
  126. }
  127. public int DataSourceCount
  128. {
  129. get
  130. {
  131. if(dataSource != null)
  132. {
  133. if(IsCustomPagingEnabled)
  134. {
  135. return virtualCount;
  136. }
  137. if(dataSource is ICollection)
  138. {
  139. return ((ICollection)dataSource).Count;
  140. }
  141. throw new HttpException(HttpRuntime.FormatResourceString("PagedDataSource_Cannot_Get_Count"));
  142. }
  143. return 0;
  144. }
  145. }
  146. public int FirstIndexInPage
  147. {
  148. get
  149. {
  150. if(dataSource != null && IsPagingEnabled && !IsCustomPagingEnabled)
  151. {
  152. return (currentPageIndex * pageSize);
  153. }
  154. return 0;
  155. }
  156. }
  157. public bool IsCustomPagingEnabled
  158. {
  159. get
  160. {
  161. return (IsPagingEnabled && allowCustomPaging);
  162. }
  163. }
  164. public bool IsFirstPage
  165. {
  166. get
  167. {
  168. return (!IsPagingEnabled || (CurrentPageIndex == 0));
  169. }
  170. }
  171. public bool IsLastPage
  172. {
  173. get
  174. {
  175. return (!IsPagingEnabled || (CurrentPageIndex == (PageCount - 1)));
  176. }
  177. }
  178. public bool IsPagingEnabled
  179. {
  180. get
  181. {
  182. return (allowPaging && pageSize != 0);
  183. }
  184. }
  185. public bool IsReadOnly
  186. {
  187. get
  188. {
  189. return false;
  190. }
  191. }
  192. public bool IsSynchronized
  193. {
  194. get
  195. {
  196. return false;
  197. }
  198. }
  199. public int PageCount
  200. {
  201. get
  202. {
  203. if(dataSource != null) {
  204. if(!IsPagingEnabled)
  205. return 1;
  206. int total = DataSourceCount;
  207. return (total + pageSize - 1)/pageSize;
  208. }
  209. return 0;
  210. }
  211. }
  212. public int PageSize
  213. {
  214. get
  215. {
  216. return pageSize;
  217. }
  218. set
  219. {
  220. pageSize = value;
  221. }
  222. }
  223. public object SyncRoot
  224. {
  225. get
  226. {
  227. return this;
  228. }
  229. }
  230. public int VirtualCount
  231. {
  232. get
  233. {
  234. return virtualCount;
  235. }
  236. set
  237. {
  238. virtualCount = value;
  239. }
  240. }
  241. public void CopyTo(Array array, int index)
  242. {
  243. IEnumerator enumerator = this.GetEnumerator();
  244. if(enumerator == null) return;
  245. while(enumerator.MoveNext())
  246. array.SetValue(enumerator.Current, index++);
  247. }
  248. public IEnumerator GetEnumerator()
  249. {
  250. int fInd = FirstIndexInPage;
  251. int count = -1;
  252. if(dataSource is ICollection)
  253. {
  254. count = Count;
  255. }
  256. if(dataSource is IList)
  257. {
  258. return (new PrivateListEnumerator((IList)dataSource, fInd, count));
  259. }
  260. if(dataSource is Array)
  261. {
  262. return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
  263. }
  264. if(dataSource is ICollection)
  265. {
  266. return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
  267. }
  268. if(allowCustomPaging)
  269. {
  270. return (new PrivateIEnumeratorEnumerator(dataSource.GetEnumerator(), Count));
  271. }
  272. return dataSource.GetEnumerator();
  273. }
  274. class PrivateIEnumeratorEnumerator : IEnumerator
  275. {
  276. private int index;
  277. private int max;
  278. private IEnumerator enumerator;
  279. public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
  280. {
  281. this.enumerator = enumerator;
  282. index = -1;
  283. max = count;
  284. }
  285. public bool MoveNext()
  286. {
  287. enumerator.MoveNext();
  288. index++;
  289. return (index < max);
  290. }
  291. public void Reset()
  292. {
  293. index = -1;
  294. enumerator.Reset();
  295. }
  296. public object Current
  297. {
  298. get
  299. {
  300. return enumerator.Current;
  301. }
  302. }
  303. }
  304. class PrivateICollectionEnumerator : IEnumerator
  305. {
  306. private int index;
  307. private int start;
  308. private int max;
  309. private ICollection collection;
  310. private IEnumerator collEnum;
  311. public PrivateICollectionEnumerator(ICollection collection, int start, int count)
  312. {
  313. this.collection = collection;
  314. this.start = start;
  315. index = -1;
  316. max = start + count;
  317. if(max > collection.Count)
  318. {
  319. max = collection.Count;
  320. }
  321. }
  322. public bool MoveNext()
  323. {
  324. if(collEnum == null)
  325. {
  326. int cIndex = 0;
  327. collEnum = collection.GetEnumerator();
  328. while(cIndex < start)
  329. {
  330. collEnum.MoveNext();
  331. cIndex++;
  332. }
  333. }
  334. collEnum.MoveNext();
  335. index++;
  336. return (start + index < max);
  337. }
  338. public void Reset()
  339. {
  340. index = -1;
  341. collEnum = null;
  342. }
  343. public object Current
  344. {
  345. get
  346. {
  347. return collEnum.Current;
  348. }
  349. }
  350. }
  351. class PrivateArrayEnumerator : IEnumerator
  352. {
  353. private int index;
  354. private int start;
  355. private int max;
  356. private object[] values;
  357. public PrivateArrayEnumerator(object[] values, int start, int count)
  358. {
  359. this.values = values;
  360. this.start = start;
  361. index = -1;
  362. max = start + count;
  363. if(max > this.values.Length)
  364. {
  365. max = this.values.Length;
  366. }
  367. }
  368. public bool MoveNext()
  369. {
  370. index++;
  371. return (index + start < max);
  372. }
  373. public void Reset()
  374. {
  375. index = -1;
  376. }
  377. public object Current
  378. {
  379. get
  380. {
  381. if(index >= 0)
  382. {
  383. return values[index + start];
  384. }
  385. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  386. }
  387. }
  388. }
  389. class PrivateListEnumerator : IEnumerator
  390. {
  391. private int index;
  392. private int start;
  393. private int max;
  394. private IList collection;
  395. public PrivateListEnumerator(IList list, int start, int count)
  396. {
  397. collection = list;
  398. this.start = start;
  399. index = -1;
  400. max = start + count;
  401. if(max > list.Count)
  402. {
  403. max = list.Count;
  404. }
  405. }
  406. public bool MoveNext()
  407. {
  408. index++;
  409. return (index + start < max);
  410. }
  411. public void Reset()
  412. {
  413. index = -1;
  414. }
  415. public object Current
  416. {
  417. get
  418. {
  419. if(index >= 0)
  420. {
  421. return collection[index + start];
  422. }
  423. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  424. }
  425. }
  426. }
  427. public string GetListName(PropertyDescriptor[] listAccessors)
  428. {
  429. return String.Empty;
  430. }
  431. public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
  432. {
  433. if(dataSource != null)
  434. {
  435. if(dataSource is ITypedList)
  436. {
  437. return ((ITypedList)dataSource).GetItemProperties(listAccessors);
  438. }
  439. }
  440. return null;
  441. }
  442. }
  443. }