How-to delete a tree node using the context menu
Posted
by frank.nimphius
on Oracle Blogs
See other posts from Oracle Blogs
or by frank.nimphius
Published on Fri, 18 Mar 2011 06:32:43 +0000
Indexed on
2011/03/18
8:14 UTC
Read the original article
Hit count: 385
Hierarchical trees in Oracle ADF make use of View Accessors, which means that only the top level node needs to be exposed as a View Object instance on the ADF Business Components Data Model. This also means that only the top level node has a representation in the PageDef file as a tree binding and iterator binding reference. Detail nodes are accessed through tree rule definitions that use the accessor mentioned above (or nested collections in the case of POJO or EJB business services).
The tree component is configured for single node selection, which however can be declaratively changed for users to press the ctrl key and selecting multiple nodes.
In the following, I explain how to create a context menu on the tree for users to delete the selected tree nodes. For this, the context menu item will access a managed bean, which then determines the selected node(s), the internal ADF node bindings and the rows they represent.
As mentioned, the ADF Business Components Data Model only needs to expose the top level node data sources, which in this example is an instance of the Locations View Object. For the tree to work, you need to have associations defined between entities, which usually is done for you by Oracle JDeveloper if the database tables have foreign keys defined
Note: As a general hint of best practices and to simplify your life: Make sure your database schema is well defined and designed before starting your development project. Don't treat the database as something organic that grows and changes with the requirements as you proceed in your project. Business service refactoring in response to database changes is possible, but should be treated as an exception, not the rule. Good database design is a necessity – even for application developers – and nothing evil.
To create the tree component, expand the Data Controls panel and drag the View Object collection to the view.
From the context menu, select the tree component entry and continue with defining the tree rules that make up the hierarchical structure.
As you see, when pressing the green plus icon in the Edit Tree Binding dialog, the data structure, Locations - Departments – Employees in my sample, shows without you having created a View Object instance for each of the nodes in the ADF Business Components Data Model.
After you configured the tree structure in the Edit Tree Binding dialog, you press OK and the tree is created. Select the tree in the page editor and open the Structure Window (ctrl+shift+S). In the Structure window, expand the tree node to access the conextMenu facet. Use the right mouse button to insert a Popup into the facet.
Repeat the same steps to insert a Menu and a Menu Item into the Popup you created.
The Menu item text should be changed to something meaningful like "Delete". Note that the custom menu item later is added to the context menu together with the default context menu options like expand and expand all.
To define the action that is executed when the menu item is clicked on, you select the Action Listener property in the Property Inspector and click the arrow icon followed by the Edit menu option. Create or select a managed bean and define a method name for the action handler.
Next, select the tree component and browse to its binding property in the Property Inspector. Again, use the arrow icon | Edit option to create a component binding in the same managed bean that has the action listener defined. The tree handle is used in the action listener code, which is shown below:
public void onTreeNodeDelete(ActionEvent actionEvent) {
//access the tree from the JSF component reference created
//using the af:tree "binding" property. The "binding" property
//creates a pair of set/get methods to access the RichTree instance
RichTree tree = this.getTreeHandler();
//get the list of selected row keys
RowKeySet rks = tree.getSelectedRowKeys();
//access the iterator to loop over selected nodes
Iterator rksIterator = rks.iterator();
//The CollectionModel represents the tree model and is
//accessed from the tree "value" property
CollectionModel model = (CollectionModel) tree.getValue();
//The CollectionModel is a wrapper for the ADF tree binding
//class, which is JUCtrlHierBinding
JUCtrlHierBinding treeBinding =
(JUCtrlHierBinding) model.getWrappedData();
//loop over the selected nodes and delete the rows they
//represent
while(rksIterator.hasNext()){
List nodeKey = (List) rksIterator.next();
//find the ADF node binding using the node key
JUCtrlHierNodeBinding node =
treeBinding.findNodeByKeyPath(nodeKey);
//delete the row.
Row rw = node.getRow();
rw.remove();
}
//only refresh the tree if tree nodes have been selected
if(rks.size() > 0){
AdfFacesContext adfFacesContext =
AdfFacesContext.getCurrentInstance();
adfFacesContext.addPartialTarget(tree);
}
}
Note: To enable multi node selection for a tree, select the tree and change the row selection setting from "single" to "multiple".
Note: a fully pictured version of this post will become available at the end of the month in a PDF summary on ADF Code Corner : http://www.oracle.com/technetwork/developer-tools/adf/learnmore/index-101235.html
© Oracle Blogs or respective owner