How to Display Data in Trees

Using Tables and Trees 10-37 Figure 10–20 nodeStamp Facet in the Structure Window The component’s value should be bound to the variable value set on the tree’s var attribute and the attribute to be displayed. For example, the tree in the File Explorer application uses folder as the value for the var attribute, and displays the name of the directory for each node. Therefore, the value of the output component used to display the directory name is {folder.name}.

10.5.2 What Happens When You Add a Tree to a Page

When you add a tree to a page, JDeveloper adds a nodeStamp facet to stamp out the nodes of the tree. Example 10–12 shows the abbreviated code for the tree in the File Explorer application that displays the directory structure. Example 10–12 ADF Faces Tree Code in a JSF Page af:tree id=folderTree var=folder binding={explorer.navigatorManager.foldersNavigator .foldersTreeComponent} value={explorer.navigatorManager.foldersNavigator. foldersTreeModel} disclosedRowKeys={explorer.navigatorManager.foldersNavigator. foldersTreeDisclosedRowKeys} rowSelection=single contextMenuId=:context2 selectionListener={explorer.navigatorManager.foldersNavigator. showSelectedFolderContent} f:facet name=nodeStamp af:panelGroupLayout af:image id=folderNodeStampImg source={folder.icon} inlineStyle=vertical-align:middle; margin-right:3px; shortDesc=folder icon af:outputText id=folderNodeStampText value={folder.name} af:panelGroupLayout f:facet af:tree

10.5.3 What Happens at Runtime: Tree Component Events

The tree is displayed in a format with nodes indented to indicate their levels in the hierarchy. The user can click nodes to expand them to show children nodes. The user can click expanded nodes to collapse them. When a user clicks one of these icons, the component generates a RowDisclosureEvent event. You can register a custom rowDisclosureListener method to handle any processing in response to the Tip: Facets can accept only one child component. Therefore, if you want to use more than one component per node, place the components in a group component that can be the facet’s direct child, as shown in Figure 10–20 . 10-38 Web User Interface Developers Guide for Oracle Application Development Framework event. For more information, see Section 10.5.4, What You May Need to Know About Programmatically Expanding and Collapsing Nodes. When a user selects or deselects a node, the tree component invokes a selectionEvent event. You can register custom selectionListener instances, which can do post-processing on the tree component based on the selected nodes. For more information, see Section 10.5.5, What You May Need to Know About Programmatically Selecting Nodes.

10.5.4 What You May Need to Know About Programmatically Expanding and Collapsing Nodes

