I'm trying to create a Dual List component but instead of lists I want to use CheckBoxTrees. So i want to build the right hand tree from the selected nodes of the left hand tree. The problem I have is: when all children of given parent node are selected getSelectionRows() and getSelectionPaths() return only the index or the path of the parent node and I have to do a lot of additional checks for each parent node and its selected children. Is there any elegant way, I can build my new tree based on the selected nodes of its ancestor and how can i get all children paths if they are all selected for current parent. Here is my code for the moment:
- Code: Select all
public void buildSelectedAccountsTree() {
int[] treeRows = availableAccountsTree.getCheckBoxTreeSelectionModel().getSelectionRows();
Arrays.sort(treeRows);
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(availableAccountsTree.getModel().getRoot());
HashMap<DefaultMutableTreeNode, DefaultMutableTreeNode> nodeMap = new HashMap<DefaultMutableTreeNode, DefaultMutableTreeNode>();
for(int i = 0; i < treeRows.length; i++) {
DefaultMutableTreeNode lastNode = rootNode;
TreePath treePath = availableAccountsTree.getPathForRow(treeRows[i]);
Object[] nodes = treePath.getPath();
for (int j = 1; j <= nodes.length-1; j++) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)nodes[j]; //first node is always the root node, miss it
if(nodeMap.get(node) == null) { //no such node, must make one
Account nodeAccount = (Account)node.getUserObject();
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(nodeAccount);
nodeMap.put(node, newNode);
lastNode.add(newNode);
lastNode = newNode;
}
else { //already such node, just put it to be lastNode
lastNode = nodeMap.get(node);
}
}
}
................
Thanks in advance.