Tuesday, March 10, 2009

Paging Internals

If you have a task to create custom paging by yourself then this post could be helpful for you.

This Paging Demo web project is based on MVC model 1.
Specifications: Servlet 2.5 / JSP 2.1 / JSTL 1.2.
It could be tested with Tomcat 6.0.x and it could be built with Maven 2.

Download project binary and sources.

Paging design:

Class diagram:

Screens: This paging is based on a java class PagingCalc (paging calculator). It could do most calculations required by paging. The class follows Immutable Design Pattern. Check the code below:

package com.blogspot.illyakeeplearning.paging;

/**
* Paging Calculator
*/

public class PagingCalc {
public static final int DEFAULT_ITEMS_COUNT = 3;
public static final int DEFAULT_INDEXES_COUNT = 5;

private int maxItemsCount;
private int maxIndexesCount;

private int currPage;
private int totalItems;

private int pagesCount;
private int startPage;
private int endPage;

/**
* Constructor
*
* @param maxItemsCount max items count on page
* @param maxIndexesCount max page indexes count in a page block
* @param currPage current page index
* @param totalItems total items count
*/

public PagingCalc(int maxItemsCount, int maxIndexesCount, int currPage, int totalItems) {
this.maxItemsCount = maxItemsCount;
this.maxIndexesCount = maxIndexesCount;
this.currPage = currPage;
this.totalItems = totalItems;

initInternalVariables();
}

/**
* Inits internal variables
*/

private void initInternalVariables() {
initPagesCount();
initStartPage();
initEndPage();
}

/**
* Inits pages count
*/

private void initPagesCount() {
pagesCount = totalItems / maxItemsCount;
pagesCount += (totalItems % maxItemsCount == 0) ? 0 : 1;
}

/**
* Inits start page
*/

private void initStartPage() {
startPage = (currPage + maxIndexesCount - 1) / maxIndexesCount;
startPage = (startPage - 1) * maxIndexesCount + 1;
}

/**
* Inits end page
*/

public void initEndPage() {
endPage = startPage + maxIndexesCount - 1;
endPage = (endPage > pagesCount) ? pagesCount : endPage;
}

/**
* Gets current page index. 1-based value
*
* @return current page index. 1-based value
*/

public int getCurrPage() {
return currPage;
}

/**
* Returns total items count
*
* @return total items count
*/

public int getTotalItems() {
return totalItems;
}

/**
* Gets start item index on page
*
* @return start item index on page
*/

public int getStartItem() {
int startItem = (currPage - 1) * maxItemsCount + 1;
return startItem;
}

/**
* Gets end item index on page
*
* @return end item index on page
*/

public int getEndItem() {
int endItem = currPage * maxItemsCount;
endItem = (endItem > totalItems) ? totalItems : endItem;
return endItem;
}

/**
* Gets pages count
*
* @return pages count
*/

public int getPagesCount() {
return pagesCount;
}

/**
* Gets start page
*
* @return start page
*/

public int getStartPage() {
return startPage;
}

/**
* Gets end page
*
* @return end page
*/

public int getEndPage() {
return endPage;
}

/**
* Is Prev Link Visible
*
* @return true, if Prev Link Visible
*/

public boolean isPrevLinkVisible() {
return (pagesCount > maxIndexesCount) && (startPage > 1);
}

/**
* Gets Prev Link
*
* @return prev link
*/

public int getPrevLink() {
return startPage - 1;
}

/**
* Is Next Link Visible
*
* @return true, if Next Link Visible
*/

public boolean isNextLinkVisible() {
return (pagesCount > maxIndexesCount) && (endPage < pagesCount);
}

/**
* Gets Next Link
*
* @return next link
*/

public int getNextLink() {
return endPage + 1;
}

/**
* Is First Link Visible
*
* @return true, if First Link Visible
*/

public boolean isFirstLinkVisible() {
return currPage != 1;
}

/**
* Gets First Link
*
* @return first link
*/

public int getFirstLink() {
return 1;
}

/**
* Is Last Link Visible
*
* @return true, if Last Link Visible
*/

public boolean isLastLinkVisible() {
return (currPage != pagesCount);
}

/**
* Gets Last Link
*
* @return last link
*/

public int getLastLink() {
return pagesCount;
}
}

PagingCalc could be initialized...
PagingCalc calc = new PagingCalc(maxItemsCount, maxIndexesCount, currPage, totalItems);

...and be used
calc.getStartPage();
calc.getEndPage();

Download project binary and sources.

No comments: