KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > component > data > UIDataPager


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.common.component.data;
18
19 import java.io.IOException JavaDoc;
20 import java.text.MessageFormat JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 import javax.faces.component.NamingContainer;
25 import javax.faces.component.UICommand;
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28 import javax.faces.context.ResponseWriter;
29 import javax.faces.event.AbortProcessingException;
30 import javax.faces.event.ActionEvent;
31 import javax.faces.event.FacesEvent;
32
33 import org.apache.log4j.Logger;
34
35 import org.alfresco.web.app.Application;
36 import org.alfresco.web.data.IDataContainer;
37 import org.alfresco.web.ui.common.Utils;
38 import org.alfresco.web.ui.common.WebResources;
39
40 /**
41  * @author Kevin Roast
42  */

43 public class UIDataPager extends UICommand
44 {
45    private static Logger s_logger = Logger.getLogger(IDataContainer.class);
46    
47    private static final String JavaDoc LAST_PAGE = "last_page";
48    private static final String JavaDoc NEXT_PAGE = "next_page";
49    private static final String JavaDoc PREVIOUS_PAGE = "prev_page";
50    private static final String JavaDoc FIRST_PAGE = "first_page";
51    private static final String JavaDoc MSG_PAGEINFO = "page_info";
52    
53    
54    // ------------------------------------------------------------------------------
55
// Construction
56

57    /**
58     * Default constructor
59     */

60    public UIDataPager()
61    {
62       setRendererType(null);
63    }
64    
65    
66    // ------------------------------------------------------------------------------
67
// Component implementation
68

69    /**
70     * @see javax.faces.component.UIComponent#encodeBegin(javax.faces.context.FacesContext)
71     */

72    public void encodeBegin(FacesContext context) throws IOException JavaDoc
73    {
74       IDataContainer dataContainer = getDataContainer();
75       if (dataContainer == null)
76       {
77          throw new IllegalStateException JavaDoc("Must nest UISortLink inside component implementing IDataContainer!");
78       }
79       
80       // this component will only render itself if the parent DataContainer is setup
81
// with a valid "pageSize" property
82
if (isRendered() == false || dataContainer.getPageSize() == -1)
83       {
84          return;
85       }
86       
87       ResponseWriter out = context.getResponseWriter();
88       ResourceBundle JavaDoc bundle = Application.getBundle(context);
89       StringBuilder JavaDoc buf = new StringBuilder JavaDoc(512);
90       
91       int currentPage = dataContainer.getCurrentPage();
92       int pageCount = dataContainer.getPageCount();
93       
94       buf.append("<span");
95       if (getAttributes().get("style") != null)
96       {
97          buf.append(" style=\"")
98             .append(getAttributes().get("style"))
99             .append('"');
100       }
101       if (getAttributes().get("styleClass") != null)
102       {
103          buf.append(" class=")
104             .append(getAttributes().get("styleClass"));
105       }
106       buf.append('>');
107       
108       // output Page X of Y text
109
buf.append(MessageFormat.format(bundle.getString(MSG_PAGEINFO), new Object JavaDoc[] {
110             Integer.toString(currentPage + 1), // current page can be zero if no data present
111
Integer.toString(pageCount)
112             }));
113       
114       buf.append("&nbsp;");
115       
116       // output HTML links or labels to render the paging controls
117
// first page
118
if (currentPage != 0)
119       {
120          buf.append("<a HREF='#' onclick=\"");
121          buf.append(generateEventScript(0));
122          buf.append("\">");
123          buf.append(Utils.buildImageTag(context, WebResources.IMAGE_FIRSTPAGE, 16, 16, bundle.getString(FIRST_PAGE)));
124          buf.append("</a>");
125       }
126       else
127       {
128          buf.append(Utils.buildImageTag(context, WebResources.IMAGE_FIRSTPAGE_NONE, 16, 16, null));
129       }
130       
131       // previous page
132
if (currentPage != 0)
133       {
134          buf.append("<a HREF='#' onclick=\"");
135          buf.append(generateEventScript(currentPage - 1));
136          buf.append("\">");
137          buf.append(Utils.buildImageTag(context, WebResources.IMAGE_PREVIOUSPAGE, 16, 16, bundle.getString(PREVIOUS_PAGE)));
138          buf.append("</a>");
139       }
140       else
141       {
142          buf.append(Utils.buildImageTag(context, WebResources.IMAGE_PREVIOUSPAGE_NONE, 16, 16, null));
143       }
144       
145       buf.append("&nbsp;");
146       
147       // clickable digits for pages 1 to 10
148
int totalIndex = (pageCount < 10 ? pageCount : 10);
149       for (int i=0; i<totalIndex; i++)
150       {
151          if (i != currentPage)
152          {
153             buf.append("<a HREF='#' onclick=\"")
154                .append(generateEventScript(i))
155                .append("\">")
156                .append(i + 1)
157                .append("</a>&nbsp;");
158          }
159          else
160          {
161             buf.append("<b>")
162                .append(i + 1)
163                .append("</b>&nbsp;");
164          }
165       }
166       // clickable digits for pages 20 to 100 (in jumps of 10)
167
if (pageCount >= 20)
168       {
169          buf.append("...&nbsp;");
170          totalIndex = (pageCount / 10) * 10;
171          totalIndex = (totalIndex < 100 ? totalIndex : 100);
172          for (int i=19; i<totalIndex; i += 10)
173          {
174             if (i != currentPage)
175             {
176                buf.append("<a HREF='#' onclick=\"")
177                   .append(generateEventScript(i))
178                   .append("\">")
179                   .append(i + 1)
180                   .append("</a>&nbsp;");
181             }
182             else
183             {
184                buf.append("<b>")
185                   .append(i + 1)
186                   .append("</b>&nbsp;");
187             }
188          }
189       }
190       // clickable digits for last page if > 10 and not already shown
191
if ((pageCount > 10) && (pageCount % 10 != 0))
192       {
193          if (pageCount-1 != currentPage)
194          {
195             if (pageCount < 20)
196             {
197                buf.append("...&nbsp;");
198             }
199             buf.append("<a HREF='#' onclick=\"")
200                .append(generateEventScript(pageCount-1))
201                .append("\">")
202                .append(pageCount)
203                .append("</a>&nbsp;");
204          }
205          else
206          {
207             if (pageCount < 20)
208             {
209                buf.append("...&nbsp;");
210             }
211             buf.append("<b>")
212                .append(pageCount)
213                .append("</b>&nbsp;");
214          }
215       }
216       
217       // next page
218
if ((dataContainer.getCurrentPage() < dataContainer.getPageCount() - 1) == true)
219       {
220          buf.append("<a HREF='#' onclick=\"");
221          buf.append(generateEventScript(currentPage + 1));
222          buf.append("\">");
223          buf.append(Utils.buildImageTag(context, WebResources.IMAGE_NEXTPAGE, 16, 16, bundle.getString(NEXT_PAGE)));
224          buf.append("</a>");
225       }
226       else
227       {
228          buf.append(Utils.buildImageTag(context, WebResources.IMAGE_NEXTPAGE_NONE, 16, 16, null));
229       }
230       
231       // last page
232
if ((dataContainer.getCurrentPage() < dataContainer.getPageCount() - 1) == true)
233       {
234          buf.append("<a HREF='#' onclick=\"");
235          buf.append(generateEventScript(dataContainer.getPageCount() - 1));
236          buf.append("\">");
237          buf.append(Utils.buildImageTag(context, WebResources.IMAGE_LASTPAGE, 16, 16, bundle.getString(LAST_PAGE)));
238          buf.append("</a>");
239       }
240       else
241       {
242          buf.append(Utils.buildImageTag(context, WebResources.IMAGE_LASTPAGE_NONE, 16, 16, null));
243       }
244       
245       buf.append("</span>");
246       
247       out.write(buf.toString());
248    }
249
250    /**
251     * @see javax.faces.component.UIComponentBase#decode(javax.faces.context.FacesContext)
252     */

253    public void decode(FacesContext context)
254    {
255       Map JavaDoc requestMap = context.getExternalContext().getRequestParameterMap();
256       String JavaDoc fieldId = getHiddenFieldName();
257       String JavaDoc value = (String JavaDoc)requestMap.get(fieldId);
258       if (value != null && value.length() != 0)
259       {
260          // we were clicked - queue an event to represent the click
261
// cannot handle the event here as other components etc. have not had
262
// a chance to decode() - we queue an event to be processed later
263
PageEvent actionEvent = new PageEvent(this, Integer.valueOf(value).intValue());
264          this.queueEvent(actionEvent);
265       }
266    }
267    
268    /**
269     * @see javax.faces.component.UICommand#broadcast(javax.faces.event.FacesEvent)
270     */

271    public void broadcast(FacesEvent event) throws AbortProcessingException
272    {
273       if (event instanceof PageEvent == false)
274       {
275          // let the super class handle events which we know nothing about
276
super.broadcast(event);
277       }
278       else
279       {
280          // found a sort event for us!
281
if (s_logger.isDebugEnabled())
282             s_logger.debug("Handling paging event to index: " + ((PageEvent)event).Page);
283          getDataContainer().setCurrentPage(((PageEvent)event).Page);
284       }
285    }
286    
287    
288    // ------------------------------------------------------------------------------
289
// Private helpers
290

291    /**
292     * Return the parent data container for this component
293     */

294    private IDataContainer getDataContainer()
295    {
296       return Utils.getParentDataContainer(getFacesContext(), this);
297    }
298    
299    /**
300     * Output the JavaScript event script to jump to a specified page
301     *
302     * @param page page index to generate script to jump too
303     */

304    private String JavaDoc generateEventScript(int page)
305    {
306       return Utils.generateFormSubmit(getFacesContext(), this, getHiddenFieldName(), Integer.toString(page));
307    }
308    
309    /**
310     * We use a hidden field name based on the parent data container component Id and
311     * the string "pager" to give a field name that can be shared by all pager links
312     * within a single data container component.
313     *
314     * @return hidden field name
315     */

316    private String JavaDoc getHiddenFieldName()
317    {
318       UIComponent dataContainer = (UIComponent)Utils.getParentDataContainer(getFacesContext(), this);
319       return dataContainer.getClientId(getFacesContext()) + NamingContainer.SEPARATOR_CHAR + "pager";
320    }
321    
322    
323    // ------------------------------------------------------------------------------
324
// Inner classes
325

326    /**
327     * Class representing the clicking of a sortable column.
328     */

329    private static class PageEvent extends ActionEvent
330    {
331       public PageEvent(UIComponent component, int page)
332       {
333          super(component);
334          Page = page;
335       }
336       
337       public int Page = 0;
338    }
339 }
340
Popular Tags