KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > entities > publishing > EditionBrowser


1 package org.infoglue.cms.entities.publishing;
2
3 import java.io.Serializable JavaDoc;
4 import java.math.BigDecimal JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7
8 /**
9  * This is a helper class that will provide convenience methods for getting information
10  * about the page browsing aspect of Editions. This could easily be made generic and
11  * should if we find the need to make more paginated pages.
12  *
13  * @author <a HREF="frank@phase2technology.com">Frank Febbraro</a>
14  */

15 public class EditionBrowser implements Serializable JavaDoc
16 {
17     private int totalEditions = 0;
18     private int startIndex = 0;
19     private int pageSize = 0;
20     private int totalPages = 0;
21     private int currentPage = 0;
22     private List JavaDoc editions = new ArrayList JavaDoc();
23
24     public EditionBrowser(int totalEditions, int pageSize, int startIndex)
25     {
26         this.totalEditions = totalEditions;
27         this.pageSize = pageSize;
28         this.startIndex = startIndex;
29         init();
30     }
31
32     private void init()
33     {
34         BigDecimal JavaDoc total = new BigDecimal JavaDoc(totalEditions);
35         BigDecimal JavaDoc page = new BigDecimal JavaDoc(pageSize);
36         BigDecimal JavaDoc start = new BigDecimal JavaDoc(startIndex + 0.5d); // handle the case where it s zero
37
totalPages = Math.max(1, total.divide(page, 0, BigDecimal.ROUND_UP).intValue());
38         currentPage = start.divide(page, 0, BigDecimal.ROUND_UP).intValue();
39     }
40
41     public int getTotalEditions() { return totalEditions; }
42     public void setTotalEditions(int i) { totalEditions = i; }
43
44     public int getStartIndex() { return startIndex; }
45     public void setStartIndex(int i) { startIndex = i; }
46
47     public int getPageSize() { return pageSize; }
48     public void setPageSize(int i) { pageSize = i; }
49
50     public List JavaDoc getEditions() { return editions; }
51     public void setEditions(List JavaDoc c) { editions = (c != null)? c : new ArrayList JavaDoc(); }
52
53     public int getTotalPages() { return totalPages; }
54     public int getCurrentPage() { return currentPage; }
55
56     /**
57      * Returns true if there will be editions on the previous page, false otherwise
58      */

59     public boolean hasPreviousPage()
60     {
61         return getPreviousPageSize() != 0;
62     }
63
64     /**
65      * Returns true if there will be editions on the next page, false otherwise
66      */

67     public boolean hasNextPage()
68     {
69         return getNextPageSize() != 0;
70     }
71
72     /**
73      * Returns the number of editions on the previous page
74      */

75     public int getPreviousPageSize()
76     {
77         return Math.min(pageSize, Math.max(0, startIndex));
78     }
79
80     /**
81      * Returns the number of editions on the next page
82      */

83     public int getNextPageSize()
84     {
85         return Math.min(pageSize, Math.max(0, totalEditions - (startIndex + pageSize)));
86     }
87
88     /**
89      * Returns the starting index of the previous page
90      */

91     public int getPreviousPageIndex()
92     {
93         return getStartIndex() - getPreviousPageSize();
94     }
95
96     /**
97      * Returns the starting index of the next page
98      */

99     public int getNextPageIndex()
100     {
101         return getStartIndex() + getEditions().size();
102     }
103 }
104
Popular Tags