CheckBoxTree selection event oddness

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.

CheckBoxTree selection event oddness

Postby hemna » Mon Sep 14, 2009 4:54 pm

Hello

I am working on a checkbox tree with 2 levels of the tree and I'm getting 2 valueChanged events being fired for every checkbox I click on.


Here is my checkbox tree.
checkbox_tree1.png
All items checked.
checkbox_tree1.png (3.02 KiB) Viewed 46187 times


If I click on the "Root, test2, what" checkbox, I get an even fired with the following list as the selection list.

Code: Select all
TreePath[] treeSelectionPaths = checkBoxTree.getCheckBoxTreeSelectionModel().getSelectionPaths();

[[Root], [Root, test1], [Root, test3], [Root, test2, who]]

1) What happened to all of the other paths that are selected?
2) Why am I getting a second TreeSelectionEvent being fired with the following list
[[Root, test2, who], [Root, test1], [Root, test3]]

Here is the CheckBoxTree after the 2 TreeSelectionEvents were fired.
checkbox_tree2.png
After the 2 TreeSelectionEvents are done firing..here is my new selections.
checkbox_tree2.png (3.13 KiB) Viewed 46187 times


Both times the valueChanged() call is handled I get incorrect paths from the getSelectionPaths() calls.

I would expect 1 event to be called with the correct list of TreePaths as follows
[[Root, test1, foo], [Root, test1, bar], [Root, test2, who], [Root, test3, is]]
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby hemna » Mon Sep 14, 2009 4:55 pm

Also note that I have DigIn mode set to true. If I turn DigIn mode off, then I seem to get 1 TreeSelectionEvent fired with the correct list of selection paths as I would expect.
I want DigIn mode on though.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby JIDE Support » Mon Sep 14, 2009 6:05 pm

Would you please give JIDE CheckBoxTreeDemo a try? The demo is showing exactly the part which you are working on now. You will be able to see every event we fired and the curretn selected paths in the demo. I tested it and it works fine. It would help if you share with us your test case.

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

Re: CheckBoxTree selection event oddness

Postby hemna » Tue Sep 15, 2009 11:09 am

I tried running the main jide_demo.jnlp from your site. I checked the source for the checkboxtree, but it doesn't set an initial list of selections.

I'm trying to pull out the checkboxtree code from my current ui to reproduce the issues with some code I can put in here. I have a container class that builds and uses the checkboxtree, that also implements the treeselection listener instead of an inline class declaration of the listener like the demo. I tried changing my code to create an inline listener class like the demo, but it didn't change anything. I'm still getting 2 events for every selection and the paths are bogus.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby hemna » Tue Sep 15, 2009 11:18 am

This is how I am using the checkboxtree

This is how I am building the list.

Code: Select all
               
      Map<String, Vector<String>> listItemsMap = getCheckBoxTreeItemsFromModel();
      
      //build the following
      // + Root
      //       + Level 1
      //         +Level 2
      //      + Level1a
      //         + Level 2b
      //         + Level 2c
      DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");

      if (listItemsMap.size() > 0) {
         Iterator<String> i = listItemsMap.keySet().iterator();
         
         while (i.hasNext()) {
            String key = i.next();
            
            DefaultMutableTreeNode level1Node = new DefaultMutableTreeNode(key);
            //now do the 2nd level
            Vector<String> level2List = listItemsMap.get(key);
            
            for (Enumeration<String> e = level2List.elements(); e.hasMoreElements();) {
               String level2String = (String)e.nextElement();
               level1Node.add(new DefaultMutableTreeNode(level2String));
            }
            
            rootNode.add(level1Node);
         }         
      }
            
      treeModel = new DefaultTreeModel(rootNode);
      checkBoxTree = new TvbCheckBoxTree(treeModel);


This is my trimmed down listener....
Code: Select all
         checkBoxTree.getCheckBoxTreeSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
               TreePath[] paths = e.getPaths();
               int psize = paths.length;
               
               TreePath[] treePaths = checkBoxTree.getCheckBoxTreeSelectionModel().getSelectionPaths();
               int size = treePaths.length;
            }            
         });



This is how I am setting the list of initial selections just after the checkboxtree is built.
Code: Select all
   protected void updateCheckBoxListSelectedItems(Map<String,Vector<String>> selectedItems) {
      if (selectedItems != null && selectedItems.size() > 0) {
         TreePath[] checkedCategoryPaths = new TreePath[getMapSizeCount(selectedItems)];
         
         Iterator<String> i = selectedItems.keySet().iterator();
         int x = 0;
         while(i.hasNext()) {
            String selectedKey = i.next();
            Vector<String> selectedVector = selectedItems.get(selectedKey);
            
            //find the level1Node in the tree.
            DefaultMutableTreeNode level1Node = checkBoxTree.findChildNode((DefaultMutableTreeNode)checkBoxTree.getModel().getRoot(),
                  selectedKey);
            
            for (Enumeration<String> e = selectedVector.elements(); e.hasMoreElements();) {
               String level2String = (String)e.nextElement();
               
               DefaultMutableTreeNode childNode = checkBoxTree.findChildNode(level1Node, level2String);
               if (childNode != null) {
                  checkedCategoryPaths[x] = new TreePath(((DefaultMutableTreeNode) childNode).getPath());
                  x++;
               }
            }
            
            checkBoxTree.collapsePath( new TreePath( ((DefaultMutableTreeNode)level1Node).getPath() ));            
         }
         
         CheckBoxTreeSelectionModel selectionModel = checkBoxTree.getCheckBoxTreeSelectionModel();
         selectionModel.setSelectionPaths(checkedCategoryPaths);
      }      
   }



