From: <fg...@us...> - 2010-04-04 17:27:49
|
Revision: 2270 http://openutils.svn.sourceforge.net/openutils/?rev=2270&view=rev Author: fgiust Date: 2010-04-04 17:27:43 +0000 (Sun, 04 Apr 2010) Log Message: ----------- MGNLUTILS-13 new EL function for handling pagination Modified Paths: -------------- trunk/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld Added Paths: ----------- trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlPagingElFunctions.java Added: trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlPagingElFunctions.java =================================================================== --- trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlPagingElFunctions.java (rev 0) +++ trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlPagingElFunctions.java 2010-04-04 17:27:43 UTC (rev 2270) @@ -0,0 +1,268 @@ +/** + * + * Magnolia generic utilities (http://www.openmindlab.com/lab/products/mgnlutils.html) + * Copyright(C) 2009-2010, Openmind S.r.l. http://www.openmindonline.it + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package it.openutils.mgnlutils.el; + +import info.magnolia.context.MgnlContext; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.NumberUtils; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; + + +/** + * @author fgiust + * @version $Id$ + */ +public class MgnlPagingElFunctions +{ + + /** + * Creates a list of Page objects that can be used to draw a pagination bar. + * @param total total number of pages + * @param visible maximum number of pages to show + * @param param name of the request parameter that will hold the current page number + * @return List of Page objects + */ + public static List<Page> pageList(int total, int visible, String param) + { + + Map<String, String> parameters = MgnlContext.getParameters(); + + StringBuffer sb = new StringBuffer(); + sb.append("?"); + boolean first = true; + for (Map.Entry<String, String> entry : parameters.entrySet()) + { + if (!StringUtils.equals(entry.getKey(), param)) + { + if (!first) + { + sb.append("&"); + } + sb.append(entry.getKey()); + sb.append("="); + sb.append(entry.getValue()); + first = false; + } + } + + if (!first) + { + sb.append("&"); + } + sb.append(param); + sb.append("="); + + String baseUrl = sb.toString(); + + List<Page> result = new ArrayList<Page>(); + + int current = Math.max(1, NumberUtils.toInt(MgnlContext.getParameter(param), 1)); + + // center pages + int start = Math.max(1, Math.min(current - visible / 2, total - visible + 1)); + int end = Math.min(start + visible, total); + + Page page = new Page(1, current == 1, baseUrl); + page.setLabel("««"); + page.setActive(current != 1); + result.add(page); + page.setCssclass("page-first"); + + int previous = Math.max(current - 1, 1); + page = new Page(previous, current == previous, baseUrl); + page.setLabel("«"); + page.setActive(current != previous); + result.add(page); + page.setCssclass("page-previous"); + + for (int j = start; j <= end; j++) + { + page = new Page(j, current == j, baseUrl); + page.setLabel(Integer.toString(j)); + page.setCssclass("page-numbered"); + result.add(page); + } + + int next = Math.min(current + 1, total); + page = new Page(next, current == next, baseUrl); + page.setLabel("»"); + page.setActive(current != next); + page.setCssclass("page-next"); + result.add(page); + + page = new Page(total, current == total, baseUrl); + page.setLabel("»»"); + page.setActive(current != total); + page.setCssclass("page-last"); + result.add(page); + + return result; + } + + public static class Page + { + + private int number; + + boolean current; + + private String url; + + private String label; + + private boolean active; + + private String cssclass; + + /** + * @param number + * @param active + * @param baseurl + */ + public Page(int number, boolean current, String baseurl) + { + this.number = number; + this.current = current; + this.url = baseurl; + active = true; + } + + /** + * Returns the number. + * @return the number + */ + public int getNumber() + { + return number; + } + + /** + * Sets the number. + * @param number the number to set + */ + public void setNumber(int number) + { + this.number = number; + } + + /** + * Returns the current. + * @return the current + */ + public boolean isCurrent() + { + return current; + } + + /** + * Sets the current. + * @param current the current to set + */ + public void setCurrent(boolean current) + { + this.current = current; + } + + /** + * Returns the url. + * @return the url + */ + public String getUrl() + { + return url + this.number; + } + + /** + * Returns the label. + * @return the label + */ + public String getLabel() + { + return label; + } + + /** + * Sets the label. + * @param label the label to set + */ + public void setLabel(String label) + { + this.label = label; + } + + /** + * Returns the active. + * @return the active + */ + public boolean isActive() + { + return active; + } + + /** + * Sets the active. + * @param active the active to set + */ + public void setActive(boolean active) + { + this.active = active; + } + + /** + * Returns the cssclass. + * @return the cssclass + */ + public String getCssclass() + { + return cssclass; + } + + /** + * Sets the cssclass. + * @param cssclass the cssclass to set + */ + public void setCssclass(String cssclass) + { + this.cssclass = cssclass; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() + { + return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE) + .append("number", this.number) + .append("url", this.url) + .append("active", this.active) + .append("current", this.current) + .append("label", this.label) + .append("cssclass", this.cssclass) + .toString(); + } + } +} Property changes on: trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlPagingElFunctions.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld =================================================================== --- trunk/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld 2010-04-04 17:12:44 UTC (rev 2269) +++ trunk/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld 2010-04-04 17:27:43 UTC (rev 2270) @@ -248,4 +248,21 @@ <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> <function-signature>java.util.Collection contentChildrenOfType(info.magnolia.cms.core.Content, java.lang.String)</function-signature> </function> -</taglib> + <function> + <name>pageList</name> + <description>Creates a list of Page objects that can be used to draw a pagination bar. + Takes as parameters the total number of pages, the maximum number of pages to show and the name of the request parameter that will hold the current page number</description> + <function-class>it.openutils.mgnlutils.el.MgnlPagingElFunctions</function-class> + <function-signature>java.util.List pageList(int, int, java.lang.String)</function-signature> + <example><![CDATA[ + <c:set var="pageList" value="${mu:pageList(numPages, maxPages, 'page')}" /> + <ul class="paging"> + <c:forEach var="page" items="${pageList}"> + <li class="page-current-${page.current} page-active-${page.active} ${page.cssclass}"> + <a href="${page.url}">${page.label}</a> + </li> + </c:forEach> + </ul> + ]]></example> + </function> +</taglib> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |