我面临的问题是,CheckboxTreeViewer
在 Linux Debian 上实现了完美的工作,但在 Windows 下却没有。首先,我不理解“setGrayed()”的概念。是否表明,只有元素中的部分子元素被选中?
所以问题是:当我在 Windows 下检查一个项目时,它会检查树中的所有其他元素。不知何故,整个逻辑被忽略了。
这是我的代码ICheckStateListener
:
@Override
public void checkStateChanged(final CheckStateChangedEvent event) {
// object checked
final TreePath path = ((ITreeSelection) this.treeViewer.getSelection()).getPaths()[0];
checkChildren(path, event.getChecked());
checkParents(path.getParentPath());
syncData();
}
public void checkParents(final TreePath path) {
if (path == null) {
return;
}
final IFieldElement<Object> treeElement = (IFieldElement<Object>) path.getLastSegment();
if (treeElement != null) {
boolean allChecked = true;
boolean allUnchecked = true;
for (final Object fieldElement : treeElement.getChildrenElements()) {
allChecked = allChecked && this.treeViewer.getChecked(fieldElement);
allUnchecked = allUnchecked && !this.treeViewer.getChecked(fieldElement);
}
if (allUnchecked) {
this.treeViewer.setChecked(treeElement, false);
this.treeViewer.setGrayed(treeElement, false);
} else
if (allChecked) {
this.treeViewer.setChecked(treeElement, true);
this.treeViewer.setGrayed(treeElement, false);
} else
if (!allUnchecked && !allChecked) {
this.treeViewer.setChecked(treeElement, true);
this.treeViewer.setGrayed(treeElement, true);
}
}
checkParents(path.getParentPath());
}
public void checkChildren(final TreePath path, final boolean checked) {
if (path == null) {
return;
}
final Object element = path.getLastSegment();
if (element != null) {
this.treeViewer.setChecked(element, checked);
this.treeViewer.setGrayed(element, false);
this.treeViewer.setSubtreeChecked(element, checked);
if (element instanceof EventMethod) {
checkEventMethod(path, element, checked);
}
}
}
编辑:我正在使用 Eclipse Indigo
提前致谢!