odd valueChanged event in CheckBoxTree after model changes

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.

odd valueChanged event in CheckBoxTree after model changes

Postby pfriend » Tue Jan 10, 2012 2:25 pm

I am using version 3.3.0 of the CheckBoxTree and am having a problem with an odd firing of a valueChanged event. In the application the tree model may be reloaded a few different times based on user input, and the problem occurs after the second model reset. I do the following:

1. Remove the TreeSelectionListeners from the existing checkboxtree selection model.
2. Set the new tree model.
3. Add the new selection listener to the checkboxtree selection model.
4. Programatically select the root node (digin is enabled).

What happens then is, the moment I refresh a node in the tree to update the color or some other attribute, a valueChanged event is getting fired for the root of the tree, and it says that everything is unchecked, even though they show as being checked. This only happens once, further updates to the tree don't trigger this problem.

Currently I am able to work around the problem with this block of code:
Code: Select all
      final DefaultTreeModel tmodel = (DefaultTreeModel) tree.getModel();
      tmodel.nodeChanged(rootnode);
      final CheckBoxTreeSelectionModel smodel = tree.getCheckBoxTreeSelectionModel();
      smodel.addTreeSelectionListener(selection_listener);


Basically, I call nodeChanged on the root DefaultMutableTreeNode even though nothing has changed. After that I add the selection listener.

Thanks in advance for any pointers.
pfriend
 
Posts: 18
Joined: Sun Dec 04, 2011 3:47 pm

Re: odd valueChanged event in CheckBoxTree after model chang

Postby JIDE Support » Tue Jan 10, 2012 5:33 pm

Can you please send us the stack information when you receive the odd valueChanged event?

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

Re: odd valueChanged event in CheckBoxTree after model chang

Postby pfriend » Thu Jan 12, 2012 1:26 pm

Turns out my workaround doesn't work 100% of the time, so I've had to rework things a bit so I recalculate every item in the tree each time valueChanged gets called. Below is the stack trace upon enter to valueChanged. Again, this is after the model has been reloaded a few times, and it appears that calling nodeChanged on the tree model triggers this. I got two of these traces in a row before they stopped, even though other nodes in the tree were also getting updated. Single event mode and digin are set to true.

Code: Select all
java.lang.Exception: valueChanged trace
   at com.example.MainUIPanel$11.valueChanged(MainUIPanel.java:533)
   at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
   at com.jidesoft.swing.CheckBoxTreeSelectionModel.notifyPathChange(Unknown Source)
   at com.jidesoft.swing.CheckBoxTreeSelectionModel.removeSelectionPaths(Unknown Source)
   at com.jidesoft.swing.CheckBoxTreeSelectionModel.removeSelectionPaths(Unknown Source)
   at javax.swing.tree.DefaultTreeSelectionModel.removeSelectionPath(DefaultTreeSelectionModel.java:427)
   at com.jidesoft.swing.CheckBoxTreeSelectionModel.revalidateSelectedTreePaths(Unknown Source)
   at com.jidesoft.swing.CheckBoxTreeSelectionModel.treeNodesChanged(Unknown Source)
   at javax.swing.tree.DefaultTreeModel.fireTreeNodesChanged(DefaultTreeModel.java:468)
   at javax.swing.tree.DefaultTreeModel.nodesChanged(DefaultTreeModel.java:330)
   at javax.swing.tree.DefaultTreeModel.nodeChanged(DefaultTreeModel.java:261)
   at com.example.MainUIPanel$16.run(MainUIPanel.java:895)
   at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
   at java.awt.EventQueue.access$000(EventQueue.java:84)
   at java.awt.EventQueue$1.run(EventQueue.java:602)
   at java.awt.EventQueue$1.run(EventQueue.java:600)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


Thanks again for looking at this.

Peter
pfriend
 
Posts: 18
Joined: Sun Dec 04, 2011 3:47 pm

Re: odd valueChanged event in CheckBoxTree after model chang

Postby JIDE Support » Thu Jan 12, 2012 3:39 pm

Looks like those selected tree path was invalid after the model is reloaded. That's why the CheckBoxTreeSelectionModel tries to remove the selection and fires the selection changed event. Please verify it by checking if the path is valid at the listener.

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

