CheckboxTreeviewer previously set checked elements after reload

283 views Asked by At

I have used JFace CheckboxTreeviewer and added ICheckStateListener to get result of checked elements. Code is as follows

    private HashSet<Object> checkElement=new HashSet<Object>();
checkboxTreeViewer.addCheckStateListener(new ICheckStateListener() {

        @Override
        public void checkStateChanged(CheckStateChangedEvent event) {
            String childNode=null;
            String rootNode=null;
            Object changed = event.getElement();
            if(changed instanceof ChildFacetNodeVo){
                checkElement.add(changed);
                ChildFacetNodeVo childFacetNodeVo=(ChildFacetNodeVo)changed;                    
                childNode=childFacetNodeVo.getLabelFacet();
                rootNode=childFacetNodeVo.getParent();
               //here to get a new result after element checked and put new result to checkboxtreeviewer
                List<RootFacetNodeVo> facetNodeVos=createFacetFilter(rootNode,childNode);
                if(facetNodeVos!=null){        

                    checkboxTreeViewer.setInput(facetNodeVos);                                
                    checkboxTreeViewer.expandToLevel(3);
                   checkboxTreeViewer.setCheckedElements(checkElement.toArray());
                }
            }                
        }
    });

Now what i need is when i checked the new element the previously checked element should not get unchecked. when i set new input in CheckboxTreeviewer nothing is visible. So how do i set prevously checked element in CheckboxTreeviewer. for example

p1
  -----A1 - previous checked
  -----A2
  -----A3
  -----A4
  -----A5
p1
  -----A6
  -----A7
  -----A8 - previous checked
  -----A8
   -----A9
p1
  -----A10
  -----A11
1

There are 1 answers

3
greg-449 On

If you call setCheckedElements you must include everything that should be checked in the array you specify.

You appear to be creating a new set of objects to go in the tree so you will have to work out which of these new items needs to be checked.

You can call checkboxTreeViewer.getCheckedElements() to get the old checked elements (do this before setting the new input). This should help you work out which of the new elements need to be checked.