2012年6月20日星期三

ADF_140:Tree组件使用指南之十:获取选中的Tree节点

运行环境:JDeveloper 11.1.2.2.0 + Oracle Database 10g Express Edition 10.2.0.1。

实际情况中,经常需要获取选中的Tree节点信息,与定制TreeSelectionListener每次点击选中一个节点略有不同,可以通过按住Ctrl键选取多个节点。

说明,本实验基于定制SelectionListener

1. 创建一个新页面:tree_selected_node.jsf
(1)设置RowSelection=mutiple,允许多选。
(2)设置Binding,指向Managed Bean中的一个属性。
(3)放置一个按钮,点击按钮,调用Managed Bean中的方法,获取选中的Tree节点

2. 完整的Managed Bean代码

package view;

import java.util.Iterator;
import java.util.List;

import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.data.RichTree;

import oracle.adf.view.rich.component.rich.data.RichTreeTable;

import oracle.jbo.Row;
import oracle.jbo.uicli.binding.JUCtrlHierBinding;
import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;

import oracle.jbo.uicli.binding.JUCtrlHierTypeBinding;

import org.apache.myfaces.trinidad.event.SelectionEvent;

import org.apache.myfaces.trinidad.model.CollectionModel;
import org.apache.myfaces.trinidad.model.RowKeySet;

import view.util.JSFUtils;

public class MyBackingBean {
private RichTree tree;

public MyBackingBean() {
}

public void treeSelectionListener(SelectionEvent selectionEvent) {
JSFUtils.invokeMethodExpression("#{bindings.DepartmentsView1.treeModel.makeCurrent}", Object.class,
SelectionEvent.class, selectionEvent);

RichTree tree = (RichTree)selectionEvent.getSource();
RowKeySet rowKeySet = selectionEvent.getAddedSet();
Iterator rksIterator = rowKeySet.iterator();
while (rksIterator.hasNext()) {
List key = (List)rksIterator.next();
JUCtrlHierBinding treeBinding = null;
CollectionModel collectionModel = (CollectionModel)tree.getValue();
treeBinding = (JUCtrlHierBinding)collectionModel.getWrappedData();
JUCtrlHierNodeBinding nodeBinding = treeBinding.findNodeByKeyPath(key);

String[] strs = nodeBinding.getAttributeNames();
for (String str : strs) {
System.out.println("########################### " + str + ":" + nodeBinding.getAttribute(str));
}

Row rw = nodeBinding.getRow();
String rowType = rw.getStructureDef().getDefName();
System.out.println("########################### rowType " + rowType);

JUCtrlHierTypeBinding typeBinding = nodeBinding.getHierTypeBinding();
String nodeStuctureDefname = typeBinding.getStructureDefName();
System.out.println("########################### nodeStuctureDefname " + nodeStuctureDefname);
}
}

public void buttonActionListener(ActionEvent actionEvent) {
RichTree treeTable = this.getTree();
RowKeySet rks = treeTable.getSelectedRowKeys();
Iterator rksIterator = rks.iterator();
while (rksIterator.hasNext()) {
List key = (List)rksIterator.next();
JUCtrlHierBinding treeTableBinding = null;
CollectionModel collectionModel = (CollectionModel)tree.getValue();
collectionModel.setRowKey(key);
treeTableBinding = (JUCtrlHierBinding)collectionModel.getWrappedData();
JUCtrlHierNodeBinding nodeBinding = treeTableBinding.findNodeByKeyPath(key);

String[] strs = nodeBinding.getAttributeNames();
for (String str : strs) {
System.out.println("########################### " + str + ":" + nodeBinding.getAttribute(str));
}

Row rw = nodeBinding.getRow();
System.out.println("########################### row " + rw);

String rowType = rw.getStructureDef().getDefName();
System.out.println("########################### rowType " + rowType);

JUCtrlHierTypeBinding typeBinding = nodeBinding.getHierTypeBinding();
String nodeStuctureDefname = typeBinding.getStructureDefName();
System.out.println("########################### nodeStuctureDefname " + nodeStuctureDefname);
}
}

public void setTree(RichTree tree) {
this.tree = tree;
}

public RichTree getTree() {
return tree;
}
}

其中buttonActionListener是点击按钮调用的方法,这里最重要的一行代码是 collectionModel.setRowKey(key);,如果没有此行代码,nodeBinding会为Null。

3. 运行效果,按住Ctrl键多选几个节点,然后点击按钮
输出如下:
########################### DepartmentId:10
########################### DepartmentName:Administration
########################### row ViewRow [oracle.jbo.Key[10 ]]
########################### rowType DepartmentsView
########################### nodeStuctureDefname model.DepartmentsView
########################### DepartmentId:20
########################### DepartmentName:Marketing
########################### row ViewRow [oracle.jbo.Key[20 ]]
########################### rowType DepartmentsView
########################### nodeStuctureDefname model.DepartmentsView
########################### DepartmentId:40
########################### DepartmentName:Human Resources
########################### row ViewRow [oracle.jbo.Key[40 ]]
########################### rowType DepartmentsView
########################### nodeStuctureDefname model.DepartmentsView
########################### DepartmentId:30
########################### DepartmentName:Purchasing
########################### row ViewRow [oracle.jbo.Key[30 ]]
########################### rowType DepartmentsView
########################### nodeStuctureDefname model.DepartmentsView

Project 下载:ADF_Tree_SelectionListener(6).7z

参考文献:
1. http://muhongdi.blog.163.com/blog/static/4325035120114161078850/
2. http://www.oracle.com/technetwork/developer-tools/adf/learnmore/26-get-selected-tree-node-data-169165.pdf

没有评论: