KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > datascroller > HtmlDataScrollerRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.datascroller;
17
18 import org.apache.myfaces.renderkit.RendererUtils;
19 import org.apache.myfaces.renderkit.html.HtmlRenderer;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.application.Application;
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.UIData;
27 import javax.faces.component.UIParameter;
28 import javax.faces.component.html.HtmlCommandLink;
29 import javax.faces.component.html.HtmlOutputText;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import java.io.IOException JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * @author Thomas Spiegl (latest modification by $Author: svieujot $)
38  * @version $Revision: 1.19 $ $Date: 2005/01/04 01:42:23 $
39  * $Log: HtmlDataScrollerRenderer.java,v $
40  * Revision 1.19 2005/01/04 01:42:23 svieujot
41  * Bugfix for last page.
42  *
43  * Revision 1.18 2005/01/04 00:28:07 svieujot
44  * dataScroller, add rowsCountVar, displayedRowsCountVar, firstRowIndexVar and lastRowIndexVar attributes.
45  *
46  * Revision 1.17 2004/12/18 16:31:21 tomsp
47  * fixed issue MYFACES-1
48  *
49  * Revision 1.16 2004/10/13 11:50:57 matze
50  * renamed packages to org.apache
51  *
52  * Revision 1.15 2004/09/02 08:57:17 manolito
53  * missing setTransient
54  *
55  * Revision 1.14 2004/08/25 16:02:12 manolito
56  * Prevent division by zero in getPageIndex
57  *
58  */

