KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > datapaginator > DataPaginatorRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33 /* Original Copyright
34  * Copyright 2004 The Apache Software Foundation.
35  *
36  * Licensed under the Apache License, Version 2.0 (the "License");
37  * you may not use this file except in compliance with the License.
38  * You may obtain a copy of the License at
39  *
40  * http://www.apache.org/licenses/LICENSE-2.0
41  *
42  * Unless required by applicable law or agreed to in writing, software
43  * distributed under the License is distributed on an "AS IS" BASIS,
44  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45  * See the License for the specific language governing permissions and
46  * limitations under the License.
47  */

48
49 package com.icesoft.faces.component.datapaginator;
50
51 import com.icesoft.faces.component.ext.HtmlCommandLink;
52 import com.icesoft.faces.component.util.CustomComponentUtils;
53 import com.icesoft.faces.context.DOMContext;
54 import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
55 import com.icesoft.faces.renderkit.dom_html_basic.HTML;
56 import com.icesoft.faces.renderkit.dom_html_basic.PassThruAttributeRenderer;
57 import org.w3c.dom.Element JavaDoc;
58
59 import javax.faces.FacesException;
60 import javax.faces.application.Application;
61 import javax.faces.component.UIComponent;
62 import javax.faces.component.UIParameter;
63 import javax.faces.context.FacesContext;
64 import java.io.IOException JavaDoc;
65 import java.util.List JavaDoc;
66 import java.util.Map JavaDoc;
67
68 public class DataPaginatorRenderer extends DomBasicRenderer {
69     public static final String JavaDoc RENDERER_TYPE = "com.icesoft.faces.DataScroller";
70
71     protected static final String JavaDoc PAGE_NAVIGATION = "idx".intern();
72
73     public boolean getRendersChildren() {
74         return true;
75     }
76
77     public void decode(FacesContext context, UIComponent component) {
78         validateParameters(context, component, DataPaginator.class);
79         Map JavaDoc parameter = context.getExternalContext().getRequestParameterMap();
80         String JavaDoc param = (String JavaDoc) parameter.get(component.getClientId(context));
81         if (param != null && param.length() >= PAGE_NAVIGATION.length()) {
82             if (param.startsWith(PAGE_NAVIGATION)) {
83                 // queue a navigation event that results from the user pressing
84
// on one of the page indexes
85
component.queueEvent(new PaginatorActionEvent(component,
86                                                               Integer.parseInt(
87                                                                       param
88                                                                               .substring(
89                                                                               PAGE_NAVIGATION.length(),
90                                                                               param.length()))));
91             } else {
92                 // queue a navigation event that results from the user pressing
93
// on the navigation buttons
94
component
95                         .queueEvent(new PaginatorActionEvent(component, param));
96             }
97         }
98     }
99
100     protected void setVariables(FacesContext facescontext,
101                                 DataPaginator scroller) throws IOException JavaDoc {
102         Map JavaDoc requestMap = facescontext.getExternalContext().getRequestMap();
103
104         String JavaDoc pageCountVar = scroller.getPageCountVar();
105         if (pageCountVar != null) {
106             int pageCount = scroller.getPageCount();
107             requestMap.put(pageCountVar, new Integer JavaDoc(pageCount));
108         }
109         String JavaDoc pageIndexVar = scroller.getPageIndexVar();
110         if (pageIndexVar != null) {
111             int pageIndex = scroller.getPageIndex();
112             if (pageIndex > scroller.getPageCount()) {
113                 pageIndex = scroller.getPageCount();
114             }
115             requestMap.put(pageIndexVar, new Integer JavaDoc(pageIndex));
116         }
117         String JavaDoc rowsCountVar = scroller.getRowsCountVar();
118         if (rowsCountVar != null) {
119             int rowsCount = scroller.getRowCount();
120             requestMap.put(rowsCountVar, new Integer JavaDoc(rowsCount));
121         }
122         String JavaDoc displayedRowsCountVar = scroller.getDisplayedRowsCountVar();
123         if (displayedRowsCountVar != null) {
124             int displayedRowsCount = scroller.getRows();
125             int max = scroller.getRowCount() - scroller.getFirstRow();
126             if (displayedRowsCount > max) {
127                 displayedRowsCount = max;
128             }
129             requestMap.put(displayedRowsCountVar,
130                            new Integer JavaDoc(displayedRowsCount));
131         }
132         String JavaDoc firstRowIndexVar = scroller.getFirstRowIndexVar();
133         if (firstRowIndexVar != null) {
134             int firstRowIndex = scroller.getFirstRow();
135             if (scroller.getRowCount() > 0) {
136                 firstRowIndex += 1;
137             }
138             requestMap.put(firstRowIndexVar, new Integer JavaDoc(firstRowIndex));
139         }
140         String JavaDoc lastRowIndexVar = scroller.getLastRowIndexVar();
141         if (lastRowIndexVar != null) {
142             int lastRowIndex = scroller.getFirstRow() + scroller.getRows();
143             int count = scroller.getRowCount();
144             if (lastRowIndex > count) {
145                 lastRowIndex = count;
146             }
147             requestMap.put(lastRowIndexVar, new Integer JavaDoc(lastRowIndex));
148         }
149     }
150
151     public void removeVariables(FacesContext facescontext,
152                                 UIComponent uiComponent) throws IOException JavaDoc {
153         DataPaginator scroller = (DataPaginator) uiComponent;
154         Map JavaDoc requestMap = facescontext.getExternalContext().getRequestMap();
155
156         String JavaDoc pageCountVar = scroller.getPageCountVar();
157         if (pageCountVar != null) {
158             requestMap.remove(pageCountVar);
159         }
160         String JavaDoc pageIndexVar = scroller.getPageIndexVar();
161         if (pageIndexVar != null) {
162             requestMap.remove(pageIndexVar);
163         }
164         String JavaDoc rowsCountVar = scroller.getRowsCountVar();
165         if (rowsCountVar != null) {
166             requestMap.remove(rowsCountVar);
167         }
168         String JavaDoc displayedRowsCountVar = scroller.getDisplayedRowsCountVar();
169         if (displayedRowsCountVar != null) {
170             requestMap.remove(displayedRowsCountVar);
171         }
172         String JavaDoc firstRowIndexVar = scroller.getFirstRowIndexVar();
173         if (firstRowIndexVar != null) {
174             requestMap.remove(firstRowIndexVar);
175         }
176         String JavaDoc lastRowIndexVar = scroller.getLastRowIndexVar();
177         if (lastRowIndexVar != null) {
178             requestMap.remove(lastRowIndexVar);
179         }
180     }
181
182     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
183             throws IOException JavaDoc {
184         validateParameters(facesContext, uiComponent, DataPaginator.class);
185         DataPaginator scroller = (DataPaginator) uiComponent;
186         if (!scroller.isModelResultSet()) {
187             super.encodeBegin(facesContext, uiComponent);
188
189             // clear children before render
190
List JavaDoc kids = scroller.getChildren();
191             for (int i = 0; i < kids.size(); i++) {
192                 UIComponent kid = (UIComponent) kids.get(i);
193                 // do not remove the output text - bug #333
194
if (!kid.getFamily().equalsIgnoreCase("javax.faces.Output")) {
195                     scroller.getChildren().remove(kids.get(i));
196                 }
197             }
198             //Reset the dataTable model before setting variables
199
scroller.getUIData().setValue(null);
200             scroller.getUIData().setRowIndex(-1);
201             setVariables(facesContext, scroller);
202         }
203     }
204
205     public void encodeChildren(FacesContext facescontext,
206                                UIComponent uicomponent) throws IOException JavaDoc {
207         validateParameters(facescontext, uicomponent, DataPaginator.class);
208         DataPaginator scroller = (DataPaginator) uicomponent;
209         if (!scroller.isModelResultSet()) {
210             boolean singlePageScroller = scroller.getPageCount() <= 1 &&
211                                          scroller.getRowsCountVar() == null &&
212                                          scroller.getDisplayedRowsCountVar() ==
213                                          null;
214             if (!singlePageScroller) {
215                 CustomComponentUtils.renderChildren(facescontext, uicomponent);
216             }
217         }
218     }
219
220     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
221             throws IOException JavaDoc {
222         validateParameters(facesContext, uiComponent, DataPaginator.class);
223         DataPaginator scroller = (DataPaginator) uiComponent;
224         if (scroller.getUIData() == null) {
225             return;
226         }
227         renderScroller(facesContext, uiComponent);
228         if (!scroller.isModelResultSet()) {
229             removeVariables(facesContext, uiComponent);
230         }
231     }
232
233
234     protected void renderScroller(FacesContext facesContext,
235                                   UIComponent uiComponent) throws IOException JavaDoc {
236         DataPaginator scroller = (DataPaginator) uiComponent;
237         if (!scroller.isModelResultSet()) {
238             if (!scroller.isRenderFacetsIfSinglePage() &&
239                 scroller.getPageCount() <= 1) {
240                 return;
241             }
242         }
243         if (scroller.getFacets().size() <= 0) {
244             return;
245         }
246
247         DOMContext domContext =
248                 DOMContext.attachDOMContext(facesContext, scroller);
249         if (!domContext.isInitialized()) {
250             Element JavaDoc table = domContext.createRootElement(HTML.TABLE_ELEM);
251             setRootElementId(facesContext, table, scroller);
252             if (PassThruAttributeRenderer.passThruAttributeExists(scroller)) {
253                 PassThruAttributeRenderer
254                         .renderAttributes(facesContext, scroller, null);
255             }
256         }
257         Element JavaDoc table = (Element JavaDoc) domContext.getRootNode();
258         DOMContext.removeChildren(table);
259
260         Element JavaDoc tr = domContext.createElement(HTML.TR_ELEM);
261         table.appendChild(tr);
262         Element JavaDoc td;
263
264         String JavaDoc styleClass = scroller.getStyleClass();
265         table.setAttribute(HTML.CLASS_ATTR, styleClass);
266
267         String JavaDoc style = scroller.getStyle();
268         if(style != null && style.length() > 0)
269             table.setAttribute(HTML.STYLE_ATTR, style);
270         else
271             table.removeAttribute(HTML.STYLE_ATTR);
272         String JavaDoc scrollButtonCellClass = scroller.getscrollButtonCellClass();
273
274         UIComponent facetComp = scroller.getFirst();
275         if (facetComp != null) {
276             td = domContext.createElement(HTML.TD_ELEM);
277             td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
278             tr.appendChild(td);
279             domContext.setCursorParent(td);
280             domContext.streamWrite(facesContext, uiComponent,
281                                    domContext.getRootNode(), td);
282             renderFacet(facesContext, scroller, facetComp,
283                         DataPaginator.FACET_FIRST);
284         }
285
286         facetComp = scroller.getFastRewind();
287         if (facetComp != null) {
288             if (scroller.isVertical()) {
289                 tr = domContext.createElement(HTML.TR_ELEM);
290                 table.appendChild(tr);
291             }
292             td = domContext.createElement(HTML.TD_ELEM);
293             td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
294             tr.appendChild(td);
295             domContext.setCursorParent(td);
296             domContext.streamWrite(facesContext, uiComponent,
297                                    domContext.getRootNode(), td);
298             renderFacet(facesContext, scroller, facetComp,
299                         DataPaginator.FACET_FAST_REWIND);
300         }
301         
302         facetComp = scroller.getPrevious();
303         if (facetComp != null) {
304             if (scroller.isVertical()) {
305                 tr = domContext.createElement(HTML.TR_ELEM);
306                 table.appendChild(tr);
307             }
308             td = domContext.createElement(HTML.TD_ELEM);
309             td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
310             tr.appendChild(td);
311             domContext.setCursorParent(td);
312             domContext.streamWrite(facesContext, uiComponent,
313                                    domContext.getRootNode(), td);
314             renderFacet(facesContext, scroller, facetComp,
315                         DataPaginator.FACET_PREVIOUS);
316         }
317         if (!scroller.isModelResultSet() && scroller.isPaginator()) {
318             if (scroller.isVertical()) {
319                 tr = domContext.createElement(HTML.TR_ELEM);
320                 table.appendChild(tr);
321             }
322             td = domContext.createElement(HTML.TD_ELEM);
323             tr.appendChild(td);
324             Element JavaDoc paginatorTable = domContext.createElement(HTML.TABLE_ELEM);
325             td.appendChild(paginatorTable);
326             renderPaginator(facesContext, uiComponent, paginatorTable,
327                             domContext);
328         }
329         facetComp = scroller.getNext();
330         if (facetComp != null) {
331             if (scroller.isVertical()) {
332                 tr = domContext.createElement(HTML.TR_ELEM);
333                 table.appendChild(tr);
334             }
335             td = domContext.createElement(HTML.TD_ELEM);
336             td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
337             tr.appendChild(td);
338             domContext.setCursorParent(td);
339             domContext.streamWrite(facesContext, uiComponent,
340                                    domContext.getRootNode(), td);
341             renderFacet(facesContext, scroller, facetComp,
342                         DataPaginator.FACET_NEXT);
343         }
344
345         facetComp = scroller.getFastForward();
346         if (facetComp != null) {
347             if (scroller.isVertical()) {
348                 tr = domContext.createElement(HTML.TR_ELEM);
349                 table.appendChild(tr);
350             }
351             td = domContext.createElement(HTML.TD_ELEM);
352             td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
353             tr.appendChild(td);
354             domContext.setCursorParent(td);
355             domContext.streamWrite(facesContext, uiComponent,
356                                    domContext.getRootNode(), td);
357             renderFacet(facesContext, scroller, facetComp,
358                         DataPaginator.FACET_FAST_FORWARD);
359         }
360         
361         facetComp = scroller.getLast();
362         if (facetComp != null) {
363             if (scroller.isVertical()) {
364                 tr = domContext.createElement(HTML.TR_ELEM);
365                 table.appendChild(tr);
366             }
367             td = domContext.createElement(HTML.TD_ELEM);
368             td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
369             tr.appendChild(td);
370             domContext.setCursorParent(td);
371             domContext.streamWrite(facesContext, uiComponent,
372                                    domContext.getRootNode(), td);
373             renderFacet(facesContext, scroller, facetComp,
374                         DataPaginator.FACET_LAST);
375         }
376         domContext.stepOver();
377         domContext.streamWrite(facesContext, uiComponent);
378     }
379
380     protected void renderFacet(FacesContext facesContext,
381                                DataPaginator scroller,
382                                UIComponent facetComp, String JavaDoc facetName)
383             throws IOException JavaDoc {
384         HtmlCommandLink link =
385                 (HtmlCommandLink) getLink(facesContext, scroller, facetName);
386
387         if (scroller.isDisabled() ||
388             (!scroller.isModelResultSet() && scroller.getPageCount() <= 1)) {
389             link.setDisabled(true);
390         } else {
391             link.setDisabled(false);
392         }
393
394         link.encodeBegin(facesContext);
395         facetComp.encodeBegin(facesContext);
396         if (facetComp.getRendersChildren()) {
397             facetComp.encodeChildren(facesContext);
398         }
399         facetComp.encodeEnd(facesContext);
400         link.encodeEnd(facesContext);
401     }
402
403     protected void renderPaginator(FacesContext facesContext,
404                                    UIComponent uiComponent,
405                                    Element JavaDoc paginatorTable,
406                                    DOMContext domContext) throws IOException JavaDoc {
407         DataPaginator scroller = (DataPaginator) uiComponent;
408         int maxPages = scroller.getPaginatorMaxPages();
409         if (maxPages <= 1) {
410             maxPages = 2;
411         }
412         int pageCount = scroller.getPageCount();
413         if (pageCount <= 1) {
414             return;
415         }
416         int pageIndex = scroller.getPageIndex();
417         if (pageIndex > pageCount) {
418             pageIndex = pageCount;
419         }
420         int delta = maxPages / 2;
421         int pages;
422         int start;
423         if (pageCount > maxPages && pageIndex > delta) {
424             pages = maxPages;
425             start = pageIndex - pages / 2 - 1;
426             if (start + pages > pageCount) {
427                 start = pageCount - pages;
428             }
429         } else {
430             pages = pageCount < maxPages ? pageCount : maxPages;
431             start = 0;
432         }
433         String JavaDoc styleClass = scroller.getPaginatorTableClass();
434         paginatorTable.setAttribute(HTML.CLASS_ATTR, styleClass);
435
436         Element JavaDoc tr = null;
437         if (!scroller.isVertical()) {
438             tr = domContext.createElement(HTML.TR_ELEM);
439             paginatorTable.appendChild(tr);
440         }
441         Element JavaDoc td;
442         UIComponent form = findForm(scroller);
443         String JavaDoc formId = null;
444         if (form == null) {
445             throw new FacesException("Form tag is missing");
446         } else {
447             formId = form.getClientId(facesContext);
448         }
449         for (int i = start, size = start + pages; i < size; i++) {
450             int idx = i + 1;
451             if (scroller.isVertical()) {
452                 tr = domContext.createElement(HTML.TR_ELEM);
453                 paginatorTable.appendChild(tr);
454             }
455             td = domContext.createElement(HTML.TD_ELEM);
456             tr.appendChild(td);
457             domContext.setCursorParent(td);
458             String JavaDoc cStyleClass;
459
460             if (idx == pageIndex) {
461                 cStyleClass = scroller.getPaginatorActiveColumnClass();
462
463             } else {
464                 cStyleClass = scroller.getPaginatorColumnClass();
465
466             }
467             if (cStyleClass != null) {
468                 td.setAttribute(HTML.CLASS_ATTR, cStyleClass);
469             }
470
471             Element JavaDoc link = getLink(facesContext, domContext, scroller,
472                                    Integer.toString(idx), idx, formId);
473             td.appendChild(link);
474         }
475     }
476
477     protected Element JavaDoc getLink(FacesContext facesContext, DOMContext domContext,
478                               DataPaginator scroller,
479                               String JavaDoc text, int pageIndex, String JavaDoc formId) {
480
481         Element JavaDoc link = domContext.createElement(HTML.ANCHOR_ELEM);
482         if (text != null) {
483             link.appendChild(domContext.createTextNode(text));
484         }
485         String JavaDoc linkid = scroller.getClientId(facesContext) +
486                         DataPaginatorRenderer.PAGE_NAVIGATION +
487                         Integer.toString(pageIndex);
488         String JavaDoc onClick = /*"document.forms['"+ formId + "']" + "['"+ formId +":_idcl']" + ".value='" + linkid + "'"+
489                 ";*/
"document.forms['" + formId + "']['" +
490               scroller.getClientId(facesContext) + "'].value='" +
491               DataPaginatorRenderer.PAGE_NAVIGATION + text + "'" +
492               ";iceSubmit(" + " document.forms['" + formId + "']," +
493               " this,event); " + "return false;";
494         link.setAttribute(HTML.ID_ATTR, linkid);
495         if (scroller.isDisabled()) {
496             link.removeAttribute(HTML.ONCLICK_ATTR);
497         } else {
498             link.setAttribute(HTML.ONCLICK_ATTR, onClick);
499         }
500         link.setAttribute(HTML.HREF_ATTR, "#");
501         PassThruAttributeRenderer.renderOnFocus(scroller, link);
502         PassThruAttributeRenderer.renderOnBlur(link);
503         return link;
504     }
505
506     protected HtmlCommandLink getLink(FacesContext facesContext,
507                                       DataPaginator scroller,
508                                       String JavaDoc facetName) {
509         Application application = facesContext.getApplication();
510
511         HtmlCommandLink link = (HtmlCommandLink) application
512                 .createComponent(HtmlCommandLink.COMPONENT_TYPE);
513         link.setId(scroller.getId() + facetName);
514         link.setTransient(true);
515         UIParameter parameter = (UIParameter) application
516                 .createComponent(UIParameter.COMPONENT_TYPE);
517         parameter.setId(scroller.getId() + facetName + "_param");
518         parameter.setTransient(true);
519         parameter.setName(scroller.getClientId(facesContext));
520         parameter.setValue(facetName);
521         List JavaDoc children = link.getChildren();
522         children.add(parameter);
523         scroller.getChildren().add(link);
524         return link;
525     }
526 }
527
Popular Tags