head	1.6;
access;
symbols
	TIGRIS_0_8_4:1.4.0.2
	NEWHELM:1.1.1.1
	TIGRIS_NOV_12_2000:1.1.1;
locks; strict;
comment	@# @;


1.6
date	2000.12.14.03.10.00;	author leonardr;	state dead;
branches;
next	1.5;

1.5
date	2000.12.07.01.52.04;	author elicia;	state Exp;
branches;
next	1.4;

1.4
date	2000.11.27.19.34.05;	author leonardr;	state Exp;
branches;
next	1.3;

1.3
date	2000.11.27.18.17.46;	author leonardr;	state Exp;
branches;
next	1.2;

1.2
date	2000.11.27.16.23.05;	author leonardr;	state Exp;
branches;
next	1.1;

1.1
date	2000.11.12.21.16.45;	author dlr;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2000.11.12.21.16.45;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.6
log
@Moved paginator stuff to its own directory.
@
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;

import java.util.List;

import org.webmacro.util.Log;
import org.webmacro.servlet.WebContext;

/**
 * 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.5 2000/12/07 01:52:04 elicia Exp $
 */
public class Paginator
{
    /**
     * Open the log file.
     */
    private static Log log = new Log("pgr", "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 = "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, 
                                 WebContext 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, WebContext 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 (WebContext ctx, int nbrPages)
    {
        String page = ctx.getForm(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.5
log
@added alphabetical indication to page numbers--so you know which range of users are found on each page (when there are multiple pages of users)
@
text
@d60 1
a60 1
 * @@version $Id: Paginator.java,v 1.4 2000/11/27 19:34:05 leonardr Exp $
@


1.4
log
@Fixed reversal of logic in intent-to-SQL translator.
@
text
@d60 1
a60 1
 * @@version $Id: Paginator.java,v 1.3 2000/11/27 18:17:46 leonardr Exp $
d189 1
a189 1
    private static final int getNbrPages (int nbrItemsPerPage, int nbrItems)
d202 1
a202 1
    private static final List getPage (List items, int pgNbr, 
@


1.3
log
@More Paginator changes.
@
text
@d60 1
a60 1
 * @@version $Id: Paginator.java,v 1.2 2000/11/27 16:23:05 leonardr Exp $
d218 1
a218 1
            searchFor = "%" + searchFor;
d220 1
a220 1
            searchFor = searchFor + "%";   
@


1.2
log
@Added user search capabilities (swiped from Joist) to UserPaginator, an almost-drop-in replacement for Paginator. Changed servlets that were paginating users to use UserPaginator
@
text
@d60 1
a60 1
 * @@version $Id: Paginator.java,v 1.1.1.1 2000/11/12 21:16:45 dlr Exp $
d105 2
a106 1
     * Handles paging of the list.
d111 11
a121 1
        int totalNbrItems = items.size();
d208 14
@


1.1
log
@Initial revision
@
text
@d2 1
a2 1
 * Copyright (c) 2000 Collab.Net.  All rights reserved.
d17 1
a17 1
 * software developed by Collab.Net (http://www.Collab.Net/)."
d27 1
a27 1
 * permission of Collab.Net.
d32 1
a32 1
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
d44 1
a44 1
 * individuals on behalf of Collab.Net.
d60 1
a60 1
 * @@version $Id: Paginator.java,v 1.2 2000/11/06 06:15:33 edk Exp $
d75 5
d111 1
@


1.1.1.1
log
@Merging newhelm tree into helm, as per IZ issue #557.  Previous major version of helm is tagged TIGRIS_NOV_11_2000.
@
text
@@
