Bug 382527 - CheckboxTableViewer setCheckedElements fails to check items
Summary: CheckboxTableViewer setCheckedElements fails to check items
Status: CLOSED WONTFIX
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.2   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: Platform-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords: needinfo
Depends on:
Blocks:
 
Reported: 2012-06-13 13:36 EDT by Jermaine Rattray CLA
Modified: 2020-02-21 13:13 EST (History)
1 user (show)

See Also:


Attachments
A patch showing the changes made. (4.83 KB, patch)
2012-06-13 13:50 EDT, Jermaine Rattray CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jermaine Rattray CLA 2012-06-13 13:36:15 EDT
Build Identifier: 20120216-1857

After instantiating and populating a CheckboxTableViewer, attempting to set the items to be checked by calling setCheckedElements does not have the expected result.
After the viewer has been populated, a subset of the list used previously as input is passed to the setCheckedElements method. There are rare cases where a single item will be checked, otherwise no change is shown.

After looking into the code it was found that the within CheckboxTableViewer.setCheckedElements a CustomHashtable, 'set', is used for storing the elements passed for selection. It was at this point that 'check', the variable which holds the target state of the viewer, is not being assigned correctly.
There were instances where the value returned by set.containsKey would return false while the element is a key/value pair within the Map.

A solution which was found is to have 'set' be a List, easily generated using Arrays.asList(elements), and 'check' assigned by calling set.contains(element);

The result is CheckboxTableViewer.setCheckedElements goes from

    public void setCheckedElements(Object[] elements) {
        assertElementsNotNull(elements);
        CustomHashtable set = newHashtable(elements.length * 2 + 1);
        for (int i = 0; i < elements.length; ++i) {
            set.put(elements[i], elements[i]);
        }
        TableItem[] items = getTable().getItems();
        for (int i = 0; i < items.length; ++i) {
            TableItem item = items[i];
            Object element = item.getData();
            if (element != null) {
                boolean check = set.containsKey(element);
                // only set if different, to avoid flicker
                if (item.getChecked() != check) {
                    item.setChecked(check);
                }
            }
        }
    }

to 

    public void setCheckedElements(Object[] elements) {
        assertElementsNotNull(elements);
         List set = Arrays.asList(elements);
        TableItem[] items = getTable().getItems();
        for (int i = 0; i < items.length; ++i) {
            TableItem item = items[i];
            Object element = item.getData();
            if (element != null) {
                boolean check = set.contains(element);
                // only set if different, to avoid flicker
                if (item.getChecked() != check) {
                    item.setChecked(check);
                }
            }
        }
    }


Reproducible: Always

Steps to Reproduce:
1.Instantiate a CheckboxTableViewer.
2.Setup databindings using a fixed list of elements.
3. Create an array containing the elements which should be checked, based on the input list used previously.
4. Make a call to setCheckedElements on the viewer, giving the array as the selection.

It will be noticed that the selection is not.
Comment 1 Jermaine Rattray CLA 2012-06-13 13:50:50 EDT
Created attachment 217304 [details]
A patch showing the changes made.
Comment 2 Eric Moffatt CLA 2012-11-06 14:48:58 EST
Could you possibly attach a test project that we can use both to show the issue and verify that the proposed patch indeed fixes it ?
Comment 3 Curtis Windatt CLA 2012-11-06 15:12:14 EST
Can you provide a snippet to reproduce?  I just tried to follow your steps and everything works as expected.  Do elements in the array passed to setCheckedElements have the same hashcode, but are not equal?

If this problem is confirmed, CheckboxTreeViewer should be fixed as well.
Comment 4 Eclipse Genie CLA 2020-02-21 13:13:37 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. As such, we're closing this bug.

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

--
The automated Eclipse Genie.