I have created a subclass for the checkboxtree to contain
some methods that I use to find nodes in the model.
Code: Select all
...
   /**
    * This method finds a child node from a node.  It only
    * searches 1 level deep.
    *
    * @param Node
    * @param String
    * @return Node
    */
   public DefaultMutableTreeNode findChildNode(DefaultMutableTreeNode parentNode, String childNameStr) {
      DefaultMutableTreeNode childNode = null;
      
      TreeModel myTreeModel = this.getModel();
      
      int rootChildCount = myTreeModel.getChildCount(parentNode);
      for (int i = 0; i < rootChildCount; i++) {
         DefaultMutableTreeNode child = (DefaultMutableTreeNode)myTreeModel.getChild(parentNode, i);
         
         if (child.getUserObject().toString().compareToIgnoreCase(childNameStr) == 0) {
            childNode = child;
            break;
         }                  
      }
      
      return childNode;
   }
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby JIDE Support » Tue Sep 15, 2009 3:03 pm

I can't see any issue on your posted codes. I tried setSelectedPaths as well in JIDE CheckBoxTreeDemo and it works fine. A test case would help. Or please dump the stack for the two events with our debug jar so we can see how it get invoked.

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

Re: CheckBoxTree selection event oddness

Postby hemna » Thu Sep 17, 2009 12:52 pm

Do you have the source and ant build.xml for your demo/samples. I'd like to try and modify your checkboxtree example and see if I can reproduce what I'm seeing.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby JIDE Support » Thu Sep 17, 2009 1:00 pm

It should be all there under examples folder when you download the evaluation package or the formal release package.

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

Re: CheckBoxTree selection event oddness

Postby hemna » Thu Sep 17, 2009 3:49 pm

I have been able to reproduce the multiple events issue. It seems to be related to the method that updates the model with the list of selections.
When you comment out the call to update the model
on line 200, you don't see multiple events happening for 1 checkbox click.


Code: Select all
updateCheckBoxListSelectedItems(getCheckBoxTreeItemsFromModel());




CheckBoxTreeDemo.java
My version of the demo that reproduces the multiple events.
(19.55 KiB) Downloaded 1278 times
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby JIDE Support » Thu Sep 17, 2009 4:11 pm

Thanks for your test case. I run it from my side and see different behavior. Looks to me, the following statement was invoked twice(line 202 and 245) which causes "multiple event". It would be only one event if I comment one of them. Please check if this is the case for you.
Code: Select all
_tree.getCheckBoxTreeSelectionModel().addTreeSelectionListener(this);

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

Re: CheckBoxTree selection event oddness

Postby hemna » Thu Sep 17, 2009 4:14 pm

oops, yah my mistake. I pulled the 2nd call on 245, and I still get mulitple events.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby hemna » Thu Sep 17, 2009 4:18 pm

just noticed as well that if I comment out the

//updateCheckBoxListSelectedItems(getCheckBoxTreeItemsFromModel());

I can still get multiple events by doing the following.

Step 1) Click on a level 1 checkbox that has 2 child checkboxes.
Step 2) click on a level 2 checkbox that is already enabled (from step 1)
result....2 events.

If I click on level2 checkbox that has a level1 checkbox parent that is NOT checked,
then I don't get multiple events.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby JIDE Support » Thu Sep 17, 2009 4:47 pm

That is as designed in isDigIn() scenario. When all nodes are selected, the selected paths are root only instead of every nodes. So in this case, if you deselect any node, you will get two events, one is to add new paths and another is to remove the existing path (root). So please try use the following code in your listener like the following to screen the remove path event.
Code: Select all
    public void valueChanged(TreeSelectionEvent e) {
        if (e.isAddedPath()) {
            boolean dirty = false;
            dirty = isCheckBoxListSelectionChanged(e);
        }
    }

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

Re: CheckBoxTree selection event oddness

Postby hemna » Thu Sep 17, 2009 5:40 pm

will this work as well, when the user unchecks a checkbox?

I basically need a way to determine if the user has changed their selections from when I originally populated the checkboxtree and set the initial selections.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby JIDE Support » Thu Sep 17, 2009 5:47 pm

Yes. It works as well at deselection scenario.

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

Re: CheckBoxTree selection event oddness

Postby hemna » Fri Sep 18, 2009 1:26 pm

There still seems to be inconsistent results coming back from the calls to get the selection paths.