59 public class HtmlDataScrollerRenderer
60     extends HtmlRenderer
61 {
62     private static final Log log = LogFactory.getLog(HtmlDataScrollerRenderer.class);
63
64     protected static final String JavaDoc FACET_FIRST = "first".intern();
65     protected static final String JavaDoc FACET_PREVOIUS = "previous".intern();
66     protected static final String JavaDoc FACET_NEXT = "next".intern();
67     protected static final String JavaDoc FACET_LAST = "last".intern();
68     protected static final String JavaDoc FACET_FAST_FORWARD = "fastf".intern();
69     protected static final String JavaDoc FACET_FAST_REWIND = "fastr".intern();
70     protected static final String JavaDoc PAGE_NAVIGATION = "idx".intern();
71
72     public static final String JavaDoc RENDERER_TYPE = "org.apache.myfaces.DataScroller";
73
74     public boolean getRendersChildren()
75     {
76         return true;
77     }
78
79     public void decode(FacesContext context, UIComponent component)
80     {
81         RendererUtils.checkParamValidity(context, component, HtmlDataScroller.class);
82
83         HtmlDataScroller scroller = (HtmlDataScroller)component;
84
85         UIData uiData = findUIData(scroller, component);
86         if (uiData == null)
87         {
88             return;
89         }
90
91         Map JavaDoc parameter = context.getExternalContext().getRequestParameterMap();
92         String JavaDoc param = (String JavaDoc)parameter.get(component.getClientId(context));
93         if (param != null)
94         {
95             if (param.equals(FACET_FIRST))
96             {
97                 uiData.setFirst(0);
98             }
99             else if (param.equals(FACET_PREVOIUS))
100             {
101                 int previous = uiData.getFirst() - uiData.getRows();
102                 if (previous >= 0)
103                     uiData.setFirst(previous);
104             }
105             else if (param.equals(FACET_NEXT))
106             {
107                 int next = uiData.getFirst() + uiData.getRows();
108                 if (next < uiData.getRowCount())
109                     uiData.setFirst(next);
110             }
111             else if (param.equals(FACET_FAST_FORWARD))
112             {
113                 int fastStep = scroller.getFastStep();
114                 if (fastStep <= 0)
115                     fastStep = 1;
116                 int next = uiData.getFirst() + uiData.getRows() * fastStep;
117                 int rowcount = uiData.getRowCount();
118                 if (next > rowcount)
119                      next = (rowcount - 1) - ((rowcount - 1) % uiData.getRows());
120                 uiData.setFirst(next);
121             }
122             else if (param.equals(FACET_FAST_REWIND))
123             {
124                 int fastStep = scroller.getFastStep();
125                 if (fastStep <= 0)
126                     fastStep = 1;
127                 int previous = uiData.getFirst() - uiData.getRows() * fastStep;
128                 if (previous < 0)
129                     previous = 0;
130                 uiData.setFirst(previous);
131             }
132             else if (param.equals(FACET_LAST))
133             {
134                 int rowcount = uiData.getRowCount();
135                 int rows = uiData.getRows();
136                 int delta = rowcount % rows;
137                 int first = delta > 0 && delta < rows ? rowcount - delta : rowcount - rows;
138                 if (first >= 0)
139                 {
140                     uiData.setFirst(first);
141                 }
142                 else
143                 {
144                     uiData.setFirst(0);
145                 }
146             }
147             else if (param.startsWith(PAGE_NAVIGATION))
148             {
149                 int index = Integer.parseInt(param.substring(PAGE_NAVIGATION.length(), param.length()));
150                 int pageCount = getPageCount(uiData);
151                 if (index > pageCount)
152                 {
153                     index = pageCount;
154                 }
155                 else if (index <= 0)
156                 {
157                     index = 1;
158                 }
159                 uiData.setFirst(uiData.getRows() * (index - 1));
160             }
161         }
162     }
163
164
165     public void encodeChildren(FacesContext facescontext, UIComponent uicomponent) throws IOException JavaDoc
166     {
167         RendererUtils.checkParamValidity(facescontext, uicomponent, HtmlDataScroller.class);
168
169         Map JavaDoc requestMap = facescontext.getExternalContext().getRequestMap();
170         HtmlDataScroller scroller = (HtmlDataScroller)uicomponent;
171
172         UIData uiData = findUIData(scroller, uicomponent);
173         if (uiData == null)
174         {
175             return;
176         }
177
178
179
180         String JavaDoc pageCountVar = scroller.getPageCountVar();
181         if (pageCountVar != null)
182         {
183             int pageCount = getPageCount(uiData);
184             requestMap.put(pageCountVar, new Integer JavaDoc(pageCount));
185         }
186         String JavaDoc pageIndexVar = scroller.getPageIndexVar();
187         if (pageIndexVar != null)
188         {
189             int pageIndex = getPageIndex(uiData);
190             requestMap.put(pageIndexVar, new Integer JavaDoc(pageIndex));
191         }
192         String JavaDoc rowsCountVar = scroller.getRowsCountVar();
193         if (rowsCountVar != null)
194         {
195             int rowsCount = uiData.getRowCount();
196             requestMap.put(rowsCountVar, new Integer JavaDoc(rowsCount));
197         }
198         String JavaDoc displayedRowsCountVar = scroller.getDisplayedRowsCountVar();
199         if (displayedRowsCountVar != null)
200         {
201             int displayedRowsCount = uiData.getRows();
202             int max = uiData.getRowCount()-uiData.getFirst();
203             if( displayedRowsCount > max )
204                 displayedRowsCount = max;
205             requestMap.put(displayedRowsCountVar, new Integer JavaDoc(displayedRowsCount));
206         }
207         String JavaDoc firstRowIndexVar = scroller.getFirstRowIndexVar();
208         if (firstRowIndexVar != null)
209         {
210             int firstRowIndex = uiData.getFirst()+1;
211             requestMap.put(firstRowIndexVar, new Integer JavaDoc(firstRowIndex));
212         }
213         String JavaDoc lastRowIndexVar = scroller.getLastRowIndexVar();
214         if (lastRowIndexVar != null)
215         {
216             int lastRowIndex = uiData.getFirst()+uiData.getRows();
217             int count = uiData.getRowCount();
218             if( lastRowIndex > count )
219                 lastRowIndex = count;
220             requestMap.put(lastRowIndexVar, new Integer JavaDoc(lastRowIndex));
221         }
222
223         RendererUtils.renderChildren(facescontext, uicomponent);
224
225         if (pageCountVar != null)
226         {
227             requestMap.remove(pageCountVar);
228         }
229         if (pageIndexVar != null)
230         {
231             requestMap.remove(pageIndexVar);
232         }
233         if (rowsCountVar != null)
234         {
235             requestMap.remove(rowsCountVar);
236         }
237         if (displayedRowsCountVar != null)
238         {
239             requestMap.remove(displayedRowsCountVar);
240         }
241         if (firstRowIndexVar != null)
242         {
243             requestMap.remove(firstRowIndexVar);
244         }
245         if (lastRowIndexVar != null)
246         {
247             requestMap.remove(lastRowIndexVar);
248         }
249     }
250
251     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
252     {
253         RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlDataScroller.class);
254
255         ResponseWriter writer = facesContext.getResponseWriter();
256         HtmlDataScroller scroller = (HtmlDataScroller)uiComponent;
257
258         UIData uiData = findUIData(scroller, uiComponent);
259         if (uiData == null)
260         {
261             return;
262         }
263
264         writer.startElement("table", scroller);
265         String JavaDoc styleClass = scroller.getStyleClass();
266         if (styleClass != null)
267         {
268             writer.writeAttribute("class", styleClass, null);
269         }
270         String JavaDoc style = scroller.getStyle();
271         if (style != null)
272         {
273             writer.writeAttribute("style", style, null);
274         }
275         writer.startElement("tr", scroller);
276
277         UIComponent facetComp = scroller.getFirst();
278         if (facetComp != null)
279         {
280             writer.startElement("td", scroller);
281             renderFacet(facesContext, scroller, facetComp, FACET_FIRST);
282             writer.endElement("td");
283         }
284         facetComp = scroller.getFastRewind();
285         if (facetComp != null)
286         {
287             writer.startElement("td", scroller);
288             renderFacet(facesContext, scroller, facetComp, FACET_FAST_REWIND);
289             writer.endElement("td");
290         }
291         facetComp = scroller.getPrevious();
292         if (facetComp != null)
293         {
294             writer.startElement("td", scroller);
295             renderFacet(facesContext, scroller, facetComp, FACET_PREVOIUS);
296             writer.endElement("td");
297         }
298         if (scroller.isPaginator())
299         {
300             writer.startElement("td", scroller);
301             renderPaginator(facesContext, scroller, uiData);
302             writer.endElement("td");
303         }
304         facetComp = scroller.getNext();
305         if (facetComp != null)
306         {
307             writer.startElement("td", scroller);
308             renderFacet(facesContext, scroller, facetComp, FACET_NEXT);
309             writer.endElement("td");
310         }
311         facetComp = scroller.getFastForward();
312         if (facetComp != null)
313         {
314             writer.startElement("td", scroller);
315             renderFacet(facesContext, scroller, facetComp, FACET_FAST_FORWARD);
316             writer.endElement("td");
317         }
318         facetComp = scroller.getLast();
319         if (facetComp != null)
320         {
321             writer.startElement("td", scroller);
322             renderFacet(facesContext, scroller, facetComp, FACET_LAST);
323             writer.endElement("td");
324         }
325
326         writer.endElement("tr");
327         writer.endElement("table");
328     }
329
330     private void renderFacet(FacesContext facesContext,
331                              HtmlDataScroller scroller,
332                              UIComponent facetComp,
333                              String JavaDoc facetName)
334         throws IOException JavaDoc
335     {
336         UIComponent link = getLink(facesContext, scroller, facetComp, facetName);
337         link.encodeBegin(facesContext);
338         facetComp.encodeBegin(facesContext);
339         if (facetComp.getRendersChildren())
340             facetComp.encodeChildren(facesContext);
341         facetComp.encodeEnd(facesContext);
342         link.encodeEnd(facesContext);
343     }
344     
345     protected void renderPaginator(FacesContext facesContext,
346                                    HtmlDataScroller scroller,
347                                    UIData uiData)
348         throws IOException JavaDoc
349     {
350         ResponseWriter writer = facesContext.getResponseWriter();
351
352         int maxPages = scroller.getPaginatorMaxPages();
353         if (maxPages <= 1)
354         {
355             maxPages = 2;
356         }
357         int pageCount = getPageCount(uiData);
358         if (pageCount <= 1)
359         {
360             return;
361         }
362         int pageIndex = getPageIndex(uiData);
363         int delta = maxPages / 2;
364
365         int pages;
366         int start;
367         if (pageCount > maxPages && pageIndex > delta)
368         {
369             pages = maxPages;
370             start = pageIndex - pages / 2 - 1;
371             if (start + pages > pageCount)
372             {
373                 start = pageCount - pages;
374             }
375         }
376         else
377         {
378             pages = pageCount < maxPages ? pageCount : maxPages;
379             start = 0;
380         }
381
382         writer.startElement("table", scroller);
383
384         String JavaDoc styleClass = scroller.getPaginatorTableClass();
385         if (styleClass != null)
386         {
387             writer.writeAttribute("class", styleClass, null);
388         }
389         String JavaDoc style = scroller.getPaginatorTableStyle();
390         if (style != null)
391         {
392             writer.writeAttribute("style", style, null);
393         }
394
395         writer.startElement("tr", scroller);
396
397         for (int i = start, size = start + pages; i < size; i++)
398         {
399             int idx = i + 1;
400             writer.startElement("td", scroller);
401             String JavaDoc cStyleClass;
402             String JavaDoc cStyle;
403             if (idx == pageIndex)
404             {
405                 cStyleClass = scroller.getPaginatorActiveColumnClass();
406                 cStyle = scroller.getPaginatorActiveColumnStyle();
407             }
408             else
409             {
410                 cStyleClass = scroller.getPaginatorColumnClass();
411                 cStyle = scroller.getPaginatorColumnStyle();
412             }
413             if (cStyleClass != null)
414             {
415                 writer.writeAttribute("class", cStyleClass, null);
416             }
417             if (cStyle != null)
418             {
419                 writer.writeAttribute("style", cStyle, null);
420             }
421
422             HtmlCommandLink link = getLink(facesContext, scroller, Integer.toString(idx), idx);
423             link.encodeBegin(facesContext);
424             link.encodeChildren(facesContext);
425             link.encodeEnd(facesContext);
426
427             writer.endElement("td");
428         }
429
430         writer.endElement("tr");
431         writer.endElement("table");
432     }
433
434     protected HtmlCommandLink getLink(FacesContext facesContext,
435                                       HtmlDataScroller scroller,
436                                       String JavaDoc text,
437                                       int pageIndex)
438     {
439         String JavaDoc id = PAGE_NAVIGATION + Integer.toString(pageIndex);
440         Application application = facesContext.getApplication();
441
442         HtmlCommandLink link =
443             (HtmlCommandLink)application.createComponent(HtmlCommandLink.COMPONENT_TYPE);
444         link.setId(scroller.getId() + id);
445         link.setTransient(true);
446         UIParameter parameter
447             = (UIParameter)application.createComponent(UIParameter.COMPONENT_TYPE);
448         parameter.setId(scroller.getId() + id + "_param");
449         parameter.setTransient(true);
450         parameter.setName(scroller.getClientId(facesContext));
451         parameter.setValue(id);
452         List JavaDoc children = link.getChildren();
453         children.add(parameter);
454         if (text != null)
455         {
456             HtmlOutputText uiText =
457                 (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE);
458             uiText.setTransient(true);
459             uiText.setValue(text);
460             children.add(uiText);
461         }
462         scroller.getChildren().add(link);
463         return link;
464     }
465
466     protected HtmlCommandLink getLink(FacesContext facesContext,
467                                       HtmlDataScroller scroller,
468                                       UIComponent facetComp,
469                                       String JavaDoc facetName)
470     {
471         Application application = facesContext.getApplication();
472
473         HtmlCommandLink link
474                 = (HtmlCommandLink)application.createComponent(HtmlCommandLink.COMPONENT_TYPE);
475         link.setId(scroller.getId() + facetName);
476         link.setTransient(true);
477         UIParameter parameter
478                 = (UIParameter)application.createComponent(UIParameter.COMPONENT_TYPE);
479         parameter.setId(scroller.getId() + facetName + "_param");
480         parameter.setTransient(true);
481         parameter.setName(scroller.getClientId(facesContext));
482         parameter.setValue(facetName);
483         List JavaDoc children = link.getChildren();
484         children.add(parameter);
485         if (facetComp != null)
486             children.add(facetComp);
487         // dirty, cause facet-comp is now child from link & scroller!
488
scroller.getChildren().add(link);
489         return link;
490     }
491
492     protected int getPageIndex(UIData uiData)
493     {
494         int rows = uiData.getRows();
495         int pageIndex;
496         if (rows > 0)
497         {
498             pageIndex = uiData.getFirst() / rows + 1;
499         }
500         else
501         {
502             log.warn("DataTable " + uiData.getClientId(FacesContext.getCurrentInstance()) + " has invalid rows attribute.");
503             pageIndex = 0;
504         }
505         if (uiData.getFirst() % rows > 0)
506         {
507             pageIndex++;
508         }
509         return pageIndex;
510     }
511
512     protected int getPageCount(UIData uiData)
513     {
514         int rows = uiData.getRows();
515         int pageCount;
516         if (rows > 0)
517         {
518             pageCount = rows <= 0 ? 1 : uiData.getRowCount() / rows;
519             if (uiData.getRowCount() % rows > 0)
520             {
521                 pageCount++;
522             }
523         }
524         else
525         {
526             rows = 1;
527             pageCount = 1;
528         }
529         return pageCount;
530     }
531
532     protected UIData findUIData(HtmlDataScroller scroller, UIComponent component)
533     {
534         String JavaDoc forStr = scroller.getFor();
535         UIComponent forComp;
536         if (forStr == null)
537         {
538             // DataScroller may be a child of uiData
539
forComp = component.getParent();
540         }
541         else
542         {
543             forComp = component.findComponent(scroller.getFor());
544             if (forComp == null)
545             {
546                 log.warn("could not find UIData referenced by attribute dataScroller@for = '" + scroller.getFor() + "'");
547             }
548         }
549         if (!(forComp instanceof UIData))
550         {
551             throw new IllegalArgumentException JavaDoc("uiComponent referenced by attribute tableScroller@for must be of type " + UIData.class.getName());
552         }
553         return (UIData)forComp;
554     }
555
556 }
557
Popular Tags