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

📄 paginghelper.java

📁 一个JAVA分页解决方案
💻 JAVA
字号:
package com.ctic.core.pub.ui.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultListModel;

import com.ctic.core.pub.client.BaseClient;
import com.ctic.core.pub.common.ModuleIDs;
import com.ctic.core.pub.common.PagingUtils;
import com.ctic.core.pub.common.UserException;
import com.ctic.core.pub.ui.CExtList;
import com.ctic.core.pub.ui.CExtTable;
import com.ctic.core.pub.ui.PagingComponent.IPagingViewer;
import com.ctic.core.pub.ui.PagingComponent.IPagingWorker;
import com.ctic.core.pub.ui.table.SimpleTable;
import com.ctic.core.pub.ui.table.SimpleTableModel;

/**
 * 方便分页控件使用的助手类
 * 
 * @author Tu_Minglei
 */
public class PagingHelper {
	private static final boolean debug = true;
	
	/**
	 * 为目标对象(控件或Model)创建<code>IPaginationViewer</code>实现,翻页结果将自动显示在目标对象中<br>
	 * <ul>当前支持的控件有:
	 * <li>CExtTable
	 * <li>CExtList
	 * </ul>
	 * <ul>当前支持的Model有:
	 * <li>CExtTableModel
	 * <li>DefaultListModel
	 * </ul>
	 * (注:1、SimpleTable和SimpleTableModel暂时支持不了;2、对其他控件或Model的支持可以按需添加)
	 * @param targetObject
	 * @return
	 */
	public static IPagingViewer createPagingViewerFor(Object targetObject){
		if(targetObject == null) throw new IllegalArgumentException("targetObject is NULL!");
		return new DefaultPaginationViewer(targetObject);
	}
	
	/**
	 * 用传入记录列表及(默认)每页记录数创建<code>IPagination</code>
	 * @param records
	 * @return
	 */
	public static IPagingWorker createPagingWorkerFor(List records){
		return new SimplePagingWorkerImpl(records);
	}
	
	/**
	 * 用传入的准备好的方法及参数创建<code>IPagination</code>
	 * @param targetMethod
	 * @param paramObjects
	 * @return
	 * @throws UserException 
	 */
	public static IPagingWorker createPagingWorkerFor(int moduleId, String serviceId, String methodName,
			Class[] paramTypes, Object[] paramObjects) throws UserException {
		return new MethodPagingWorkerImpl(moduleId, serviceId, methodName, paramTypes)
					.setUserParamObjects(paramObjects);
	}
	
	/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
	 * 						接口及内部类
	 **++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
	/**
	 * 通过传入列表数据执行所需功能的<code>IPagingWorker</code>的简单实现
	 * 
	 * @author Tu_Minglei
	 */
	static class SimplePagingWorkerImpl implements IPagingWorker {
		private List	records;
		
		public SimplePagingWorkerImpl(List records){
			this.records = records;
		}
		
		public int getTatalRecordCount() {
			return (records == null) ? 0 : records.size();
		}

		public List getRecords(int startRowNo, int count) {
			if((startRowNo <= 0) || (startRowNo > getTatalRecordCount()) || records == null) return null;
			return records.subList((startRowNo-1), Math.min(((startRowNo-1) + count), getTatalRecordCount()));
		}
		
	}
	
	/**
	 * 通过传入方法、参数等执行所需功能的<code>IPagingWorker</code>实现
	 * 
	 * @author Tu_Minglei
	 */
	static class MethodPagingWorkerImpl implements IPagingWorker {
		private GeneralUserServiceAgent userAgent;
		private Map<String,Object> userRequestMap;
		
		private int 	cacheCapacity = 1000;
		
		private int		totalRecordCount;
		private	List	cachedRecords;
		private int 	firstRecordNo;
		
		public MethodPagingWorkerImpl(int moduleId, String serviceId, String methodName, Class[] paramTypes){
			this.userAgent = new GeneralUserServiceAgent(moduleId, serviceId, methodName, paramTypes);
			userRequestMap = new HashMap<String, Object>();
			userRequestMap.put("userModuleId", moduleId);
			userRequestMap.put("userServiceId", serviceId);
			userRequestMap.put("userMethodName", methodName);
			userRequestMap.put("userParamTypes", paramTypes);
		}
		
		public MethodPagingWorkerImpl setCacheCapacity(int cacheCapacity){
			this.cacheCapacity = cacheCapacity;
			return this;
		}
		
		public MethodPagingWorkerImpl setUserParamObjects(Object[] paramObjects) throws UserException {
			userRequestMap.put("userParamObjects", paramObjects);
			totalRecordCount = PagingServiceAgent.getInstance().executeForCount(userRequestMap);
			return this;
		}
		
		public List getRecords(int startRowNo, int count) {
			if((startRowNo <= 0) || (startRowNo > getTatalRecordCount())) return null;
			try {
				adjustCachedRecords(startRowNo, count);
				return cachedRecords.subList((startRowNo - firstRecordNo), 
						Math.min(((startRowNo - firstRecordNo) + count), cachedRecords.size()));
			} catch (Exception e) {
				return null;
			}
		}

		public int getTatalRecordCount() {
			return totalRecordCount;
		}
		
