Search in sources :

Example 1 with Paginator

use of com.liferay.faces.alloy.component.paginator.Paginator in project liferay-faces-alloy by liferay.

the class PaginatorRenderer method encodeBegin.

@Override
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    // Encode the starting <div> element that represents the alloy:paginator.
    Paginator paginator = (Paginator) uiComponent;
    String clientId = paginator.getClientId(facesContext);
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    responseWriter.startElement("div", uiComponent);
    responseWriter.writeAttribute("id", clientId, null);
    RendererUtil.encodeStyleable(responseWriter, (Styleable) uiComponent);
    // Determine the current pagination state of the associated UIData component.
    UIViewRoot viewRoot = facesContext.getViewRoot();
    Locale locale = viewRoot.getLocale();
    UIData uiData = paginator.getUIData();
    int first = uiData.getFirst();
    int rows = uiData.getRows();
    int curPage = (first / rows) + 1;
    int rowCount = uiData.getRowCount();
    int pageCount = (int) Math.ceil((double) rowCount / (double) rows);
    // If the summary is to be positioned above the pagination controls, then encode the summary.
    String summaryPosition = paginator.getSummaryPosition();
    if ("top".equals(summaryPosition)) {
        encodeSummary(facesContext, responseWriter, paginator, summaryPosition, locale, first, curPage, rows, rowCount, pageCount);
    }
    // If any of the pagination controls are the be encoded, then
    boolean showPageNumberControls = paginator.isShowPageNumberControls();
    boolean showFirstPageControl = paginator.isShowFirstPageControl();
    boolean showLastPageControl = paginator.isShowLastPageControl();
    boolean showNextPageControl = paginator.isShowNextPageControl();
    boolean showPreviousPageControl = paginator.isShowPreviousPageControl();
    if (showPageNumberControls || showFirstPageControl || showLastPageControl || showNextPageControl || showPreviousPageControl) {
        // Encode the starting <div> element that represents the Bootstrap pagination component.
        responseWriter.startElement("div", uiComponent);
        // Encode the starting <ul> element that represents the unordered list of pagination controls.
        responseWriter.startElement("ul", uiComponent);
        responseWriter.writeAttribute("class", "pagination", null);
        // summary.
        if ("left".equals(summaryPosition)) {
            encodeSummary(facesContext, responseWriter, paginator, summaryPosition, locale, first, curPage, rows, rowCount, pageCount);
        }
        // Encode the "First Page" pagination control.
        if (showFirstPageControl) {
            encodeFirstPageListItem(facesContext, responseWriter, paginator, clientId, first);
        }
        // Encode the "Previous Page" pagination control.
        if (showPreviousPageControl) {
            encodePrevPageListItem(facesContext, responseWriter, paginator, clientId, first);
        }
        // If pagination controls for page numbers are to be encoded, then
        if (showPageNumberControls) {
            // Determine the first and last page numbers controls.
            int firstPage = paginator.getFirstPage();
            int lastPage = firstPage + paginator.getMaxPageNumberControls() - 1;
            while (curPage < firstPage) {
                firstPage -= 1;
                lastPage -= 1;
            }
            while (curPage > lastPage) {
                firstPage += 1;
                lastPage += 1;
            }
            // Remember the state of the first page number that is displayed.
            paginator.setFirstPage(firstPage);
            if (lastPage > pageCount) {
                lastPage = pageCount;
            }
            // Encode the pagination controls for the appropriate page numbers.
            for (int i = firstPage; i <= lastPage; i++) {
                encodePageNumberListItem(facesContext, responseWriter, paginator, clientId, i, curPage, first, rows, rowCount);
            }
        }
        // Encode the "Next Page" pagination control.
        if (showNextPageControl) {
            encodeNextPageListItem(facesContext, responseWriter, paginator, uiData, clientId);
        }
        // Encode the "Last Page" pagination control.
        if (showLastPageControl) {
            encodeLastPageListItem(facesContext, responseWriter, paginator, uiData, clientId);
        }
        // If the summary is to be positioned on the right of the pagination controls, then encode the summary.
        if ("right".equals(summaryPosition)) {
            encodeSummary(facesContext, responseWriter, paginator, summaryPosition, locale, first, curPage, rows, rowCount, pageCount);
        }
        // Encode the closing </div> element that represents the unordered list of pagination controls.
        responseWriter.endElement("ul");
        // Encode the closing </div> element that represents the Bootstrap pagination component.
        responseWriter.endElement("div");
    }
    // If the summary is to be positioned beneath the pagination controls, then encode the summary.
    if ("bottom".equals(summaryPosition)) {
        encodeSummary(facesContext, responseWriter, paginator, summaryPosition, locale, first, curPage, rows, rowCount, pageCount);
    }
}
Also used : Locale(java.util.Locale) ResponseWriter(javax.faces.context.ResponseWriter) UIViewRoot(javax.faces.component.UIViewRoot) Paginator(com.liferay.faces.alloy.component.paginator.Paginator) UIData(javax.faces.component.UIData)

