KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Generate a jump bar like : [0,1,2,...,10,11,12,...,21,22,23]
16  *
17  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
18  * @version $Revision: 1.1.1.1 $
19  */

20 public class Range
21 {
22    /**
23     * @param left is the max number of items displayed on the left part
24     * @param middle is the max number of items displayed in the middle part
25     * @param right is the max number of items displayed in the right part
26     */

27    public Range(int left,
28                 int middle,
29                 int right)
30    {
31       this.left = left;
32       this.middle = middle;
33       this.right = right;
34    }
35
36    /** DOCUMENT_ME */
37    protected final int left;
38
39    /** DOCUMENT_ME */
40    protected final int middle;
41
42    /** DOCUMENT_ME */
43    protected final int right;
44
45    /**
46     * Begin the generated string.
47     */

48    protected void start(StringBuffer JavaDoc buffer)
49    {
50       buffer.append("[");
51    }
52
53    /**
54     * End the generated string.
55     */

56    protected void end(StringBuffer JavaDoc buffer)
57    {
58       buffer.append("]");
59    }
60
61    /**
62     * Generates a non last normal item.
63     */

64    protected void normal(StringBuffer JavaDoc buffer,
65                          int index)
66    {
67       buffer.append(index).append(",");
68    }
69
70    /**
71     * Generates a last normal item.
72     */

73    protected void lastNormal(StringBuffer JavaDoc buffer,
74                              int index)
75    {
76       buffer.append(index);
77    }
78
79    /**
80     * Generates a non last enhanced item.
81     */

82    protected void enhanced(StringBuffer JavaDoc buffer,
83                            int index)
84    {
85       buffer.append("-").append(index).append("-,");
86    }
87
88    /**
89     * Generates a last enhanced item.
90     */

91    protected void lastEnhanced(StringBuffer JavaDoc buffer,
92                                int index)
93    {
94       buffer.append("-").append(index).append("-");
95    }
96
97    /**
98     * Generates a gap.
99     */

100    protected void gap(StringBuffer JavaDoc buffer)
101    {
102       buffer.append(" ... ");
103    }
104
105    /**
106     * Generates a separator.
107     */

108    protected void separe(StringBuffer JavaDoc buffer)
109    {
110       buffer.append(", ");
111    }
112
113    /**
114     * Do the job.
115     */

116    protected final String JavaDoc build(int size,
117                                 int offset)
118    {
119       // Computes the boundaries a priori
120
int l1 = 0;
121       int r1 = left;
122       int l2 = offset - (middle / 2);
123       int r2 = offset + ((middle + 1) / 2);
124       int l3 = size - right;
125       int r3 = size;
126
127       // Check that the left part is not bigger than the entire range
128
if (size < r1)
129       {
130          r1 = size;
131       }
132
133       // Check that the right part does not span on the left one
134
if (l3 < r1)
135       {
136          l3 = r1;
137       }
138
139       // Correct the left boundary of the mid part if necessary
140
if (l2 < r1)
141       {
142          l2 = r1;
143       }
144
145       // Correct the right boundary of the mid part if necessary
146
if (l3 < r2)
147       {
148          r2 = l3;
149       }
150
151       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
152       start(buffer);
153
154       // Displays the left part
155
range(buffer, l1, offset, r1);
156
157       if (l2 < r2)
158       {
159          gap(buffer, r1, l2);
160          range(buffer, l2, offset, r2);
161          gap(buffer, r2, l3);
162          range(buffer, l3, offset, r3);
163       }
164       else if (l3 < r3)
165       {
166          gap(buffer, r1, l3);
167          range(buffer, l3, offset, r3);
168       }
169
170       end(buffer);
171
172       return buffer.toString();
173    }
174
175    private void gap(StringBuffer JavaDoc buffer,
176                     int from,
177                     int to)
178    {
179       if (from < to)
180       {
181          gap(buffer);
182       }
183       else if (from == to)
184       {
185          separe(buffer);
186       }
187    }
188
189    /**
190     *
191     * @param buffer the output buffer
192     * @param from inclusive
193     * @param offset the highlighted offset
194     * @param to exclusive
195     */

196    private void range(StringBuffer JavaDoc buffer,
197                       int from,
198                       int offset,
199                       int to)
200    {
201       boolean last = from < to--;
202       if ((from <= offset) && (offset <= to))
203       {
204          while (from < offset)
205          {
206             normal(buffer, from++);
207          }
208
209          if (from < to)
210          {
211             enhanced(buffer, from++);
212          }
213          while (from < to)
214          {
215             normal(buffer, from++);
216          }
217
218          if (last)
219          {
220             if (offset == from)
221             {
222                lastEnhanced(buffer, from);
223             }
224             else
225             {
226                lastNormal(buffer, from);
227             }
228          }
229       }
230       else
231       {
232          while (from < to)
233          {
234             normal(buffer, from++);
235          }
236
237          if (last)
238          {
239             lastNormal(buffer, from);
240          }
241       }
242    }
243 }
Popular Tags