		private void adjustCachedRecords(int startRowNo, int count) {
			int cachedCount = (cachedRecords == null) ? 0 : cachedRecords.size();
			
			if((firstRecordNo < startRowNo) && (startRowNo + count < firstRecordNo + cachedCount)) {
				// 所需记录范围已在当前缓存记录范围内,无需调整
				return;
			}
			
			if(startRowNo < firstRecordNo) //需要向前滑动缓存范围窗口
				firstRecordNo = Math.max(1, (firstRecordNo - cacheCapacity / 2));
			if(startRowNo + count > firstRecordNo + cachedCount) // 需要向后滑动缓存范围窗口
				firstRecordNo = Math.min(startRowNo, (firstRecordNo + cacheCapacity / 2));
			
			// 把最新的分页信息存入用户参数对象中
			if(userRequestMap.get("userParamObjects") != null){
				for(Object obj : (Object[])userRequestMap.get("userParamObjects")){
					if(PagingUtils.canSupportPaging(obj)){
						PagingUtils.setSkipResults(obj, firstRecordNo - 1);
						PagingUtils.setMaxResults(obj, cacheCapacity);
						break;
					}
				}
			}
			cachedRecords = (List)userAgent.execute((Object[])userRequestMap.get("userParamObjects"));
			if(cachedRecords == null) cachedRecords = new ArrayList();
		}
	}
	
	/**
	 * Assistant class for MethodPagingWorkerImpl
	 * @author Tu_Minglei
	 */
	static class GeneralUserServiceAgent extends BaseClient {
		private String 	userServiceId;
		private String 	userMethodName;
		private Class[] userParamTypes;

		public GeneralUserServiceAgent(int moduleId, String serviceId, String methodName, Class[] paramTypes) {
			super(moduleId);
			this.userServiceId= serviceId;
			this.userMethodName=methodName;
			this.userParamTypes=paramTypes;
		}
		
		public Object execute(Object[] userParamObjects) {
			try {
				return super.invokeServer(userServiceId, userMethodName, userParamObjects, userParamTypes);
			} catch (UserException e) {
				return null;
			}
		}		
	}
	
	/**
	 * Assistant class for MethodPagingWorkerImpl
	 * @author Tu_Minglei
	 */
	static class PagingServiceAgent extends BaseClient {
		private static PagingServiceAgent instance;
		
		public PagingServiceAgent(){
			super(ModuleIDs.MODULE_PROD);
		}
		
		public static PagingServiceAgent getInstance(){
			if(instance == null) instance = new PagingServiceAgent();
			return instance;
		}
		
		public int executeForCount(Map<String,Object> userRequestMap) throws UserException{
			return (Integer)super.invokeServer("pagingService", "getTotalRecordCount",
					new Object[] { userRequestMap }, new Class[] { Map.class });
		}
	}
	
	/**
	 * <code>IPaginationViewer</code>的默认实现,可以把翻页结果自动显示在目标对象中
	 * <ul>当前支持的控件有:
	 * <li>CExtTable
	 * <li>CExtList
	 * </ul>
	 * <ul>当前支持的Model有:
	 * <li>CExtTableModel
	 * <li>DefaultListModel
	 * </ul>
	 * (注:1、SimpleTable和SimpleTableModel暂时支持不了;2、对其他控件或Model的支持可以按需添加)
	 * 
	 * @author Tu_Minglei
	 */
	static class DefaultPaginationViewer implements IPagingViewer {
		private Object targetObject;
		
		public DefaultPaginationViewer(Object targetObject){
			if(!canSupport(targetObject)) 
				throw new IllegalArgumentException("Unsupported object: "+targetObject.getClass().getName());
			this.targetObject = targetObject;
		}

		public void showRecords(List records) {
			if(targetObject instanceof CExtTable){
				((CExtTable)targetObject).removeAllRowBeans();
				((CExtTable)targetObject).addRowBeans(records);
				return;
			}
			if(targetObject instanceof CExtList){
				((CExtList)targetObject).removeAllElements();
				((CExtList)targetObject).addElements(records);
				return;
			}
			
			if(targetObject instanceof CExtTableModel){
				((CExtTableModel)targetObject).removeAllRowBeans();
				((CExtTableModel)targetObject).addRowBeans(records);
				return;
			}
			if(targetObject instanceof DefaultListModel){
				((DefaultListModel)targetObject).removeAllElements();
				if(records != null){
					for(Object record : records){
						((DefaultListModel)targetObject).addElement(record);
					}
				}
			}
			
//			if(targetObject instanceof SimpleTable){
//				((SimpleTable)targetObject).setObjects(new Object[0], Object.class);
//				if(records != null){
//					((SimpleTable)targetObject).setObjects(records.toArray(), records.get(0).getClass());
//				}
//			}
		}
		
		private boolean canSupport(Object targetObject){
			if(targetObject instanceof CExtTable) return true;
			if(targetObject instanceof CExtList) return true;
			if(targetObject instanceof CExtTableModel) return true;
			if(targetObject instanceof DefaultListModel) return true;
			
//			if(targetObject instanceof SimpleTable) return true;
//			if(targetObject instanceof SimpleTableModel) return true;
			
			return false;
		}
	}
	
}

⌨️ 快捷键说明

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