Bug 216339 - [Viewers] Removing non-selected element from Tree resets focus
Summary: [Viewers] Removing non-selected element from Tree resets focus
Status: RESOLVED WORKSFORME
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Boris Bokowski CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 191365
  Show dependency tree
 
Reported: 2008-01-23 15:46 EST by Brian Bauman CLA
Modified: 2008-01-30 17:24 EST (History)
2 users (show)

See Also:


Attachments
sample project to illustrate problem (9.61 KB, application/zip)
2008-01-23 15:52 EST, Brian Bauman CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Brian Bauman CLA 2008-01-23 15:46:14 EST
I am utilizing org.eclipse.ui.progress.DeferredTreeContentManager to populate a TreeViewer.  The code in the DTCM returns a "place holder" element to be included in the tree while a back ground job actually populates the Tree's contents.  After the tree's contents are finished, the place holder object is removed from the Tree to leave only the remaining valid elements.

My problem is that I have a long list of items and when the place holder element is removed, the focus/scroll bars are set to the bottom of the list.  This is an obvious change in behavior from the old/traditional way of populating the Tree as when the initialization was done the focus was set on the top of the Tree.

Since it would be a huge help to get this fixed, I will attach an extremely simple sample project to illustrate the problem.
Comment 1 Brian Bauman CLA 2008-01-23 15:52:19 EST
Created attachment 87694 [details]
sample project to illustrate problem

This is a simple project with a view.  Inside the view there is a boolean value called DEFERRED_MODE.  When this value is set to false, the traditional way of populating the Tree is used.  When this value is set to true, the DeferredTreeContentManager is used to populate the Tree.  

You will see when the value is false, when the tree is initialized, the focus is at the top of the Tree.  When the value is true, the removal of the "place holder" object in DeferredTreeContentManager sets the focus of the Tree to the bottom.  

This problem will not only be relevant to PDE, but anyone who tries to use DeferredTreeContentManager with a list of elements long enough to need scroll bars.
Comment 2 Steve Northover CLA 2008-01-30 14:27:05 EST
Boris, we can look at this together or I can look into it with an SWT only example.  It sounds like an SWT bug.
Comment 3 Boris Bokowski CLA 2008-01-30 17:24:08 EST
I was able to track this down. SWT has nothing to do with it, it is the sorting. While objects are being added to the viewer, the "Pending" item ends up at the bottom of the table ("Pending" coming after "Object 0"..."Object 99"). The "Pending" item however has the focus, it received it when you created the tree, being the first and only item. So when that item is finally removed after all real items have been added, SWT moves the focus to the item beside it, which happens to be the last item "Object 99".

One possible fix is to use a sorter that keeps the pending node at the beginning, for example like this:

viewer.setSorter(new ViewerSorter(){
	public int compare(Viewer viewer, Object e1, Object e2) {
		if (e1 instanceof TreeObject && e2 instanceof TreeObject) {
			return super.compare(viewer, e1, e2);
		} else if (e1 instanceof TreeObject) {
			return 1;
		} else {
			return -1;
		}
	}
});

Another solution would be to not set a sorter, if the natural order of the elements as returned by the content provider is the order you want.