ListViewPagedDataSource.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // System.Web.UI.WebControls.ListView
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007-2008 Novell, Inc
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.ComponentModel;
  32. namespace System.Web.UI.WebControls
  33. {
  34. public class ListViewPagedDataSource : ICollection, IEnumerable, ITypedList
  35. {
  36. sealed class ListEnumerator : IEnumerator
  37. {
  38. int index;
  39. int startIndex;
  40. int end;
  41. IList list;
  42. public object Current {
  43. get { return list [startIndex + index]; }
  44. }
  45. public ListEnumerator (IList list, int startIndex, int end)
  46. {
  47. this.list = list;
  48. this.index = -1;
  49. this.startIndex = startIndex;
  50. this.end = startIndex + end;
  51. }
  52. public bool MoveNext ()
  53. {
  54. index++;
  55. return (startIndex + index) < end;
  56. }
  57. // See:
  58. // http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewpageddatasource.getenumerator.aspx
  59. // (Note 1)
  60. public void Reset ()
  61. {
  62. index = -1;
  63. }
  64. }
  65. sealed class CollectionEnumerator : IEnumerator
  66. {
  67. int index;
  68. int startIndex;
  69. int end;
  70. ICollection collection;
  71. IEnumerator enumerator;
  72. public object Current {
  73. get {
  74. if (enumerator != null)
  75. return enumerator.Current;
  76. return null;
  77. }
  78. }
  79. public CollectionEnumerator (ICollection collection, int startIndex, int end)
  80. {
  81. this.collection = collection;
  82. this.index = -1;
  83. this.startIndex = startIndex;
  84. this.end = end;
  85. }
  86. public bool MoveNext ()
  87. {
  88. if (enumerator == null) {
  89. enumerator = collection.GetEnumerator ();
  90. for (int i = 0; i < startIndex; i++)
  91. enumerator.MoveNext ();
  92. }
  93. index++;
  94. enumerator.MoveNext ();
  95. return (startIndex + index) < end;
  96. }
  97. // See:
  98. // http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewpageddatasource.getenumerator.aspx
  99. // (Note 1)
  100. public void Reset ()
  101. {
  102. index = -1;
  103. enumerator = null;
  104. }
  105. }
  106. public ListViewPagedDataSource ()
  107. {
  108. StartRowIndex = 0;
  109. MaximumRows = 0;
  110. AllowServerPaging = false;
  111. TotalRowCount = 0;
  112. }
  113. public void CopyTo (Array array, int index)
  114. {
  115. }
  116. public IEnumerator GetEnumerator ()
  117. {
  118. IEnumerable ds = DataSource;
  119. if (ds == null)
  120. return null;
  121. IList list = ds as IList;
  122. if (list != null)
  123. return new ListEnumerator (list, AllowServerPaging ? 0 : StartRowIndex, Count);
  124. ICollection collection = ds as ICollection;
  125. if (collection != null)
  126. return new CollectionEnumerator (collection, AllowServerPaging ? 0 : StartRowIndex, Count);
  127. return ds.GetEnumerator ();
  128. }
  129. public PropertyDescriptorCollection GetItemProperties (PropertyDescriptor [] listAccessors)
  130. {
  131. IEnumerable ds = DataSource;
  132. if (ds == null || !(ds is ITypedList))
  133. return null;
  134. return ((ITypedList) ds).GetItemProperties (listAccessors);
  135. }
  136. public string GetListName (PropertyDescriptor [] listAccessors)
  137. {
  138. return String.Empty;
  139. }
  140. public bool AllowServerPaging {
  141. get;
  142. set;
  143. }
  144. public int Count {
  145. get {
  146. IEnumerable ds = DataSource;
  147. if (ds == null)
  148. return 0;
  149. bool onLastPage = OnLastPage;
  150. int maxRows = MaximumRows;
  151. if (!onLastPage && maxRows >= 0)
  152. return maxRows;
  153. // LAMESPEC: MSDN says that DataSourceCount should be subtracted
  154. // from StartRowIndex
  155. return DataSourceCount - StartRowIndex;
  156. }
  157. }
  158. public IEnumerable DataSource {
  159. get;
  160. set;
  161. }
  162. public int DataSourceCount {
  163. get {
  164. IEnumerable ds = DataSource;
  165. if (ds == null)
  166. return 0;
  167. if (!(ds is ICollection))
  168. throw new InvalidOperationException ("The data source object does not implement the System.Collections..::.ICollection interface.");
  169. if (IsServerPagingEnabled)
  170. return TotalRowCount;
  171. return ((ICollection) ds).Count;
  172. }
  173. }
  174. public bool IsReadOnly {
  175. get { return false; }
  176. }
  177. public bool IsServerPagingEnabled {
  178. get { return AllowServerPaging; }
  179. }
  180. public bool IsSynchronized {
  181. get { return false; }
  182. }
  183. public int MaximumRows {
  184. get;
  185. set;
  186. }
  187. public int StartRowIndex {
  188. get;
  189. set;
  190. }
  191. public object SyncRoot {
  192. get { return this; }
  193. }
  194. public int TotalRowCount {
  195. get;
  196. set;
  197. }
  198. bool OnLastPage {
  199. get { return ((StartRowIndex + MaximumRows) >= DataSourceCount); }
  200. }
  201. }
  202. }