⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractpager.java

📁 Document will be uploaded soon
💻 JAVA
字号:
package com.component.pagination;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

/**
 * Abstract class implementing some common methods in Pager.
 * 
 * Note : Ideas for implementing 2-level pager. 
 * 
 * 1) Abstract pager should contain second-level pager, this pager is
 *    always a Numeric Pager. 
 * 
 * 2) User/Programmer doesn't have any control over the 2-level pager.
 * 
 * 3) PageElementIndex needs to be changed to contain one more index 
 *    apart from the two existing now.
 *    a) PageIndex
 *    b) SubPage Index
 *    c) Index in SubPage // Needs more thought on this.
 * 
 * 4) PageSelectionModel also needs to be changed accordingly(especially BitSet-related things)
 *    PageSelectionEvent, PageSelectionListener needs to be updated.
 * 
 * 5) The method signature of the methods in Pager interface will change slightly, esplacially 
 *    getPage() and similar methods.  
 * 
 * 6) JPagination will contain one more JPageBar for sub page bar, need to add action listener for this.
 * 
 * 7) The currentPage pointer in the PaginationModel(in turn in Pager) needs to take into account now 
 *    the 2-level pager's existence. 
 * 
 * @author chetan_bh
 * 
 */
public abstract class AbstractPager implements Pager {
		
	/*
	 * This pageMap datastructure needs to be modified a little.
	 * Right now it is a Map of String->Vector<PageElement>.
	 * 
	 * But after the Second level pagination it will be
	 * Map of String->Map<Map> value Map is from String->Vector<PageElement>
	 * 
	 * Key set in the outer map are page indices of first level paging of full user data, 
	 * Key set in the inner maps are page indices of the second level paging of first level page.
	 * 
	 */
	//Map<String,Vector> pageMap;
	
	Map<String, Map<String,Vector<PageElement>>> pageMap;
	
	// TODO A vector of pageIndices is needed because the pageMap keyset is not ordered,
	// but page Indices is a ordered list.
	Vector<String> pageIndices; // A vector of Strings which are key to pageMap.
	
	int numberOfElements;
	
	Vector<PageElement> rawData;
	
	// TODO this is a configurable parameter.
	/**
	 * Elements per page attribute means different for different pagers,
	 * 
	 * For numeric pager it is a very impoertant parameter, all pages except the last page
	 * should mandatorily have exaclty the number of specified number of elements per page.
	 * 
	 * For non-numeric pager this rule applies only for pages which have more than specified 
	 * number of elements per page, i.e for secondary pagination.
	 * 
	 * So getter setter methods and property change handler code, for this attribute should 
	 * go to respective pagers.
	 */
	int elementsPerPage;
	
	int numberOfPages;
	
	int currentPageIndex = -1;
	
	PageIndex currPageIndex;
	
	String currentPageIndexStr = "";
	
	public AbstractPager(PageElement[] elements)
	{
		this(convertToVector(elements));
	}
	
	public AbstractPager(Vector<PageElement> data)
	{
		this(data, PaginationConstants.DEFAULT_ELEMENTS_PER_PAGE);
	}
	
	public AbstractPager(Vector<PageElement> data, int elementsPerPage)
	{
		this.rawData = data;  // Added on 28/11/06 this is needed for setElementsPerPAge setter method,
		                      // otherwise this method has no access to rawData;
		this.elementsPerPage = elementsPerPage;
		numberOfElements = data.size();
		//System.out.println("number of elements "+numberOfElements);
		numberOfPages = (int)Math.ceil(numberOfElements / (double)elementsPerPage);
		//System.out.println("number Of Pages needed ==>> "+numberOfPages);
		pageIndices = new Vector<String>();
		pageMap = page(data);
		
		//System.out.println("pageMap.keySet() "+pageMap.keySet());
	}
	
	//TODO page index starts from 1 unlike standard JAVA indexing which starts from 0.
	/*
	 * This is a default Numeric Pager for the pagination component.
	 * 
	 * Since this a Numeric Pager, no need to call the subPage() method at any cost.
	 * But this method now needs to take care of modified pagemap datastructure.
	 * 
	 * For NumericPager the pageMap DS will be like this
	 * pageMap given Numeric Index returns a Map which again contains the same 
	 * Numeric Index to get Vector<PageElement> Vector.
	 * 
	 */
	protected Map<String, Map<String,Vector<PageElement>>> page(Vector<PageElement> data)
	{
		Map<String,Map<String, Vector<PageElement>>> returner = new HashMap<String, Map<String,Vector<PageElement>>>();
		Iterator<PageElement> elementsIter = data.iterator();
		
		int pageCounter = 1;
		int elementsCounter = 0;
		String currentPage = "";
		Vector<PageElement> page = new Vector<PageElement>();
		
		while(elementsIter.hasNext())
		{
			PageElement pageElement = elementsIter.next();
			if(elementsCounter == elementsPerPage)
			{
				currentPage = ""+pageCounter;
				pageIndices.add(currentPage);
				Map<String, Vector<PageElement>> subPageMap = new HashMap<String, Vector<PageElement>>();
				subPageMap.put(currentPage, page);
				returner.put(currentPage, subPageMap);
				page = new Vector<PageElement>();
				elementsCounter = 0;
				pageCounter++;
			}
			page.add(pageElement);
			elementsCounter++;
		}
		currentPage = ""+pageCounter;
		pageIndices.add(currentPage);     // again page indices need to be broken by numeric pager.
		Map<String, Vector<PageElement>> subPageMap = new HashMap<String, Vector<PageElement>>();
		subPageMap.put(currentPage, page);
		returner.put(currentPage, subPageMap);  // TODO last page added to the pageMap.
		
		//System.out.println("pageIndices "+pageIndices);
		
		return returner;
	}
	