Example 2 with Paginator

use of com.liferay.faces.alloy.component.paginator.Paginator in project liferay-faces-alloy by liferay.

the class PaginatorRenderer method decode.

@Override
public void decode(FacesContext facesContext, UIComponent uiComponent) {
    boolean activatedPaginationControl = false;
    // Decode the client behaviors.
    RendererUtil.decodeClientBehaviors(facesContext, uiComponent);
    // If there is a UIData component associated with the paginator, then
    Paginator paginator = (Paginator) uiComponent;
    UIData uiData = paginator.getUIData();
    if (uiData != null) {
        // Determine the pagination option selected by the user
        ExternalContext externalContext = facesContext.getExternalContext();
        Map<String, String> requestParameterMap = externalContext.getRequestParameterMap();
        String paginatorActionParamName = uiComponent.getClientId().concat("_paginatorAction");
        String paginatorAction = requestParameterMap.get(paginatorActionParamName);
        if (paginatorAction != null) {
            // the first row in the data model.
            if ("firstPage".equals(paginatorAction)) {
                uiData.setFirst(0);
                activatedPaginationControl = true;
            } else // component to display the previous set of rows in the data model.
            if ("previousPage".equals(paginatorAction)) {
                int rows = uiData.getRows();
                int first = uiData.getFirst();
                first -= rows;
                if (first >= 0) {
                    uiData.setFirst(first);
                }
                activatedPaginationControl = true;
            } else // display the next set of rows in the data model.
            if ("nextPage".equals(paginatorAction)) {
                int rows = uiData.getRows();
                int rowCount = uiData.getRowCount();
                int first = uiData.getFirst();
                first += rows;
                if (first < rowCount) {
                    uiData.setFirst(first);
                }
                activatedPaginationControl = true;
            } else // display the last set of rows in the data model.
            if ("lastPage".equals(paginatorAction)) {
                uiData.setFirst((uiData.getRowCount() / uiData.getRows()) * uiData.getRows());
                activatedPaginationControl = true;
            } else // Otherwise, assume that the user clicked on a page number and cause the associated UIData component to
            // display the corresponding set of rows.
            {
                try {
                    int pageNumber = Integer.parseInt(paginatorAction);
                    int pageIndex = pageNumber - 1;
                    int first = pageIndex * uiData.getRows();
                    if (first > uiData.getRowCount()) {
                        first = 0;
                    }
                    uiData.setFirst(first);
                    activatedPaginationControl = true;
                } catch (NumberFormatException e) {
                    logger.error("Invalid parameter value paginatorAction=[{0}]", paginatorAction);
                }
            }
        }
        // RENDER_RESPONSE phase.
        if (activatedPaginationControl) {
            facesContext.renderResponse();
        }
    } else {
        logger.error("The alloy:paginator must have it's for attribute set or it must be inside of an f:facet of an alloy:dataTable or alloy:dataList");
    }
}
Also used : ExternalContext(javax.faces.context.ExternalContext) Paginator(com.liferay.faces.alloy.component.paginator.Paginator) UIData(javax.faces.component.UIData)

Aggregations

Paginator (com.liferay.faces.alloy.component.paginator.Paginator)2 UIData (javax.faces.component.UIData)2 Locale (java.util.Locale)1 UIViewRoot (javax.faces.component.UIViewRoot)1 ExternalContext (javax.faces.context.ExternalContext)1 ResponseWriter (javax.faces.context.ResponseWriter)1