Build new CheckBoxTree based on selected nodes

This is the forum for JIDE Common Layer which is open sourced at https://github.com/jidesoft/jide-oss. Please note, JIDE technical support doesn't monitor this forum as often as other forums. Please consider subscribe for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Moderator: JIDE Support

Forum rules
Community driven forum for open source JIDE Common Layer. JIDE technical support doesn't monitor this forum as often as other forums. If you only use JIDE Common Layer, please consider subscribing for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Build new CheckBoxTree based on selected nodes

Postby Born » Mon Dec 27, 2010 10:05 am

Hi,

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.
Born
 
Posts: 3
Joined: Mon Dec 27, 2010 9:11 am

Re: Build new CheckBoxTree based on selected nodes

Postby JIDE Support » Mon Dec 27, 2010 11:32 am

Please try to invoke CheckBoxTree#setDigIn(false). However, you will not be able to select the parent node to select all its descendant in this mode.

Thanks,
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am

Re: Build new CheckBoxTree based on selected nodes

Postby Born » Mon Dec 27, 2010 1:17 pm

Hi again and thanks for the quick answer.
Yes i saw this method, but as you said I would not be able to select all parent's descendants and this is functionality i want to keep. So i guess i have to write a workaround code and manually check for this.
By the way, is this the desired behavior?

Regards.
Born
 
Posts: 3
Joined: Mon Dec 27, 2010 9:11 am

Re: Build new CheckBoxTree based on selected nodes

Postby JIDE Support » Mon Dec 27, 2010 1:32 pm

Yes, that is exactly what isDigIn() is designed for. Please use CheckBoxTree#getCheckBoxTreeSelectionModel()#isPathSelected() directly to check if the child path is selected. Please make sure the path you passed into that method is exactly the path the CheckBoxTree keeps.

Thanks,
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am

Re: Build new CheckBoxTree based on selected nodes

Postby Born » Mon Dec 27, 2010 4:31 pm

Actually I meant getSelectionRows() and getSelectionPaths() behavior in case all children of given parent are selected.
Born
 
Posts: 3
Joined: Mon Dec 27, 2010 9:11 am

Re: Build new CheckBoxTree based on selected nodes

Postby JIDE Support » Mon Dec 27, 2010 8:34 pm

Yes. If the digIn flag is true, CheckBoxTreeSelectionModel#getSelectionRows()/getSelectionPaths() returns only the parent node although CheckBoxTreeSelectionModel#isPathSelected(theChildNode, true) would be able to return correct value. I'm afraid you would have to keep your work around code there to achieve your goal.

Thanks,
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am


Return to JIDE Common Layer Open Source Project Discussion (Community Driven)

Who is online

Users browsing this forum: No registered users and 14 guests