Community
Participate
Working Groups
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.
Worst. Written. Bug. Report. Ever. Come down to my office if you want a demo.
*** Bug 54116 has been marked as a duplicate of this bug. ***
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.
I looked at the setSelection method more closely and it does call an “internal expand”. I will investigate more and submit a patch.
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
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.
Re-open if it sitll does not work for you.
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.