package com.wen;
import java.util.*;
/**
* 用于处理画面翻页的类
* <p>Copyright: 2003</p>
* @author Filippo Guan
* @version 1.0
*/
public class ListPage {
private List totalList;
private int currentPageNum;
private int totalPageNum;
private int limit;
public ListPage(List _totalList,int _pageLimit) {
if (_totalList == null) {
totalList = new ArrayList();
}
else {
totalList = _totalList;
}
limit = _pageLimit;
currentPageNum = 1;
totalPageNum = totalList.size()%limit==0 ? totalList.size()/limit : totalList.size()/limit+1;
}
public ListPage(){
this(new ArrayList());
}
public ListPage(List _totalList){
this(_totalList,10);
}
public void clear(){
this.totalList = new Vector();
currentPageNum = 1;
totalPageNum = 0;
}
public int size(){
return totalList.size();
}
public List getTotalList(){
return totalList;
}
public int getTotalPageNum(){
if (totalList == null) return 0;
totalPageNum = totalList.size()%limit==0 ? totalList.size()/limit : totalList.size()/limit+1;
return this.totalPageNum;
}
public int getPageLimit(){
return limit;
}
public void setPageLimit(int _Limit){
limit = _Limit;
}
// -----------------------------------------------------------
public Object getLineObject(int _LineNum){
if (totalList == null) return null;
Object retObj = totalList.get((currentPageNum-1)*limit + _LineNum - 1);
return retObj;
}
// -----------------------------------------------------------
public void setCurrentPageNum(int _currentPageNum){
currentPageNum = _currentPageNum;
}
public int getCurrentPageNum(){
return currentPageNum;
}
public boolean hasPreviousPage(){
return currentPageNum > 1;
}
public boolean hasNextPage(){
return currentPageNum < totalPageNum;
}
public List getCurrentPage(){
return getPage(currentPageNum);
}
public List getFirstPage(){
return this.getPage(1);
}
public List getLastPage(){
return this.getPage(totalPageNum);
}
public List getPreviousPage(){
if (!hasPreviousPage()) throw new NullPointerException("Has not Previous page");
return this.getPage(currentPageNum - 1);
}
public List getNextPage(){
if (!hasNextPage()) throw new NullPointerException("Has not Next page");
return this.getPage(currentPageNum + 1);
}
public List turnPreviousPage(){
this.currentPageNum--;
return this.getCurrentPage();
}
public List turnNextPage(){
this.currentPageNum++;
return this.getCurrentPage();
}
public List turnFirstPage(){
this.currentPageNum = 1;
return this.getCurrentPage();
}
public List turnLastPage(){
this.currentPageNum = this.totalPageNum;
return this.getCurrentPage();
}
// -----------------------------------------------------------
public List getPage(int _pageNum){
Vector vec = new Vector();
for(int i=(_pageNum-1)*limit;i<_pageNum*limit && i<totalList.size();i++){
vec.add(totalList.get(i));
}
return vec;
}
public boolean isFirstPage(int _pageNum){
return _pageNum == 1;
}
public boolean isLastPage(int _pageNum){
return _pageNum == totalPageNum;
}
public static void main(String[] args) {
Vector tryVec = new Vector();
for (int i=0;i<49;i++){
tryVec.add("test" + Integer.toString(i));
}
ListPage page = new ListPage(tryVec,10);
System.err.println("MaxPage=" + page.getTotalPageNum());
if (page.hasNextPage()) page.turnNextPage();
Vector retVec = new Vector(page.getCurrentPage());
try{
System.in.read();
} catch (Exception e){
}
}
}