2 * Namespace: System.Web.UI.WebControls
\r
3 * Class: PagedDataSource
\r
5 * Author: Gaurav Vaish
\r
6 * Maintainer: gvaish@iitk.ac.in
\r
7 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
\r
8 * Implementation: yes
\r
11 * (C) Gaurav Vaish (2002)
\r
15 using System.ComponentModel;
\r
16 using System.Collections;
\r
18 using System.Web.UI;
\r
20 namespace System.Web.UI.WebControls
\r
22 public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
\r
24 private int pageSize;
\r
25 private bool allowPaging;
\r
26 private int currentPageIndex;
\r
27 private bool allowCustomPaging;
\r
28 private int virtualCount;
\r
30 private IEnumerable dataSource;
\r
32 public PagedDataSource()
\r
37 private void Initialize()
\r
40 allowPaging = false;
\r
41 currentPageIndex = 0;
\r
42 allowCustomPaging = false;
\r
46 public bool AllowCustomPaging
\r
50 return allowCustomPaging;
\r
54 allowCustomPaging = value;
\r
58 public bool AllowPaging
\r
66 allowPaging = value;
\r
74 if(dataSource != null)
\r
78 return DataSourceCount;
\r
80 if(IsCustomPagingEnabled)
\r
86 return (DataSourceCount - FirstIndexInPage);
\r
94 public int CurrentPageIndex
\r
98 return currentPageIndex;
\r
103 currentPageIndex = value;
\r
107 public IEnumerable DataSource
\r
115 dataSource = value;
\r
119 public int DataSourceCount
\r
123 if(dataSource != null)
\r
125 if(IsCustomPagingEnabled)
\r
127 return virtualCount;
\r
129 if(dataSource is ICollection)
\r
131 return ((ICollection)dataSource).Count;
\r
133 throw new HttpException(HttpRuntime.FormatResourceString("PagedDataSource_Cannot_Get_Count"));
\r
139 public int FirstIndexInPage
\r
143 if(dataSource != null && IsPagingEnabled && IsCustomPagingEnabled)
\r
145 return (currentPageIndex * pageSize);
\r
151 public bool IsCustomPagingEnabled
\r
155 return (IsPagingEnabled && allowCustomPaging);
\r
159 public bool IsFirstPage
\r
163 return (!IsPagingEnabled || (CurrentPageIndex == 0));
\r
167 public bool IsLastPage
\r
171 return (!IsPagingEnabled || (CurrentPageIndex == PageCount));
\r
175 public bool IsPagingEnabled
\r
179 return (allowPaging && pageSize != 0);
\r
183 public bool IsReadOnly
\r
191 public bool IsSynchronized
\r
199 public int PageCount
\r
203 if(dataSource != null)
\r
205 int total = DataSourceCount;
\r
206 if(!IsPagingEnabled)
\r
210 return (total + pageSize - 1)/pageSize;
\r
216 public int PageSize
\r
228 public object SyncRoot
\r
236 public int VirtualCount
\r
240 return virtualCount;
\r
244 virtualCount = value;
\r
248 public void CopyTo(Array array, int index)
\r
250 foreach(object current in this)
\r
252 array.SetValue(array, index++);
\r
256 public IEnumerator GetEnumerator()
\r
258 int fInd = FirstIndexInPage;
\r
260 if(dataSource is ICollection)
\r
265 if(dataSource is IList)
\r
267 return (new PrivateListEnumerator((IList)dataSource, fInd, count));
\r
269 if(dataSource is Array)
\r
271 return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
\r
273 if(dataSource is ICollection)
\r
275 return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
\r
277 if(allowCustomPaging)
\r
279 return (new PrivateIEnumeratorEnumerator(dataSource.GetEnumerator(), Count));
\r
281 return dataSource.GetEnumerator();
\r
284 class PrivateIEnumeratorEnumerator : IEnumerator
\r
289 private IEnumerator enumerator;
\r
291 public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
\r
293 this.enumerator = enumerator;
\r
298 public bool MoveNext()
\r
300 enumerator.MoveNext();
\r
302 return (index < max);
\r
305 public void Reset()
\r
308 enumerator.Reset();
\r
311 public object Current
\r
315 return enumerator.Current;
\r
320 class PrivateICollectionEnumerator : IEnumerator
\r
326 private ICollection collection;
\r
327 private IEnumerator collEnum;
\r
329 public PrivateICollectionEnumerator(ICollection collection, int start, int count)
\r
331 this.collection = collection;
\r
332 this.start = start;
\r
334 max = start + count;
\r
335 if(max > collection.Count)
\r
337 max = collection.Count;
\r
341 public bool MoveNext()
\r
343 if(collEnum == null)
\r
346 collEnum = collection.GetEnumerator();
\r
347 while(cIndex < start)
\r
349 collEnum.MoveNext();
\r
352 collEnum.MoveNext();
\r
354 return (start + index < max);
\r
357 public void Reset()
\r
363 public object Current
\r
367 return collEnum.Current;
\r
372 class PrivateArrayEnumerator : IEnumerator
\r
377 private object[] values;
\r
379 public PrivateArrayEnumerator(object[] values, int start, int count)
\r
381 this.values = values;
\r
382 this.start = start;
\r
384 max = start + count;
\r
385 if(max > this.values.Length)
\r
387 max = this.values.Length;
\r
391 public bool MoveNext()
\r
394 return (index + start < max);
\r
397 public void Reset()
\r
402 public object Current
\r
408 return values[index + start];
\r
410 throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
\r
415 class PrivateListEnumerator : IEnumerator
\r
420 private IList collection;
\r
422 public PrivateListEnumerator(IList list, int start, int count)
\r
425 this.start = start;
\r
427 max = start + count;
\r
428 if(max > list.Count)
\r
434 public bool MoveNext()
\r
437 return (index + start < max);
\r
440 public void Reset()
\r
445 public object Current
\r
451 return collection[index + start];
\r
453 throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
\r
458 public string GetListName(PropertyDescriptor[] listAccessors)
\r
460 return String.Empty;
\r
463 public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
\r
465 if(dataSource != null)
\r
467 if(dataSource is ITypedList)
\r
469 return ((ITypedList)dataSource).GetItemProperties(listAccessors);
\r