View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.element;
17  
18  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
20  import org.kuali.rice.krad.uif.util.LifecycleElement;
21  
22  /**
23   * The NumberedPager widget is used to display a list of links horizontally in a page selection user interface.
24   * The user can select a page to jump to, go to prev/next page, or go to the first or last page.  This widget needs to
25   * know the numberOfPages total, and the currentPage the user is on currently, so this widget must be fed this
26   * information from the code.
27   *
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   * @see org.kuali.rice.krad.uif.layout.StackedLayoutManager
30   */
31  @BeanTag(name = "numberedPager", parent = "Uif-NumberedPager")
32  public class NumberedPager extends Pager {
33      private static final long serialVersionUID = -6495003633052595157L;
34  
35      private int maxNumberedLinksShown;
36      private boolean renderPrevNext;
37      private boolean renderFirstLast;
38  
39      protected int pagesStart;
40      protected int pagesEnd;
41  
42      private String firstText;
43      private String lastText;
44  
45      /**
46       * performFinalize calculates the pagesStart and pagesEnd properties (using numberOfPages, currentPage, and
47       * maxNumberedLinksShown - these must be set) which determines pages shown by the widget
48       *
49       * @param model the current model
50       * @param parent parent container
51       */
52      @Override
53      public void performFinalize(Object model, LifecycleElement parent) {
54          super.performFinalize(model, parent);
55  
56          if (maxNumberedLinksShown >= this.getNumberOfPages()) {
57              // Show all pages if possible to do so
58              pagesStart = 1;
59              pagesEnd = this.getNumberOfPages();
60          } else {
61              // Determine how many pages max shown before an after the current page
62              int beforeAfterShown = (int) Math.floor((double) maxNumberedLinksShown / 2.0);
63              pagesStart = this.getCurrentPage() - beforeAfterShown;
64              pagesEnd = this.getCurrentPage() + beforeAfterShown;
65  
66              // If maxNumberedLinksShown is even and cannot have an equal amount of pages showing before
67              // and after the current page, so trim one off the end
68              if (pagesEnd - pagesStart == maxNumberedLinksShown) {
69                  pagesEnd = pagesEnd - 1;
70              }
71  
72              // The pagesEnd is within range of numberOfPages total, therefore show the last pages
73              if (pagesEnd > this.getNumberOfPages()) {
74                  pagesEnd = this.getNumberOfPages();
75                  pagesStart = this.getNumberOfPages() - maxNumberedLinksShown + 1;
76              }
77  
78              // The pageStart is within range, therefore show the first pages
79              if (pagesStart < 1) {
80                  pagesStart = 1;
81                  if (maxNumberedLinksShown < this.getNumberOfPages()) {
82                      pagesEnd = maxNumberedLinksShown;
83                  }
84              }
85          }
86      }
87  
88      /**
89       * The maximum number of NUMBERED links shown at once for pages, if number of pages that exist exceed this value,
90       * the pager omits some pages before and/or after the current page (which are revealed during while
91       * navigating using a carousel effect)
92       *
93       * @return the maximum number of NUMBERED links to show
94       */
95      @BeanTagAttribute
96      public int getMaxNumberedLinksShown() {
97          return maxNumberedLinksShown;
98      }
99  
100     /**
101      * Set the maximum number of NUMBERED links shown
102      *
103      * @param maxNumberedLinksShown
104      */
105     public void setMaxNumberedLinksShown(int maxNumberedLinksShown) {
106         this.maxNumberedLinksShown = maxNumberedLinksShown;
107     }
108 
109     /**
110      * Returns true if this pager widget is rendering the "First" and "Last" links
111      *
112      * @return true if rendering "First" and "Last" links
113      */
114     @BeanTagAttribute
115     public boolean isRenderFirstLast() {
116         return renderFirstLast;
117     }
118 
119     /**
120      * Set renderFirstLast
121      *
122      * @param renderFirstLast
123      */
124     public void setRenderFirstLast(boolean renderFirstLast) {
125         this.renderFirstLast = renderFirstLast;
126     }
127 
128     /**
129      * Returns true if this pager widget is rendering the "Prev" and "Next" links
130      *
131      * @return true if rendering "First" and "Last" links
132      */
133     @BeanTagAttribute
134     public boolean isRenderPrevNext() {
135         return renderPrevNext;
136     }
137 
138     /**
139      * Set renderPrevNext
140      *
141      * @param renderPrevNext
142      */
143     public void setRenderPrevNext(boolean renderPrevNext) {
144         this.renderPrevNext = renderPrevNext;
145     }
146 
147     /**
148      * The first page number to render; this is set by the framework
149      *
150      * @return pages start
151      */
152     public int getPagesStart() {
153         return pagesStart;
154     }
155 
156     /**
157      * The last page number to render; this is set by the framework
158      *
159      * @return last page number to render
160      */
161     public int getPagesEnd() {
162         return pagesEnd;
163     }
164 
165     /**
166      * The text to use on the first link.
167      *
168      * @return the first link text
169      */
170     @BeanTagAttribute
171     public String getFirstText() {
172         return firstText;
173     }
174 
175     /**
176      * @see NumberedPager#getFirstText()
177      */
178     public void setFirstText(String firstText) {
179         this.firstText = firstText;
180     }
181 
182     /**
183      * The text to use for the last link.
184      *
185      * @return the last link text
186      */
187     @BeanTagAttribute
188     public String getLastText() {
189         return lastText;
190     }
191 
192     /**
193      * @see NumberedPager#getLastText()
194      */
195     public void setLastText(String lastText) {
196         this.lastText = lastText;
197     }
198 
199 }