1 package org.jahia.taglibs.pagination; 2 3 import java.io.IOException ; 4 import java.util.Iterator ; 5 6 import javax.servlet.jsp.JspException ; 7 import javax.servlet.jsp.JspTagException ; 8 import javax.servlet.jsp.tagext.BodyContent ; 9 import javax.servlet.jsp.tagext.BodyTagSupport ; 10 11 import org.jahia.data.beans.ContainerListBean; 12 import org.jahia.data.beans.PaginationBean; 13 import org.jahia.data.containers.JahiaContainerList; 14 import org.jahia.data.containers.JahiaContainerListPagination; 15 import org.jahia.taglibs.container.ContainerListTag; 16 17 28 29 public class CListPaginationTag extends BodyTagSupport { 30 31 private JahiaContainerListPagination cPagination = null; 32 private int pageNumber = 0; 33 private boolean isCurrentPage = false; 34 private Iterator iterator; 35 36 private int nbStepPerPage = -1; private String skipOnePageOnly = "true"; 39 private int startPageIndex = 0; 40 private int stopPageIndex = 0; 41 private String name = null; 42 43 46 public int getNbStepPerPage () { 47 return this.nbStepPerPage; 48 } 49 50 53 public void setNbStepPerPage (int aNbStepPerPage) { 54 this.nbStepPerPage = aNbStepPerPage; 55 } 56 57 60 public String skipOnePageOnly () { 61 return this.skipOnePageOnly; 62 } 63 64 67 public void setSkipOnePageOnly (String val) { 68 if (val != null) { 69 this.skipOnePageOnly = val.toLowerCase(); 70 } 71 } 72 73 76 public int getPageNumber () { 77 return this.pageNumber; 78 } 79 80 83 public boolean isCurrentPage () { 84 return isCurrentPage; 85 } 86 87 90 public int getStopPageIndex () { 91 return this.stopPageIndex; 92 } 93 94 97 public int getStartPageIndex () { 98 return this.startPageIndex; 99 } 100 101 public String getName () { 102 return name; 103 } 104 105 public void setName (String aName) { 106 this.name = aName; 107 } 108 109 public int doStartTag () { 110 111 JahiaContainerList cList = null; 112 if (getName() != null) { 113 ContainerListBean containerListBean = (ContainerListBean) 114 pageContext.findAttribute( 115 getName()); 116 cList = containerListBean.getJahiaContainerList(); 117 } else { 118 ContainerListTag containerListTag = (ContainerListTag) 120 findAncestorWithClass(this, 121 ContainerListTag.class); 122 if (containerListTag != null) { 123 cList = containerListTag.getContainerList(); 124 } 125 } 126 if (cList != null) { 127 cPagination = cList.getCtnListPagination(); 128 if (cPagination != null && cPagination.isValid()) { 129 if (cPagination.getNbPages() == 1 && 130 this.skipOnePageOnly.equals("true")) { 131 return SKIP_BODY; 132 } 133 if (this.nbStepPerPage <= 0) { 134 this.startPageIndex = 1; 135 this.stopPageIndex = cPagination.getNbPages(); 136 this.pageNumber = 1; 137 } else { 138 if (cPagination.getCurrentPageIndex() <= this.nbStepPerPage) { 139 this.startPageIndex = 1; 140 this.stopPageIndex = this.nbStepPerPage; 141 } else { 142 if ( (cPagination.getCurrentPageIndex() % 143 this.nbStepPerPage) == 0) { 144 this.startPageIndex = (cPagination. 145 getCurrentPageIndex() - this.nbStepPerPage) + 1; 146 } else { 147 this.startPageIndex = cPagination. 148 getCurrentPageIndex() - 149 (cPagination. 150 getCurrentPageIndex() % this.nbStepPerPage) + 1; 151 } 152 this.stopPageIndex = this.startPageIndex + 153 this.nbStepPerPage - 1; 154 } 155 } 156 iterator = cPagination.getPages().iterator(); 157 while (iterator.hasNext() && 158 (this.pageNumber < this.startPageIndex - 1)) { 159 iterator.next(); 160 this.pageNumber += 1; 161 } 162 if (iterator.hasNext()) { 163 Integer I = (Integer ) iterator.next(); 164 this.pageNumber = I.intValue(); 165 this.isCurrentPage = (this.pageNumber == 166 cPagination.getCurrentPageIndex()); 167 if (getId() != null) { 168 if (getPaginationBean() != null) { 169 pageContext.setAttribute(getId(), getPaginationBean()); 170 } else { 171 pageContext.removeAttribute(getId()); 172 } 173 } 174 return EVAL_BODY_BUFFERED; 175 } else { 176 return SKIP_BODY; 177 } 178 } 179 } 180 return SKIP_BODY; 181 } 182 183 public int doAfterBody () 184 throws JspTagException { 185 BodyContent body = getBodyContent(); 186 try { 187 body.writeOut(getPreviousOut()); 188 } catch (IOException e) { 189 throw new JspTagException ("CListPaginationTag.doAfterBody : " + 190 e.getMessage()); 191 } 192 193 body.clearBody(); 195 if (iterator.hasNext() && (this.pageNumber < this.stopPageIndex)) { 196 Integer I = (Integer ) iterator.next(); 197 this.pageNumber = I.intValue(); 198 this.isCurrentPage = (this.pageNumber == 199 cPagination.getCurrentPageIndex()); 200 if (getId() != null) { 201 pageContext.setAttribute(getId(), getPaginationBean()); 202 } 203 return EVAL_BODY_BUFFERED; 204 } 205 return SKIP_BODY; 206 } 207 208 public int doEndTag () 209 throws JspException { 210 cPagination = null; 213 pageNumber = 0; 214 isCurrentPage = false; 215 216 nbStepPerPage = -1; skipOnePageOnly = "true"; 219 startPageIndex = 0; 220 stopPageIndex = 0; 221 222 name = null; 223 return EVAL_PAGE; 224 } 225 226 public PaginationBean getPaginationBean() { 227 228 return new PaginationBean(this.pageNumber, this.isCurrentPage); 229 } 230 231 } | Popular Tags |