最近有个需求,需要用F7选择客户组别,物料组别、供应商组别等,但是用户体验就不说了,点F7出来一张表,没任何层次结构,客户相当不满意,要选择出来一颗树,现在讲F7显示效果如下,并列出代码。
需要实现这个功能,要做如下
注意:我的包全部在如下路径,如果要自己修改,请自己替换路径:com.kingdee.eas.shine.utils.client
1、设计TreeUI:继承CoreUI,放一个TreeView,一个Tree 一个确定按钮,一个取消按钮
2、发布TreeUI元数据,实现类代码如下(未写注释)
/** * output package name */ package com.kingdee.eas.shine.utils.client; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Iterator; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import org.apache.log4j.Logger; import com.kingdee.bos.BOSException; import com.kingdee.bos.ctrl.swing.tree.DefaultKingdeeTreeNode; import com.kingdee.bos.ctrl.swing.tree.KingdeeTreeModel; import com.kingdee.bos.metadata.data.SortType; import com.kingdee.bos.metadata.entity.EntityViewInfo; import com.kingdee.bos.metadata.entity.FilterInfo; import com.kingdee.bos.metadata.entity.SelectorItemCollection; import com.kingdee.bos.metadata.entity.SorterItemCollection; import com.kingdee.bos.metadata.entity.SorterItemInfo; import com.kingdee.bos.ui.face.CoreUIObject; import com.kingdee.eas.framework.ITreeBase; import com.kingdee.eas.framework.TreeBaseCollection; import com.kingdee.eas.framework.TreeBaseInfo; import com.kingdee.eas.shine.cm.CostObjectTreeFactory; import com.kingdee.eas.util.client.MsgBox; /** * output class name */ public class TreeUI extends AbstractTreeUI { private static final Logger logger = CoreUIObject.getLogger(TreeUI.class); ITreeBase iTreeBase; TreeBaseInfo selectData; private boolean isCancel; public static final String STRTREEBASE = "STRTREEBASE"; public static final String TREE_TITLE = "TREE_TITLE"; public static final String TREE_FILTER = "TREE_FILTER"; /** * output class constructor */ public TreeUI() throws Exception { super(); isCancel = false; } public boolean isCanceled() { return isCancel; } public TreeBaseInfo getData() { return selectData; } @Override public void onLoad() throws Exception { super.onLoad(); buildTree(); btnOK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnOK_click(); } }); btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { isCancel = true; disposeUIWindow(); } }); treeMain.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { treeMain_valueChanged(e); } }); treeMain.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { treeMain_clicked(e); } }); } protected void treeMain_clicked(MouseEvent e) { if (e.getClickCount() == 2) { btnOK_click(); } } private void buildTree() throws BOSException { EntityViewInfo evi = new EntityViewInfo(); SelectorItemCollection sic = evi.getSelector(); sic.add("*"); SorterItemCollection sorters = new SorterItemCollection(); SorterItemInfo sorter = new SorterItemInfo("longNumber"); sorter.setSortType(SortType.ASCEND); sorters.add(sorter); evi.setSorter(sorters); String title = (String) getUIContext().get(TREE_TITLE); setUITitle(title); iTreeBase = (ITreeBase) getUIContext().get(STRTREEBASE); FilterInfo filter = (FilterInfo) getUIContext().get(TREE_FILTER); if(filter!=null){ evi.setFilter(filter); } TreeBaseCollection collection = iTreeBase.getTreeBaseCollection(evi); DefaultKingdeeTreeNode root = new DefaultKingdeeTreeNode(title); DefaultKingdeeTreeNode lastNode = root; String lastLongNumber = null; for (Iterator iterator = collection.iterator(); iterator.hasNext();) { TreeBaseInfo group = (TreeBaseInfo) iterator.next(); if (lastLongNumber == null) { lastLongNumber = group.getLongNumber(); DefaultKingdeeTreeNode groupNode = new DefaultKingdeeTreeNode(group.getName()); lastNode = groupNode; root.add(groupNode); } else { String longNumber = group.getLongNumber(); DefaultKingdeeTreeNode groupNode = new DefaultKingdeeTreeNode(group.getName()); if (longNumber.startsWith(lastLongNumber)) { lastNode.add(groupNode); } else { DefaultKingdeeTreeNode parent = getParent(lastNode, group.getLevel()); if (parent != null) { parent.add(groupNode); } } lastNode = groupNode; lastLongNumber = longNumber; } lastNode.setUserObject(group); } treeMain.setModel(new KingdeeTreeModel(root)); } private DefaultKingdeeTreeNode getParent(DefaultKingdeeTreeNode node, int level) { if (node.getLevel() == 0) { return node; } else if (node != null) { Object object = node.getUserObject(); if (object != null && object instanceof TreeBaseInfo) { TreeBaseInfo group = (TreeBaseInfo) object; if ((level - 1) == group.getLevel()) { return node; } else { return getParent((DefaultKingdeeTreeNode) node.getParent(), level); } } } return null; } protected void btnOK_click() { if (selectData == null) { MsgBox.showWarning(this, "请选择节点"); abort(); } isCancel = false; disposeUIWindow(); } protected void treeMain_valueChanged(TreeSelectionEvent e) { if (e.getNewLeadSelectionPath() != null) { DefaultKingdeeTreeNode node = (DefaultKingdeeTreeNode) e.getNewLeadSelectionPath().getLastPathComponent(); if (node != null) { Object object = node.getUserObject(); if (object != null && object instanceof TreeBaseInfo) { selectData = (TreeBaseInfo) object; } } } } }
3、做一个类实现KDPromptSelector接口
package com.kingdee.eas.shine.utils.client; import java.awt.Dialog; import java.awt.Frame; import java.awt.KeyboardFocusManager; import java.awt.Window; import com.kingdee.bos.ctrl.swing.KDPromptSelector; import com.kingdee.bos.metadata.entity.FilterInfo; import com.kingdee.bos.ui.face.IUIWindow; import com.kingdee.bos.ui.face.UIFactory; import com.kingdee.eas.common.client.UIContext; import com.kingdee.eas.common.client.UIFactoryName; import com.kingdee.eas.framework.ITreeBase; import com.kingdee.eas.framework.client.CoreUI; import com.kingdee.eas.util.client.ExceptionHandler; public class TreeF7 implements KDPromptSelector { private IUIWindow window; private String title; ITreeBase iTreeBase; CoreUI owner; FilterInfo filter; public TreeF7(ITreeBase iTreeBase, CoreUI owner, String title,FilterInfo filter) { this.iTreeBase = iTreeBase; this.owner = owner; this.title = title; this.filter=filter; } public Object getData() { Object data = null; try { if (window != null) data = ((TreeUI) window.getUIObject()).getData(); } catch (Exception e) { ExceptionHandler.handle(e); } return data; } public boolean isCanceled() { boolean isCanceled = true; try { isCanceled = ((TreeUI) window.getUIObject()).isCanceled(); } catch (Exception e) { ExceptionHandler.handle(e); } return isCanceled; } public void show() { try { getWindow().show(); } catch (Exception e) { ExceptionHandler.handle(e); } } private IUIWindow getWindow() throws Exception { if (window == null) { Object owner = null; Window win = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); if (win instanceof Frame) { owner = (Frame) win; } else if (win instanceof Dialog) { owner = (Dialog) win; } UIContext ctx = new UIContext(owner); ctx.put(TreeUI.TREE_TITLE, title); ctx.put(TreeUI.STRTREEBASE, iTreeBase); ctx.put(TreeUI.TREE_FILTER, filter); window = UIFactory.createUIFactory(UIFactoryName.MODEL).create(TreeUI.class.getName(), ctx); } return window; } public void setTitle(String title) { this.title = title; window = null; } public String getTitle() { return title; } }
3、监听F7的选择事件
kdtMaterial_materialgroup_PromptBox.addSelectorListener(new SelectorListener(){ public void willShow(SelectorEvent paramSelectorEvent) { try { IMaterialGroup iTree = MaterialGroupFactory.getRemoteInstance(); FilterInfo filter= new FilterInfo(); filter.getFilterItems().add(new FilterItemInfo("groupStandard.number","BaseGroupStandard")); TreeF7 f7 = new TreeF7(iTree, getui(), "物料组别",filter); kdtMaterial_materialgroup_PromptBox.setSelector(f7); } catch (Exception e1) { handUIException(e1); } }});
protected CoreUI getui() { return this; }