Search code examples
javaswteclipse-rcptreeviewer

Set initial input for ContainerCheckedTreeViewer


I have trouble setting the initial input for a ContainerCheckedTreeViewer in an Eclipse ViewPart. I set the initial input inside the createPartControl method, but the content is not rendered/visible. This is only an issue when the view is created (e.g. Eclispe startup/opening the view for the first time). Setting the input (e.g. by an oberservable) after the view is fully initialized works and the tree is rendered correctly.

Here is my part createPartControl method which does not work and the update method which works.

private ContainerCheckedTreeViewer viewer;
//...
@Override
public void createPartControl(Composite parent) {
    //Optional<ModelObject> data = null;
    //Setting data
    //...
    contentProvider = new ViewContentProvider(viewer);
    viewer = new ContainerCheckedTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.setContentProvider(contentProvider);
    viewer.setLabelProvider(new DefaultEObjectLabelProvider());
    viewer.addCheckStateListener(this);
    viewer.setSorter(null);
    if(data.isPresent()){
        viewer.setInput(data); //This is actually called, breakpoint is hit, data looks ok
    }   
    viewer.expandAll();

    //doing other stuff
}

@Override
public void update(Observable o, Object arg) {
    if (arg instanceof ModelObject){
        viewer.setInput(arg); //This works...
    }   
}

Did I miss something or is there some other method early in the lifecycle of a ViewPart that should be used to set default data?


Solution

  • Well, it's late, probably too late.

    I set an Optional as input, which won't work because the ViewContentProvider expects a ModelObject, not a Optional<ModelObject>.

    Changing viewer.setInput(data) to viewer.setInput(data.get()) does the trick.