001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033: /* Original Copyright
034: * Copyright 2004 The Apache Software Foundation.
035: *
036: * Licensed under the Apache License, Version 2.0 (the "License");
037: * you may not use this file except in compliance with the License.
038: * You may obtain a copy of the License at
039: *
040: * http://www.apache.org/licenses/LICENSE-2.0
041: *
042: * Unless required by applicable law or agreed to in writing, software
043: * distributed under the License is distributed on an "AS IS" BASIS,
044: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
045: * See the License for the specific language governing permissions and
046: * limitations under the License.
047: */
048:
049: package com.icesoft.faces.component.datapaginator;
050:
051: import com.icesoft.faces.component.ext.HtmlCommandLink;
052: import com.icesoft.faces.component.util.CustomComponentUtils;
053: import com.icesoft.faces.context.DOMContext;
054: import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
055: import com.icesoft.faces.renderkit.dom_html_basic.HTML;
056: import com.icesoft.faces.renderkit.dom_html_basic.PassThruAttributeRenderer;
057: import org.w3c.dom.Element;
058:
059: import javax.faces.FacesException;
060: import javax.faces.application.Application;
061: import javax.faces.component.UIComponent;
062: import javax.faces.component.UIParameter;
063: import javax.faces.context.FacesContext;
064: import java.io.IOException;
065: import java.util.List;
066: import java.util.Map;
067:
068: public class DataPaginatorRenderer extends DomBasicRenderer {
069: public static final String RENDERER_TYPE = "com.icesoft.faces.DataScroller";
070:
071: protected static final String PAGE_NAVIGATION = "idx".intern();
072:
073: public boolean getRendersChildren() {
074: return true;
075: }
076:
077: public void decode(FacesContext context, UIComponent component) {
078: validateParameters(context, component, DataPaginator.class);
079: Map parameter = context.getExternalContext()
080: .getRequestParameterMap();
081: String param = (String) parameter.get(component
082: .getClientId(context));
083: if (param != null && param.length() >= PAGE_NAVIGATION.length()) {
084: if (param.startsWith(PAGE_NAVIGATION)) {
085: // queue a navigation event that results from the user pressing
086: // on one of the page indexes
087: component.queueEvent(new PaginatorActionEvent(
088: component, Integer.parseInt(param.substring(
089: PAGE_NAVIGATION.length(), param
090: .length()))));
091: } else {
092: // queue a navigation event that results from the user pressing
093: // on the navigation buttons
094: component.queueEvent(new PaginatorActionEvent(
095: component, param));
096: }
097: }
098: }
099:
100: protected void setVariables(FacesContext facescontext,
101: DataPaginator scroller) throws IOException {
102: Map requestMap = facescontext.getExternalContext()
103: .getRequestMap();
104:
105: String pageCountVar = scroller.getPageCountVar();
106: if (pageCountVar != null) {
107: int pageCount = scroller.getPageCount();
108: requestMap.put(pageCountVar, new Integer(pageCount));
109: }
110: String pageIndexVar = scroller.getPageIndexVar();
111: if (pageIndexVar != null) {
112: int pageIndex = scroller.getPageIndex();
113: if (pageIndex > scroller.getPageCount()) {
114: pageIndex = scroller.getPageCount();
115: }
116: requestMap.put(pageIndexVar, new Integer(pageIndex));
117: }
118: String rowsCountVar = scroller.getRowsCountVar();
119: if (rowsCountVar != null) {
120: int rowsCount = scroller.getRowCount();
121: requestMap.put(rowsCountVar, new Integer(rowsCount));
122: }
123: String displayedRowsCountVar = scroller
124: .getDisplayedRowsCountVar();
125: if (displayedRowsCountVar != null) {
126: int displayedRowsCount = scroller.getRows();
127: int max = scroller.getRowCount() - scroller.getFirstRow();
128: if (displayedRowsCount > max) {
129: displayedRowsCount = max;
130: }
131: requestMap.put(displayedRowsCountVar, new Integer(
132: displayedRowsCount));
133: }
134: String firstRowIndexVar = scroller.getFirstRowIndexVar();
135: if (firstRowIndexVar != null) {
136: int firstRowIndex = scroller.getFirstRow();
137: if (scroller.getRowCount() > 0) {
138: firstRowIndex += 1;
139: }
140: requestMap
141: .put(firstRowIndexVar, new Integer(firstRowIndex));
142: }
143: String lastRowIndexVar = scroller.getLastRowIndexVar();
144: if (lastRowIndexVar != null) {
145: int lastRowIndex = scroller.getFirstRow()
146: + scroller.getRows();
147: int count = scroller.getRowCount();
148: if (lastRowIndex > count) {
149: lastRowIndex = count;
150: }
151: requestMap.put(lastRowIndexVar, new Integer(lastRowIndex));
152: }
153: }
154:
155: public void removeVariables(FacesContext facescontext,
156: UIComponent uiComponent) throws IOException {
157: DataPaginator scroller = (DataPaginator) uiComponent;
158: Map requestMap = facescontext.getExternalContext()
159: .getRequestMap();
160:
161: String pageCountVar = scroller.getPageCountVar();
162: if (pageCountVar != null) {
163: requestMap.remove(pageCountVar);
164: }
165: String pageIndexVar = scroller.getPageIndexVar();
166: if (pageIndexVar != null) {
167: requestMap.remove(pageIndexVar);
168: }
169: String rowsCountVar = scroller.getRowsCountVar();
170: if (rowsCountVar != null) {
171: requestMap.remove(rowsCountVar);
172: }
173: String displayedRowsCountVar = scroller
174: .getDisplayedRowsCountVar();
175: if (displayedRowsCountVar != null) {
176: requestMap.remove(displayedRowsCountVar);
177: }
178: String firstRowIndexVar = scroller.getFirstRowIndexVar();
179: if (firstRowIndexVar != null) {
180: requestMap.remove(firstRowIndexVar);
181: }
182: String lastRowIndexVar = scroller.getLastRowIndexVar();
183: if (lastRowIndexVar != null) {
184: requestMap.remove(lastRowIndexVar);
185: }
186: }
187:
188: public void encodeBegin(FacesContext facesContext,
189: UIComponent uiComponent) throws IOException {
190: validateParameters(facesContext, uiComponent,
191: DataPaginator.class);
192: DataPaginator scroller = (DataPaginator) uiComponent;
193: if (!scroller.isModelResultSet()) {
194: super .encodeBegin(facesContext, uiComponent);
195:
196: // clear children before render
197: List kids = scroller.getChildren();
198: for (int i = 0; i < kids.size(); i++) {
199: UIComponent kid = (UIComponent) kids.get(i);
200: // do not remove the output text - bug #333
201: if (!kid.getFamily().equalsIgnoreCase(
202: "javax.faces.Output")) {
203: scroller.getChildren().remove(kids.get(i));
204: }
205: }
206: //Reset the dataTable model before setting variables
207: scroller.getUIData().setValue(null);
208: scroller.getUIData().setRowIndex(-1);
209: setVariables(facesContext, scroller);
210: }
211: }
212:
213: public void encodeChildren(FacesContext facescontext,
214: UIComponent uicomponent) throws IOException {
215: validateParameters(facescontext, uicomponent,
216: DataPaginator.class);
217: DataPaginator scroller = (DataPaginator) uicomponent;
218: if (!scroller.isModelResultSet()) {
219: boolean singlePageScroller = scroller.getPageCount() <= 1
220: && scroller.getRowsCountVar() == null
221: && scroller.getDisplayedRowsCountVar() == null;
222: if (!singlePageScroller) {
223: CustomComponentUtils.renderChildren(facescontext,
224: uicomponent);
225: }
226: }
227: }
228:
229: public void encodeEnd(FacesContext facesContext,
230: UIComponent uiComponent) throws IOException {
231: validateParameters(facesContext, uiComponent,
232: DataPaginator.class);
233: DataPaginator scroller = (DataPaginator) uiComponent;
234: if (scroller.getUIData() == null) {
235: return;
236: }
237: renderScroller(facesContext, uiComponent);
238: if (!scroller.isModelResultSet()) {
239: removeVariables(facesContext, uiComponent);
240: }
241: }
242:
243: protected void renderScroller(FacesContext facesContext,
244: UIComponent uiComponent) throws IOException {
245: DataPaginator scroller = (DataPaginator) uiComponent;
246: if (!scroller.isModelResultSet()) {
247: if (!scroller.isRenderFacetsIfSinglePage()
248: && scroller.getPageCount() <= 1) {
249: return;
250: }
251: }
252: if (scroller.getFacets().size() <= 0) {
253: return;
254: }
255:
256: DOMContext domContext = DOMContext.attachDOMContext(
257: facesContext, scroller);
258: if (!domContext.isInitialized()) {
259: Element table = domContext
260: .createRootElement(HTML.TABLE_ELEM);
261: setRootElementId(facesContext, table, scroller);
262: if (PassThruAttributeRenderer
263: .passThruAttributeExists(scroller)) {
264: PassThruAttributeRenderer.renderAttributes(
265: facesContext, scroller, null);
266: }
267: }
268: Element table = (Element) domContext.getRootNode();
269: DOMContext.removeChildren(table);
270:
271: Element tr = domContext.createElement(HTML.TR_ELEM);
272: table.appendChild(tr);
273: Element td;
274:
275: String styleClass = scroller.getStyleClass();
276: table.setAttribute(HTML.CLASS_ATTR, styleClass);
277:
278: String style = scroller.getStyle();
279: if (style != null && style.length() > 0)
280: table.setAttribute(HTML.STYLE_ATTR, style);
281: else
282: table.removeAttribute(HTML.STYLE_ATTR);
283: String scrollButtonCellClass = scroller
284: .getscrollButtonCellClass();
285:
286: UIComponent facetComp = scroller.getFirst();
287: if (facetComp != null) {
288: td = domContext.createElement(HTML.TD_ELEM);
289: td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
290: tr.appendChild(td);
291: domContext.setCursorParent(td);
292: domContext.streamWrite(facesContext, uiComponent,
293: domContext.getRootNode(), td);
294: renderFacet(facesContext, scroller, facetComp,
295: DataPaginator.FACET_FIRST);
296: }
297:
298: facetComp = scroller.getFastRewind();
299: if (facetComp != null) {
300: if (scroller.isVertical()) {
301: tr = domContext.createElement(HTML.TR_ELEM);
302: table.appendChild(tr);
303: }
304: td = domContext.createElement(HTML.TD_ELEM);
305: td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
306: tr.appendChild(td);
307: domContext.setCursorParent(td);
308: domContext.streamWrite(facesContext, uiComponent,
309: domContext.getRootNode(), td);
310: renderFacet(facesContext, scroller, facetComp,
311: DataPaginator.FACET_FAST_REWIND);
312: }
313:
314: facetComp = scroller.getPrevious();
315: if (facetComp != null) {
316: if (scroller.isVertical()) {
317: tr = domContext.createElement(HTML.TR_ELEM);
318: table.appendChild(tr);
319: }
320: td = domContext.createElement(HTML.TD_ELEM);
321: td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
322: tr.appendChild(td);
323: domContext.setCursorParent(td);
324: domContext.streamWrite(facesContext, uiComponent,
325: domContext.getRootNode(), td);
326: renderFacet(facesContext, scroller, facetComp,
327: DataPaginator.FACET_PREVIOUS);
328: }
329: if (!scroller.isModelResultSet() && scroller.isPaginator()) {
330: if (scroller.isVertical()) {
331: tr = domContext.createElement(HTML.TR_ELEM);
332: table.appendChild(tr);
333: }
334: td = domContext.createElement(HTML.TD_ELEM);
335: tr.appendChild(td);
336: Element paginatorTable = domContext
337: .createElement(HTML.TABLE_ELEM);
338: td.appendChild(paginatorTable);
339: renderPaginator(facesContext, uiComponent, paginatorTable,
340: domContext);
341: }
342: facetComp = scroller.getNext();
343: if (facetComp != null) {
344: if (scroller.isVertical()) {
345: tr = domContext.createElement(HTML.TR_ELEM);
346: table.appendChild(tr);
347: }
348: td = domContext.createElement(HTML.TD_ELEM);
349: td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
350: tr.appendChild(td);
351: domContext.setCursorParent(td);
352: domContext.streamWrite(facesContext, uiComponent,
353: domContext.getRootNode(), td);
354: renderFacet(facesContext, scroller, facetComp,
355: DataPaginator.FACET_NEXT);
356: }
357:
358: facetComp = scroller.getFastForward();
359: if (facetComp != null) {
360: if (scroller.isVertical()) {
361: tr = domContext.createElement(HTML.TR_ELEM);
362: table.appendChild(tr);
363: }
364: td = domContext.createElement(HTML.TD_ELEM);
365: td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
366: tr.appendChild(td);
367: domContext.setCursorParent(td);
368: domContext.streamWrite(facesContext, uiComponent,
369: domContext.getRootNode(), td);
370: renderFacet(facesContext, scroller, facetComp,
371: DataPaginator.FACET_FAST_FORWARD);
372: }
373:
374: facetComp = scroller.getLast();
375: if (facetComp != null) {
376: if (scroller.isVertical()) {
377: tr = domContext.createElement(HTML.TR_ELEM);
378: table.appendChild(tr);
379: }
380: td = domContext.createElement(HTML.TD_ELEM);
381: td.setAttribute(HTML.CLASS_ATTR, scrollButtonCellClass);
382: tr.appendChild(td);
383: domContext.setCursorParent(td);
384: domContext.streamWrite(facesContext, uiComponent,
385: domContext.getRootNode(), td);
386: renderFacet(facesContext, scroller, facetComp,
387: DataPaginator.FACET_LAST);
388: }
389: domContext.stepOver();
390: domContext.streamWrite(facesContext, uiComponent);
391: }
392:
393: protected void renderFacet(FacesContext facesContext,
394: DataPaginator scroller, UIComponent facetComp,
395: String facetName) throws IOException {
396: HtmlCommandLink link = (HtmlCommandLink) getLink(facesContext,
397: scroller, facetName);
398:
399: if (scroller.isDisabled()
400: || (!scroller.isModelResultSet() && scroller
401: .getPageCount() <= 1)) {
402: link.setDisabled(true);
403: } else {
404: link.setDisabled(false);
405: }
406:
407: link.encodeBegin(facesContext);
408: facetComp.encodeBegin(facesContext);
409: if (facetComp.getRendersChildren()) {
410: facetComp.encodeChildren(facesContext);
411: }
412: facetComp.encodeEnd(facesContext);
413: link.encodeEnd(facesContext);
414: }
415:
416: protected void renderPaginator(FacesContext facesContext,
417: UIComponent uiComponent, Element paginatorTable,
418: DOMContext domContext) throws IOException {
419: DataPaginator scroller = (DataPaginator) uiComponent;
420: int maxPages = scroller.getPaginatorMaxPages();
421: if (maxPages <= 1) {
422: maxPages = 2;
423: }
424: int pageCount = scroller.getPageCount();
425: if (pageCount <= 1) {
426: return;
427: }
428: int pageIndex = scroller.getPageIndex();
429: if (pageIndex > pageCount) {
430: pageIndex = pageCount;
431: }
432: int delta = maxPages / 2;
433: int pages;
434: int start;
435: if (pageCount > maxPages && pageIndex > delta) {
436: pages = maxPages;
437: start = pageIndex - pages / 2 - 1;
438: if (start + pages > pageCount) {
439: start = pageCount - pages;
440: }
441: } else {
442: pages = pageCount < maxPages ? pageCount : maxPages;
443: start = 0;
444: }
445: String styleClass = scroller.getPaginatorTableClass();
446: paginatorTable.setAttribute(HTML.CLASS_ATTR, styleClass);
447:
448: Element tr = null;
449: if (!scroller.isVertical()) {
450: tr = domContext.createElement(HTML.TR_ELEM);
451: paginatorTable.appendChild(tr);
452: }
453: Element td;
454: UIComponent form = findForm(scroller);
455: String formId = null;
456: if (form == null) {
457: throw new FacesException("Form tag is missing");
458: } else {
459: formId = form.getClientId(facesContext);
460: }
461: for (int i = start, size = start + pages; i < size; i++) {
462: int idx = i + 1;
463: if (scroller.isVertical()) {
464: tr = domContext.createElement(HTML.TR_ELEM);
465: paginatorTable.appendChild(tr);
466: }
467: td = domContext.createElement(HTML.TD_ELEM);
468: tr.appendChild(td);
469: domContext.setCursorParent(td);
470: String cStyleClass;
471:
472: if (idx == pageIndex) {
473: cStyleClass = scroller.getPaginatorActiveColumnClass();
474:
475: } else {
476: cStyleClass = scroller.getPaginatorColumnClass();
477:
478: }
479: if (cStyleClass != null) {
480: td.setAttribute(HTML.CLASS_ATTR, cStyleClass);
481: }
482:
483: Element link = getLink(facesContext, domContext, scroller,
484: Integer.toString(idx), idx, formId);
485: td.appendChild(link);
486: }
487: }
488:
489: protected Element getLink(FacesContext facesContext,
490: DOMContext domContext, DataPaginator scroller, String text,
491: int pageIndex, String formId) {
492:
493: Element link = domContext.createElement(HTML.ANCHOR_ELEM);
494: if (text != null) {
495: link.appendChild(domContext.createTextNode(text));
496: }
497: String linkid = scroller.getClientId(facesContext)
498: + DataPaginatorRenderer.PAGE_NAVIGATION
499: + Integer.toString(pageIndex);
500: String onClick = /*"document.forms['"+ formId + "']" + "['"+ formId +":_idcl']" + ".value='" + linkid + "'"+
501: ";*/"document.forms['" + formId + "']['"
502: + scroller.getClientId(facesContext) + "'].value='"
503: + DataPaginatorRenderer.PAGE_NAVIGATION + text + "'"
504: + ";iceSubmit(" + " document.forms['" + formId + "'],"
505: + " this,event); " + "return false;";
506: link.setAttribute(HTML.ID_ATTR, linkid);
507: if (scroller.isDisabled()) {
508: link.removeAttribute(HTML.ONCLICK_ATTR);
509: } else {
510: link.setAttribute(HTML.ONCLICK_ATTR, onClick);
511: }
512: link.setAttribute(HTML.HREF_ATTR, "#");
513: PassThruAttributeRenderer.renderOnFocus(scroller, link);
514: PassThruAttributeRenderer.renderOnBlur(link);
515: return link;
516: }
517:
518: protected HtmlCommandLink getLink(FacesContext facesContext,
519: DataPaginator scroller, String facetName) {
520: Application application = facesContext.getApplication();
521:
522: HtmlCommandLink link = (HtmlCommandLink) application
523: .createComponent(HtmlCommandLink.COMPONENT_TYPE);
524: link.setId(scroller.getId() + facetName);
525: link.setTransient(true);
526: UIParameter parameter = (UIParameter) application
527: .createComponent(UIParameter.COMPONENT_TYPE);
528: parameter.setId(scroller.getId() + facetName + "_param");
529: parameter.setTransient(true);
530: parameter.setName(scroller.getClientId(facesContext));
531: parameter.setValue(facetName);
532: List children = link.getChildren();
533: children.add(parameter);
534: scroller.getChildren().add(link);
535: return link;
536: }
537: }
|