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

📄 defaultpageselectionmodel.java

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

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

import javax.swing.event.EventListenerList;

/**
 * This class should maintain a Map of pageIndex to BitSet
 * This map is empty initially, as and when user makes a selection in a page
 * the selected elements pageIndex is inserted into this map as a key, the value 
 * for this key will be a BitSet with indexInPage bitset set to true indicating 
 * that an element is elected in this particular page.
 * 
 * isSelection Empty returns true if the keyset size in the map is emopty.
 * 
 * count of selected page elements is the summation of all true bits in all the 
 * Bitsets in the Map.
 * 
 * BitSet size() and lenght() functions are confusing be careful. 
 * 
 * TODO Undesired Effects in this class : The BitSet in not in Syn with the
 *      pages that are always dynamic (because of various reasons like frame resized, pager changed, 
 *      elementsPerPage paramter changed, and so on) Because of this the users selection is lost or
 *      completely changed because of this problem.
 *      
 *      For every event of page change the BitSet and the selections in the BitSet should get automatically
 *      updated.
 *  
 * @author chetan_bh
 */
public class DefaultPageSelectionModel implements PageSelectionModel {
	
	private static int BIT_SET_DEFAULT_SIZE = 20;
	
	private int bitSetDefaultSize = BIT_SET_DEFAULT_SIZE;
	
	private int selectionMode = MULTIPLE_SELECTION;
	
	protected EventListenerList listenerList = new EventListenerList();
	
	Map<String,BitSet> selectionsMap = new HashMap<String, BitSet>();
	
	public void addPageSelectionListener(PageSelectionListener l) {
		listenerList.add(PageSelectionListener.class, l);
	}
	
	public void removePageSelectionListener(PageSelectionListener l) {
		listenerList.remove(PageSelectionListener.class, l);
	}
	
	// TODO different APIs needed for different types of Clear.
	// 1) Clear current page selections.
	// 2) Clear selections in all page.
	//public void clearSelection() {
	//	
	//}
	
	// TODO The following two function is not needed. can be removed.
	//public int getMaxSelectionIndex() {
	//	return 0;
	//}

	//public int getMinSelectionIndex() {
	//	return 0;
	//}

	public int getSelectionMode() {
		return selectionMode;
	}

	public boolean isSelectedIndex(String pageIndex, int index) {
		//return value.get(index);
		BitSet pageBitSet = selectionsMap.get(pageIndex);
		if(pageBitSet == null)
			return false;
		else
		{
			if(pageBitSet.get(index))
				return true;
		}
		return false; 
	}
	
	public boolean isSelectionEmpty() {
		if(selectionsMap.keySet().size() == 0)
			return true;
		else
			return false;
	}
	
	// TODO after setting selection mode appropraite Event should be fired to all 
	// listeners, one among many listeners is the JPAgination listener.
	public void setSelectionMode(int selectionMode) {
		switch (selectionMode) {
		case SINGLE_SELECTION:
		case MULTIPLE_SELECTION:
		    this.selectionMode = selectionMode;
		    break;
		default:
		    throw new IllegalArgumentException("invalid selectionMode");
		}
	}
	
	public void fireSelectionValueChanged(PageSelectionEvent selectionEvent) {
		Object sourceObject = selectionEvent.getSource();
		
		JPageElement pageElementComp = null;
		
		if(sourceObject instanceof JPageElement)
			pageElementComp = (JPageElement) sourceObject;
		
		PageElementIndex elementIndex = selectionEvent.getIndex();
		
		String pageIndex = elementIndex.getMainPageIndex(); // getPageIndex();
		
		int indexInPage = elementIndex.getIndexInPage();
		
		BitSet pageBitSet = selectionsMap.get(pageIndex);
		
		if(pageBitSet == null)
		{
			// TODO remove this hardCoded value 10, DONE;
			pageBitSet = new BitSet(bitSetDefaultSize);
			if(pageElementComp.isSelected())
				pageBitSet.set(indexInPage);
			else
				pageBitSet.clear(indexInPage);
			selectionsMap.put(pageIndex, pageBitSet);
		}else
		{
			if(pageElementComp.isSelected())
				pageBitSet.set(indexInPage);
			else
				pageBitSet.clear(indexInPage);
		}
	}
	
