0001: /*
0002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
0003: *
0004: * "The contents of this file are subject to the Mozilla Public License
0005: * Version 1.1 (the "License"); you may not use this file except in
0006: * compliance with the License. You may obtain a copy of the License at
0007: * http://www.mozilla.org/MPL/
0008: *
0009: * Software distributed under the License is distributed on an "AS IS"
0010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
0011: * License for the specific language governing rights and limitations under
0012: * the License.
0013: *
0014: * The Original Code is ICEfaces 1.5 open source software code, released
0015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
0016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
0017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
0018: *
0019: * Contributor(s): _____________________.
0020: *
0021: * Alternatively, the contents of this file may be used under the terms of
0022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
0023: * License), in which case the provisions of the LGPL License are
0024: * applicable instead of those above. If you wish to allow use of your
0025: * version of this file only under the terms of the LGPL License and not to
0026: * allow others to use your version of this file under the MPL, indicate
0027: * your decision by deleting the provisions above and replace them with
0028: * the notice and other provisions required by the LGPL License. If you do
0029: * not delete the provisions above, a recipient may use your version of
0030: * this file under either the MPL or the LGPL License."
0031: *
0032: */
0033: /* Original Copyright
0034: * Copyright 2004 The Apache Software Foundation.
0035: *
0036: * Licensed under the Apache License, Version 2.0 (the "License");
0037: * you may not use this file except in compliance with the License.
0038: * You may obtain a copy of the License at
0039: *
0040: * http://www.apache.org/licenses/LICENSE-2.0
0041: *
0042: * Unless required by applicable law or agreed to in writing, software
0043: * distributed under the License is distributed on an "AS IS" BASIS,
0044: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0045: * See the License for the specific language governing permissions and
0046: * limitations under the License.
0047: */
0048: package com.icesoft.faces.component.datapaginator;
0049:
0050: import com.icesoft.faces.component.CSS_DEFAULT;
0051: import com.icesoft.faces.component.ext.taglib.Util;
0052: import com.icesoft.faces.application.D2DViewHandler;
0053: import org.apache.commons.logging.Log;
0054: import org.apache.commons.logging.LogFactory;
0055:
0056: import javax.faces.component.ActionSource;
0057: import javax.faces.component.UIComponent;
0058: import javax.faces.component.UIData;
0059: import javax.faces.component.html.HtmlPanelGroup;
0060: import javax.faces.context.FacesContext;
0061: import javax.faces.el.EvaluationException;
0062: import javax.faces.el.MethodBinding;
0063: import javax.faces.el.ValueBinding;
0064: import javax.faces.event.AbortProcessingException;
0065: import javax.faces.event.ActionEvent;
0066: import javax.faces.event.ActionListener;
0067: import javax.faces.event.FacesEvent;
0068: import javax.faces.event.PhaseId;
0069: import java.sql.ResultSet;
0070:
0071: /**
0072: *
0073: */
0074: public class DataPaginator extends HtmlPanelGroup implements
0075: ActionSource {
0076: private final Log log = LogFactory.getLog(DataPaginator.class);
0077:
0078: private static final String FIRST_FACET_NAME = "first";
0079: private static final String LAST_FACET_NAME = "last";
0080: private static final String NEXT_FACET_NAME = "next";
0081: private static final String PREVIOUS_FACET_NAME = "previous";
0082: private static final String FAST_FORWARD_FACET_NAME = "fastforward";
0083: private static final String FAST_REWIND_FACET_NAME = "fastrewind";
0084:
0085: // just for caching the associated uidata
0086: private transient UIData _UIData;
0087:
0088: private MethodBinding _actionListener;
0089: private Boolean disabled = null;
0090:
0091: /**
0092: * @return COMPONENT_TYPE
0093: */
0094: public String getComponentType() {
0095: return COMPONENT_TYPE;
0096: }
0097:
0098: /**
0099: * @return DEFAULT_RENDERER_TYPE
0100: * @see javax.faces.component.UIComponent#getRendererType()
0101: */
0102: public String getRendererType() {
0103: return DEFAULT_RENDERER_TYPE;
0104: }
0105:
0106: /**
0107: * <p>Set the value of the <code>disabled</code> property.</p>
0108: */
0109: public void setDisabled(boolean disabled) {
0110: this .disabled = new Boolean(disabled);
0111: ValueBinding vb = getValueBinding("disabled");
0112: if (vb != null) {
0113: vb.setValue(getFacesContext(), this .disabled);
0114: this .disabled = null;
0115: }
0116: }
0117:
0118: /**
0119: * <p>Return the value of the <code>disabled</code> property.</p>
0120: */
0121: public boolean isDisabled() {
0122: if (!Util.isEnabledOnUserRole(this )) {
0123: return true;
0124: }
0125: if (disabled != null) {
0126: return disabled.booleanValue();
0127: }
0128: ValueBinding vb = getValueBinding("disabled");
0129: Boolean v = vb != null ? (Boolean) vb
0130: .getValue(getFacesContext()) : null;
0131: return v != null ? v.booleanValue() : false;
0132: }
0133:
0134: /**
0135: * @see javax.faces.component.UIComponentBase#queueEvent(javax.faces.event.FacesEvent)
0136: */
0137: public void queueEvent(FacesEvent event) {
0138: if (event != null && event instanceof ActionEvent) {
0139: if (isImmediate()) {
0140: event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
0141: } else {
0142: event.setPhaseId(PhaseId.INVOKE_APPLICATION);
0143: }
0144: }
0145: super .queueEvent(event);
0146: }
0147:
0148: /**
0149: * @see javax.faces.component.UIComponentBase#broadcast(javax.faces.event.FacesEvent)
0150: */
0151: public void broadcast(FacesEvent event)
0152: throws AbortProcessingException {
0153: super .broadcast(event);
0154:
0155: if (event instanceof PaginatorActionEvent) {
0156: PaginatorActionEvent scrollerEvent = (PaginatorActionEvent) event;
0157: broadcastToActionListener(scrollerEvent);
0158: UIData uiData = getUIData();
0159: if (uiData == null) {
0160: return;
0161: }
0162:
0163: int pageindex = scrollerEvent.getPageIndex();
0164: if (pageindex == -1) {
0165: String facet = scrollerEvent.getScrollerfacet();
0166: if (FACET_FIRST.equals(facet)) {
0167: gotoFirstPage();
0168: } else if (FACET_PREVIOUS.equals(facet)) {
0169: gotoPreviousPage();
0170: } else if (FACET_NEXT.equals(facet)) {
0171: gotoNextPage();
0172: } else if (FACET_FAST_FORWARD.equals(facet)) {
0173: gotoFastForward();
0174: } else if (FACET_FAST_REWIND.equals(facet)) {
0175: gotoFastRewind();
0176: } else if (FACET_LAST.equals(facet)) {
0177: gotoLastPage();
0178: }
0179: } else {
0180: int pageCount = getPageCount();
0181: if (pageindex > pageCount) {
0182: pageindex = pageCount;
0183: } else if (pageindex <= 0) {
0184: pageindex = 1;
0185: }
0186: uiData.setFirst(uiData.getRows() * (pageindex - 1));
0187: }
0188: }
0189: }
0190:
0191: protected void broadcastToActionListener(PaginatorActionEvent event) {
0192: FacesContext context = getFacesContext();
0193:
0194: try {
0195: MethodBinding actionListenerBinding = getActionListener();
0196: if (actionListenerBinding != null) {
0197: actionListenerBinding.invoke(context,
0198: new Object[] { event });
0199: }
0200: // super.broadcast(event) does this itself
0201: //ActionListener[] actionListeners = getActionListeners();
0202: //if(actionListeners != null) {
0203: // for(int i = 0; i < actionListeners.length; i++) {
0204: // actionListeners[i].processAction(event);
0205: // }
0206: //}
0207: } catch (EvaluationException e) {
0208: Throwable cause = e.getCause();
0209: if (cause != null
0210: && cause instanceof AbortProcessingException) {
0211: throw (AbortProcessingException) cause;
0212: }
0213: throw e;
0214: }
0215: }
0216:
0217: /**
0218: * <p>Return the instance of the <code>UIData</code> associated to this
0219: * component.</p>
0220: */
0221: public UIData getUIData() {
0222: if (_UIData == null) {
0223: _UIData = findUIData();
0224: }
0225: return _UIData;
0226: }
0227:
0228: public void setUIData(UIData uiData) {
0229: _UIData = uiData;
0230: }
0231:
0232: /**
0233: * <p>Return the value of the <code>pageIndex</code> property.</p>
0234: */
0235: public int getPageIndex() {
0236: UIData uiData = getUIData();
0237: int rows = uiData.getRows();
0238: int pageCount = getPageCount();
0239: int pageIndex = 0;
0240: if (rows > 0) {
0241: pageIndex = uiData.getFirst() / rows + 1;
0242: // if the page index > pageCount then some rows may have been removed
0243: // so we need to show the last page which may be a partial or full page
0244: // worth of data
0245: if (pageIndex > pageCount) {
0246: pageIndex = pageCount;
0247: int rowCount = uiData.getRowCount();
0248: int countOfRecordsOnFinalPage = rowCount % rows;
0249: if (countOfRecordsOnFinalPage == 0) {
0250: // full page of data
0251: if (rowCount - rows >= 0) {
0252: uiData.setFirst(rowCount - rows);
0253: } else {
0254: uiData.setFirst(0);
0255: }
0256: } else {
0257: // partial page of data
0258: uiData.setFirst(rowCount
0259: - countOfRecordsOnFinalPage);
0260: }
0261: }
0262: } else {
0263: log.warn("DataTable "
0264: + uiData.getClientId(FacesContext
0265: .getCurrentInstance())
0266: + " has invalid rows attribute.");
0267: pageIndex = 0;
0268: }
0269:
0270: if (uiData.getFirst() % rows > 0) {
0271: pageIndex++;
0272: }
0273: return pageIndex;
0274: }
0275:
0276: /**
0277: * <p>Return the value of the <code>pageCount</code> property.</p>
0278: */
0279: public int getPageCount() {
0280: UIData uiData = getUIData();
0281: int rows = uiData.getRows();
0282:
0283: int pageCount;
0284: if (rows > 0) {
0285: pageCount = rows <= 0 ? 1 : uiData.getRowCount() / rows;
0286: if (uiData.getRowCount() % rows > 0) {
0287: pageCount++;
0288: }
0289: } else {
0290: rows = 1;
0291: pageCount = 1;
0292: }
0293: return pageCount;
0294: }
0295:
0296: /**
0297: * <p>Return the value of the <code>rowCount</code> property.</p>
0298: */
0299: public int getRowCount() {
0300: return getUIData().getRowCount();
0301: }
0302:
0303: /**
0304: * <p>Return the value of the <code>rows</code> property.</p>
0305: */
0306: public int getRows() {
0307: return getUIData().getRows();
0308: }
0309:
0310: /**
0311: * <p>Return the value of the <code>first</code> property.</p>
0312: */
0313: public int getFirstRow() {
0314: return getUIData().getFirst();
0315: }
0316:
0317: /**
0318: * <p>Return the instance of <code>UIData</code> associated to this
0319: * component.</p>
0320: */
0321: protected UIData findUIData() {
0322: String forStr = getFor();
0323: UIComponent forComp;
0324: if (forStr == null) {
0325: // DataPaginator may be a child of uiData
0326: forComp = getParent();
0327: } else {
0328:
0329: //forComp = findComponent(forStr);
0330:
0331: forComp = D2DViewHandler.findComponent(forStr, this );
0332: }
0333: if (forComp == null) {
0334: throw new IllegalArgumentException(
0335: "could not find UIData referenced by attribute dataScroller@for = '"
0336: + forStr + "'");
0337: } else if (!(forComp instanceof UIData)) {
0338: throw new IllegalArgumentException(
0339: "uiComponent referenced by attribute dataScroller@for = '"
0340: + forStr + "' must be of type "
0341: + UIData.class.getName() + ", not type "
0342: + forComp.getClass().getName());
0343: }
0344: return (UIData) forComp;
0345: }
0346:
0347: /**
0348: * <p>Set the value of the <code>first</code> facet for this component.</p>
0349: */
0350: public void setFirst(UIComponent first) {
0351: getFacets().put(FIRST_FACET_NAME, first);
0352: }
0353:
0354: /**
0355: * <p>Return the value of the <code>first</code> facet of this
0356: * component.</p>
0357: */
0358: public UIComponent getFirst() {
0359: return (UIComponent) getFacets().get(FIRST_FACET_NAME);
0360: }
0361:
0362: /**
0363: * <p>Set the value of the <code>last</code> facet for this component.</p>
0364: */
0365: public void setLast(UIComponent last) {
0366: getFacets().put(LAST_FACET_NAME, last);
0367: }
0368:
0369: /**
0370: * <p>Return the value of the <code>last</code> facet of this
0371: * component.</p>
0372: */
0373: public UIComponent getLast() {
0374: return (UIComponent) getFacets().get(LAST_FACET_NAME);
0375: }
0376:
0377: /**
0378: * <p>Set the value of the <code>next</code> facet for this component.</p>
0379: */
0380: public void setNext(UIComponent next) {
0381: getFacets().put(NEXT_FACET_NAME, next);
0382: }
0383:
0384: /**
0385: * <p>Return the value of the <code>next</code> facet of this
0386: * component.</p>
0387: */
0388: public UIComponent getNext() {
0389: return (UIComponent) getFacets().get(NEXT_FACET_NAME);
0390: }
0391:
0392: /**
0393: * <p>Set the value of the <code>fast forward</code> facet for this
0394: * component.</p>
0395: */
0396: public void setFastForward(UIComponent previous) {
0397: getFacets().put(FAST_FORWARD_FACET_NAME, previous);
0398: }
0399:
0400: /**
0401: * <p>Return the value of the <code>fast forward</code> facet of this
0402: * component.</p>
0403: */
0404: public UIComponent getFastForward() {
0405: return (UIComponent) getFacets().get(FAST_FORWARD_FACET_NAME);
0406: }
0407:
0408: /**
0409: * <p>Set the value of the <code>fast rewind</code> facet for this
0410: * component.</p>
0411: */
0412: public void setFastRewind(UIComponent previous) {
0413: getFacets().put(FAST_REWIND_FACET_NAME, previous);
0414: }
0415:
0416: /**
0417: * <p>Return the value of the <code>fast rewind</code> facet of this
0418: * component.</p>
0419: */
0420: public UIComponent getFastRewind() {
0421: return (UIComponent) getFacets().get(FAST_REWIND_FACET_NAME);
0422: }
0423:
0424: /**
0425: * <p>Set the value of the <code>previous</code> facet for this
0426: * component.</p>
0427: */
0428: public void setPrevious(UIComponent previous) {
0429: getFacets().put(PREVIOUS_FACET_NAME, previous);
0430: }
0431:
0432: /**
0433: * <p>Return the value of the <code>previous</code> facet of this
0434: * component.</p>
0435: */
0436: public UIComponent getPrevious() {
0437: return (UIComponent) getFacets().get(PREVIOUS_FACET_NAME);
0438: }
0439:
0440: /**
0441: * <p>Return a flag indicating whether this component is responsible for
0442: * rendering its child components.</p>
0443: */
0444: public boolean getRendersChildren() {
0445: return true;
0446: }
0447:
0448: /**
0449: * @see javax.faces.component.ActionSource#getAction()
0450: */
0451: public MethodBinding getAction() {
0452: // not used
0453: return null;
0454: }
0455:
0456: /**
0457: * @see javax.faces.component.ActionSource#setAction(javax.faces.el.MethodBinding)
0458: */
0459: public void setAction(MethodBinding action) {
0460: throw new UnsupportedOperationException(
0461: "Defining an action is not supported. Use an actionListener");
0462: }
0463:
0464: /**
0465: * @see javax.faces.component.ActionSource#setActionListener(javax.faces.el.MethodBinding)
0466: */
0467: public void setActionListener(MethodBinding actionListener) {
0468: _actionListener = actionListener;
0469: }
0470:
0471: /**
0472: * @see javax.faces.component.ActionSource#getActionListener()
0473: */
0474: public MethodBinding getActionListener() {
0475: return _actionListener;
0476: }
0477:
0478: /**
0479: * @see javax.faces.component.ActionSource#addActionListener(javax.faces.event.ActionListener)
0480: */
0481: public void addActionListener(ActionListener listener) {
0482: addFacesListener(listener);
0483: }
0484:
0485: /**
0486: * @see javax.faces.component.ActionSource#getActionListeners()
0487: */
0488: public ActionListener[] getActionListeners() {
0489: return (ActionListener[]) getFacesListeners(ActionListener.class);
0490: }
0491:
0492: /**
0493: * @see javax.faces.component.ActionSource#removeActionListener(javax.faces.event.ActionListener)
0494: */
0495: public void removeActionListener(ActionListener listener) {
0496: removeFacesListener(listener);
0497: }
0498:
0499: public static final String COMPONENT_TYPE = "com.icesoft.faces.DataScroller";
0500: public static final String COMPONENT_FAMILY = "javax.faces.Panel";
0501: private static final String DEFAULT_RENDERER_TYPE = "com.icesoft.faces.DataScroller";
0502: private static final boolean DEFAULT_IMMEDIATE = false;
0503: private static final boolean DEFAULT_VERTICAL = false;
0504: private String _for = null;
0505: private Integer _fastStep = null;
0506: private String _pageIndexVar = null;
0507: private String _pageCountVar = null;
0508: private String _rowsCountVar = null;
0509: private String _displayedRowsCountVar = null;
0510: private String _firstRowIndexVar = null;
0511: private String _lastRowIndexVar = null;
0512: private String _style = null;
0513: private String _styleClass = null;
0514: private String _columnClasses = null;
0515: private Boolean _paginator = null;
0516: private Integer _paginatorMaxPages = null;
0517: private Boolean _renderFacetsIfSinglePage = null;
0518: private Boolean _immediate;
0519: private Boolean _vertical;
0520: public static final String FACET_FIRST = "first".intern();
0521: public static final String FACET_PREVIOUS = "previous".intern();
0522: public static final String FACET_NEXT = "next".intern();
0523: public static final String FACET_LAST = "last".intern();
0524: public static final String FACET_FAST_FORWARD = "fastf".intern();
0525: public static final String FACET_FAST_REWIND = "fastr".intern();
0526:
0527: public DataPaginator() {
0528: setRendererType(DEFAULT_RENDERER_TYPE);
0529: }
0530:
0531: /* (non-Javadoc)
0532: * @see javax.faces.component.UIComponent#getFamily()
0533: */
0534: public String getFamily() {
0535: return COMPONENT_FAMILY;
0536: }
0537:
0538: /**
0539: * <p>Set the value of the <code>for</code> property.</p>
0540: */
0541: public void setFor(String forValue) {
0542: _for = forValue;
0543: }
0544:
0545: /**
0546: * <p>Return the value of the <code>for</code> property.</p>
0547: */
0548: public String getFor() {
0549: if (_for != null) {
0550: return _for;
0551: }
0552: ValueBinding vb = getValueBinding("for");
0553: return vb != null ? (String) vb.getValue(getFacesContext())
0554: : null;
0555: }
0556:
0557: /**
0558: * <p>Set the value of the <code>fastStep</code> property.</p>
0559: */
0560: public void setFastStep(int fastStep) {
0561: _fastStep = new Integer(fastStep);
0562: }
0563:
0564: /**
0565: * <p>Return the value of the <code>fastStep</code> property.</p>
0566: */
0567: public int getFastStep() {
0568: if (_fastStep != null) {
0569: return _fastStep.intValue();
0570: }
0571: ValueBinding vb = getValueBinding("fastStep");
0572: Integer v = vb != null ? (Integer) vb
0573: .getValue(getFacesContext()) : null;
0574: return v != null ? v.intValue() : Integer.MIN_VALUE;
0575: }
0576:
0577: /**
0578: * <p>Set the value of the <code>pageIndexVar</code> property.</p>
0579: */
0580: public void setPageIndexVar(String pageIndexVar) {
0581: _pageIndexVar = pageIndexVar;
0582: }
0583:
0584: /**
0585: * <p>Return the value of the <code>pageIndexVar</code> property.</p>
0586: */
0587: public String getPageIndexVar() {
0588: if (_pageIndexVar != null) {
0589: return _pageIndexVar;
0590: }
0591: ValueBinding vb = getValueBinding("pageIndexVar");
0592: return vb != null ? (String) vb.getValue(getFacesContext())
0593: : null;
0594: }
0595:
0596: /**
0597: * <p>Set the value of the <code>pageCountVar</code> property.</p>
0598: */
0599: public void setPageCountVar(String pageCountVar) {
0600: _pageCountVar = pageCountVar;
0601: }
0602:
0603: /**
0604: * <p>Return the value of the <code>pageCountVar</code> property.</p>
0605: */
0606: public String getPageCountVar() {
0607: if (_pageCountVar != null) {
0608: return _pageCountVar;
0609: }
0610: ValueBinding vb = getValueBinding("pageCountVar");
0611: return vb != null ? (String) vb.getValue(getFacesContext())
0612: : null;
0613: }
0614:
0615: /**
0616: * <p>Set the value of the <code>rowsCountVar</code> property.</p>
0617: */
0618: public void setRowsCountVar(String rowsCountVar) {
0619: _rowsCountVar = rowsCountVar;
0620: }
0621:
0622: /**
0623: * <p>Return the value of the <code>rowsCountVar</code> property.</p>
0624: */
0625: public String getRowsCountVar() {
0626: if (_rowsCountVar != null) {
0627: return _rowsCountVar;
0628: }
0629: ValueBinding vb = getValueBinding("rowsCountVar");
0630: return vb != null ? (String) vb.getValue(getFacesContext())
0631: : null;
0632: }
0633:
0634: /**
0635: * <p>Set the value of the <code>displayedRowsCountVar</code> property.</p>
0636: */
0637: public void setDisplayedRowsCountVar(String displayedRowsCountVar) {
0638: _displayedRowsCountVar = displayedRowsCountVar;
0639: }
0640:
0641: /**
0642: * <p>Return the value of the <code>displayedRowsCountVar</code>
0643: * property.</p>
0644: */
0645: public String getDisplayedRowsCountVar() {
0646: if (_displayedRowsCountVar != null) {
0647: return _displayedRowsCountVar;
0648: }
0649: ValueBinding vb = getValueBinding("displayedRowsCountVar");
0650: return vb != null ? (String) vb.getValue(getFacesContext())
0651: : null;
0652: }
0653:
0654: /**
0655: * <p>Set the value of the <code>firstRowIndexVar</code> property.</p>
0656: */
0657: public void setFirstRowIndexVar(String firstRowIndexVar) {
0658: _firstRowIndexVar = firstRowIndexVar;
0659: }
0660:
0661: /**
0662: * <p>Return the value of the <code>firstRowIndexVar</code> property.</p>
0663: */
0664: public String getFirstRowIndexVar() {
0665: if (_firstRowIndexVar != null) {
0666: return _firstRowIndexVar;
0667: }
0668: ValueBinding vb = getValueBinding("firstRowIndexVar");
0669: return vb != null ? (String) vb.getValue(getFacesContext())
0670: : null;
0671: }
0672:
0673: /**
0674: * <p>Set the value of the <code>lastRowIndexVar</code> property.</p>
0675: */
0676: public void setLastRowIndexVar(String lastRowIndexVar) {
0677: _lastRowIndexVar = lastRowIndexVar;
0678: }
0679:
0680: /**
0681: * <p>Return the value of the <code>lastRowIndexVar</code> property.</p>
0682: */
0683: public String getLastRowIndexVar() {
0684: if (_lastRowIndexVar != null) {
0685: return _lastRowIndexVar;
0686: }
0687: ValueBinding vb = getValueBinding("lastRowIndexVar");
0688: return vb != null ? (String) vb.getValue(getFacesContext())
0689: : null;
0690: }
0691:
0692: /**
0693: * <p>Set the value of the <code>style</code> property.</p>
0694: */
0695: public void setStyle(String style) {
0696: _style = style;
0697: }
0698:
0699: /**
0700: * <p>Return the value of the <code>style</code> property.</p>
0701: */
0702: public String getStyle() {
0703: if (_style != null) {
0704: return _style;
0705: }
0706: ValueBinding vb = getValueBinding("style");
0707: return vb != null ? (String) vb.getValue(getFacesContext())
0708: : null;
0709: }
0710:
0711: /**
0712: * <p>Set the value of the <code>styleClass</code> property.</p>
0713: */
0714: public void setStyleClass(String styleClass) {
0715: _styleClass = styleClass;
0716: }
0717:
0718: /**
0719: * <p>Return the value of the <code>styleClass</code> property.</p>
0720: */
0721: public String getStyleClass() {
0722: return Util.getQualifiedStyleClass(this , _styleClass,
0723: CSS_DEFAULT.DATA_PAGINATOR_BASE, "styleClass",
0724: isDisabled());
0725: }
0726:
0727: public String getBaseStyleClass() {
0728: return CSS_DEFAULT.DATA_PAGINATOR_BASE;
0729: }
0730:
0731: /**
0732: * <p>Set the value of the <code>paginator</code> property.</p>
0733: */
0734: public void setPaginator(boolean paginator) {
0735: _paginator = Boolean.valueOf(paginator);
0736: }
0737:
0738: /**
0739: * <p>Return the value of the <code>paginator</code> property.</p>
0740: */
0741: public boolean isPaginator() {
0742: if (_paginator != null) {
0743: return _paginator.booleanValue();
0744: }
0745: ValueBinding vb = getValueBinding("paginator");
0746: Boolean v = vb != null ? (Boolean) vb
0747: .getValue(getFacesContext()) : null;
0748: return v != null ? v.booleanValue() : false;
0749: }
0750:
0751: /**
0752: * <p>Set the value of the <code>paginatorMaxPages</code> property.</p>
0753: */
0754: public void setPaginatorMaxPages(int paginatorMaxPages) {
0755: _paginatorMaxPages = new Integer(paginatorMaxPages);
0756: }
0757:
0758: /**
0759: * <p>Return the value of the <code>paginatorMaxPages</code> property.</p>
0760: */
0761: public int getPaginatorMaxPages() {
0762: if (_paginatorMaxPages != null) {
0763: return _paginatorMaxPages.intValue();
0764: }
0765: ValueBinding vb = getValueBinding("paginatorMaxPages");
0766: Integer v = vb != null ? (Integer) vb
0767: .getValue(getFacesContext()) : null;
0768: return v != null ? v.intValue() : Integer.MIN_VALUE;
0769: }
0770:
0771: /**
0772: * <p>Return the value of the <code>paginatorTableClass</code>
0773: * property.</p>
0774: */
0775: public String getPaginatorTableClass() {
0776: return Util.getQualifiedStyleClass(this ,
0777: CSS_DEFAULT.PAGINATOR_TABLE_CLASS, isDisabled());
0778:
0779: }
0780:
0781: /**
0782: * <p>Return the value of the <code>paginatorColumnClass</code>
0783: * property.</p>
0784: */
0785: public String getPaginatorColumnClass() {
0786: return Util.getQualifiedStyleClass(this ,
0787: CSS_DEFAULT.PAGINATOR_COLUMN_CLASS, isDisabled());
0788: }
0789:
0790: /**
0791: * <p>Return the value of the <code>scrollButtonCellClass</code>
0792: * property.</p>
0793: */
0794: public String getscrollButtonCellClass() {
0795: return Util.getQualifiedStyleClass(this ,
0796: CSS_DEFAULT.DATA_PAGINATOR_SCROLL_BUTTON_CELL_CLASS,
0797: isDisabled());
0798: }
0799:
0800: /**
0801: * <p>Return the value of the <code>paginatorActiveColumnClass</code>
0802: * property.</p>
0803: */
0804: public String getPaginatorActiveColumnClass() {
0805: return Util
0806: .getQualifiedStyleClass(this ,
0807: CSS_DEFAULT.PAGINATOR_ACTIVE_COLUMN_CLASS,
0808: isDisabled());
0809: }
0810:
0811: /**
0812: * <p>Set the value of the <code>renderFacetsIfSinglePage</code>
0813: * property.</p>
0814: */
0815: public void setRenderFacetsIfSinglePage(
0816: boolean renderFacetsIfSinglePage) {
0817: _renderFacetsIfSinglePage = Boolean
0818: .valueOf(renderFacetsIfSinglePage);
0819: }
0820:
0821: /**
0822: * <p>Return the value of the <code>renderFacetsIfSinglePage</code>
0823: * property.</p>
0824: */
0825: public boolean isRenderFacetsIfSinglePage() {
0826: if (_renderFacetsIfSinglePage != null) {
0827: return _renderFacetsIfSinglePage.booleanValue();
0828: }
0829: ValueBinding vb = getValueBinding("renderFacetsIfSinglePage");
0830: Boolean v = vb != null ? (Boolean) vb
0831: .getValue(getFacesContext()) : null;
0832: return v != null ? v.booleanValue() : true;
0833: }
0834:
0835: /**
0836: * <p>Set the value of the <code>immediate</code> property.</p>
0837: */
0838: public void setImmediate(boolean immediate) {
0839: _immediate = Boolean.valueOf(immediate);
0840: }
0841:
0842: /**
0843: * <p>Return the value of the <code>immediate</code> property.</p>
0844: */
0845: public boolean isImmediate() {
0846: if (_immediate != null) {
0847: return _immediate.booleanValue();
0848: }
0849: ValueBinding vb = getValueBinding("immediate");
0850: Boolean v = vb != null ? (Boolean) vb
0851: .getValue(getFacesContext()) : null;
0852: return v != null ? v.booleanValue() : DEFAULT_IMMEDIATE;
0853: }
0854:
0855: /**
0856: * <p>Set the value of the <code>vertical</code> property.</p>
0857: */
0858: public void setVertical(boolean vertical) {
0859: _vertical = Boolean.valueOf(vertical);
0860: }
0861:
0862: /**
0863: * <p>Return the value of the <code>vertical</code> property.</p>
0864: */
0865: public boolean isVertical() {
0866: if (_vertical != null) {
0867: return _vertical.booleanValue();
0868: }
0869: ValueBinding vb = getValueBinding("vertical");
0870: Boolean v = vb != null ? (Boolean) vb
0871: .getValue(getFacesContext()) : null;
0872: return v != null ? v.booleanValue() : DEFAULT_VERTICAL;
0873: }
0874:
0875: /**
0876: * <p>Return the value of the <code>rendered</code> property.</p>
0877: */
0878: public boolean isRendered() {
0879: if (!Util.isRenderedOnUserRole(this )) {
0880: return false;
0881: }
0882: return super .isRendered();
0883: }
0884:
0885: /**
0886: * <p>Gets the state of the instance as a <code>Serializable</code>
0887: * Object.</p>
0888: */
0889: public Object saveState(FacesContext context) {
0890: Object values[] = new Object[18];
0891: values[0] = super .saveState(context);
0892: values[1] = _for;
0893: values[2] = _fastStep;
0894: values[3] = _pageIndexVar;
0895: values[4] = _pageCountVar;
0896: values[5] = _rowsCountVar;
0897: values[6] = _displayedRowsCountVar;
0898: values[7] = _firstRowIndexVar;
0899: values[8] = _lastRowIndexVar;
0900: values[9] = _style;
0901: values[10] = _styleClass;
0902: values[11] = _columnClasses;
0903: values[12] = _paginator;
0904: values[13] = _paginatorMaxPages;
0905: values[14] = _renderFacetsIfSinglePage;
0906: values[15] = _immediate;
0907: values[16] = saveAttachedState(context, _actionListener);
0908: values[17] = _vertical;
0909: return values;
0910: }
0911:
0912: /**
0913: * <p>Perform any processing required to restore the state from the entries
0914: * in the state Object.</p>
0915: */
0916: public void restoreState(FacesContext context, Object state) {
0917: Object values[] = (Object[]) state;
0918: super .restoreState(context, values[0]);
0919: _for = (String) values[1];
0920: _fastStep = (Integer) values[2];
0921: _pageIndexVar = (String) values[3];
0922: _pageCountVar = (String) values[4];
0923: _rowsCountVar = (String) values[5];
0924: _displayedRowsCountVar = (String) values[6];
0925: _firstRowIndexVar = (String) values[7];
0926: _lastRowIndexVar = (String) values[8];
0927: _style = (String) values[9];
0928: _styleClass = (String) values[10];
0929: _columnClasses = (String) values[11];
0930: _paginator = (Boolean) values[12];
0931: _paginatorMaxPages = (Integer) values[13];
0932: _renderFacetsIfSinglePage = (Boolean) values[14];
0933: _immediate = (Boolean) values[15];
0934: _actionListener = (MethodBinding) restoreAttachedState(context,
0935: values[16]);
0936: _vertical = (Boolean) values[17];
0937: }
0938:
0939: /**
0940: * Sets the dataPaginator to the first page
0941: */
0942: public void gotoFirstPage() {
0943: getUIData().setFirst(0);
0944: }
0945:
0946: /**
0947: * Sets the dataPaginator to the previous page
0948: */
0949: public void gotoPreviousPage() {
0950: if (isModelResultSet()) {
0951: int first = getUIData().getFirst() - getUIData().getRows();
0952: if (first < 0) {
0953: first = 0;
0954: }
0955: getUIData().setFirst(first);
0956: } else {
0957: int previous = getUIData().getFirst()
0958: - getUIData().getRows();
0959: int rowCount = getRowCount();
0960: if (previous >= 0) {
0961: if (previous > rowCount) {
0962: previous = rowCount;
0963: }
0964: getUIData().setFirst(previous);
0965: }
0966: }
0967: }
0968:
0969: /**
0970: * Sets the dataPaginator to the next page
0971: */
0972: public void gotoNextPage() {
0973: int next = getUIData().getFirst() + getUIData().getRows();
0974: if (isModelResultSet()) {
0975: getUIData().setRowIndex(next);
0976: if (getUIData().isRowAvailable()) {
0977: getUIData().setFirst(next);
0978: }
0979: } else {
0980: if (next < getUIData().getRowCount()) {
0981: getUIData().setFirst(next);
0982: }
0983: }
0984: }
0985:
0986: /**
0987: * Sets the dataPaginator to the (n) number of pages forward, defined by the
0988: * fastStep property
0989: */
0990: public void gotoFastForward() {
0991: int fastStep = getFastStep();
0992: if (fastStep <= 0) {
0993: fastStep = 1;
0994: }
0995: int next = getUIData().getFirst() + getUIData().getRows()
0996: * fastStep;
0997: int rowcount = getUIData().getRowCount();
0998: if (next >= rowcount) {
0999: next = (rowcount - 1)
1000: - ((rowcount - 1) % getUIData().getRows());
1001: }
1002: getUIData().setFirst(next);
1003: }
1004:
1005: /**
1006: * Sets the dataPaginator to the (n) number of pages back, defined by the
1007: * fastStep property
1008: */
1009: public void gotoFastRewind() {
1010: int fastStep = getFastStep();
1011: if (fastStep <= 0) {
1012: fastStep = 1;
1013: }
1014: int previous = getUIData().getFirst() - getUIData().getRows()
1015: * fastStep;
1016: if (previous < 0) {
1017: previous = 0;
1018: }
1019: getUIData().setFirst(previous);
1020: }
1021:
1022: /**
1023: * Sets the dataPaginator to the last page
1024: */
1025: public void gotoLastPage() {
1026: if (isModelResultSet()) {
1027: int first = getUIData().getFirst();
1028: while (true) {
1029: getUIData().setRowIndex(first + 1);
1030: if (getUIData().isRowAvailable()) {
1031: first++;
1032: } else {
1033: break;
1034: }
1035: }
1036: getUIData().setFirst(
1037: first - (first % getUIData().getRows()));
1038: } else {
1039: int rowcount = getUIData().getRowCount();
1040: int rows = getUIData().getRows();
1041: int delta = rowcount % rows;
1042: int first = delta > 0 && delta < rows ? rowcount - delta
1043: : rowcount - rows;
1044: if (first >= 0) {
1045: getUIData().setFirst(first);
1046: } else {
1047: getUIData().setFirst(0);
1048: }
1049: }
1050: }
1051:
1052: /**
1053: * Return a boolean value, whether current page is the last page
1054: */
1055: public boolean isLastPage() {
1056: return (getPageIndex() >= getPageCount()) ? true : false;
1057: }
1058:
1059: private String enabledOnUserRole = null;
1060: private String renderedOnUserRole = null;
1061:
1062: /**
1063: * <p>Set the value of the <code>enabledOnUserRole</code> property.</p>
1064: */
1065: public void setEnabledOnUserRole(String enabledOnUserRole) {
1066: this .enabledOnUserRole = enabledOnUserRole;
1067: }
1068:
1069: /**
1070: * <p>Return the value of the <code>enabledOnUserRole</code> property.</p>
1071: */
1072: public String getEnabledOnUserRole() {
1073: if (enabledOnUserRole != null) {
1074: return enabledOnUserRole;
1075: }
1076: ValueBinding vb = getValueBinding("enabledOnUserRole");
1077: return vb != null ? (String) vb.getValue(getFacesContext())
1078: : null;
1079: }
1080:
1081: /**
1082: * <p>Set the value of the <code>renderedOnUserRole</code> property.</p>
1083: */
1084: public void setRenderedOnUserRole(String renderedOnUserRole) {
1085: this .renderedOnUserRole = renderedOnUserRole;
1086: }
1087:
1088: /**
1089: * <p>Return the value of the <code>renderedOnUserRole</code> property.</p>
1090: */
1091: public String getRenderedOnUserRole() {
1092: if (renderedOnUserRole != null) {
1093: return renderedOnUserRole;
1094: }
1095: ValueBinding vb = getValueBinding("renderedOnUserRole");
1096: return vb != null ? (String) vb.getValue(getFacesContext())
1097: : null;
1098: }
1099:
1100: private Boolean modelResultSet = null;
1101:
1102: public boolean isModelResultSet() {
1103:
1104: if (modelResultSet == null) {
1105: if (getUIData().getValue() instanceof ResultSet) {
1106: modelResultSet = Boolean.TRUE;
1107: } else {
1108: if (getUIData().getValue() instanceof javax.faces.model.DataModel) {
1109: javax.faces.model.DataModel dataModel = (javax.faces.model.DataModel) getUIData()
1110: .getValue();
1111: if (dataModel.getRowCount() == -1) {
1112: modelResultSet = Boolean.TRUE;
1113: return modelResultSet.booleanValue();
1114: }
1115: }
1116: modelResultSet = Boolean.FALSE;
1117: }
1118: }
1119: return modelResultSet.booleanValue();
1120: }
1121: }
|