2009-02-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web.Extensions / System.Web.UI.WebControls / NextPreviousPagerField.cs
1 //
2 // System.Web.UI.WebControls.NextPreviousPagerField
3 //
4 // Authors:
5 //   Marek Habersack (mhabersack@novell.com)
6 //
7 // (C) 2007-2008 Novell, Inc
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 #if NET_3_5
31 using System;
32 using System.ComponentModel;
33 using System.Security.Permissions;
34 using System.Web;
35 using System.Web.UI;
36
37 namespace System.Web.UI.WebControls
38 {
39         [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         public class NextPreviousPagerField : DataPagerField
42         {
43                 int _startRowIndex;
44                 int _maximumRows;
45                 int _totalRowCount;
46                 int _fieldIndex;
47                 
48                 public NextPreviousPagerField ()
49                 {
50                 }
51
52                 protected override void CopyProperties (DataPagerField newField)
53                 {
54                         base.CopyProperties (newField);
55
56                         NextPreviousPagerField field = newField as NextPreviousPagerField;
57                         if (field == null)
58                                 return;
59                         
60                         field.ButtonCssClass = ButtonCssClass;
61                         field.ButtonType = ButtonType;
62                         field.FirstPageImageUrl = FirstPageImageUrl;
63                         field.FirstPageText = FirstPageText;
64                         field.LastPageImageUrl = LastPageImageUrl;
65                         field.LastPageText = LastPageText;
66                         field.NextPageImageUrl = NextPageImageUrl;
67                         field.NextPageText = NextPageText;
68                         field.PreviousPageImageUrl = PreviousPageImageUrl;
69                         field.PreviousPageText = PreviousPageText;
70                         field.ShowFirstPageButton = ShowFirstPageButton;
71                         field.ShowLastPageButton = ShowLastPageButton;
72                         field.ShowNextPageButton = ShowNextPageButton;
73                         field.ShowPreviousPageButton = ShowPreviousPageButton;
74                 }
75
76                 // What's the fieldIndex used for?
77                 public override void CreateDataPagers (DataPagerFieldItem container, int startRowIndex, int maximumRows, int totalRowCount, int fieldIndex)
78                 {
79                         _startRowIndex = startRowIndex;
80                         _maximumRows = maximumRows;
81                         _totalRowCount = totalRowCount;
82                         _fieldIndex = fieldIndex;
83
84                         bool setPagePropertiesNeeded = false;
85                         bool queryMode = GetQueryModeStartRowIndex (_totalRowCount, _maximumRows, ref _startRowIndex, ref setPagePropertiesNeeded);
86                         bool enablePrevFirst = _startRowIndex >= _maximumRows;
87                         bool enableNextLast = (_startRowIndex + _maximumRows) < _totalRowCount;
88                         bool addNonBreakingSpace = RenderNonBreakingSpacesBetweenControls;
89
90                         if (ShowFirstPageButton)
91                                 CreateButton (container, DataControlCommands.FirstPageCommandArgument, FirstPageText, FirstPageImageUrl, 0,
92                                               queryMode, enablePrevFirst, addNonBreakingSpace);
93                         
94                         int newPageNum = -1;
95                         if (ShowPreviousPageButton) {
96                                 if (queryMode)
97                                         newPageNum = (_startRowIndex / _maximumRows) - 1;
98                                 
99                                 CreateButton (container, DataControlCommands.PreviousPageCommandArgument, PreviousPageText, PreviousPageImageUrl, newPageNum,
100                                               queryMode, enablePrevFirst, addNonBreakingSpace);
101                         }
102                         
103                         if (ShowNextPageButton) {
104                                 if (queryMode)
105                                         newPageNum = (_startRowIndex + _maximumRows) / _maximumRows;
106                                 
107                                 CreateButton (container, DataControlCommands.NextPageCommandArgument, NextPageText, NextPageImageUrl, newPageNum,
108                                               queryMode, enableNextLast, addNonBreakingSpace);
109                         }
110                         
111                         if (ShowLastPageButton) {
112                                 if (queryMode) {
113                                         newPageNum = _totalRowCount / _maximumRows;
114                                         if ((_totalRowCount % _maximumRows) == 0)
115                                                 newPageNum--;
116                                 }
117                                 
118                                 CreateButton (container, DataControlCommands.LastPageCommandArgument, LastPageText, LastPageImageUrl, newPageNum,
119                                               queryMode, enableNextLast, addNonBreakingSpace);
120                         }
121                         
122                         if (setPagePropertiesNeeded)
123                                 DataPager.SetPageProperties (_startRowIndex, _maximumRows, true);
124                 }
125
126                 void CreateButton (DataPagerFieldItem container, string commandName, string text, string imageUrl, int pageNum,
127                                    bool queryMode, bool enabled, bool addNonBreakingSpace)
128                 {
129                         WebControl ctl = null;
130                         
131                         if (queryMode) {
132                                 pageNum++;
133                                 HyperLink h = new HyperLink ();
134                                 h.Text = text;
135                                 h.ImageUrl = imageUrl;
136                                 h.Enabled = enabled;
137                                 h.NavigateUrl = GetQueryStringNavigateUrl (pageNum);
138                                 h.CssClass = ButtonCssClass;
139                                 ctl = h;
140                         } else {
141                                 if (!enabled && RenderDisabledButtonsAsLabels) {
142                                         Label l = new Label ();
143                                         l.Text = text;
144                                         ctl = l;
145                                 } else {
146                                         switch (ButtonType) {
147                                                 case ButtonType.Button:
148                                                         Button btn = new Button ();
149                                                         btn.CommandName = commandName;
150                                                         btn.Text = text;
151                                                         ctl = btn;
152                                                         break;
153
154                                                 case ButtonType.Link:
155                                                         LinkButton lbtn = new LinkButton ();
156                                                         lbtn.CommandName = commandName;
157                                                         lbtn.Text = text;
158                                                         ctl = lbtn;
159                                                         break;
160
161                                                 case ButtonType.Image:
162                                                         ImageButton ibtn = new ImageButton ();
163                                                         ibtn.CommandName = commandName;
164                                                         ibtn.ImageUrl = imageUrl;
165                                                         ibtn.AlternateText = text;
166                                                         ctl = ibtn;
167                                                         break;
168                                         }
169
170                                         if (ctl != null) {
171                                                 ctl.Enabled = enabled;
172                                                 ctl.CssClass = ButtonCssClass;
173                                         }
174                                 }
175                         }
176
177                         if (ctl != null) {
178                                 container.Controls.Add (ctl);
179                                 if (addNonBreakingSpace)
180                                         container.Controls.Add (new LiteralControl ("&nbsp;"));
181                         }
182                 }
183                 
184                 protected override DataPagerField CreateField ()
185                 {
186                         return new NextPreviousPagerField ();
187                 }
188
189                 public override bool Equals (object o)
190                 {
191                         NextPreviousPagerField field = o as NextPreviousPagerField;
192                         if (field == null)
193                                 return false;
194                         
195                         // Compare using the properties that are copied in CopyProperties
196                         if (field.ButtonCssClass != ButtonCssClass)
197                                 return false;
198
199                         if (field.ButtonType != ButtonType)
200                                 return false;
201
202                         if (field.FirstPageImageUrl != FirstPageImageUrl)
203                                 return false;
204                         
205                         if (field.FirstPageText != FirstPageText)
206                                 return false;
207                         
208                         if (field.LastPageImageUrl != LastPageImageUrl)
209                                 return false;
210                         
211                         if (field.LastPageText != LastPageText)
212                                 return false;
213                         
214                         if (field.NextPageImageUrl != NextPageImageUrl)
215                                 return false;
216                         
217                         if (field.NextPageText != NextPageText)
218                                 return false;
219                         
220                         if (field.PreviousPageImageUrl != PreviousPageImageUrl)
221                                 return false;
222                         
223                         if (field.PreviousPageText != PreviousPageText)
224                                 return false;
225                         
226                         if (field.ShowFirstPageButton != ShowFirstPageButton)
227                                 return false;
228                         
229                         if (field.ShowLastPageButton != ShowLastPageButton)
230                                 return false;
231                         
232                         if (field.ShowNextPageButton != ShowNextPageButton)
233                                 return false;
234                         
235                         if (field.ShowPreviousPageButton != ShowPreviousPageButton)
236                                 return false;
237
238                         return true;
239                 }
240
241                 public override int GetHashCode ()
242                 {
243                         int ret = 0;
244
245                         // Base the calculation on the properties that are copied in CopyProperties
246                         ret |= ButtonCssClass.GetHashCode ();
247                         ret |= ButtonType.GetHashCode ();
248                         ret |= FirstPageImageUrl.GetHashCode ();
249                         ret |= FirstPageText.GetHashCode ();
250                         ret |= LastPageImageUrl.GetHashCode ();
251                         ret |= LastPageText.GetHashCode ();
252                         ret |= NextPageImageUrl.GetHashCode ();
253                         ret |= NextPageText.GetHashCode ();
254                         ret |= PreviousPageImageUrl.GetHashCode ();
255                         ret |= PreviousPageText.GetHashCode ();
256                         ret |= ShowFirstPageButton.GetHashCode ();
257                         ret |= ShowLastPageButton.GetHashCode ();
258                         ret |= ShowNextPageButton.GetHashCode ();
259                         ret |= ShowPreviousPageButton.GetHashCode ();
260
261                         return ret;
262                 }
263
264                 public override void HandleEvent (CommandEventArgs e)
265                 {
266                         string commandName = e.CommandName;
267                         int newStartIndex = -1;
268                         int pageSize = DataPager.PageSize;
269                         
270                         if (String.Compare (commandName, DataControlCommands.FirstPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0)
271                                 newStartIndex = 0;
272                         else if (String.Compare (commandName, DataControlCommands.LastPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
273                                 int lastPageMod = _totalRowCount % pageSize;
274                                 if (lastPageMod == 0)
275                                         newStartIndex = _totalRowCount - pageSize;
276                                 else
277                                         newStartIndex = _totalRowCount - lastPageMod;
278                         } else if (String.Compare (commandName, DataControlCommands.NextPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
279                                 newStartIndex = _startRowIndex + pageSize;
280                                 if (newStartIndex > _totalRowCount)
281                                         newStartIndex = _totalRowCount - pageSize;
282                         } else if (String.Compare (commandName, DataControlCommands.PreviousPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
283                                 newStartIndex = _startRowIndex - pageSize;
284                                 if (newStartIndex < 0)
285                                         newStartIndex = 0;
286                         }
287
288                         if (newStartIndex != -1)
289                                 DataPager.SetPageProperties (newStartIndex, pageSize, true);
290                 }
291
292                 public string ButtonCssClass {
293                         get {
294                                 string s = ViewState ["ButtonCssClass"] as string;
295                                 if (s != null)
296                                         return s;
297
298                                 return String.Empty;
299                         }
300                         
301                         set { ViewState ["ButtonCssClass"] = value; }
302                 }
303
304                 public ButtonType ButtonType {
305                         get {
306                                 object o = ViewState ["ButtonType"];
307                                 if (o != null)
308                                         return (ButtonType) o;
309
310                                 return ButtonType.Link;
311                         }
312                         
313                         set { ViewState ["ButtonType"] = value; }
314                 }
315
316                 public string FirstPageImageUrl {
317                         get {
318                                 string s = ViewState ["FirstPageImageUrl"] as string;
319                                 if (s != null)
320                                         return s;
321
322                                 return String.Empty;
323                         }
324                         
325                         set { ViewState ["FirstPageImageUrl"] = value; }
326                 }
327
328                 public string FirstPageText {
329                         get {
330                                 string s = ViewState ["FirstPageText"] as string;
331                                 if (s != null)
332                                         return s;
333
334                                 return "First";
335                         }
336                         
337                         set { ViewState ["FirstPageText"] = value; }
338                 }
339
340                 public string LastPageImageUrl {
341                         get {
342                                 string s = ViewState ["LastPageImageUrl"] as string;
343                                 if (s != null)
344                                         return s;
345
346                                 return String.Empty;
347                         }
348                         
349                         set { ViewState ["LastPageImageUrl"] = value; }
350                 }
351
352                 public string LastPageText {
353                         get {
354                                 string s = ViewState ["LastPageText"] as string;
355                                 if (s != null)
356                                         return s;
357
358                                 return "Last";
359                         }
360                         
361                         set { ViewState ["LastPageText"] = value; }
362                 }
363
364                 public string NextPageImageUrl {
365                         get {
366                                 string s = ViewState ["NextPageImageUrl"] as string;
367                                 if (s != null)
368                                         return s;
369
370                                 return String.Empty;
371                         }
372                         
373                         set { ViewState ["NextPageImageUrl"] = value; }
374                 }
375
376                 public string NextPageText {
377                         get {
378                                 string s = ViewState ["NextPageText"] as string;
379                                 if (s != null)
380                                         return s;
381
382                                 return "Next";
383                         }
384                         
385                         set { ViewState ["NextPageText"] = value; }
386                 }
387
388                 public string PreviousPageImageUrl {
389                         get {
390                                 string s = ViewState ["PreviousPageImageUrl"] as string;
391                                 if (s != null)
392                                         return s;
393
394                                 return String.Empty;
395                         }
396                         
397                         set { ViewState ["PreviousPageImageUrl"] = value; }
398                 }
399
400                 public string PreviousPageText {
401                         get {
402                                 string s = ViewState ["PreviousPageText"] as string;
403                                 if (s != null)
404                                         return s;
405
406                                 return "Previous";
407                         }
408                         
409                         set { ViewState ["PreviousPageText"] = value; }
410                 }
411
412                 public bool RenderDisabledButtonsAsLabels {
413                         get {
414                                 object o = ViewState ["RenderDisabledButtonsAsLabels"];
415                                 if (o != null)
416                                         return (bool) o;
417
418                                 return false;
419                         }
420                         
421                         set { ViewState ["RenderDisabledButtonsAsLabels"] = value; }
422                 }
423
424                 public bool RenderNonBreakingSpacesBetweenControls {
425                         get {
426                                 object o = ViewState ["RenderNonBreakingSpacesBetweenControls"];
427                                 if (o != null)
428                                         return (bool) o;
429
430                                 return true;
431                         }
432                         
433                         set { ViewState ["RenderNonBreakingSpacesBetweenControls"] = value; }
434                 }
435
436                 public bool ShowFirstPageButton {
437                         get {
438                                 object o = ViewState ["ShowFirstPageButton"];
439                                 if (o != null)
440                                         return (bool) o;
441
442                                 return false;
443                         }
444                         
445                         set { ViewState ["ShowFirstPageButton"] = value; }
446                 }
447
448                 public bool ShowLastPageButton {
449                         get {
450                                 object o = ViewState ["ShowLastPageButton"];
451                                 if (o != null)
452                                         return (bool) o;
453
454                                 return false;
455                         }
456                         
457                         set { ViewState ["ShowLastPageButton"] = value; }
458                 }
459
460                 public bool ShowNextPageButton {
461                         get {
462                                 object o = ViewState ["ShowNextPageButton"];
463                                 if (o != null)
464                                         return (bool) o;
465
466                                 return true;
467                         }
468                         
469                         set { ViewState ["ShowNextPageButton"] = value; }
470                 }
471
472                 public bool ShowPreviousPageButton {
473                         get {
474                                 object o = ViewState ["ShowPreviousPageButton"];
475                                 if (o != null)
476                                         return (bool) o;
477
478                                 return true;
479                         }
480                         
481                         set { ViewState ["ShowPreviousPageButton"] = value; }
482                 }
483         }
484 }
485 #endif