1 /**
2 * Copyright 2005-2014 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.widget;
17
18 import org.kuali.rice.krad.uif.component.Component;
19 import org.kuali.rice.krad.uif.util.LifecycleElement;
20
21 /**
22 * The Pager widget is used to display a list of links horizontally in a page selection user interface. The user can
23 * select a page to jump to, go to prev/next page, or go to the first or last page. This widget needs to know
24 * the numberOfPages total, and the currentPage the user is on currently, so this widget must be fed this information
25 * from the code.
26 *
27 * @author Kuali Rice Team (rice.collab@kuali.org)
28 * @see org.kuali.rice.krad.uif.layout.StackedLayoutManager
29 */
30 public class Pager extends WidgetBase {
31 private String linkScript;
32 private int maxNumberedLinksShown;
33 private int numberOfPages;
34 private int currentPage;
35 private boolean renderPrevNext;
36 private boolean renderFirstLast;
37
38 protected int pagesStart;
39 protected int pagesEnd;
40
41 public Pager() {
42 super();
43 }
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 no pages or 1 page, do not render
57 if (numberOfPages == 0 || numberOfPages == 1) {
58 this.setRender(false);
59 }
60
61 if (maxNumberedLinksShown >= numberOfPages) {
62 // Show all pages if possible to do so
63 pagesStart = 1;
64 pagesEnd = numberOfPages;
65 } else {
66 // Determine how many pages max shown before an after the current page
67 int beforeAfterShown = (int) Math.floor((double) maxNumberedLinksShown / 2.0);
68 pagesStart = currentPage - beforeAfterShown;
69 pagesEnd = currentPage + beforeAfterShown;
70
71 // If maxNumberedLinksShown is even and cannot have an equal amount of pages showing before
72 // and after the current page, so trim one off the end
73 if (pagesEnd - pagesStart == maxNumberedLinksShown) {
74 pagesEnd = pagesEnd - 1;
75 }
76
77 // The pagesEnd is within range of numberOfPages total, therefore show the last pages
78 if (pagesEnd > numberOfPages) {
79 pagesEnd = numberOfPages;
80 pagesStart = numberOfPages - maxNumberedLinksShown + 1;
81 }
82
83 // The pageStart is within range, therefore show the first pages
84 if (pagesStart < 1) {
85 pagesStart = 1;
86 if (maxNumberedLinksShown < numberOfPages) {
87 pagesEnd = maxNumberedLinksShown;
88 }
89 }
90 }
91
92 this.linkScript = "e.preventDefault();" + this.linkScript;
93 }
94
95 /**
96 * The script to execute when a link is clicked (should probably use the "this" var in most cases, to determine
97 * page number selected - see retrieveStackedPage(linkElement, collectionId) js function)
98 *
99 * @return the script to execute when a link is clicked
100 */
101 public String getLinkScript() {
102 return linkScript;
103 }
104
105 /**
106 * Set the link js script
107 *
108 * @param linkScript the link js script
109 */
110 public void setLinkScript(String linkScript) {
111 this.linkScript = linkScript;
112 }
113
114 /**
115 * The maximum number of NUMBERED links shown at once for pages, if number of pages that exist exceed this value,
116 * the pager omits some pages before and/or after the current page (which are revealed during while
117 * navigating using a carousel effect)
118 *
119 * @return the maximum number of NUMBERED links to show
120 */
121 public int getMaxNumberedLinksShown() {
122 return maxNumberedLinksShown;
123 }
124
125 /**
126 * Set the maximum number of NUMBERED links shown
127 *
128 * @param maxNumberedLinksShown
129 */
130 public void setMaxNumberedLinksShown(int maxNumberedLinksShown) {
131 this.maxNumberedLinksShown = maxNumberedLinksShown;
132 }
133
134 /**
135 * Number of pages TOTAL that make up the component being paged (this must be set by the framework based on some
136 * list size)
137 *
138 * @return the number of pages used in this pager
139 */
140 public int getNumberOfPages() {
141 return numberOfPages;
142 }
143
144 /**
145 * Set the TOTAL number of pages
146 *
147 * @param numberOfPages
148 */
149 public void setNumberOfPages(int numberOfPages) {
150 this.numberOfPages = numberOfPages;
151 }
152
153 /**
154 * The current page being shown by this pager widget (this must be set when the page is changed)
155 *
156 * @return the current page being shown
157 */
158 public int getCurrentPage() {
159 return currentPage;
160 }
161
162 /**
163 * Set the current page
164 *
165 * @param currentPage
166 */
167 public void setCurrentPage(int currentPage) {
168 this.currentPage = currentPage;
169 }
170
171 /**
172 * Returns true if this pager widget is rendering the "First" and "Last" links
173 *
174 * @return true if rendering "First" and "Last" links
175 */
176 public boolean isRenderFirstLast() {
177 return renderFirstLast;
178 }
179
180 /**
181 * Set renderFirstLast
182 *
183 * @param renderFirstLast
184 */
185 public void setRenderFirstLast(boolean renderFirstLast) {
186 this.renderFirstLast = renderFirstLast;
187 }
188
189 /**
190 * Returns true if this pager widget is rendering the "Prev" and "Next" links
191 *
192 * @return true if rendering "First" and "Last" links
193 */
194 public boolean isRenderPrevNext() {
195 return renderPrevNext;
196 }
197
198 /**
199 * Set renderPrevNext
200 *
201 * @param renderPrevNext
202 */
203 public void setRenderPrevNext(boolean renderPrevNext) {
204 this.renderPrevNext = renderPrevNext;
205 }
206
207 /**
208 * The first page number to render; this is set by the framework
209 *
210 * @return pages start
211 */
212 public int getPagesStart() {
213 return pagesStart;
214 }
215
216 /**
217 * The last page number to render; this is set by the framework
218 *
219 * @return last page number to render
220 */
221 public int getPagesEnd() {
222 return pagesEnd;
223 }
224 }