1. 程式人生 > >java通用分頁工具類

java通用分頁工具類

package com.oceansoft.swl.qt.common.domain;

import java.util.List;

import com.oceansoft.uums.common.constant.Constant;

/**
* 通用分頁工具類
*
* @author LL
*
* @param
*/
public class MfsyPagination {

private List<T> list; // 物件記錄結果集
private int totalRows = 0; // 總記錄數
private int pageSize = MfsyConstant.PAGE_SIZE; // 每頁顯示記錄數 這裡可隨自己的要求更改
private int totalPage = 1; // 總頁數
private int pageIndex = 1; // 當前頁

private boolean isFirstPage = false; // 是否為第一頁
private boolean isLastPage = false; // 是否為最後一頁
private boolean isPreviousPage = false; // 是否有前一頁
private boolean isNextPage = false; // 是否有下一頁
// private boolean isHasPreviousPage = false; // 是否有前一頁
// private boolean isHasNextPage = false; // 是否有下一頁

private int navigatePages = Constant.NAVIGATE_PAGES; // 導航頁碼數
private int[] navigatePageNumbers; // 所有導航頁號

public MfsyPagination(int totalRows, int pageIndex) {
    init(totalRows, pageIndex, pageSize);
}

public MfsyPagination(int totalRows, int pageIndex, int pageSize) {
    init(totalRows, pageIndex, pageSize);
}

public MfsyPagination(List<T> list, int totalRows, int pageIndex) {
    super();
    init(totalRows, pageIndex, pageSize);
    this.list = list;
    this.totalRows = totalRows;
    this.pageIndex = pageIndex;
}

public MfsyPagination(List<T> list, int totalRows, int pageSize, int pageIndex) {
    super();
    init(totalRows, pageIndex, pageSize);
    this.list = list;
    this.totalRows = totalRows;
    this.pageSize = pageSize;
    this.pageIndex = pageIndex;
}

private void init(int totalRows, int pageIndex, int pageSize) {
    // 設定基本引數
    this.totalRows = totalRows;
    this.pageSize = pageSize;
    this.totalPage = (this.totalRows - 1) / this.pageSize + 1;

    // 根據輸入可能錯誤的當前號碼進行自動糾正
    if (pageIndex < 1) {
        this.pageIndex = 1;
    } else if (pageIndex > this.totalPage) {
        this.pageIndex = this.totalPage;
    } else {
        this.pageIndex = pageIndex;
    }
    // 基本引數設定之後進行導航頁面的計算
    calcNavigatePageNumbers();
    // 以及頁面邊界的判定
    judgePageBoudary();
}

/**
 * 計算導航頁
 */
private void calcNavigatePageNumbers() {
    // 當總頁數小於或等於導航頁碼數時
    if (totalPage <= navigatePages) {
        navigatePageNumbers = new int[totalPage];
        for (int i = 0; i < totalPage; i++) {
            navigatePageNumbers[i] = i + 1;
        }
    } else { // 當總頁數大於導航頁碼數時
        navigatePageNumbers = new int[navigatePages];
        int startNum = pageIndex - navigatePages / 2;
        int endNum = pageIndex + navigatePages / 2;

        if (startNum < 1) {
            startNum = 1;
            // (最前navPageCount頁
            for (int i = 0; i < navigatePages; i++) {
                navigatePageNumbers[i] = startNum++;
            }
        } else if (endNum > totalPage) {
            endNum = totalPage;
            // 最後navPageCount頁
            for (int i = navigatePages - 1; i >= 0; i--) {
                navigatePageNumbers[i] = endNum--;
            }
        } else {
            // 所有中間頁
            for (int i = 0; i < navigatePages; i++) {
                navigatePageNumbers[i] = startNum++;
            }
        }
    }
}

/**
 * 判定頁面邊界
 */
private void judgePageBoudary() {
    isFirstPage = (1 == pageIndex);
    isLastPage = (pageIndex == totalPage && 1 != pageIndex);
    isPreviousPage = (1 != pageIndex);
    isNextPage = (pageIndex != totalPage);
}

public void setList(List<T> list) {
    this.list = list;
}

/**
 * 得到當前頁的內容
 * 
 * @return {List}
 */
public List<T> getList() {
    return list;
}

/**
 * 得到記錄總數
 * 
 * @return {int}
 */
public int getTotalRows() {
    return totalRows;
}

/**
 * 得到每頁顯示多少條記錄
 * 
 * @return int
 */
public int getPageSize() {
    return pageSize;
}

/**
 * 得到頁面總數
 * 
 * @return {int}
 */
public int getTotalPage() {
    return totalPage;
}

/**
 * 得到當前頁號
 * 
 * @return {int}
 */
public int getPageIndex() {
    return pageIndex;
}

/**
 * 得到所有導航頁號
 * 
 * @return {int[]}
 */
public int[] getNavigatePageNumbers() {
    return navigatePageNumbers;
}

public boolean isFirstPage() {
    return isFirstPage;
}

public boolean isLastPage() {
    return isLastPage;
}

public boolean isPreviousPage() {
    return isPreviousPage;
}

public boolean isNextPage() {
    return isNextPage;
}

public String toString() {
    String str = new String();
    str = "[" + "totalRows=" + totalRows + ",totalPage=" + totalPage + ",pageIndex=" + pageIndex + ",pageSize="
            + pageSize + ",isFirstPage=" + isFirstPage + ",isLastPage=" + isLastPage + ",isPreviousPage="
            + isPreviousPage + ",hasNextPage=" + isNextPage + ",navigatePageNumbers=";
    int len = navigatePageNumbers.length;
    if (len > 0)
        str += (navigatePageNumbers[0]);
    for (int i = 1; i < len; i++) {
        str += (" " + navigatePageNumbers[i]);
    }
    str += "]";
    return str;
}

}