Community
Participate
Working Groups
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.
Created attachment 217304 [details] A patch showing the changes made.
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 ?
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.
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.