eclipse-rcpjfacetreeviewer

CheckboxTreeViewer on checked Children node get Parent node value


I am using JFace CheckboxTreeViewer and adding on ICheckStateListener to get checked elemets,

My CheckboxTreeViewer structure is as follows,

P1
 ----Child1
 ----Child2
 ----Child3
 ----Child4
P2
 ----Child6
 ----Child7
 ----Child8
 ----Child9

My Requirement is that when I am checked a Children node to get its Related parent node

for example

when I checked Child8 then get Parent Node p2 when I checked Child2 then get Parent Node p1

how to achieve this?


Solution

  • You get the element that has changed from the CheckStateChangedEvent passed to the listener by calling the getElement method:

    public void checkStateChanged(CheckStateChangedEvent event) {
    
      Object changed = event.getElement();
    

    This is the object that your tree content provider provided. So you can get its parent by asking the content provider:

      ITreeContentProvider provider = (ITreeContentProvider)viewer.getContentProvider();
    
      Object parent = provider.getParent(changed);
    

    where viewer is the CheckboxTreeViewer.