	/**
	 * Returns the count of selected page elements in all pages.
	 */
	public int getSelectionCount()
	{
		int noOfSelections = 0;
		if(isSelectionEmpty())
			return 0;
		
		Iterator mapKeySetIter = selectionsMap.keySet().iterator();
		while(mapKeySetIter.hasNext())
		{
			String pageIndex = (String)mapKeySetIter.next();
			
			BitSet pageBitSet = selectionsMap.get(pageIndex);
			noOfSelections = noOfSelections + pageBitSet.cardinality();
		}
		
		return noOfSelections;
	}
	
	/**
	 * Following APIs should fire appropriate events to all listener.
	 * One among many listeners is the JPagination component, accordingly 
	 * it should update its UI.  
	 */
	public void clearAll() {
		selectionsMap.clear();
	}
	
	/**
	 * Select All is not simple because it has to get all available pageIndices
	 * and the size of each page.
	 */
	public void selectAll() {
		
	}
	
	/**
	 * Clears all the selections in the current displayed page.
	 */
	public void clearPage(String pageIndex) {
		BitSet pageBitSet = selectionsMap.get(pageIndex);
		if(pageBitSet == null)
		{
			pageBitSet = new BitSet(bitSetDefaultSize);
			selectionsMap.put(pageIndex, pageBitSet);
		}
		pageBitSet.clear();
	}
	
	/**
	 * Selects all the selections in the current page displayed. 
	 */
	public void selectPage(String pageIndex) {
		BitSet pageBitSet = selectionsMap.get(pageIndex);
		if(pageBitSet == null)
		{
			pageBitSet = new BitSet(bitSetDefaultSize);
			selectionsMap.put(pageIndex, pageBitSet);
		}
		pageBitSet.set(0, bitSetDefaultSize);
	}
	
	/**
	 * Inverts the selections in the current page.
	 */
	public void invertPageSelection(String pageIndex)
	{
		BitSet pageBitSet = selectionsMap.get(pageIndex);
		if(pageBitSet == null)
		{
			pageBitSet = new BitSet(bitSetDefaultSize);
			selectionsMap.put(pageIndex, pageBitSet);
		}
		pageBitSet.flip(0, pageBitSet.size());
	}
	
	/**
	 * Returns a collection of selected page element's page indices.
	 */
	public Vector<PageElementIndex> getSelectedPageIndices()
	{
		Vector<PageElementIndex> selectedPageIndices = new Vector<PageElementIndex>();
		
		Iterator<String> selectionsMapKeyIterator = selectionsMap.keySet().iterator();
		while(selectionsMapKeyIterator.hasNext())
		{
			String pageIndex = selectionsMapKeyIterator.next();
			BitSet pageBitSet = selectionsMap.get(pageIndex);
			for(int i = 0; i < pageBitSet.size(); i++)
			{
				PageElementIndex pageElementIndex = new PageElementIndex(pageIndex, i);
				// if the corresponding page element is selected this bit will be set to true.
				if(pageBitSet.get(i))
					selectedPageIndices.add(pageElementIndex);
			}
		}
		
		return selectedPageIndices;
	}
	
	public static void main(String[] args)
	{
		PageSelectionModel pageSelectionModel = new DefaultPageSelectionModel();
		
		//pageSelectionModel.fireSelectionValueChanged(new PageSelectionEvent(new JCheckBox(),"1",0));
		//pageSelectionModel.fireSelectionValueChanged(new PageSelectionEvent(new JCheckBox(),"2",4));
		//pageSelectionModel.fireSelectionValueChanged(new PageSelectionEvent(new JCheckBox(),"7",2));
		//pageSelectionModel.fireSelectionValueChanged(new PageSelectionEvent(new JCheckBox(),"7",5));
		
		pageSelectionModel.fireSelectionValueChanged(new PageSelectionEvent(new JPageElement(null,new PageElementImpl("xyz"),true,new PageElementIndex("1",2)), new PageElementIndex("1",2)));
		//pageSelectionModel.fireSelectionValueChanged(new PageSelectionEvent();
		//pageSelectionModel.fireSelectionValueChanged(new PageSelectionEvent();
		//pageSelectionModel.fireSelectionValueChanged(new PageSelectionEvent();
		
		int selectionCount = pageSelectionModel.getSelectionCount();
		System.out.println("selection Count "+selectionCount);
		
		Vector selectedPageIndeices = pageSelectionModel.getSelectedPageIndices();
		System.out.println("selectedPAegIndices "+selectedPageIndeices);
	}
	
}

⌨️ 快捷键说明

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