2010-02-18 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / PagedDataSource.cs
1 // System.Web.UI.WebControls.PagedDataSource.cs
2 //
3 // Author: Duncan Mak (duncan@novell.com)
4 //         Jackson Harper  (jackson@ximian.com) 
5 //
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
7 //
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:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
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.
26 //
27
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Security.Permissions;
31
32 namespace System.Web.UI.WebControls {
33
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
37         {
38                 int page_size, current_page_index, virtual_count;
39                 bool allow_paging, allow_custom_paging;
40                 IEnumerable source;
41                 
42 #if NET_2_0
43                 bool allow_server_paging;
44 #endif
45                 
46                 public PagedDataSource ()
47                 {
48                         page_size = 10;
49                 }
50
51                 public bool AllowCustomPaging {
52                         get { return allow_custom_paging; }
53                         set { 
54                                 allow_custom_paging = value; 
55 #if NET_2_0
56                                 // AllowCustomPaging and AllowServerPaging are mutually exclusive
57                                 if (allow_custom_paging)
58                                         allow_server_paging = false;
59 #endif
60                         }
61                 }
62
63                 public bool AllowPaging {
64                         get { return allow_paging; }
65                         set { allow_paging = value; }
66                 }
67
68                 public int Count {
69                         get {
70                                 if (source == null)
71                                         return 0;
72                                 
73                                 if (IsPagingEnabled) {
74                                         if (IsCustomPagingEnabled || !IsLastPage)
75                                                 return page_size;
76                                         return DataSourceCount - FirstIndexInPage;
77                                 }
78
79                                 return DataSourceCount;
80                         }
81                 }                                               
82
83                 public int CurrentPageIndex {
84                         get { return current_page_index; }
85                         set { current_page_index = value; }
86                 }
87
88                 public IEnumerable DataSource {
89                         get { return source; }
90                         set { source = value; }
91                 }
92
93                 public int DataSourceCount {
94                         get {
95                                 if (source == null)
96                                         return 0;
97                                 
98                                 if (IsCustomPagingEnabled 
99 #if NET_2_0
100                                     || IsServerPagingEnabled
101 #endif
102                                 )
103                                         return virtual_count;
104
105                                 if (source is ICollection)
106                                         return ((ICollection) source).Count;
107
108                                 throw new HttpException ("The data source must implement ICollection");
109                         }
110                 }
111
112                 public int FirstIndexInPage {
113                         get {
114                                 if (!IsPagingEnabled || IsCustomPagingEnabled || 
115 #if NET_2_0
116                                     IsServerPagingEnabled || 
117 #endif
118                                     source == null)
119                                         return 0;
120                                 
121                                 return current_page_index * page_size;
122                         }
123                 }
124
125                 public bool IsCustomPagingEnabled {
126                         get { return IsPagingEnabled && allow_custom_paging; }
127                 }
128
129 #if NET_2_0
130                 public bool IsServerPagingEnabled {
131                         get { return IsPagingEnabled && allow_server_paging; }
132                 }
133 #endif
134
135                 public bool IsFirstPage {
136                         get { 
137                                 if (!allow_paging)
138                                         return true;
139                                 
140                                 return current_page_index == 0; 
141                         }
142                 }
143
144                 public bool IsLastPage {
145                         get {
146                                 if (!allow_paging || page_size == 0)
147                                         return true;
148
149                                 return  (current_page_index == (PageCount - 1));
150                         }
151                 }
152
153                 public bool IsPagingEnabled {
154                         get { return (allow_paging && page_size != 0); }
155                 }
156
157                 public bool IsReadOnly {
158                         get { return false; } // as documented
159                 }
160
161                 public bool IsSynchronized {
162                         get { return false; } // as documented
163                 }
164
165                 public int PageCount {
166                         get {
167                                 if (source == null)
168                                         return 0;
169                                 
170                                 if (!IsPagingEnabled || DataSourceCount == 0 || page_size == 0)
171                                         return 1;
172                                 
173                                 return (DataSourceCount + page_size - 1) / page_size;
174                         }
175                 }
176                 
177                 public int PageSize {
178                         get { return page_size; }
179                         set { page_size = value; }
180                 }
181
182                 public object SyncRoot {
183                         get { return this; }
184                 }
185
186                 public int VirtualCount {
187                         get { return virtual_count; }
188                         set { virtual_count = value; }
189                 }
190 #if NET_2_0
191                 public bool AllowServerPaging {
192                         get {
193                                 return allow_server_paging;
194                         }
195                         set {
196                                 allow_server_paging = value;
197                                 // AllowCustomPaging and AllowServerPaging are mutually exclusive
198                                 if (allow_server_paging)
199                                         allow_custom_paging = false;
200                         }
201                 }
202 #endif
203
204                 public void CopyTo (Array array, int index)
205                 {
206                         foreach (object o in source)
207                                 array.SetValue (o, index++);
208                 }
209
210                 public IEnumerator GetEnumerator ()
211                 {
212                         // IList goes first, as it implements ICollection
213                         IList list = source as IList;
214                         int first = 0;
215                         int count;
216                         int limit;
217                         if (list != null) {
218                                 first = FirstIndexInPage;
219                                 count = ((ICollection) source).Count;
220                                 limit = ((first + page_size) > count) ? (count - first) : page_size;
221                                 return GetListEnum (list, first, first + limit);
222                         }
223
224                         ICollection col = source as ICollection;
225                         if (col != null) {
226                                 first = FirstIndexInPage;
227                                 count = col.Count;
228                                 limit = ((first + page_size) > count) ? (count - first) : page_size;
229                                 return GetEnumeratorEnum (col.GetEnumerator (), first, first + page_size);
230                         }
231
232                         return source.GetEnumerator ();
233                 }
234
235                 public PropertyDescriptorCollection GetItemProperties (PropertyDescriptor [] list_accessors)
236                 {
237                         ITypedList typed = source as ITypedList;
238                         if (typed == null)
239                                 return null;
240                         return typed.GetItemProperties (list_accessors);
241                 }
242
243                 public string GetListName (PropertyDescriptor [] list_accessors)
244                 {
245                         return String.Empty; // as documented
246                 }
247
248                 IEnumerator GetListEnum (IList list, int start, int end)
249                 {
250                         if (!AllowPaging)
251                                 end = list.Count;
252                         else if (start >= list.Count)
253                                 yield break;
254                         
255                         for (int i = start; i < end; i++)
256                                 yield return list [i];
257                 }
258
259                 IEnumerator GetEnumeratorEnum (IEnumerator e, int start, int end)
260                 {
261                         for (int i = 0; i < start; i++)
262                                 e.MoveNext ();
263                         for (int i = start; (!allow_paging || i < end) && e.MoveNext (); i++)
264                                 yield return e.Current;
265                 }
266         }
267 }
268