	/**
	 * A utility method to convert array of page element to Vector of page element. 
	 * @param elements
	 * @return
	 */
	private static Vector<PageElement> convertToVector(PageElement[] elements)
	{
		Vector<PageElement> returner = new Vector<PageElement>();
		for(int i = 0; i < elements.length; i++)
		{
			returner.add(elements[i]);
		}
		return returner;
	}
	
	/**
	 * Returns a page.
	 */
	public Vector<PageElement> getPage(PageIndex pageIndex)
	{
		
		String firstKey = pageIndex.getMainPageIndex();
		String secondKey = pageIndex.getSubPageIndex();
		
		Map<String, Vector<PageElement>> subPageMap = pageMap.get(firstKey);
		
		Vector<PageElement> page = subPageMap.get(secondKey);
		int requestedPageIndex = -1;
		try{
			requestedPageIndex = Integer.parseInt(index);
		}catch(NumberFormatException nfe)
		{
			requestedPageIndex = pageIndices.indexOf(index);
			requestedPageIndex++;
		}
		currentPageIndexStr = index;
		currentPageIndex = --requestedPageIndex;
		
		return page;
	}
	
	
	/**
	 * Returns true if there a pages next to current page, else false.
	 */
	public boolean hasNextPage()
	{
		if(currentPageIndex < (pageIndices.size()-1))
			return true;
		return false;
	}
	
	/**
	 * returns true if there are pages back to current page, else false.
	 */
	public boolean hasPreviousPage()
	{
		if(currentPageIndex > 0)
			return true;
		return false;
	}
	
	/**
	 * Returns the first page in the current pagination.
	 */
	/*
	 * Return first main page's first sub page.
	 */
	public Vector<PageElement> firstPage()
	{
		//System.out.println("firstPage requested");
		currentPageIndex = 0;
		currentPageIndexStr = pageIndices.get(currentPageIndex);
		//currentPageIndexStr = "1"; // TODO A bug was threr related to this hardcoding.
		String index = pageIndices.get(currentPageIndex);
		return pageMap.get(index);
	}
	
	/**
	 * Returns the page next to the current page.
	 */
	public Vector<PageElement> nextPage()
	{
		//System.out.println("nextPage :: currentPageIndex "+currentPageIndex);
		currentPageIndex++;
		//System.out.println("after "+currentPageIndex);
		String index = pageIndices.get(currentPageIndex);
		currentPageIndexStr = index;
		Vector<PageElement> nextPage = pageMap.get(index);
		
		return nextPage;
	}
	
	/**
	 * Returns the page previous to the current page. 
	 */
	public Vector<PageElement> previousPage()
	{
		//System.out.println("previousPage :: currentPageIndex "+currentPageIndex);
		currentPageIndex--;
		//System.out.println("after "+currentPageIndex);
		String index = pageIndices.get(currentPageIndex);
		currentPageIndexStr = index;
		Vector<PageElement> previousPage = pageMap.get(index);

		return previousPage;
	}
	
//	/**
//	 * This method should be deleted here, and moved to PageBarModel
//	 */
//	public Vector nextPageIndices() {
//		return pageIndices;
//	}

//	public Vector previousPageIndices() {
//		// This should actually return one part of Page Indices.
//		return pageIndices;
//	}
	
	/**
	 * Returns a collection of page indices.
	 */
	public Vector<String> getAllPageIndices()
	{
		return pageIndices;
	}
	
	public Set<String> getAllSubPageIndices(String mainPageIndex)
	{
		return (pageMap.get(mainPageIndex).keySet());
	}
	
	public String toString()
	{
		return pageMap.toString();
	}
	
//	/**
//	 * This method is needed for PageBarModel to decide on the next and previous
//	 * pageIndices in needed or not, to update fresh set of page indices.
//	 */
//	public String getCurrentPageIndex()
//	{
//		return currentPageIndexStr;
//	}
	
	public PageIndex getCurrentPageIndex()
	{
		return currPageIndex;
	}
	
	/**
	 * Returns the page element in a particular page, else null..
	 */
	public PageElement getPageElement(PageElementIndex pageElemIndex)
	{
		PageElement returner = null;
		
		//String pageIndex = pageElemIndex.getPageIndex();
		String mainPageIndex = pageElemIndex.getMainPageIndex(); // new
		String subPageIndex = pageElemIndex.getSubPageIndex();   // new
		int indexInPage = pageElemIndex.getIndexInPage();
		
		Map<String, Vector<PageElement>> subPageMap = pageMap.get(mainPageIndex); //pageMap.get(pageIndex);
		Vector page = subPageMap.get(subPageIndex);
		// TODO this is work around for BitSet not having same lenght as the page size
		// TODO handle null in the calling function.
		if(indexInPage < page.size())
			returner = (PageElement)page.get(indexInPage);
		else
			returner = null;
		
		
		return returner;
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -