head	1.5;
access;
symbols
	TIGRIS_1_1_0RC2:1.4
	TIGRIS_1_1_0RC1:1.4
	TIGRIS_1_1:1.4.0.6
	TIGRIS_1_0_8:1.4
	TIGRIS_1_0_8RC3:1.4
	TIGRIS_1_0_8RC2:1.4
	TIGRIS_1_0_8RC1:1.4
	TIGRIS_1_0_7:1.4
	TIGRIS_1_0_7RC3:1.4
	TIGRIS_1_0_7RC2:1.4
	TIGRIS_1_0_7RC1:1.4
	TIGRIS_1_0_6:1.4
	TIGRIS_1_0_6RC5:1.4
	TIGRIS_1_0_6RC4:1.4
	TIGRIS_1_0_6RC3:1.4
	TIGRIS_1_0_6RC2:1.4
	TIGRIS_1_0_6RC1:1.4
	TIGRIS_1_0_5:1.4
	TIGRIS_1_0_5RC6:1.4
	TIGRIS_1_0_5RC5:1.4
	TIGRIS_1_0_5RC4:1.4
	TIGRIS_1_0_5RC3:1.4
	TIGRIS_1_0_5RC2:1.4
	TIGRIS_1_0_5RC1:1.4
	TIGRIS_1_0_4:1.4
	TIGRIS_1_0_3:1.4
	TIGRIS_1_0_2:1.4
	TIGRIS_1_0_1:1.4
	TIGRIS_1_0:1.4.0.8
	TIGRIS_1_0_0:1.4
	TIGRIS_1_0_0_RC1:1.4.0.4
	TIGRIS_0_9_2_4:1.2
	TIGRIS_0_9_2_3:1.2
	HELM_PEER_PORT_BRANCH:1.4.0.2
	TURBINE_PEER:1.4
	TIGRIS_0_9_2_1:1.4
	TIGRIS_0_9_2:1.2.0.4
	TIGRIS_0_9_0:1.2.0.2;
locks; strict;
comment	@# @;


1.5
date	2001.05.23.01.50.23;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2001.01.26.18.29.11;	author edk;	state Exp;
branches
	1.4.2.1;
next	1.3;

1.3
date	2001.01.26.05.24.08;	author edk;	state dead;
branches;
next	1.2;

1.2
date	2000.12.20.23.21.13;	author kmaples;	state Exp;
branches;
next	1.1;

1.1
date	2000.12.14.03.10.00;	author leonardr;	state Exp;
branches;
next	;

1.4.2.1
date	2001.01.28.00.52.09;	author dlr;	state dead;
branches;
next	;


desc
@@


1.5
log
@Ported Helm from WebMacro to Velocity.
@
text
@/* ================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */


package org.tigris.helm.tool.paginate;

import java.util.List;

import org.apache.turbine.util.Log;

import org.apache.velocity.context.Context;

import org.tigris.helm.tool.ContextAPI;

/**
 * Paginates a list for partial display (i.e. showing only a window of the 
 * list elements at a time).
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@version $Id: Paginator.java,v 1.4 2001/01/26 18:29:11 edk Exp $
 */
public class Paginator
{
    /**
     * Whether or not to log debugging information.
     */
    private static final boolean DEBUG = true;

    /**
     * The context key used to add the total numbner of items in the list.
     */
    public static final String TOTAL_ITEMS = "totalItems";

    /**
     * The context key used to retrieve the current page number.
     */
    public static final String PAGE_KEY = "paginated_page";

    /**
     * The context key used to add the previous page number.
     */
    public static final String PREV_PAGE = "prevPage";

    /**
     * The context key used to add the current page number.
     */
    public static final String CUR_PAGE = "currentPage";

    /**
     * The context key used to add the next page number.
     */
    public static final String NEXT_PAGE = "nextPage";

    /**
     * The context key used to add the list of page numbers.
     */
    public static final String PAGES = "pages";

    /**
     * Handles paging of the list in cases where the list represents all
     * items avaliable.
     */
    public static List paginate (List items, int nbrItemsPerPage, 
                                 Context context)
    {
        return paginate(items, items.size(), nbrItemsPerPage, context);
    }

    /**
     * Handles paging of the list in cases where there may be more items
     * total than are in the list (eg. if some items were filtered out
     * due to a query).
     */
    public static List paginate (List items, int totalNbrItems,
                                 int nbrItemsPerPage, Context context)
    {
        context.put(TOTAL_ITEMS, new Integer(totalNbrItems));
        if (DEBUG) Log.debug("totalNbrItems=" + totalNbrItems);

        if (totalNbrItems > 0)
        {
            int nbrPages = getNbrPages(nbrItemsPerPage, totalNbrItems);
            if (DEBUG) Log.debug("nbrPages=" + nbrPages);

            if (nbrPages > 1)
            {
                int curPageNbr = getCurrentPageNbr(context, nbrPages);
                context.put(CUR_PAGE, new Integer(curPageNbr));

                // Create the page links.
                Integer[] pages = new Integer[nbrPages];
                for (int i = 0; i < nbrPages; i++)
                {
                    pages[i] = new Integer(i + 1);
                }
                context.put(PAGES, pages);
                context.put(PREV_PAGE, 
                            (curPageNbr > 1 ? 
                             new Integer(curPageNbr - 1) : null));
                context.put(NEXT_PAGE, 
                            (curPageNbr < nbrPages ? 
                             new Integer(curPageNbr + 1) : null));

                // Get the subset of items to display.
                items = getPage(items, curPageNbr, nbrItemsPerPage);
            }
        }
        else
        {
            items = null;
        }

        return items;
    }

    /**
     * Returns a valid page number for the page to display.
     *
     * @@param ctx      The context to pull the page number from.
     * @@param nbrPages The highest possible page number.
     */
    private static final int getCurrentPageNbr (Context ctx, int nbrPages)
    {
        String page = ContextAPI.getParameter(ctx, PAGE_KEY);
        int pgNbr = (page != null ? Integer.parseInt(page) : 1);
        if (pgNbr < 1)
        {
            pgNbr = 1;
        }
        else if (pgNbr > nbrPages)
        {
            pgNbr = nbrPages;
        }
        return pgNbr;
    }

    /**
     * Calculates the number of pages based on the number of items.
     *
     * @@param nbrItemsPerPage The number of items to display per page.
     * @@param nbrItems        The total number of items.
     * @@return                The number of pages to display items on.
     */
    protected static final int getNbrPages (int nbrItemsPerPage, int nbrItems)
    {
        // The cast is to assure the division returns a float.
        return (int)Math.ceil((float)nbrItems / nbrItemsPerPage);
    }

    /**
     * Gets the sub list of items to display for the current page.
     *
     * @@param items           The complete list of items.
     * @@param pgNbr           The current page number.
     * @@param nbrItemsPerPage The number of items to display per page.
     */
    protected static final List getPage (List items, int pgNbr, 
                                       int nbrItemsPerPage)
    {
        return items.subList
            ((pgNbr - 1) * nbrItemsPerPage, 
             Math.min(pgNbr * nbrItemsPerPage, items.size()));
    }

    /**
     * Quick and dirty function to take a type of thing being
     * searched for and turning it into an SQL regular expression.
     */
    protected static String makeSQLRegex(String type, String searchFor)
    {
        if (type == null) type = "Contains";
        if (type.equals("Contains") || type.equals("StartsWith"))
            searchFor = searchFor + "%";
        if (type.equals("Contains") || type.equals("EndsWith"))
            searchFor = "%" + searchFor;   
        return searchFor;
    }
}
@


1.4
log
@readding all the files as part of the process of returning to the way
things were as of tuesday
@
text
@d52 5
a56 2
import org.webmacro.util.Log;
import org.webmacro.servlet.WebContext;
d63 1
a63 1
 * @@version $Id: Paginator.java,v 1.2 2000/12/20 23:21:13 kmaples Exp $
a67 5
     * Open the log file.
     */
    private static Log log = new Log("pgr", "Paginator");

    /**
d70 1
a70 1
    private static final boolean debug = true;
d107 1
a107 1
                                 WebContext context)
d118 1
a118 1
                                 int nbrItemsPerPage, WebContext context)
d121 1
a121 1
        if (debug) log.debug("totalNbrItems=" + totalNbrItems);
d126 1
a126 1
            if (debug) log.debug("nbrPages=" + nbrPages);
d165 1
a165 1
    private static final int getCurrentPageNbr (WebContext ctx, int nbrPages)
d167 1
a167 1
        String page = ctx.getForm(PAGE_KEY);
@


1.4.2.1
log
@Porting to peer system.
@
text
@d60 1
a60 1
 * @@version $Id: Paginator.java,v 1.4 2001/01/26 18:29:11 edk Exp $
@


1.3
log
@revert this stuff to its state as of 2001/01/24 12:42:00 GMT -- with
the following exceptions, which have some different modifications so
they'll work with changes which have happened in joist:

ProjectNewsEdit.java
SkinManager.java
SkinStyleElementValueManager.java
StyleElementManager.java
StyleManager.java

it compiles.  i dunno if it works, though.
@
text
@@


1.2
log
@Changed key 'page' to 'paginated_page' to avoid name clash w/ a noodle key named 'page'
@
text
@d60 1
a60 1
 * @@version $Id: Paginator.java,v 1.1 2000/12/14 03:10:00 leonardr Exp $
@


1.1
log
@Moved paginator stuff to its own directory.
@
text
@d60 1
a60 1
 * @@version $Id: Paginator.java,v 1.5 2000/12/07 01:52:04 elicia Exp $
d82 1
a82 1
    public static final String PAGE_KEY = "page";
@

