KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > util > Paginator


1
2  /*
3  -- GeiNuke --
4 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
5  http://www.hostingjava.it/-geinuke/
6
7 This file is part of GeiNuke.
8
9     GeiNuke is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     GeiNuke is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with GeiNuke; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */

23 package com.geinuke.util;
24
25 import java.util.ArrayList JavaDoc;
26
27 public class Paginator {
28     protected ArrayList JavaDoc list=null;
29     protected int pageDim=0;
30     protected String JavaDoc baseURL=null;
31     protected int currentPage=0;
32     
33     public Paginator(){}
34     
35     public Paginator(int pageDim,String JavaDoc baseURL){
36         this.baseURL=baseURL;
37         this.pageDim=pageDim;
38     }
39     
40     public Paginator(ArrayList JavaDoc list,int pageDim,String JavaDoc baseURL){
41         this.setBaseURL(baseURL);
42         this.pageDim=pageDim;
43         this.list=list;
44     }
45     
46     public int getLastPage(){
47         if(this.list==null)
48             return 0;
49         int last=0;
50         last=this.list.size()/this.pageDim;
51         if(this.list.size() % this.pageDim!=0)
52             last++;
53         return last;
54     }
55     
56     public String JavaDoc getBaseURL() {
57         return baseURL;
58     }
59     public void setBaseURL(String JavaDoc baseURL) {
60             this.baseURL = baseURL;
61             //this.baseURL=this.baseURL.replaceAll("[?]wp=[0-9]+","");
62
this.baseURL=this.baseURL.replaceAll("[?]wp=[0-9]+[&]","?");
63             this.baseURL=this.baseURL.replaceAll("[&]wp=[0-9]+","");
64     }
65     public ArrayList JavaDoc getList() {
66         return list;
67     }
68     public void setList(ArrayList JavaDoc list) {
69         this.list = list;
70     }
71     public int getPageDim() {
72         return pageDim;
73     }
74     public void setPageDim(int pageDim) {
75         this.pageDim = pageDim;
76     }
77     
78     public ArrayList JavaDoc getPage(int page){
79         if(this.list==null)
80             return new ArrayList JavaDoc();
81         if(page>this.getLastPage())
82             page=this.getLastPage();
83         this.setCurrentPage(page);
84         ArrayList JavaDoc pageL=null;
85         boolean flag=true;
86         flag=flag && page>0 && list.size() > (page-1)* pageDim;
87         if(flag){
88             pageL=new ArrayList JavaDoc();
89             int st=(page-1)* pageDim ;
90             for(int i=st;i<list.size() && i<st+pageDim;i++){
91                 pageL.add(list.get(i));
92                 
93             }
94         }
95         
96         return pageL;
97     }
98     
99     public int getCurrentPage() {
100         if(currentPage==0)
101             return 1;
102         return currentPage;
103     }
104     public void setCurrentPage(int currentPage) {
105         this.currentPage = currentPage;
106     }
107 }
108
Popular Tags