📄 jahiacontainerlistpagination.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaContainerListPagination// NK 05.05.2002////package org.jahia.data.containers;import java.util.*; // Vector, Enumerationimport org.jahia.utils.*;import org.jahia.exceptions.JahiaException;import org.jahia.params.ParamBean;/** * <p>Handle containers list pagination</p> * * @author Khue Nguyen <a href="mailto:khue@jahia.org">khue@jahia.org</a> * @version 1.0 */public class JahiaContainerListPagination{ private static final String CLASS_NAME = JahiaContainerListPagination.class.getName(); /** The total number of containers. **/ private int size = 0; /** The number of items ( containers ) to display per page. **/ private int windowSize = -1; /** The current windowOffset. **/ private int windowOffset = -1; /** The current page index ( the currently displayed page ). **/ private int currentPageIndex = 0; /** The vector of pages indexes **/ private Vector pages = new Vector(); /** The index of the first displayed item. **/ private int firstItemIndex = 0; /** The index of the last displayed item. **/ private int lastItemIndex = 0; /** Is the status of this instance valid or not **/ private boolean isValid = false; //------------------------------------------------------------------------- /** * Constructor * * @param int size, the total number of items. * @param int windowSize, the number of items ( containers ) to display per page. * @param int windowOffset, the current windowOffset. */ public JahiaContainerListPagination( int size, int windowSize , int windowOffset ) { this.size = size; this.windowSize = windowSize; this.windowOffset = windowOffset; this.paginate(); } //------------------------------------------------------------------------- /** * Constructor * * windowSize and windowOffset are computed from request parameters : * <pre> * "ctnscroll_" + containerListName * </pre> * * Or if not present, loaded from container list scrolling properties : * <pre> * "windowSize" and "windowOffset" * </pre> * * size is retrieved from theContainerList.getFullSize() * * * @param JahiaConainterList cList, the container list. * @param ParamBean jParams, the param bean. * @param int newWindowSize, the new window size option, set to -1 to deactivate. */ public JahiaContainerListPagination( JahiaContainerList theContainerList, ParamBean jParams, int newWindowSize ) throws JahiaException { //JahiaConsole.println(CLASS_NAME+".Constructor(ctnlist,jparams,newWindowSize)","Started for ctnlist [" + theContainerList.getID() + "] - " + theContainerList.getDefinition().getName()); if ( theContainerList == null ) { return; } // we now check in we are in a scrollable container list, and retrieve // only the values of the containers that must be displayed. int windowSize = -1; int windowOffset = -1; JahiaContainerDefinition ctnDef = theContainerList.getDefinition(); Properties ctnDefProps = ctnDef.getProperties(); int defWindowSize = -1; int defWindowOffset = -1; if (ctnDefProps.size() > 0) { try { if (ctnDefProps.getProperty("windowSize") != null) { defWindowSize = Integer.parseInt(ctnDefProps.getProperty("windowSize")); defWindowOffset = 0; } if (ctnDefProps.getProperty("windowOffset") != null) { defWindowOffset = Integer.parseInt(ctnDefProps.getProperty("windowOffset")); } } catch (NumberFormatException nfe) { } } //JahiaConsole.println(CLASS_NAME+".Constructor","defWindowSize : " + defWindowSize); //JahiaConsole.println(CLASS_NAME+".Constructor","defWindowOffset : " + defWindowOffset); if ( (defWindowSize >= 1) && (defWindowOffset >= 0) ) { windowSize = defWindowSize; windowOffset = defWindowOffset; } //JahiaConsole.println(CLASS_NAME+".Constructor","windowSize : " + windowSize); //JahiaConsole.println(CLASS_NAME+".Constructor","windowOffset : " + windowOffset); if (jParams != null) { String containerListName = theContainerList.getDefinition().getName(); String scrollStr = jParams.getParameter("ctnscroll_" + containerListName); if (scrollStr != null) { // we have an window scrolling code for this container list. /* JahiaConsole.println(CLASS_NAME+".Constructor", "Found window parameters for container list " + containerListName); */ int separatorPos = scrollStr.indexOf("_"); if ((separatorPos != -1) && (separatorPos + 1 < scrollStr.length())) { // seperator is present, looks like a valid parameter. We // must still test if we have two integer values. String windowSizeStr = scrollStr.substring(0, separatorPos); String windowOffsetStr = scrollStr.substring(separatorPos + 1, scrollStr.length()); try { windowSize = Integer.parseInt(windowSizeStr); windowOffset = Integer.parseInt(windowOffsetStr); if ((windowSize < -1) || (windowOffset < -1) ) { // little sanity check to avoid simple hacks. windowSize = -1; windowOffset = -1; } } catch (NumberFormatException nfe) { windowSize = -1; windowOffset = -1; } } } // Check for customized window size String custWindowSize = jParams.getParameter(containerListName + "_windowsize"); try { int val = Integer.parseInt(custWindowSize); if ( val > 0 ){ windowSize = val; } } catch ( Throwable t ){ } } // set the new window size if needed if ( newWindowSize > 0 ){ windowSize = newWindowSize; } if ( windowOffset > (theContainerList.getFullSize()-1) ) { windowOffset = 0; // Perhaps the number of container has changed ( i.e : some ctn has been deleted ), // so display the first page } if ( ( windowOffset % windowSize ) != 0) { windowOffset = 0; } this.size = theContainerList.getFullSize(); this.windowSize = windowSize; this.windowOffset = windowOffset; //JahiaConsole.println(CLASS_NAME+".Constructor","size : " + this.size + // " windowSize : " + windowSize + // " windowOffset : " + windowOffset); this.paginate(); } //------------------------------------------------------------------------- /** * Return the total number of items ( containers ). * * @return int the total number of itms. */ public int getSize() { return this.size; } //------------------------------------------------------------------------- /** * Return the windowSize , the number of items per page. * * @return int windowSize, the number of items per page */ public int getWindowSize() { return this.windowSize; } //------------------------------------------------------------------------- /** * Return the windowOffset , the current window Offset. * * @return int windowOffset, the current window offset. */ public int getWindowOffset() { return this.windowOffset; } //------------------------------------------------------------------------- /** * Return the valid status. * * @return boolean true if valid, false on error. */ public boolean isValid() { return this.isValid; } //------------------------------------------------------------------------- /** * Return the current page Index, ( the index of the displayed page ). * Starting from 1 * * @return int current pageIndex. */ public int getCurrentPageIndex() { return this.currentPageIndex; } //------------------------------------------------------------------------- /** * Return the index of the first item listed in the current displayed page. * Starting from 0 * * @return int index of the first item listed in the current displayed page. */ public int getFirstItemIndex() { return this.firstItemIndex; } //------------------------------------------------------------------------- /** * Return the index of the last item listed in the current displayed page. * Starting from 0 * * @return int index of the last item listed in the current displayed page. */ public int getLastItemIndex() { return this.lastItemIndex; } //------------------------------------------------------------------------- /** * Return the vector of paginated pages as index numbers starting from 1 to n. * * 1,2,3,4.. * * @return Vector the vector of paginated pages as index numbers ( Integer ). */ public Vector getPages() { return this.pages; } //------------------------------------------------------------------------- /** * Return the number of pages. * * @return int the number of pages. */ public int getNbPages() { if ( this.pages == null ){ return 0; } return this.pages.size(); } //------------------------------------------------------------------------- /** * Return the windowOffset for a given page index ( page index start from 1 to n). * The computed value is equals to : (pageIndex-1) * windowSize * * @param int the page index must be > 0. * @return int the windowOffset for a given page index, -1 on error. */ public int getWindowOffset(int pageIndex) { if ( !isValid() || (pageIndex<=0) ){ return -1; } try { int val = (pageIndex-1) * this.windowSize ; if ( val <= this.size ){ return val; } } catch ( Throwable t ) { t.printStackTrace(); } return -1; } //------------------------------------------------------------------------- /** * Return the scrolling value ( i.e : '5_10' ) for a given page index ( page index start from 1 to n). * * <pre> * Scrolling value format is : windowSize + "_" + windowOffset * </pre> * * @param int the page index must be > 0. * @return String the scrolling value for the given page index, null on error. */ public String getScrollingValue(int pageIndex) { int windowOffset = getWindowOffset(pageIndex); if ( pageIndex <0 ){ return null; } return ( String.valueOf(getWindowSize()) + "_" + String.valueOf(windowOffset) ); } //------------------------------------------------------------------------- /** * Perform pagination tasks. * */ private void paginate() { //JahiaConsole.println(CLASS_NAME+".paginate","Started"); try { if ( ( this.windowSize < 0 ) || ( this.windowOffset < 0 ) || ( ( this.windowOffset % this.windowSize ) != 0) ) { return; // not valid value. } // Compute current page index. this.currentPageIndex = ( this.windowOffset / this.windowSize ) + 1; //JahiaConsole.println(CLASS_NAME+".paginate","Current page index : " + this.currentPageIndex); // Compute the first and last item indexes if ( this.currentPageIndex == 1 ) { this.firstItemIndex = 0; } else { this.firstItemIndex = ( this.currentPageIndex * this.windowSize ) - this.windowSize; } this.lastItemIndex = ( this.currentPageIndex * this.windowSize ) -1; if ( this.lastItemIndex > this.size-1 ) { this.lastItemIndex = this.size-1; } //JahiaConsole.println(CLASS_NAME+".paginate","firtsItemIndex : " + this.firstItemIndex); //JahiaConsole.println(CLASS_NAME+".paginate","lastItemIndex : " + this.lastItemIndex); // Compute the vector of page indexes if ( this.size>0 ) { if ( this.size <= this.windowSize ) { this.pages.add( new Integer(1) ); } else { int nbPages = ( this.size / this.windowSize ); for ( int i=0 ; i<nbPages; i++ ) { this.pages.add( new Integer(i+1) ); } if ( (this.size % this.windowSize) != 0 ) { int newPageIndex = this.pages.size() + 1; this.pages.add( new Integer(newPageIndex) ); } } } // everything seems ok this.isValid = true; } catch ( Throwable t ) { t.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -