All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sakaiproject.jsf2.renderer.PagerRenderer Maven / Gradle / Ivy

Go to download

This is the Maven project for the custom JSF 2 widgets. The widgets and the resources projects are closely tied together. These widgets will be deployed as a jar file containing Sakai JSF 2 widgets (components). Web applications can include this jar in order to use the Sakai JSF 2 widgets in a JSF tool.

There is a newer version: 23.3
Show newest version
/**
 * Copyright (c) 2003-2021 The Apereo Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://opensource.org/licenses/ecl2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sakaiproject.jsf2.renderer;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Map;
import java.util.MissingResourceException;

import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

import lombok.extern.slf4j.Slf4j;

import org.sakaiproject.jsf2.util.LocaleUtil;
import org.sakaiproject.jsf2.util.RendererUtil;

@Slf4j
public class PagerRenderer extends Renderer {

    private static final String BUNDLE_NAME = "pager";
    public static final int MAX_PAGE_SIZE = 200;

    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        if (!component.isRendered()) return;

        // get state

        ResponseWriter out = context.getResponseWriter();
        String clientId = component.getClientId(context);
        //String formId = getFormId(context, component);

        int pageSize = getInt(context, component, "pageSize", MAX_PAGE_SIZE);
        int totalItems = getInt(context, component, "totalItems", 0);
        int firstItem = getInt(context, component, "firstItem", 0);
        int lastItem = getInt(context, component, "lastItem", -1);
        if (log.isDebugEnabled()) log.debug("encodeBegin: firstItem=" + firstItem + ", pageSize=" + pageSize + ", value=" + getString(context, component, "value", null));

        // in case we are rendering before decode()ing we need to adjust the states
        adjustState(context, component, firstItem, lastItem, pageSize, totalItems, firstItem, lastItem, pageSize);

        pageSize = getInt(context, component, "pageSize", MAX_PAGE_SIZE);
        totalItems = getInt(context, component, "totalItems", 0);
        firstItem = getInt(context, component, "firstItem", 0);
        lastItem = getInt(context, component, "lastItem", -1);

        // get stuff for pageing buttons
        String idFirst = clientId+"_first";
        String idPrev = clientId+"_prev";
        String idNext = clientId+"_next";
        String idLast = clientId+"_last";
        String idPastItem = clientId+"_pastItem";
        boolean renderFirst = getBoolean(context, component, "renderFirst", true);
        boolean renderPrev = getBoolean(context, component, "renderPrev", true);
        boolean renderNext = getBoolean(context, component, "renderNext", true);
        boolean renderLast = getBoolean(context, component, "renderLast", true);
        boolean renderPageSize = getBoolean(context, component, "renderPageSize", true);
        String labelFirst = getString(context, component, "textFirst", "|<");
        String labelPrev = getString(context, component, "textPrev", "<");
        String labelNext = getString(context, component, "textNext", ">");
        String labelLast = getString(context, component, "textLast", ">|");
        String textItem = getString(context, component, "textItem", "items");
        String titleFirst = MessageFormat.format(getString(context, component, "titleFirst", "First {0} {1}"), pageSize, textItem);
        String titlePrev = MessageFormat.format(getString(context, component, "titlePrev", "Previous {0} {1}"), pageSize, textItem);
        String titleNext = MessageFormat.format(getString(context, component, "titleNext", "Next {0} {1}"), pageSize, textItem);
        String titleLast = MessageFormat.format(getString(context, component, "titleLast", "Last {0} {1}"), pageSize, textItem);

        // TODO: Do this elsewhere? (component vs renderer)
        boolean disabledFirst = (firstItem == 0);
        boolean disabledPrev = (firstItem == 0);
        boolean disabledNext = (pageSize == 0) || (firstItem + pageSize >= totalItems);
        boolean disabledLast = disabledNext;
        boolean accesskeys = getBoolean(context, component, "accesskeys", false);
        String accesskeyFirst = (accesskeys) ? "f" : null;
        String accesskeyPrev = (accesskeys) ? "p" : null;
        String accesskeyNext = (accesskeys) ? "n" : null;
        String accesskeyLast = (accesskeys) ? "l" : null;

        // get stuff for page size selection and display

        String textPageSize = getString(context, component, "textPageSize", "Show {0}");
        String pageSizesStr = getString(context, component, "pageSizes", "20,100,200");
        String[] pageSizes = pageSizesStr.split(",");
        String idSelect = clientId+"_pageSize";

        String textStatus;
        if (totalItems > 0) {
            textStatus = getString(context, component, "textStatus", "Viewing {0} to {1} of {2} {3}");
        } else {
            textStatus = getString(context, component, "textStatusZeroItems", "Viewing 0 {3}");
        }

        Object[] args = new Object[] {String.valueOf(firstItem+1), String.valueOf(lastItem), String.valueOf(totalItems), textItem};
        textStatus = MessageFormat.format(textStatus, args);

        // prepare the dropdown for selecting the
        // TODO: Probably need to cache this for performance
        String onchangeHandler = "javascript:this.form.submit(); return false;";
        String selectedValue = pageSize <= 0 || pageSize > MAX_PAGE_SIZE ? String.valueOf(MAX_PAGE_SIZE) : String.valueOf(pageSize);
        String[] optionTexts = new String[pageSizes.length];
        String[] optionValues = new String[pageSizes.length];
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy