Search code examples
javacheckboxswtrcp

Set the checkbox of a CheckboxTreeViewer when the row is clicked


I'm looking for a generic approach to check the checkbox of CheckBoxTreeViewer, when the row / the item is selected. I've found a similar question regarding the CheckBoxTableViewer, which answer helped me, but it doesn't apply to the CheckBoxTreeViewer. I assume, I have to use the ISelectionChangedListener.


Solution

  • I solved the problem with the grayed state by calling the CheckStateListener, which is responsible to maintain the checked and grayed state of the elements.

    public void selectionChanged(final SelectionChangedEvent event) {
        Object selection = ((StructuredSelection) event.getSelection()).getFirstElement();
        if (selection != null) {
            boolean state = !checkboxTreeViewer.getChecked(selection);
            checkboxTreeViewer.setChecked(selection, state);
            checkboxTreeViewer.setSelection(StructuredSelection.EMPTY);
            checkStateListener.checkStateChanged(new CheckStateChangedEvent((ICheckable) event.getSource(), selection,
                state));
        }
    
    }