Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 332736

Summary: [Viewers] TreeViewer.setSelection(ITreeSelection) can select wrong item if multiple leaf nodes hold the same data
Product: [Eclipse Project] Platform Reporter: Adrian Price <adrianp.quatinus>
Component: UIAssignee: Platform UI Triaged <platform-ui-triaged>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: billfenlason, remy.suen, truesoft.romania
Version: 3.4.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard: stalebug
Attachments:
Description Flags
Reproducer
none
Corrected Reproducer none

Description Adrian Price CLA 2010-12-16 08:54:02 EST
Build Identifier:  3.4.2

If you have a tree viewer that contains duplicate elements on different tree paths, passing a ITreeSelection to TreeViewer.setSelection(ISelection) often fails to select the correct items.

This is because TreeViewer.isSameSelection(List, Item) tests only for matching data but fails to take account of fact that 'items' and 'current' refer to *different* tree items! Thus, a previously selected item will remain selected if its data equals that of the item identified by the tree path. If the current selection does not have the same data as the new selection, things work correctly.

Reproducible: Always

Steps to Reproduce:
For example:
A
  B
    X <-- previously selected
  C
    X <-- cannot select this item using its tree path

In other words setSelection(TreeSelection[A,C,X]) will actually leave TreePath[A,B,X] selected, which is not the correct item.
Comment 1 Remy Suen CLA 2010-12-16 08:57:09 EST
Do you have sample code to reproduce this problem? Does the problem occur on Eclipse 3.6.1?
http://download.eclipse.org/eclipse/downloads/drops/R-3.6.1-201009090800/index.php
Comment 2 Adrian Price CLA 2010-12-16 15:59:05 EST
I'll see if I can put a simple reproducer together. It looks like Eclipse-3.6.1 AbstractTreeViewer.isSameSelection(List, Item[]) uses identical logic so I'd reckon that this is still a problem.
Comment 3 Adrian Price CLA 2010-12-16 17:26:47 EST
Created attachment 185382 [details]
Reproducer

Based on JFace Snippet002TreeViewer.
Comment 4 Bill Fenlason CLA 2013-05-20 18:08:29 EDT
Just a question: 

Is it not true that the model has to be hierarchical in nature, and that getParent must return only one node?  

If that is the case, one of the two paths must be invalid, because the parent of X can not be both B and C?

Isn't it true that just because a path can be specified does not mean that it is valid?

I notice that in the attachment  code the node parents are not set.  Don't they have to be?

This is a bit like multiple inheritance, and I didn't think it was allowed.
Comment 5 Adrian Price CLA 2013-05-20 18:41:25 EDT
Created attachment 231226 [details]
Corrected Reproducer
Comment 6 Adrian Price CLA 2013-05-20 18:57:54 EDT
In reply to comment #4:

- Re. the reproducer: good catch - for some reason I had assumed that JFace TreeNodes behaved like EMF, where parent.setChildren(childNodes) implicitly calls child.setParent(parent) on each child and vice-versa; apparently it does not, but the attached corrected reproducer still exhibits the same problem.

- It is *not* true that the underlying model has to be hierarchical in nature, but clearly for any given TreeNode, getParent() can only return one node. That is not the same as saying that two tree nodes may not hold the same data (which they may). Were this not the case, and the underlying model had to be strictly hierarchical (i.e., only containment parent:child references), the API would not need the TreePath class.
Comment 7 Bill Fenlason CLA 2013-05-21 09:20:41 EDT
I was thinking that the widget item data was directly linked to the model element, but now I understand that is not the case.  

While I was checking the code related to a bug I submitted (408380), I seem to remember seeing a comment that the code was dependent on the getParent function of the model (implying hierarchical nature).  I may be mistaken about what I read.

Allowing the same data for different paths implies that the map should (in effect) use the path, rather than the path element, as the key to locate the corresponding widget.  I'll be interested to go back and see how it is implemented and how the map is maintained when model elements are added or deleted, particularly when an element with children is involved.  Since I'm dealing with large trees (depth levels of 50 or more) I'm also concerned about performance.

Perhaps the difficulty in mapping a tree path to the corresponding widget is related to my problem.  It involves using the TreeSelection returned by getSelection in a following setSelection.
Comment 8 Eclipse Genie CLA 2020-03-28 12:41:53 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.