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

Bug 30745

Summary: [Viewers] TableViewer selecting items when it shouldn't
Product: [Eclipse Project] Platform Reporter: Jared Burns <jared_burns>
Component: UIAssignee: Platform-UI-Inbox <Platform-UI-Inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: enhancement    
Priority: P4 CC: heath.borders, Tod_Creasey
Version: 2.1   
Target Milestone: ---   
Hardware: Other   
OS: All   
Whiteboard:

Description Jared Burns CLA 2003-01-31 15:55:21 EST
Build 20030129

If you have a TableViewer with the same object appearing multiple times, the
selection will be lost during refresh if you select any item except the first
instance of the item.

For example, if you have a table with the following:
A
B
A <-- This item selected

A call to TableViewer.refresh() will result in:
A <-- This item selected
B
A

This has introduced bugs in the Ant UI's tables that allow the user to add
the same Ant target to a viewer.
Comment 1 Jared Burns CLA 2003-01-31 15:59:04 EST
The workaround is to forcibly clear the selection every time the view is
refreshed and reset the selection to the correct item.
Comment 2 Nick Edgar CLA 2003-02-01 23:25:07 EST
The viewers generally don't handle multiple equal items well.

In this case, the viewer tries to preserve selection across the refresh by 
remembering the selected model elements and re-selecting them after.
The refresh may completely scramble the SWT TableItems (e.g. changing sort 
order), so it has to remember the selection in terms of model elements, not SWT 
items.  But this means that it loses the knowledge that it's actually the 
second A that's selected, not the first (or more accurately, it never had this 
knowledge in the first place).

Fixing this properly is a major change to the viewers, which will not happen 
for M5 (see below for details).

Is it possible for you to call add() instead of refresh() in this case?  That 
would avoid the problem entirely.

You could also try subclassing the viewer and overriding preservingSelection() 
to do nothing other than run the runnable.
But this will cause the selection to be incorrect if the order of elements 
changes.

Alternatively, you could have it check the selection for any change (using 
getSelection) before calling setSelectionToWidget.  This will work for the case 
where the elements and their order hasn't changed (refresh reuses TableItems 
rather than recreating them, so, as far as SWT is concerned, the selection 
hasn't changed).
But this won't solve the problem in general (e.g. if C is inserted before 
either A), and it's not something I want to add at this point to 
StructuredViewer, since getting the selection can be quite costly (O(N) for 
trees in Windows).

To handle multiple equal elements properly in the viewers would require two 
major design changes:
1. Support a mapping from one element to multiple SWT items in findItem.  This 
includes being able to map from an element to multiple items in the elementMap.
2. Avoid reusing SWT items.  This has downsides in terms of performance and UI 
flashiness.

Comment 3 Jared Burns CLA 2003-02-02 10:09:25 EST
In my case, I actually know that the order of the items has changed - it's in
our implementation of the "move up"/"move down" actions. The "add" workaround
won't work for us, because we're not adding elements. Our code changes the
order of the elements in our content provider, then calls refresh() so the
viewer picks up the ordering change, and then manually sets the selection
to what it should be with the new ordering.

I could have sworn that this bug didn't exist when I first wrote this code to
handle the duplicate item case (a month ago?). Is this code new?

For M5, I think we'll just go with the ugly workaround (clear the selection
after the refresh and then set it ourselves). For 2.1, we'll probably go with
the viewer subclassing.
Comment 4 Nick Edgar CLA 2003-02-03 07:48:52 EST
There was a change recently in how viewers compare elements.  You can now set 
an element comparer to change how equals and hashCode are done.  See 
IElementComparer.  However, this should not affect how viewers refresh (unless 
you set one).

Comment 5 Jared Burns CLA 2003-02-03 10:21:56 EST
As a sanity check, I went back and tested this behavior on the 20021218 build.
Turns out I'm not sane as the problem exists in that build too. So the code
that's affecting us isn't the new code.
Comment 6 Nick Edgar CLA 2003-02-13 10:53:26 EST
There are no plans for the UI team to work on this defect until higher priority 
items are addressed. If you would like to work on this defect, please let us 
know on the platform-ui-dev mailing list.
Comment 7 Tod Creasey CLA 2006-06-22 08:35:31 EDT
There are currently no plans to work on this feature