Re: odd valueChanged event in CheckBoxTree after model chang

Postby pfriend » Thu Jan 12, 2012 3:59 pm

The model gets reloaded, then the selection listener is set. Then I click and expand everything:

Code: Select all
final CheckBoxTreeSelectionModel model = tree.getCheckBoxTreeSelectionModel();
model.addSelectionPath(new TreePath(rootnode));
for (int i = 0; i < tree.getRowCount(); i++) {
     tree.expandRow(i);


Then I start running things based on what is selected (which is everything for this particular test). There is no other code that checks/unchecks nodes and none are altered via the UI. The node rendering is altered as things progress and the foreground color is updated or some state in the node user object is updated. Given this, what kind of validation should I be doing?

Thanks,

Peter
pfriend
 
Posts: 18
Joined: Sun Dec 04, 2011 3:47 pm

Re: odd valueChanged event in CheckBoxTree after model chang

Postby JIDE Support » Thu Jan 12, 2012 4:09 pm

This line is suspicious since this newly created TreePath is not the one exists in the JTree.
Code: Select all
model.addSelectionPath(new TreePath(rootnode));

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

Re: odd valueChanged event in CheckBoxTree after model chang

Postby pfriend » Thu Jan 12, 2012 5:04 pm

I think I see what you are getting at, but not sure how else a TreePath can be created except from an existing node object, which is indeed in the tree since several lines prior I have:

Code: Select all
tree.setModel(new DefaultTreeModel(rootnode));


I tried altering the addSelectionPath() to:

Code: Select all
model.addSelectionPath(new TreePath(rootnode.getPath()));


but that was much worse since it ends up generating one event with the root node listed twice, which throws everything off. I can't really use setSelectionRow() since that is a method of the tree itself, not check check box selection model.

I dug through the docs a bit and noticed that addSelectionPath() is inherited from DefaultTreeSelectionModel, then I noticed this method:

addSelectionPaths(javax.swing.tree.TreePath[] paths)
Overrides the method in DefaultTreeSelectionModel to consider digIn mode.

That sounded more like what I needed since I am using digin, but using that resulted in the root node showing up twice in valueChanged once I added the root node to the selection.

I removed the code which selects the root node entirely, so in the UI once the model reloads nothing is checked. Checking the root node also ends up listing the root node twice in the valueChanged event, so it seems unrelated to the method of node selection but is an issue with loading a new model.

At the time that I am reloading the model in the tree, is there a good way to wipe out and reinitialize the checkboxtree model as well?

Thanks,

Peter
pfriend
 
Posts: 18
Joined: Sun Dec 04, 2011 3:47 pm

Re: odd valueChanged event in CheckBoxTree after model chang

Postby pfriend » Thu Jan 12, 2012 6:21 pm

So I discovered something interesting. Recall that after the model reload I was getting the root node listed twice in valueChanged(). I notice that if I continue to do model reloads, the number of root node duplications increases. So with 5 reloads I see 5 instances of the root node showing up via valueChanged().

The first thing I do before loading the new model is remove the selection listeners so that repopulating the tree doesn't trigger the listener code. Currently I am using this:

Code: Select all
      CheckBoxTreeSelectionModel model = tree.getCheckBoxTreeSelectionModel();
      TreeSelectionListener[] listeners = model.getTreeSelectionListeners();
      if (listeners != null) {
         for (TreeSelectionListener t : listeners) {
            model.removeTreeSelectionListener(t);
         }
      }


If something about that is incorrect it could be the cause. I add the listener back in after the model is reset.

I did try one other thing, and it seems to be working. Thinking that possibly resetting the tree model wasn't enough to clear state in the checkbox selection model, I added this code to the tree reset code I run before resetting the model:

Code: Select all
      CheckBoxTreeSelectionModel model = tree.getCheckBoxTreeSelectionModel();
      TreeSelectionListener[] listeners = model.getTreeSelectionListeners();
      if (listeners != null) {
         for (TreeSelectionListener t : listeners) {
            model.removeTreeSelectionListener(t);
         }
      }
      TreePath[] paths = {new TreePath(tree.getModel().getRoot())};
      model.removeSelectionPaths(paths);


So far that is working no matter how many model reloads I do, and it also seems to have removed the spurious valueChanged event generated when I call nodeChanged().

Thanks,

Peter
pfriend
 
Posts: 18
Joined: Sun Dec 04, 2011 3:47 pm

Re: odd valueChanged event in CheckBoxTree after model chang

Postby JIDE Support » Thu Jan 12, 2012 6:49 pm

Can you please share with us the entire tree model reset code that works for you now?

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

Re: odd valueChanged event in CheckBoxTree after model chang

Postby pfriend » Fri Jan 13, 2012 2:28 pm

The bulk of it has been posted. Basically...

Code: Select all
/* clear out existing tree */
CheckBoxTreeSelectionModel model = tree.getCheckBoxTreeSelectionModel();
TreeSelectionListener[] listeners = model.getTreeSelectionListeners();
if (listeners != null) {
     for (TreeSelectionListener t : listeners) {
          model.removeTreeSelectionListener(t);
     }
}
TreePath[] paths = {new TreePath(tree.getModel().getRoot())};
model.removeSelectionPaths(paths);
tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("") {{}}));
/* Time passes, user clicks on something that triggers population of tree. */
DefaultMutableTreeNode rootnode = new DefaultMutableTreeNode(new SomeUserObject());
/* build rest of tree ... */
tree.setModel(new DefaultTreeModel(rootnode));
/* add listener, then select root node to select whole tree */
final CheckBoxTreeSelectionModel model = tree.getCheckBoxTreeSelectionModel();
TreePath[] paths = {new TreePath(rootnode)};
model.addTreeSelectionListener(selection_listener);
model.addSelectionPaths(paths);


