1 // System.Web.UI.WebControls.PagedDataSource.cs
3 // Author: Duncan Mak (duncan@novell.com)
4 // Jackson Harper (jackson@ximian.com)
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Security.Permissions;
32 namespace System.Web.UI.WebControls {
34 // CAS - no inheritance demand required because the class is sealed
35 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36 public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
38 int page_size, current_page_index, virtual_count;
39 bool allow_paging, allow_custom_paging, allow_server_paging;
42 public PagedDataSource ()
47 public bool AllowCustomPaging {
48 get { return allow_custom_paging; }
50 allow_custom_paging = value;
52 // AllowCustomPaging and AllowServerPaging are mutually exclusive
53 if (allow_custom_paging)
54 allow_server_paging = false;
59 public bool AllowPaging {
60 get { return allow_paging; }
61 set { allow_paging = value; }
69 if (IsPagingEnabled) {
70 if (IsCustomPagingEnabled || !IsLastPage)
72 return DataSourceCount - FirstIndexInPage;
75 return DataSourceCount;
79 public int CurrentPageIndex {
80 get { return current_page_index; }
81 set { current_page_index = value; }
84 public IEnumerable DataSource {
85 get { return source; }
86 set { source = value; }
89 public int DataSourceCount {
94 if (IsCustomPagingEnabled
96 || IsServerPagingEnabled
101 if (source is ICollection)
102 return ((ICollection) source).Count;
104 throw new HttpException ("The data source must implement ICollection");
108 public int FirstIndexInPage {
110 if (!IsPagingEnabled || IsCustomPagingEnabled ||
112 IsServerPagingEnabled ||
117 return current_page_index * page_size;
121 public bool IsCustomPagingEnabled {
122 get { return IsPagingEnabled && allow_custom_paging; }
126 public bool IsServerPagingEnabled {
127 get { return IsPagingEnabled && allow_server_paging; }
131 public bool IsFirstPage {
136 return current_page_index == 0;
140 public bool IsLastPage {
142 if (!allow_paging || page_size == 0)
145 return (current_page_index == (PageCount - 1));
149 public bool IsPagingEnabled {
150 get { return (allow_paging && page_size != 0); }
153 public bool IsReadOnly {
154 get { return false; } // as documented
157 public bool IsSynchronized {
158 get { return false; } // as documented
161 public int PageCount {
166 if (!IsPagingEnabled || DataSourceCount == 0 || page_size == 0)
169 return (DataSourceCount + page_size - 1) / page_size;
173 public int PageSize {
174 get { return page_size; }
175 set { page_size = value; }
178 public object SyncRoot {
182 public int VirtualCount {
183 get { return virtual_count; }
184 set { virtual_count = value; }
187 public bool AllowServerPaging {
189 return allow_server_paging;
192 allow_server_paging = value;
193 // AllowCustomPaging and AllowServerPaging are mutually exclusive
194 if (allow_server_paging)
195 allow_custom_paging = false;
200 public void CopyTo (Array array, int index)
202 foreach (object o in source)
203 array.SetValue (o, index++);
206 public IEnumerator GetEnumerator ()
208 // IList goes first, as it implements ICollection
209 IList list = source as IList;
214 first = FirstIndexInPage;
215 count = ((ICollection) source).Count;
216 limit = ((first + page_size) > count) ? (count - first) : page_size;
217 return GetListEnum (list, first, first + limit);
220 ICollection col = source as ICollection;
222 first = FirstIndexInPage;
224 limit = ((first + page_size) > count) ? (count - first) : page_size;
225 return GetEnumeratorEnum (col.GetEnumerator (), first, first + page_size);
228 return source.GetEnumerator ();
231 public PropertyDescriptorCollection GetItemProperties (PropertyDescriptor [] list_accessors)
233 ITypedList typed = source as ITypedList;
236 return typed.GetItemProperties (list_accessors);
239 public string GetListName (PropertyDescriptor [] list_accessors)
241 return String.Empty; // as documented
245 internal class ListEnum : IEnumerator
252 internal ListEnum(IList list, int start, int end)
257 this.ind = start - 1;
260 public bool MoveNext()
266 public void Reset() { ind = start - 1; }
267 public object Current { get { return list[ind]; }}
270 private IEnumerator GetListEnum (IList list, int start, int end)
274 return new ListEnum(list, start, end);
277 internal class EnumeratorEnum : IEnumerator
283 PagedDataSource parent;
285 internal EnumeratorEnum(PagedDataSource parent, IEnumerator en, int start, int end)
287 this.parent = parent;
291 this.ind = start - 1;
292 for (int i = 0; i < start; i++)
296 public bool MoveNext()
299 return (!parent.allow_paging || ind < end) && en.MoveNext ();
304 throw new NotSupportedException();
307 public object Current { get { return en.Current; }}
310 private IEnumerator GetEnumeratorEnum (IEnumerator e, int start, int end)
312 return new EnumeratorEnum(this, e, start, end);
315 private IEnumerator GetListEnum (IList list, int start, int end)
319 else if (start >= list.Count)
322 for (int i = start; i < end; i++)
323 yield return list [i];
326 private IEnumerator GetEnumeratorEnum (IEnumerator e, int start, int end)
328 for (int i = 0; i < start; i++)
330 for (int i = start; (!allow_paging || i < end) && e.MoveNext (); i++)
331 yield return e.Current;