| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- /**
- * Namespace: System.Web.UI.WebControls
- * Class: PagedDataSource
- *
- * Author: Gaurav Vaish
- * Maintainer: [email protected]
- * Contact: <[email protected]>, <[email protected]>
- * Implementation: yes
- * Status: 100%
- *
- * (C) Gaurav Vaish (2002)
- */
- using System;
- using System.ComponentModel;
- using System.Collections;
- using System.Web;
- using System.Web.UI;
- namespace System.Web.UI.WebControls
- {
- public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
- {
- private int pageSize;
- private bool allowPaging;
- private int currentPageIndex;
- private bool allowCustomPaging;
- private int virtualCount;
- private IEnumerable dataSource;
- public PagedDataSource()
- {
- Initialize();
- }
- private void Initialize()
- {
- pageSize = 10;
- allowPaging = false;
- currentPageIndex = 0;
- allowCustomPaging = false;
- virtualCount = 0;
- }
- public bool AllowCustomPaging
- {
- get
- {
- return allowCustomPaging;
- }
- set
- {
- allowCustomPaging = value;
- }
- }
- public bool AllowPaging
- {
- get
- {
- return allowPaging;
- }
- set
- {
- allowPaging = value;
- }
- }
- public int Count
- {
- get
- {
- if(dataSource != null)
- {
- if(IsPagingEnabled)
- {
- return DataSourceCount;
- }
- if(IsCustomPagingEnabled)
- {
- return pageSize;
- }
- if(IsLastPage)
- {
- return (DataSourceCount - FirstIndexInPage);
- }
- return pageSize;
- }
- return 0;
- }
- }
- public int CurrentPageIndex
- {
- get
- {
- return currentPageIndex;
- }
- }
- public IEnumerable DataSource
- {
- get
- {
- return dataSource;
- }
- set
- {
- dataSource = value;
- }
- }
- public int DataSourceCount
- {
- get
- {
- if(dataSource != null)
- {
- if(IsCustomPagingEnabled)
- {
- return virtualCount;
- }
- if(dataSource is ICollection)
- {
- return ((ICollection)dataSource).Count;
- }
- throw new HttpException(HttpRuntime.FormatResourceString("PagedDataSource_Cannot_Get_Count"));
- }
- return 0;
- }
- }
- public int FirstIndexInPage
- {
- get
- {
- if(dataSource != null && IsPagingEnabled && IsCustomPagingEnabled)
- {
- return (currentPageIndex * pageSize);
- }
- return 0;
- }
- }
- public bool IsCustomPagingEnabled
- {
- get
- {
- return (IsPagingEnabled && allowCustomPaging);
- }
- }
- public bool IsFirstPage
- {
- get
- {
- return (!IsPagingEnabled || (CurrentPageIndex == 0));
- }
- }
- public bool IsLastPage
- {
- get
- {
- return (!IsPagingEnabled || (CurrentPageIndex == PageCount));
- }
- }
- public bool IsPagingEnabled
- {
- get
- {
- return (allowPaging && pageSize != 0);
- }
- }
- public bool IsReadOnly
- {
- get
- {
- return false;
- }
- }
- public bool IsSynchronized
- {
- get
- {
- return false;
- }
- }
- public int PageCount
- {
- get
- {
- if(dataSource != null)
- {
- int total = DataSourceCount;
- if(!IsPagingEnabled)
- {
- return total;
- }
- return (total + pageSize - 1)/pageSize;
- }
- return 0;
- }
- }
- public int PageSize
- {
- get
- {
- return pageSize;
- }
- set
- {
- pageSize = value;
- }
- }
- public object SyncRoot
- {
- get
- {
- return this;
- }
- }
- public int VirtualCount
- {
- get
- {
- return virtualCount;
- }
- set
- {
- virtualCount = value;
- }
- }
- public void CopyTo(Array array, int index)
- {
- foreach(object current in this)
- {
- array.SetValue(array, index++);
- }
- }
- public IEnumerator GetEnumerator()
- {
- int fInd = FirstIndexInPage;
- int count = -1;
- if(dataSource is ICollection)
- {
- count = Count;
- }
- if(dataSource is IList)
- {
- return (new PrivateListEnumerator((IList)dataSource, fInd, count));
- }
- if(dataSource is Array)
- {
- return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
- }
- if(dataSource is ICollection)
- {
- return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
- }
- if(allowCustomPaging)
- {
- return (new PrivateIEnumeratorEnumerator(dataSource.GetEnumerator(), Count));
- }
- return dataSource.GetEnumerator();
- }
- class PrivateIEnumeratorEnumerator : IEnumerator
- {
- private int index;
- private int max;
- private IEnumerator enumerator;
- public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
- {
- this.enumerator = enumerator;
- index = -1;
- max = count;
- }
- public bool MoveNext()
- {
- enumerator.MoveNext();
- index++;
- return (index < max);
- }
- public void Reset()
- {
- index = -1;
- enumerator.Reset();
- }
- public object Current
- {
- get
- {
- return enumerator.Current;
- }
- }
- }
- class PrivateICollectionEnumerator : IEnumerator
- {
- private int index;
- private int start;
- private int max;
- private ICollection collection;
- private IEnumerator collEnum;
- public PrivateICollectionEnumerator(ICollection collection, int start, int count)
- {
- this.collection = collection;
- this.start = start;
- index = -1;
- max = start + count;
- if(max > collection.Count)
- {
- max = collection.Count;
- }
- }
- public bool MoveNext()
- {
- if(collEnum == null)
- {
- int cIndex = 0;
- collEnum = collection.GetEnumerator();
- while(cIndex < start)
- {
- collEnum.MoveNext();
- }
- }
- collEnum.MoveNext();
- index++;
- return (start + index < max);
- }
- public void Reset()
- {
- index = -1;
- collEnum = null;
- }
- public object Current
- {
- get
- {
- return collEnum.Current;
- }
- }
- }
- class PrivateArrayEnumerator : IEnumerator
- {
- private int index;
- private int start;
- private int max;
- private object[] values;
- public PrivateArrayEnumerator(object[] values, int start, int count)
- {
- this.values = values;
- this.start = start;
- index = -1;
- max = start + count;
- if(max > this.values.Length)
- {
- max = this.values.Length;
- }
- }
- public bool MoveNext()
- {
- index++;
- return (index + start < max);
- }
- public void Reset()
- {
- index = -1;
- }
- public object Current
- {
- get
- {
- if(index >= 0)
- {
- return values[index + start];
- }
- throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
- }
- }
- }
- class PrivateListEnumerator : IEnumerator
- {
- private int index;
- private int start;
- private int max;
- private IList collection;
- public PrivateListEnumerator(IList list, int start, int count)
- {
- collection = list;
- this.start = start;
- index = -1;
- max = start + count;
- if(max > list.Count)
- {
- max = list.Count;
- }
- }
- public bool MoveNext()
- {
- index++;
- return (index + start < max);
- }
- public void Reset()
- {
- index = -1;
- }
- public object Current
- {
- get
- {
- if(index >= 0)
- {
- return collection[index + start];
- }
- throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
- }
- }
- }
- public string GetListName(PropertyDescriptor[] listAccessors)
- {
- return String.Empty;
- }
- public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
- {
- if(dataSource != null)
- {
- if(dataSource is ITypedList)
- {
- return ((ITypedList)dataSource).GetItemProperties(listAccessors);
- }
- }
- return null;
- }
- }
- }
|