* PagedDataSource.cs: If it's not a list or collection we don't
[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;
29 using System.Collections;
30 using System.ComponentModel;
31
32 namespace System.Web.UI.WebControls {
33         public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
34         {
35                 int page_size, current_page_index, virtual_count;
36                 bool allow_paging, allow_custom_paging;
37                 IEnumerable source;
38                 
39                 public PagedDataSource ()
40                 {
41                         page_size = 10;
42                         allow_paging = false;
43                         current_page_index = 0;
44                         allow_custom_paging = false;
45                         virtual_count = 0;
46                 }
47
48                 public bool AllowCustomPaging {
49                         get { return allow_custom_paging; }
50                         set { allow_custom_paging = value; }
51                 }
52
53                 public bool AllowPaging {
54                         get { return allow_paging; }
55                         set { allow_paging = value; }
56                 }
57
58                 public int Count {
59                         get {
60                                 if (DataSource == null)
61                                         return 0;
62                                 
63                                 if (IsPagingEnabled == true) {
64                                         if (IsCustomPagingEnabled == true || IsLastPage == false)
65                                                 return PageSize;
66
67                                         if (IsCustomPagingEnabled == false && IsLastPage == true)
68                                                 return FirstIndexInPage - DataSourceCount;
69                                 }
70
71                                 return DataSourceCount;
72                         }
73                 }                                               
74
75                 public int CurrentPageIndex {
76                         get { return current_page_index; }
77                         set { current_page_index = value; }
78                 }
79
80                 public IEnumerable DataSource {
81                         get { return source; }
82                         set { source = value; }
83                 }
84
85                 public int DataSourceCount {
86                         get {
87                                 if (source == null)
88                                         return 0;
89                                 
90                                 if (IsCustomPagingEnabled)
91                                         return virtual_count;
92
93                                 if (source is ICollection)
94                                         return ((ICollection) source).Count;
95
96                                 throw new HttpException ("The data source must implement ICollection");
97                         }
98                 }
99
100                 public int FirstIndexInPage {
101                         get {
102                                 if (!IsPagingEnabled || IsCustomPagingEnabled || DataSource == null)
103                                         return 0;
104                                 
105                                 return current_page_index * page_size;
106                         }
107                 }
108
109                 public bool IsCustomPagingEnabled {
110                         get { return IsPagingEnabled && AllowCustomPaging; }
111                 }
112
113                 public bool IsFirstPage {
114                         get { 
115                                 if (!AllowPaging)
116                                         return true;
117                                 
118                                 return CurrentPageIndex == 0; 
119                         }
120                 }
121
122                 public bool IsLastPage {
123                         get {
124                                 if (!AllowPaging)
125                                         return true;
126
127                                 if (PageCount == 0)
128                                         return false;
129
130                                 return CurrentPageIndex == PageCount - 1;
131                         }
132                 }
133
134                 public bool IsPagingEnabled {
135                         get { return AllowPaging && (PageSize != 0); }
136                 }
137
138                 public bool IsReadOnly {
139                         get { return false; } // as documented
140                 }
141
142                 public bool IsSynchronized {
143                         get { return false; } // as documented
144                 }
145
146                 public int PageCount {
147                         get {
148                                 if (DataSource == null)
149                                         return 0;
150
151                                 if (!IsPagingEnabled || DataSourceCount == 0 || PageSize == 0)
152                                         return 1;
153
154                                 return (DataSourceCount + PageSize - 1) / PageSize;
155                         }
156                 }
157                 
158                 public int PageSize {
159                         get { return page_size; }
160                         set { page_size = value; }
161                 }
162
163                 public object SyncRoot {
164                         get { return this; }
165                 }
166
167                 public int VirtualCount {
168                         get { return virtual_count; }
169                         set { virtual_count = value; }
170                 }
171 #if NET_2_0
172                 [MonoTODO]
173                 public bool AllowServerPaging {
174                         get {
175                                 throw new NotImplementedException ();
176                         }
177                         set {
178                                 throw new NotImplementedException ();
179                         }
180                 }
181
182                 [MonoTODO]
183                 public DataSourceSelectArguments DataSourceSelectArguments {
184                         get {
185                                 throw new NotImplementedException ();
186                         }
187                         set {
188                                 throw new NotImplementedException ();
189                         }
190                 }
191                 [MonoTODO]
192                 public DataSourceView DataSourceView {
193                         get {
194                                 throw new NotImplementedException ();
195                         }
196                         set {
197                                 throw new NotImplementedException ();
198                         }
199                 }
200
201                 [MonoTODO]
202                 public void SetItemCountFromPageIndex (int highestPageIndex)
203                 {
204                         throw new NotImplementedException ();
205                 }
206 #endif
207
208                 public void CopyTo (Array array, int index)
209                 {
210                         foreach (object o in source)
211                                 array.SetValue (o, index++);
212                 }
213
214                 public IEnumerator GetEnumerator ()
215                 {
216                         ICollection col = source as ICollection;
217                         if (col != null)
218                                 return GetEnumeratorEnum (col.GetEnumerator (),
219                                                 FirstIndexInPage, FirstIndexInPage + PageSize);
220
221                         IList list = source as IList;
222                         if (list != null)
223                                 return GetListEnum (list, FirstIndexInPage,
224                                                 FirstIndexInPage + PageSize);
225
226                         return source.GetEnumerator ();
227                 }
228
229                 public PropertyDescriptorCollection GetItemProperties (
230                         PropertyDescriptor [] list_accessors)
231                 {
232                         ITypedList typed = DataSource as ITypedList;
233                         if (typed == null)
234                                 return null;
235                         return typed.GetItemProperties (list_accessors);
236                 }
237
238                 public string GetListName (PropertyDescriptor [] list_accessors)
239                 {
240                         return String.Empty; // as documented
241                 }
242
243                 private IEnumerator GetListEnum (IList list, int start, int end)
244                 {
245                         if (!AllowPaging)
246                                 end = list.Count;
247                         for (int i = start; i < end; i++)
248                                 yield return list [i];
249                 }
250
251                 private IEnumerator GetEnumeratorEnum (IEnumerator e, int start, int end)
252                 {
253                         for (int i = 0; i < start; i++)
254                                 e.MoveNext ();
255                         for (int i = start; (!allow_paging || i < end) && e.MoveNext (); i++)
256                                 yield return e.Current;
257                 }
258         }
259 }