KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > forums > helper > GotoWithOffset


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under GPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portlet.forums.helper;
12
13
14 /**
15  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
16  * @author <a HREF="mailto:theute@jboss.org">Thomas Heute</a>
17  * @version $Revision: 1.1.1.1 $
18  */

19 public class GotoWithOffset
20    extends Range
21 {
22    private int perPage;
23    private String JavaDoc URL;
24    private int offset;
25    private int size;
26    private String JavaDoc gotoPageLabel;
27    private String JavaDoc previousLabel;
28    private String JavaDoc nextLabel;
29
30    /**
31     * Creates a new {@link GotoWithOffset} object.
32     *
33     * @param size DOCUMENT_ME
34     * @param offset DOCUMENT_ME
35     * @param perPage DOCUMENT_ME
36     * @param URL DOCUMENT_ME
37     * @param gotoPageLabel DOCUMENT_ME
38     * @param previousLabel DOCUMENT_ME
39     * @param nextLabel DOCUMENT_ME
40     */

41    public GotoWithOffset(int size,
42                          int offset,
43                          int perPage,
44                          String JavaDoc URL,
45                          String JavaDoc gotoPageLabel,
46                          String JavaDoc previousLabel,
47                          String JavaDoc nextLabel)
48    {
49       super(3, 3, 3);
50       this.size = size;
51       this.offset = offset;
52       this.perPage = perPage;
53       this.URL = URL;
54       this.gotoPageLabel = gotoPageLabel;
55       this.previousLabel = previousLabel;
56       this.nextLabel = nextLabel;
57    }
58
59    /**
60     * DOCUMENT_ME
61     *
62     * @param offset DOCUMENT_ME
63     */

64    public void setOffset(int offset)
65    {
66       this.offset = offset;
67    }
68
69    /**
70     * DOCUMENT_ME
71     *
72     * @param buffer DOCUMENT_ME
73     */

74    protected final void start(StringBuffer JavaDoc buffer)
75    {
76       buffer.append(gotoPageLabel);
77       buffer.append(" ");
78       int offset = (int) Math.floor((double) this.offset / (double) perPage);
79       if (offset > 0)
80       {
81          offset = (offset - 1) * perPage;
82          buffer.append(" <a HREF=\"").append(URL).append(offset).append("\">").append(previousLabel).append("</a>&nbsp;&nbsp;");
83       }
84    }
85
86    /**
87     * DOCUMENT_ME
88     *
89     * @param buffer DOCUMENT_ME
90     */

91    protected final void end(StringBuffer JavaDoc buffer)
92    {
93       int nextOffset = (int) (Math.floor((double) this.offset / (double) perPage) + 1) * perPage;
94       if (nextOffset < size)
95       {
96          buffer.append("&nbsp;&nbsp;<a HREF=\"").append(URL).append(nextOffset).append("\">").append(nextLabel)
97                .append("</a>&nbsp;&nbsp;");
98       }
99    }
100
101    /**
102     * DOCUMENT_ME
103     *
104     * @param buffer DOCUMENT_ME
105     * @param index DOCUMENT_ME
106     */

107    protected final void normal(StringBuffer JavaDoc buffer,
108                                int index)
109    {
110       lastNormal(buffer, index);
111       buffer.append(", ");
112    }
113
114    /**
115     * DOCUMENT_ME
116     *
117     * @param buffer DOCUMENT_ME
118     * @param index DOCUMENT_ME
119     */

120    protected final void enhanced(StringBuffer JavaDoc buffer,
121                                  int index)
122    {
123       lastEnhanced(buffer, index);
124       buffer.append(", ");
125    }
126
127    /**
128     * DOCUMENT_ME
129     *
130     * @param buffer DOCUMENT_ME
131     * @param index DOCUMENT_ME
132     */

133    protected final void lastNormal(StringBuffer JavaDoc buffer,
134                                    int index)
135    {
136       buffer.append("<a HREF=\"").append(URL).append(index * perPage).append("\">").append(index + 1).append("</a>");
137    }
138
139    /**
140     * DOCUMENT_ME
141     *
142     * @param buffer DOCUMENT_ME
143     * @param index DOCUMENT_ME
144     */

145    protected final void lastEnhanced(StringBuffer JavaDoc buffer,
146                                      int index)
147    {
148       buffer.append("<b>").append(index + 1).append("</b>");
149    }
150
151    /**
152     * DOCUMENT_ME
153     *
154     * @return DOCUMENT_ME
155     */

156    public String JavaDoc generate()
157    {
158       if (size > perPage)
159       {
160          return build((int) Math.ceil(size / (double) perPage), (int) Math.floor(offset / (double) perPage));
161       }
162       else
163       {
164          return "";
165       }
166    }
167 }
Popular Tags