KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > utils > PageList


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.commons.utils;
6
7 import java.util.* ;
8 import org.exoplatform.commons.exception.ExoMessageException;
9 /**
10  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
11  * @since Oct 21, 2004
12  * @version $Id: PageList.java,v 1.2 2004/10/25 03:36:58 tuan08 Exp $
13  */

14 abstract public class PageList {
15   final static public PageList EMPTY_LIST = new ObjectPageList(new ArrayList(), 10) ;
16   
17   private int pageSize_ ;
18   private int available_ = 0;
19   private int availablePage_ = 1;
20   protected int currentPage_ = -1 ;
21   protected List currentListPage_ ;
22   
23   public PageList(int pageSize) {
24     pageSize_ = pageSize ;
25   }
26   
27   public int getPageSize() { return pageSize_ ; }
28   public void setPageSize(int pageSize) {
29     pageSize_ = pageSize ;
30     setAvailablePage(available_) ;
31   }
32   
33   public int getCurrentPage() { return currentPage_ ; }
34   public int getAvailable() { return available_ ; }
35   
36   public int getAvailablePage() { return availablePage_ ; }
37   
38   public List currentPage() throws Exception JavaDoc {
39     if(currentListPage_ == null) {
40       populateCurrentPage(currentPage_) ;
41     }
42     return currentListPage_ ;
43   }
44   
45   abstract protected void populateCurrentPage(int page) throws Exception JavaDoc ;
46   
47   public List getPage(int page) throws Exception JavaDoc {
48     checkAndSetPage(page) ;
49     populateCurrentPage(page) ;
50     return currentListPage_ ;
51   }
52   
53   abstract public List getAll() throws Exception JavaDoc ;
54   
55   protected void checkAndSetPage(int page) throws Exception JavaDoc {
56     if(page < 1 || page > availablePage_) {
57       Object JavaDoc[] args = { Integer.toString(page), Integer.toString(availablePage_) } ;
58       throw new ExoMessageException("PageList.page-out-of-range", args) ;
59     }
60     currentPage_ = page ;
61   }
62   
63   protected void setAvailablePage(int available) {
64     available_ = available ;
65     if (available == 0) {
66       availablePage_ = 1 ;
67       currentPage_ = 1 ;
68     } else {
69       int pages = available / pageSize_ ;
70       if ( available % pageSize_ > 0) pages++ ;
71       availablePage_ = pages ;
72       currentPage_ = 1 ;
73     }
74   }
75   
76   public int getFrom() {
77     return (currentPage_ - 1) * pageSize_ ;
78   }
79   
80   public int getTo() {
81     int to = currentPage_ * pageSize_ ;
82     if (to > available_ ) to = available_ ;
83     return to ;
84   }
85 }
Popular Tags