Is that enough detail? The line which removes any selections from the prior model seems to be what fixes it all.

Thanks,

Peter
pfriend
 
Posts: 18
Joined: Sun Dec 04, 2011 3:47 pm

Re: odd valueChanged event in CheckBoxTree after model chang

Postby JIDE Support » Fri Jan 13, 2012 2:51 pm

Thanks for bug report. Will clear the selection paths on model changes in CheckBoxTreeSelectionModel in next regular release.

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

Re: odd valueChanged event in CheckBoxTree after model chang

Postby JIDE Support » Fri Jan 13, 2012 2:55 pm

Please check if the following code could work around well in your application.
Code: Select all
        _tree = new CheckBoxTree(treeModel) {
            @Override
            public void setModel(TreeModel newModel) {
                if (getModel() != newModel) {
                    clearSelection();
                }
                super.setModel(newModel);
            }
        };

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

Re: odd valueChanged event in CheckBoxTree after model chang

Postby pfriend » Fri Jan 13, 2012 3:05 pm

Perhaps I am confused, but I thought that the JTree selection and CheckBox selection were distinct, and it looks like the code above would only clear the selection on the JTree model. Or do those selection changes get propagated to the check box model?

Thanks,

Peter
pfriend
 
Posts: 18
Joined: Sun Dec 04, 2011 3:47 pm

Re: odd valueChanged event in CheckBoxTree after model chang

Postby JIDE Support » Fri Jan 13, 2012 4:04 pm

My bad that wrong code is pasted. The correct code should be.
Code: Select all
        _tree = new CheckBoxTree(treeModel) {
            @Override
            protected CheckBoxTreeSelectionModel createCheckBoxTreeSelectionModel(TreeModel model) {
                return new CheckBoxTreeSelectionModel(model) {
                    @Override
                    public void setModel(TreeModel model) {
                        if (getModel() != model) {
                            clearSelection();
                        }
                        super.setModel(model);
                    }
                };
            }
        };

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

Re: odd valueChanged event in CheckBoxTree after model chang

Postby pfriend » Fri Jan 13, 2012 5:30 pm

I tried that out and so far no problems. I'll let you know if any problems show up during the rest of my testing. Thanks for the fix. :-)

Cheers,

Peter
pfriend
 
Posts: 18
Joined: Sun Dec 04, 2011 3:47 pm

Re: odd valueChanged event in CheckBoxTree after model chang

Postby JIDE Support » Wed Jan 25, 2012 5:35 pm

Just so you know, this is fixed in 3.3.4 just released.

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: Google [Bot] and 8 guests