Here is the original list after starting the sample app I had modified and provided in a previous post.

I modified the valueChanged() method to this
Code: Select all
   public void valueChanged(TreeSelectionEvent e) {
      boolean dirty = false;
      TreePath[] paths = e.getPaths();
      TreePath[] treeSelectionPaths = _tree.getCheckBoxTreeSelectionModel().getSelectionPaths();
      
      if (e.isAddedPath()) {
            dirty = isCheckBoxListSelectionChanged(e);
        }
      
   }


Demo1.png
The CheckboxList after starting the sample app.
Demo1.png (51.96 KiB) Viewed 46172 times


When I click on the checkbox for Level1c - Blah3 I get the following
Code: Select all
paths = [[Root, Level1a], [Root, Level1b], [Root, Level1c, Bar3]]
treeSelectionPaths = [[Root], [Root, Level1a], [Root, Level1b], [Root, Level1c, Bar3]]

This makes sense as blah3 has been unchecked. the treeSelectionPaths says that all nodes under level1a are checked as are Level1b, but only Bar3 is checked under Level1c. completely correct.


Now, when I click Level1c - Blah3 again, to re-enable it...
Code: Select all
paths = [[Root]]
treeSelectionPaths = [[Root, Level1a], [Root, Level1b], [Root, Level1c, Bar3], [Root]]

Neither of those 2 values seem correct. treeSelectionPaths says that all in leve1a is checked (correct), all nodes in leve1b are checked (correct), but that only Bar3 is checked under Level1c (incorrect as I just clicked Blah3).

Looking at the same values during the 2nd call to valueChanged() for the same click on Blah3. This is where I ignore this call by the "if (e.isAddedPath())" check
Code: Select all
paths = [[Root, Level1c, Bar3], [Root, Level1b], [Root, Level1a]]
treeSelectionPaths = [[Root]]

This is still incorrect. The paths that are returned should be [[Root, Level1a], [Root, Level1b], [Root, Level1c]] to show that all nodes are enabled.
At no time do I get a correct listing of paths that show all nodes are enabled during valueChanged() time after I re-enable a previously unchecked level2 node.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby JIDE Support » Fri Sep 18, 2009 1:38 pm

It is kind of confusing however it is a correct behavior. First of all, please look at the value in the event, which is printed by your code with the prefix "paths = ". The values with prefix "treeSelectionPaths = " is not a final state at that event.

When you unselect Blah3, the value is like the following, which means, Level1a, Level1b and Level1c/Bar3 are selected. Correct.
Code: Select all
paths = [[Root, Level1a], [Root, Level1b], [Root, Level1c, Bar3]]

When you select Blah3 again, the value is like the following, which means, root (including all nodes) are selected. Correct.
Code: Select all
paths = [[Root]]

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

Re: CheckBoxTree selection event oddness

Postby hemna » Fri Sep 18, 2009 1:56 pm

ok I guess the problem is that treeSelectionPaths can't be trusted as valid then, because in the case of rechecking an unchecked checkbox...treeSelectionPaths is not correct at the time valueChanged is called.
that's not what the documentation says.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby hemna » Fri Sep 18, 2009 2:04 pm

Well, I stand corrected. the paths value still isn't always correct either.

For example,
I have the same tree...and all nodes are UNselected to start.
Demo2.png
unchecked intially
Demo2.png (50.62 KiB) Viewed 46171 times


I then click level1c - blah3
Code: Select all
paths = [[Root, Level1c, Blah3]]
treeSelectionPaths = [[Root, Level1c, Blah3]]

That is correct.

Now click level1a - Bar
Code: Select all
paths = [[Root, Level1a, Bar]]
treeSelectionPaths = [[Root, Level1c, Blah3], [Root, Level1a, Bar]]


As you can see paths is not accurate for the entire selection list, but treeSelectionPaths is.
How can I programmatically know which call I need to make to get the correct entire selection list?

In your previous post, you mentioned I need to use paths....that's not accurate in this test case.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby JIDE Support » Fri Sep 18, 2009 2:14 pm

You got me. :) We will fix the behavior so that you have a reliable field to use when you get the event.

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

Re: CheckBoxTree selection event oddness

Postby hemna » Fri Sep 18, 2009 2:34 pm

Thanks a lot for your help. If you have any information when the fix might get in and what version, I would greatly appreciate it.

Thanks again,
Walt
walter.boring@hp.com
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckBoxTree selection event oddness

Postby JIDE Support » Tue Sep 22, 2009 11:58 am

When I tried to "fix" this issue, I eventually figured out that we already had this setting. Please try invoke the following code and respect the treeSelectionPaths you get from the current selection model.
Code: Select all
        _tree.getCheckBoxTreeSelectionModel().setSingleEventMode(true);

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

Re: CheckBoxTree selection event oddness

Postby hemna » Mon Sep 28, 2009 4:12 pm

Thank you very much, that seemed to have made the tree act sanely :)
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am


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

Who is online

Users browsing this forum: No registered users and 90 guests