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.kns.util;
17
18 /**
19 * This class provides utilities to support the rendering of tables in Kuali without using display tag.
20 *
21 * Normally, displaytag handles the rendering of Kuali tables on various screens, but
22 * there are situations where displaytag is inadequate for the task (e.g. multiple value lookups).
23 * In particular, display tag does not handle POSTing of forms when switching between pages and sorting.
24 *
25 * @deprecated Only used in KNS classes, use KRAD.
26 */
27 @Deprecated
28 public final class TableRenderUtil {
29
30 private TableRenderUtil() {
31 throw new UnsupportedOperationException("do not call");
32 }
33
34 /**
35 * Returns the minimum number of pages needed to display a result set of the given page
36 *
37 * @param resultSize number of results
38 * @param maxRowsPerPage maximum number of rows
39 *
40 * @return
41 */
42 public static int computeTotalNumberOfPages(int resultSize, int maxRowsPerPage) {
43 int numPages = resultSize / maxRowsPerPage;
44 if (resultSize % maxRowsPerPage != 0) {
45 // partial page
46 numPages++;
47 }
48 return numPages;
49 }
50
51 /**
52 * This method computes the list index of the first row of the given page
53 *
54 * @param pageNumber first page is index 0
55 * @param resultSize the size of the list being rendered
56 * @param maxRowsPerPage max number of rows on a page
57 * @return the index in the result list of the first row of the given page
58 */
59 public static int computeStartIndexForPage(int pageNumber, int resultSize, int maxRowsPerPage) {
60 if (pageNumber < 0 && pageNumber >= computeTotalNumberOfPages(resultSize, maxRowsPerPage)) {
61 return -1;
62 }
63 return pageNumber * maxRowsPerPage;
64 }
65
66 /**
67 * This method computes the index of the last row of the given page
68 *
69 * @param pageNumber first page is index 0
70 * @param resultSize the size of the list being rendered
71 * @param maxRowsPerPage max number of rows on a page
72 * @return the index in the result list of the last row of the given page
73 */
74 public static int computeLastIndexForPage(int pageNumber, int resultSize, int maxRowsPerPage) {
75 int startIndex = computeStartIndexForPage(pageNumber, resultSize, maxRowsPerPage);
76 if (startIndex == -1) {
77 return -1;
78 }
79 if (startIndex + maxRowsPerPage - 1 < resultSize) {
80 return startIndex + maxRowsPerPage - 1;
81 }
82 // partial page
83 return resultSize - 1;
84 }
85 }