The Rhino TreeViewPane is a Component which displays a set of hierarchical data in an outline form. Multiple trees can be displayed, one at a time. The individual nodes in each tree are Items; each level in each tree contains Items within a Category or Association. The structure of the trees are specified almost entirely in Properties Files.
The TreeViewPane extends the JScrollPane class, and can thus be displayed within any Frame. The tree in the TreeViewPane is a JTree. Each node in the tree is associated with an Item in a particular Category or Association. Each node in the tree has an Icon and a name. The Icon can be a FtrIcon and thus can visually respond to changes in the state of its associated Item. It is possible to customize the display of the name of the Item by specifying an ItemNameRendererFormat.
The JTree in the TreeViewPane is also accessible so that one can take advantage of all of its capabilities (including listening for selections).
As with most Rhino UI classes, there are two basic steps in the creation of a TreeViewPane. First, one adds properties to the properties file which define the structure of the tree. Second, one writes the code which creates a new TreeViewPane which is defined by those properties. The correlation between the properties and the TreeViewPane is a name, a String, which is used as a prefix to the various property names. Pass this string to the TreeViewPane constructor as the prefix argument.
The various properties which define the structure of the tree are shown below. Default values, if any, are shown in parentheses.
For example, suppose the Properties file contains the following entries:<prefix>.com.shoon.MyCategory.displayAttr = {0}: {1} <prefix>.com.shoon.MyCategory.displayAttrArg0 = ITEM_TYPE <prefix>.com.shoon.MyCategory.displayAttrArg1 = ITEM_NAMEAnd let's suppose the Item corresponding to a given node of the tree has the following Attributes:ITEM_TYPE = Personal Name ITEM_NAME = HowardThen the following call will be made to render the name of the node (using the Attributes of its Item item):java.text.MessageFormat("{0}: {1}", new Object { item.getValueString("ITEM_TYPE"), item.getValueString("ITEM_NAME") });Thus the name of the Item (and the node in the tree) will be rendered as:Personal Name: Howard
To create a new TreeViewPane, you must specify the Item which will serve as the root of the tree and a name which will be used to find the Properties. Note that the type of the Item must match the type of Category specified in the Properties for level0 of the tree. Here is a simple example which creates a TreeViewPane and adds it to the Frame (The <prefix> is "MyTree"):
TreeViewPane treeViewPane = new TreeViewPane(uic, hc, rs, rootItem, "MyTree"); add(treeViewPane);
See the description of TreeViewPane for a full description of the Class and its constructor arguments.
The tree displayed by default is tree 0 (see Tree Structure above).
To change trees programmatically, for example, using a menu, call TreeViewPane.setTree(int) or TreeViewPane.setTree(java.lang.String).
To listen for user selection of a node in the tree, use the standard JTree calls. For example, use TreeViewPane.addTreeSelectionListener(TreeSelectionListener) to add a listener which fires when a node in the tree is selected. You can also use TreeViewPane.addActionListener(ActionListener) to listen for the user performing an action upon a node in the tree.
Here is a portion of the Properties file which defines the structure of four tree (example adapted from the FailSafe Manager 2.0 product):
MyTree.tree0 = groupsResources MyTree.tree1 = resources MyTree.tree2 = groups MyTree.tree3 = policies MyTree.groupsResources.level0 = com.sgi.fsmgr.category.ClusterCategory MyTree.groupsResources.level1 = com.sgi.fsmgr.category.ResourceGroupCategory MyTree.groupsResources.level2 = com.sgi.fsmgr.category.ResourceCategory MyTree.resources.level0 = com.sgi.fsmgr.category.ClusterCategory MyTree.resources.level1 = com.sgi.fsmgr.category.ResourceCategory MyTree.groups.level0 = com.sgi.fsmgr.category.ClusterCategory MyTree.groups.level1 = com.sgi.fsmgr.category.ResourceGroupCategory MyTree.policies.level0 = com.sgi.fsmgr.category.ClusterCategory MyTree.policies.level1 = com.sgi.fsmgr.category.FailoverPolicyCategory MyTree.policies.level1.useAssoc = false MyTree.com.sgi.fsmgr.category.ResourceCategory.displayAttr = {0}: {1} MyTree.com.sgi.fsmgr.category.ResourceCategory.displayAttrArg0 = _RESOURCE_TYPE MyTree.com.sgi.fsmgr.category.ResourceCategory.displayAttrArg1 = _RESOURCE MyTree.com.sgi.fsmgr.category.ResourceCategory.stateAttr = CAM_STATUS MyTree.com.sgi.fsmgr.category.ResourceCategory.ONLINE_PENDING.blink = true MyTree.com.sgi.fsmgr.category.ResourceCategory.OFFLINE_PENDING.blink = true MyTree.com.sgi.fsmgr.category.ResourceCategory.ERROR.blink = true MyTree.com.sgi.fsmgr.category.ResourceCategory.itemCompare = \ com.sgi.fsmgr.detailView.CategoryItemCompare MyTree.com.sgi.fsmgr.category.ResourceGroupCategory.stateAttr = CAM_STATUS MyTree.com.sgi.fsmgr.category.ResourceGroupCategory.ERROR.blink = true MyTree.com.sgi.fsmgr.category.ResourceGroupCategory.ONLINE_PENDING.blink = true MyTree.com.sgi.fsmgr.category.ResourceGroupCategory.OFFLINE_PENDING.blink = true MyTree.com.sgi.fsmgr.category.ResourceGroupCategory.itemCompare = \ com.sgi.fsmgr.detailView.CategoryItemCompare MyTree.com.sgi.fsmgr.category.ClusterCategory.stateAttr = CAM_STATUS MyTree.com.sgi.fsmgr.category.ClusterCategory.INACTIVE.blink = true
These example Properties define four (4) trees, one of which is displayed in the TreeViewPane at any given time. The Item at the root of the tree must be in the "Cluster" Category. Any given tree can be dynamically chosen for display. To select the "resources" tree, for example, to be displayed in the TreeViewPane, the following calls are equivalent:
treeViewPane.setTree(1); treeViewPane.setTree("resources");The four trees which can be displayed are as follows:
Four (4) Categories of Items can be displayed in the tree, as follows:
To listen for a node in the tree being acted upon (double-clicked) by the user, the following code is used:
treeViewPane.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // get the node of the tree that has been selected // DefaultMutableTreeNode node = (DefaultMutableTreeNode) (((TreePath)event.getSource()).getLastPathComponent()); // ... node actions go here ... try { // get the Item that belongs to the selected node // ItemUserObject nodeInfo = (ItemUserObject)node.getUserObject(); Item item = nodeInfo.getItem(); // ... actions upon the Item go here ... } catch (ClassCastException ex) { } } });
To listen for a node in the tree being selected by the user, the following code is used:
treeViewPane.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent event) { // get the node of the tree that has been selected // DefaultMutableTreeNode node = (DefaultMutableTreeNode) (event.getPath().getLastPathComponent()); // ... node actions go here ... try { // get the Item that belongs to the selected node // ItemUserObject nodeInfo = (ItemUserObject)node.getUserObject(); Item item = nodeInfo.getItem(); // ... actions upon the Item go here ... } catch (ClassCastException ex) { } } });