CheckboxTreeViewer:默认情况下无法展开节点

时间:2013-10-22 14:27:14

标签: java checkbox swt jface treeviewer

我最近在自己的CheckboxTreeViewer中实施了Dialog。到目前为止,此工作正常,但默认情况下树不允许我扩展节点。它仅在我选中复选框时才有效,如下图所示:

这是默认情况。正如您所看到的,虽然它有子节点,但不可能扩展节点:

勾选复选框后,它可以正常工作:

enter image description here

我已经尝试使用setExpandPreCheckFilters,但没有成功:

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);

有什么想法吗?

-----------------------------------更新----------- --------------------------

我找到了问题的原因。我忘了检查 hasChildren 方法中的每个元素。以下代码现在适用于我:

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;
        }
    }

谢谢你的帮助!!

1 个答案:

答案 0 :(得分:2)

来自setAutoExpandLevel的SWT javadoc:

  

值0表示没有自动展开;

     

1意味着不可见的根元素被扩展(因为大多数   具体子类不显示根元素,通常没有   使用值0和1)之间的实际差异;

     

2意味着顶级元素被扩展,但不是他们的孩子;

     

3意味着顶级元素和他们的孩子都会被扩展   不是孙子孙女;

因此,您应将自动扩展级别设置为3,而不是2。

由于您发布的代码不完整,我还想提一下,调用setAutoExpandLevel()时也很重要。在内部,当输入改变时调用它。所以它应该在setRoot()之前调用。

下面是一个示例代码,用于构建与您类似的树并展开节点: 公共类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) {
    }
}

}