gpt4 book ai didi

java - 复选框树查看器 : Can't expand nodes by default

转载 作者:行者123 更新时间:2023-11-30 04:11:24 25 4
gpt4 key购买 nike

我最近在自己的 Dialog 中实现了一个 CheckboxTreeViewer。到目前为止,这工作得很好,只是树默认不允许我扩展节点。它仅在我选中该复选框时才起作用,如下图所示:

这是默认的。正如您所看到的,尽管节点有子节点,但无法展开该节点:

选中复选框后,它就起作用了:

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

感谢您的帮助!!

最佳答案

来自 setAutoExpandLevel 的 SWT javadoc:

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;

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

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

下面是构建像您一样的树并扩展节点的示例代码:公共(public)类 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) {
}
}

}

关于java - 复选框树查看器 : Can't expand nodes by default,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19520645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com