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

Bug 183043

Summary: [Viewers] TreeViewer.setSelection(selection, true) does not expand elements
Product: [Eclipse Project] Platform Reporter: Nick Edgar <n.a.edgar>
Component: UIAssignee: Platform UI Triaged <platform-ui-triaged>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: bokowski, christoph.linder, misschocoe-itpro, tom.schindl
Version: 3.3Keywords: helpwanted
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard: stalebug

Description Nick Edgar CLA 2007-04-18 15:44:40 EDT
3.3 M6

If a tree viewer's setSelection is called with reveal==true, only the first element in the selection is revealed, expanding elements if necessary.
I would expect it to expand all parent elements to reveal all elements in the selection.

The code in question is in AbstractTreeViewer.setSelectionToWidget, where it has:
	if (reveal && newSelection.size() > 0) {
		showItem((Item) newSelection.get(0));
	}
Comment 1 Nick Edgar CLA 2007-04-18 15:53:52 EDT
A workaround is to do:

        viewer.setSelection(selection, true);
        
        // workaround for eclipse bug 183043
        Tree tree = viewer.getTree();
        TreeItem[] items = tree.getSelection();
        for (int i = 1; i < items.length; ++i) {  // first item is already showing
            TreeItem item = items[i];
            TreeItem parent = item.getParentItem();
            while (parent != null) {
                parent.setExpanded(true);
                parent = parent.getParentItem();
            }
        }

Note that I'm not using Tree.showItem(TreeItem), since that could possibly cause repeated scrolling (its Javadoc says it adjusts both scrolling and expansion).  Scrolling should only be adjusted for the first item in the selection.

Comment 2 Boris Bokowski CLA 2007-04-18 17:00:53 EDT
Until recently, SWT would scroll whenever you called TreeItem.setExpanded() (on Windows).  I believe this is why TreeViewer does not expand parents when setting the selection.

This is a fairly common operation, and it's late in the cycle. Since there is a workaround, I would like to postpone this until after the 3.3 release. Setting target milestone to 3.4.
Comment 3 Boris Bokowski CLA 2008-05-02 14:56:39 EDT
Mass update - removing 3.4 target. This was one of the bugs I marked for investigation (and potential fixing) in 3.4 but I ran out of time. Please ping on the bug if fixing it would be really important for 3.4, and does not require API changes or feature work.
Comment 4 Mélanie Gauthier CLA 2009-02-23 15:21:33 EST
I've ran into that problem too. I've noticed that once user has a branch of the tree, I'm able to programmatically select and reveal any item in that branch. So as a workaround, I do the following if the selection I've sent didn't work:

if (viewer.getTree().getSelectionCount() == 0
                    || (((IStructuredSelection) desiredSelection).size() != viewer.getTree().getSelectionCount())) {               
         TreePath[] treePaths = viewer.getExpandedTreePaths();
         viewer.expandAll();
         viewer.setExpandedTreePaths(treePaths);                
         viewer.setSelection(selection, true);
}

This works fine if I have a 3 levels tree or less, but expandAll doesn't expand further than that.

A
--B
---C
----D

So if I'm trying to select D, only C is selected and it isn't expanded to show D.
Comment 5 Boris Bokowski CLA 2009-11-26 09:55:17 EST
Hitesh is now responsible for watching bugs in the [Viewers] component area.
Comment 6 Christoph Linder CLA 2012-01-04 04:41:21 EST
Bump.

This is also reproducible on Linux (32bit)
Comment 7 Paul Webster CLA 2012-01-04 07:25:35 EST
(In reply to comment #6)
> 
> This is also reproducible on Linux (32bit)

No one is looking at this area at the moment.

http://wiki.eclipse.org/Platform_UI/How_to_Contribute

PW
Comment 8 Lars Vogel CLA 2019-11-14 03:11:45 EST
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.

If you have further information on the current state of the bug, please add it. 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.

If the bug is still relevant, please remove the "stalebug" whiteboard tag.