KDTable工具类

package com.kingdee.eas.szhk.mealandship.mealassembly.utils;

import java.util.ArrayList;
import java.util.List;

import com.kingdee.bos.ctrl.extendcontrols.BizDataFormat;
import com.kingdee.bos.ctrl.extendcontrols.KDBizPromptBox;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTDefaultCellEditor;
import com.kingdee.bos.ctrl.kdf.table.KDTable;
import com.kingdee.bos.ctrl.kdf.util.render.ObjectValueRender;
import com.kingdee.eas.basedata.master.cssp.client.F7CustomerSimpleSelector;
import com.kingdee.eas.basedata.master.cssp.client.F7SupplierSimpleSelector;
import com.kingdee.eas.basedata.master.material.client.F7BaseSelector;
import com.kingdee.eas.basedata.master.material.client.F7MaterialSimpleSelector;

/**
 * KDTable操作工具类
 * @author masb 2010-05-06
 *
 */
public class KDTableUtil {

	/**
	 * 设置行是否显示
	 */
	public static void setRowIsHide(boolean bHided,IRow row){
		row.getStyleAttributes().setHided(bHided);
	}
	
	/**
	 * 隐藏和指定值不等的行
	 * @param bHided
	 */
	public static void setRowIsHideByValue(IRow row,String colName,Object value){
		if(row.getCell(colName).getValue() == null){
			setRowIsHide(false,row);
		}else{
			boolean isEquals = value.equals(row.getCell(colName).getValue());
			setRowIsHide(!isEquals,row);
		}
		
	}

	/**
	 * 隐藏表格中所有和指定值相等的行
	 * @param table
	 * @param colName
	 * @param value
	 */
	public static void setRowsIsHideByValue(KDTable table,String colName,Object value){
		if(table == null || table.getRowCount() <= 0){
			return;
		}
		table.checkParsed();
		int count = table.getRowCount();
		IRow row = null;
		for (int i = count -1; i >= 0; i --) {
			row = table.getRow(i);
			setRowIsHideByValue(row,colName,value);
		}
	}
	
	/**
	 * 隐藏表格所有行
	 * @param table
	 */
	public static void setAllRowsHided(KDTable table){
		if(checkEmpty(table)){
			return ;
		}
		table.checkParsed();
		int count = table.getRowCount();
		IRow row = null;
		for (int i = count -1; i >= 0; i --) {
			row = table.getRow(i);
			setRowIsHide(true,row);
		}
	}
	
	/**
	 * 根据行设置单元格的值
	 * @param row
	 * @param colName
	 * @param value
	 */
	public static void setRowCellValue(IRow row,String colName,Object value){
		row.getCell(colName).setValue(value);
	}
	
	/**
	 * 根据表格设置单元格的值
	 * @param table
	 * @param colName
	 * @param value
	 * @param rowIndex
	 */
	public static void setTableCellValue(KDTable table,String colName,Object value,int rowIndex){
		if(checkEmpty(table)){
			return ;
		}
		IRow row = table.getRow(rowIndex);
		setRowCellValue(row,colName,value);
	}
	
	/**
	 * 比较行中单元格的值
	 * @param row
	 * @param colName
	 * @param source
	 * @return
	 */
	public static boolean compareCellValue(IRow row,String colName,Object source){
		return source.equals(row.getCell(colName).getValue());
	}
	
	/**
	 * 设置行中单元格的值为Null
	 * @param row
	 * @param colName
	 * @param source
	 * @param nullColName
	 */
	public static void setCellValueToNull(IRow row,String colName,Object source,String nullColName){
		if(compareCellValue(row,colName,source)){
			row.getCell(nullColName).setValue(null);
		}
	}
	
	/**
	 * 设置表格中单元格的值为Null
	 * @param table
	 * @param compareCol
	 * @param toCol
	 * @param compareVal
	 */
	public static void setTableCellValueToNull(KDTable table,String compareCol,String toCol,Object compareVal){
		if(checkEmpty(table)){
			return ;
		}
		int count = table.getRowCount();
		IRow row = null;
		for(int i=0;i<count;i++){
			row = table.getRow(i);
			setCellValueToNull(row,compareCol,compareVal,toCol);
		}
	}

