Question

I recently implemented a CheckboxTreeViewer in my own Dialog. This works fine so far except that the tree doesn't allow me to expand nodes by default. It only works when I check the checkbox, as you can see in the following images:

This is by default. As you can see, it's not possible to expand the node, though it has children:

After checking the check box, it works:

enter image description here

I already tried to use setExpandPreCheckFilters , but with no success:

Composite container = (Composite) super.createDialogArea(parent);
tv = new CheckboxTreeViewer(container, SWT.MULTI | SWT.H_SCROLL| SWT.V_SCROLL);
GridData gridData = new GridData(GridData.FILL_BOTH);
tv.getTree().setLayoutData(gridData);
tv.setContentProvider(new FeaturePropertyDialogContentProvider());
tv.setLabelProvider(new FeaturePropertyDialogLabelProvider());
tv.setAutoExpandLevel(2);
tv.setExpandPreCheckFilters(true);

Any ideas?

-----------------------------------Update-------------------------------------

I found the reason of the problem. I forgott to check every element in the hasChildren method. The following code is working now for me:

public boolean hasChildren(Object element) {
        if (element instanceof ProductLine) {
            ProductLine productLine = (ProductLine) element;
            if (productLine.getPropertyList() != null) {
                return true;
            } else {
                return false;
            }
        }
        if (element instanceof PropertyList) {
            PropertyList propertyList = (PropertyList) element;
            if (!(propertyList.getGeneralPlatforms().isEmpty())) {
                return true;
            } else {
                return false;
            }
        } else if (element instanceof GeneralPlatform) {
            GeneralPlatform platform = (GeneralPlatform) element;
            if (!(platform.getHardwareElements().isEmpty())) {
                return true;
            } else {
                return false;
            }
        } else if (element instanceof HardwareElement) {
            HardwareElement hw = (HardwareElement) element;
            if (!(hw.getHardwareElements().isEmpty())
                    || !(hw.getProperties().isEmpty())) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

Thx for your help!!

Was it helpful?

Solution

From the SWT javadoc of setAutoExpandLevel:

The value 0 means that there is no auto-expand;

1 means that the invisible root element is expanded (since most concrete subclasses do not show the root element, there is usuallyno practical difference between using the values 0 and 1);

2 means that top-level elements are expanded, but not their children;

3 means that top-level elements are expanded, and their children, but not grandchildren;

So you should set auto expand level to 3, not 2.

Since the code you posted is not complete, I would like to also mention that it is important also when you call setAutoExpandLevel(). Internally it is called when input is changed. So it should be called before setRoot().

Below is a sample code that builds a tree like yours and expands the nodes: public class CheckTreeSnippet {

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    CheckboxTreeViewer tv = new CheckboxTreeViewer(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    GridData gridData = new GridData(GridData.FILL_BOTH);
    tv.getTree().setLayoutData(gridData);
    tv.setAutoExpandLevel(3);
    tv.setContentProvider(new FeaturePropertyDialogContentProvider());
    tv.setInput("root");

    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

private static class FeaturePropertyDialogContentProvider implements ITreeContentProvider {

    @Override
    public Object[] getElements(Object inputElement) {
        return this.getChildren(inputElement);
    }

    @Override
    public Object[] getChildren(Object parentElement) {
        switch ((String) parentElement) {
            case "root":
                return new String[]{"Platform XYZ12", "Platform ZUPP"};
            case "Platform XYZ12":
                return new String[]{"Microcontroller TPU23"};
            case "Platform ZUPP":
                return new Object[]{"Sensor", "Precaler IO"};
            case "Sensor":
                return new Object[]{"unknown child 1", "unknown child 3"};
            default:
                return new String[0];
        }
    }

    @Override
    public Object getParent(Object element) {
        return null;
    }

    @Override
    public boolean hasChildren(Object element) {
        return this.getChildren(element).length > 0;
    }

    @Override
    public void dispose() {
    }

    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top