package vincent.util;
public class PageControl {
public int totalRowsAmount;//total rows
public boolean rowsAmountSet;//whether the totalRowsAmout has been reset
public int pageSize=5; //the rows in one page default to 1
public int currentPage=1; //the current page number
public int nextPage;
public int previousPage;
public int totalPages;//total pages number
public boolean hasNext;//whether it has next page
public boolean hasPrevious; //whether it has previous page
public String description;
int pageStartRow;
int pageEndRow;
/**
* the constructor
* @param totalRows
*/
public PageControl(int totalRows){
setTotalRowsAmount(totalRows);
}
public PageControl(){}
/**
* set the total rows amount
* @param totalRows
*/
public void setTotalRowsAmount(int totalRows) {
if(!this.rowsAmountSet){
totalRowsAmount = totalRows;
//introduce "divide" to make the even out
int divide = totalRowsAmount/pageSize;
if(totalRowsAmount==divide*pageSize){
totalPages = divide;
}
else{
totalPages=divide+1;
}
setCurrentPage(1);
this.rowsAmountSet=true;
}
}
/**
* set the current page to the specified number
* @param pageNumber
*/
public void setCurrentPage(int pageNumber) {
currentPage = pageNumber;
nextPage=currentPage+1;
previousPage=currentPage-1;
//Count the start row and the end row
if(currentPage*pageSize<totalRowsAmount){
pageEndRow=currentPage*pageSize;
pageStartRow=pageEndRow-pageSize+1;
}else{
pageEndRow=totalRowsAmount;
pageStartRow=pageSize*(totalPages-1)+1;
}
if (nextPage>totalPages){
hasNext=false;
}else{
hasNext=true;
}
if(previousPage==0){
hasPrevious=false;
}else{
hasPrevious=true;
};
System.out.println(this.description());
}
/**
* get the current page
* @return
*/
public int getCurrentPage() {
return currentPage;
}
/**
* whether it has next page
* @return
*/
public boolean isHasNext() {
return hasNext;
}
/**
* whether it has previous page
* @return
*/
public boolean isHasPrevious() {
return hasPrevious;
}
//a bunch of get
public int getNextPage() {
return nextPage;
}
public int getPageSize() {
return pageSize;
}
public int getPreviousPage() {
return previousPage;
}
public int getTotalPages() {
return totalPages;
}
public int getTotalRowsAmount() {
return totalRowsAmount;
}
public void setHasNext(boolean b) {
hasNext = b;
}
public void setHasPrevious(boolean b) {
hasPrevious = b;
}
public void setNextPage(int i) {
nextPage = i;
}
public void setPageSize(int i) {
pageSize = i;
}
public void setPreviousPage(int i) {
previousPage = i;
}
public void setTotalPages(int i) {
totalPages = i;
}
public int getPageEndRow() {
return pageEndRow;
}
public int getPageStartRow() {
return pageStartRow;
}
public String getDescription(){
String description="Total:"+this.getTotalRowsAmount()+
" items "+this.getTotalPages() +" pages";
// this.currentPage+" Previous "+this.hasPrevious +
// " Next:"+this.hasNext+
// " start row:"+this.pageStartRow+
// " end row:"+this.pageEndRow;
return description;
}
public String description(){
String description="Total:"+this.getTotalRowsAmount()+
" items "+this.getTotalPages() +" pages,Current page:"+
this.currentPage+" Previous "+this.hasPrevious +
" Next:"+this.hasNext+
" start row:"+this.pageStartRow+
" end row:"+this.pageEndRow;
return description;
}
}