Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 57798 - [Viewers] TreeViewer.setSelection fails to select an item if its node has not yet been expanded
Summary: [Viewers] TreeViewer.setSelection fails to select an item if its node has not...
Status: RESOLVED WORKSFORME
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---   Edit
Assignee: Ines Khelifi CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-04-07 15:40 EDT by Kim Horne CLA
Modified: 2004-11-30 09:20 EST (History)
3 users (show)

See Also:


Attachments
View to test setting selection to non visible item (14.73 KB, application/octet-stream)
2004-04-22 22:48 EDT, Ines Khelifi CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kim Horne CLA 2004-04-07 15:40:09 EDT
M8

Create a treeviewer whos content provider will produce some known item at level
2 of the tree (not off of the root)

call treeviewer.setSelection(<selection containing known object>, true)

the treeviewer does not select the item because the leaf nodes have not yet been
created.
Comment 1 Kim Horne CLA 2004-04-07 15:41:24 EDT
Worst. Written.  Bug.  Report.  Ever.  Come down to my office if you want a demo.
Comment 2 Tod Creasey CLA 2004-04-15 08:59:50 EDT
*** Bug 54116 has been marked as a duplicate of this bug. ***
Comment 3 Ines Khelifi CLA 2004-04-22 16:59:44 EDT
The problem is that the setSelection method does not expand the tree if the 
element is not visible. If you call an “expandAll” before setting the 
selection to the 2nd level, it works fine.
I will add code that expands to the item if it's not visible.
Comment 4 Ines Khelifi CLA 2004-04-22 17:08:44 EDT
I looked at the setSelection method more closely and it does call an “internal 
expand”. I will investigate more and submit a patch.
Comment 5 Ines Khelifi CLA 2004-04-22 22:41:59 EDT
I looked at the code again and I believe this is not a bug. In order to
setSelection to an item that has not been visible yet, the getParent() method
needs to be implemented (the setSelection method calls
AbstractTreeViewer#internalExpand(Object element, boolean expandedState), which
requires the setParent method to be implemented correctly).  It makes sense,
since in order to set the selection to the child item, it needs to know about
the parent to expand it. I am attaching the plug in I used for testing, which
now works once I implemented the getParent() method.

Let me know if you run into any other problems.

I am using:
Eclipse Platform
Version: 3.0.0
Build id: 200403261517
Comment 6 Ines Khelifi CLA 2004-04-22 22:48:39 EDT
Created attachment 9886 [details]
View to test setting selection to non visible item

This plug in is a view that will set the selection to the first item's child in
the viewer.
Comment 7 Ines Khelifi CLA 2004-04-23 11:28:39 EDT
Re-open if it sitll does not work for you.
Comment 8 Erol Koç CLA 2004-11-30 09:20:03 EST
Using I200411240800

I had the same problem while working on a TreeViewer similar to the package
explorer.

One observation was that if a new folder in this structure is created and is
meant to be selected, it might be possible that at the time when setSelection is
called, the new folder is not contained in the tree. So at the place where
'internalExpand' was called, the returned widget was null and therefore, no
selection was possible.

To fix this problem, 'refresh()' had to be called on the TreeViewer, forcing to
insert and display the new folder before actually select it.