The RowDisclosureEvent event has two RowKeySet objects: the RemovedSet object for all the collapsed nodes and the AddedSet object for all the expanded nodes. The component expands the subtrees under all nodes in the added set and collapses the subtrees under all nodes in the removed set. Your custom rowDisclosureListener method can do post-processing, on the tree component, as shown in Example 10–13 . Example 10–13 Tree Table Component with rowDisclosureListener af:treeTable id=folderTree var=directory value={fs.treeModel} binding={editor.component} rowselection=multiple columnselection=multiple focusRowKey={fs.defaultFocusRowKey} selectionListener={fs.Table} contextMenuId=treeTableMenu rowDisclosureListener={fs.handleRowDisclosure} The backing bean method that handles row disclosure events is shown in Example 10–14 . The example illustrates expansion of a tree node. For the contraction of a tree node, you would use getRemovedSet. Example 10–14 Backing Bean Method for RowDisclosureEvent public void handleRowDisclosureRowDisclosureEvent rowDisclosureEvent throws Exception { Object rowKey = null; Object rowData = null; RichTree tree = RichTree rowDisclosureEvent.getSource; RowKeySet rks = rowDisclosureEvent.getAddedSet; if rks = null { int setSize = rks.size; if setSize 1 { throw new ExceptionUnexpected multiple row disclosure added row sets found.; } if setSize == 0 { nothing in getAddedSet indicates this is a node contraction, not expansion. If interested only in handling node expansion at this point, return. return; } rowKey = rks.iterator.next; tree.setRowKeyrowKey; Using Tables and Trees 10-39 rowData = tree.getRowData; Do whatever is necessary for accessing tree node from rowData, by casting it to an appropriate data structure for example, a Java map or Java bean, and so forth. } } Trees and tree tables use an instance of the oracle.adf.view.rich.model.RowKeySet class to keep track of which nodes are expanded. This instance is stored as the disclosedRowKeys attribute on the component. You can use this instance to control the expand or collapse state of an node in the hierarchy programatically, as shown in Example 10–15 . Any node contained by the RowKeySet instance is expanded, and all other nodes are collapsed. The addAll method adds all elements to the set, and the and removeAll method removes all the nodes from the set. Example 10–15 Tree Component with disclosedRowKeys Attribute af:tree var=node inlineStyle=width:90; height:300px id=displayRowTable varStatus=vs rowselection=single disclosedRowKeys={treeTableTestData.disclosedRowKeys} value={treeTableTestData.treeModel} The backing bean method that handles the disclosed row keys is shown in Example 10–16 . Example 10–16 Backing Bean Method for Handling Row Keys public RowKeySet getDisclosedRowKeys { if disclosedRowKeys == null { Create the PathSet that we will use to store the initial expansion state for the tree RowKeySet treeState = new RowKeySetTreeImpl; RowKeySet requires access to the TreeModel for currency. TreeModel model = getTreeModel; treeState.setCollectionModelmodel; Make the model point at the root node int oldIndex = model.getRowIndex; model.setRowKeynull; forint i = 1; i=19; ++i { model.setRowIndexi; treeState.setContainedtrue; } model.setRowIndexoldIndex; disclosedRowKeys = treeState; } return disclosedRowKeys; } 10-40 Web User Interface Developers Guide for Oracle Application Development Framework

10.5.5 What You May Need to Know About Programmatically Selecting Nodes

The tree and tree table components allow nodes to be selected, either a single node only, or multiple nodes. If the component allows multiple selections, users can select multiple nodes using Control+click and Shift+click operations. When a user selects or deselects a node, the tree component fires a selectionEvent event. This event has two RowKeySet objects: the RemovedSet object for all the deselected nodes and the AddedSet object for all the selected nodes. Tree and tree table components keep track of which nodes are selected using an instance of the class oracle.adf.view.rich.model.RowKeySet. This instance is stored as the selectedRowKeys attribute on the component. You can use this instance to control the selection state of a node in the hierarchy programatically. Any node contained by the RowKeySet instance is deemed selected, and all other nodes are not selected. The addAll method adds all nodes to the set, and the and removeAll method removes all the nodes from the set. Tree and tree table node selection works in the same way as table row selection. You can refer to sample code for table row selection in Section 10.2.8, What You May Need to Know About Performing an Action on Selected Rows in Tables.

10.6 Displaying Data in Tree Tables

The ADF Faces tree table component displays hierarchical data in the form of a table. The display is more elaborate than the display of a tree component, because the tree table component can display columns of data for each tree node in the hierarchy. The component includes mechanisms for focusing on subtrees within the main tree, as well as expanding and collapsing nodes in the hierarchy. Figure 10–21 shows the tree table used in the File Explorer application. Like the tree component, the tree table can display the hierarchical relationship between the files in the collection. And like the table component, it can also display attribute values for each file. Figure 10–21 Tree Table in the File Explorer Application The immediate children of a tree table component must be column components, in the same way as for table components. Unlike the table, the tree table component has a nodeStamp facet which holds the column that contains the primary identifier of an node in the hierarchy. The treeTable component supports the same stamping behavior as the Tree component for details, see Section 10.5, Displaying Data in Trees . For example, in the File Explorer application as shown in Figure 10–21 , the primary identifier is the file name. This column is what is contained in the nodeStamp facet. The other columns, such as Type and Size, display attribute values on the primary identifier, and these columns are the direct children of the tree table component. This tree table uses node as the value of the variable that will be used to stamp out the data for each node in the nodeStamp facet column and each component in the child columns. Example 10–17 shows abbreviated code for the tree table in the File Explorer application.