?? paginghelper.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 + -