	private static boolean checkEmpty(KDTable table) {
		if(table == null || table.getRowCount() <= 0){
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 根据单元格值比较结果删除行
	 * 不符合则删除
	 */
	public static void removeRowsByCompareVal(KDTable table,String colName,Object compareVal){
		if(checkEmpty(table)){
			return ;
		}
		table.checkParsed();
		int count = table.getRowCount();
		for(int i =count-1;i>=0;i--){
			IRow row = table.getRow(i);
			boolean isEquals = compareCellValue(row,colName,compareVal);
			if(isEquals){
				table.removeRow(i);
			}
		}
	}
	
	public static List getRowsByCompreValue(KDTable table,String colName,Object compareVal){
		List rowList = new ArrayList();
		if(checkEmpty(table)){
			return rowList;
		}
		table.checkParsed();
		int count = table.getRowCount();
		for(int i =count-1;i>=0;i--){
			IRow row = table.getRow(i);
			boolean isEquals = compareCellValue(row,colName,compareVal);
			if(isEquals){
				rowList.add(row);
			}
		}
		return rowList;
	}
	
	/**
	 * 设置F7列的编辑器
	 * @param table
	 * @param cloName
	 * @param queryPK
	 * @param formatter
	 */
	public static void initF7ColumnByName(KDTable table,String colName,String queryInfo,String[] formatter){
		KDBizPromptBox kdBizPromptBox = new KDBizPromptBox();
		kdBizPromptBox.setDisplayFormat(formatter[0]);
		kdBizPromptBox.setEditFormat(   formatter[1]);
		kdBizPromptBox.setCommitFormat( formatter[2]);
		kdBizPromptBox.setQueryInfo(queryInfo);
		
    	KDTDefaultCellEditor cellEditor = new KDTDefaultCellEditor(kdBizPromptBox);
		table.getColumn(colName).setEditor(cellEditor);
		
		ObjectValueRender table_F7_OVR = new ObjectValueRender();
		table_F7_OVR.setFormat(new BizDataFormat("$name$"));
        table.getColumn(colName).setRenderer(table_F7_OVR);
	}
	
	/**
	 * 设置F7列的编辑器
	 * @param table
	 * @param cloName
	 * @param queryPK
	 * @param formatter
	 */
	public static void initF7ColumnByIndex(KDTable table,int colIndex,String queryInfo,String[] formatter){
		KDBizPromptBox kdBizPromptBox = new KDBizPromptBox();
		kdBizPromptBox.setDisplayFormat(formatter[0]);
		kdBizPromptBox.setEditFormat(   formatter[1]);
		kdBizPromptBox.setCommitFormat( formatter[2]);
		kdBizPromptBox.setQueryInfo(queryInfo);
		
    	KDTDefaultCellEditor cellEditor = new KDTDefaultCellEditor(kdBizPromptBox);
		table.getColumn(colIndex).setEditor(cellEditor);
	}
	
	/**
	 * 获取某个单元格的值
	 * @param row
	 * @param colName
	 * @return
	 */
	public static Object getCellValue(IRow row ,String colName){
		return row.getCell(colName).getValue();
	}
	
	/**
	 * 获取选中行中某个单元格的值
	 * @param table
	 * @param colName
	 * @return
	 */
	public static Object getCellValueInSelectedRow(KDTable table,String colName){
		if(checkEmpty(table) || table.getSelectManager().size() <= 0){
			return "";
		}
		int rowIndex = table.getSelectManager().get(0).getTop();
		return getCellValue(table.getRow(rowIndex),colName);
	}
	
	public static KDBizPromptBox innitF7SPrompt(String tblName,Object objUI){
		final KDBizPromptBox objPrmt = new KDBizPromptBox();   
		F7BaseSelector selector = null;//new F7BaseSelector(this, null, objPrmt);   
		objPrmt.setVisible(true);   
		objPrmt.setEditable(true);   
		objPrmt.setEditFormat("$number$");   
        objPrmt.setCommitFormat("$number$;$name$");   
        objPrmt.setDisplayFormat("$name$");             
		 if(tblName.equalsIgnoreCase("T_BD_Customer")){//客户   
			 objPrmt.setQueryInfo("com.kingdee.eas.basedata.master.cssp.app.F7SupplierDefaultQuery");   
			 selector = new F7CustomerSimpleSelector(objUI, objPrmt);
			 objPrmt.setSelector(selector);   
		 }else if(tblName.equalsIgnoreCase("T_BD_Supplier")){//供应商   
			 objPrmt.setQueryInfo("com.kingdee.eas.basedata.master.cssp.app.F7SupplierQuery");   
			 selector = new F7SupplierSimpleSelector(objUI, objPrmt);     
			 objPrmt.setSelector(selector);   
		 }else if(tblName.equalsIgnoreCase("T_BD_Material")){//物料   
			 objPrmt.setQueryInfo("com.kingdee.eas.basedata.master.material.app.F7MaterialBaseInfoQuery");   
			 selector = new F7MaterialSimpleSelector(objUI, objPrmt);
			 objPrmt.setSelector(selector);   
		 }else{
			 
		 }
		return objPrmt;
	}
	
	public static F7BaseSelector innitF7Selector(String tblName,KDBizPromptBox objPrmt,Object objUI){
		F7BaseSelector selector = null;//new F7BaseSelector(this, null, objPrmt);   
		 if(tblName.equalsIgnoreCase("T_BD_Customer")){//客户   
			 objPrmt.setQueryInfo("com.kingdee.eas.basedata.master.cssp.app.F7CustomerQuery");   
			 selector = new F7CustomerSimpleSelector(objUI, objPrmt);   
		 }else if(tblName.equalsIgnoreCase("T_BD_Supplier")){//供应商   
			 objPrmt.setQueryInfo("com.kingdee.eas.basedata.master.cssp.app.F7SupplierQuery");   
			 selector = new F7SupplierSimpleSelector(objUI, objPrmt);     
		 }else if(tblName.equalsIgnoreCase("T_BD_Material")){//物料   
			 objPrmt.setQueryInfo("com.kingdee.eas.basedata.master.material.app.F7MaterialBaseInfoQuery");   
			 selector = new F7MaterialSimpleSelector(objUI, objPrmt);
		 }else{
			 
		 }
		return selector;
	}
	
}

你可能感兴趣的:(table)