CheckedTreeSelectionDialog最初检查元素

时间:2012-12-13 14:16:28

标签: java eclipse dialog swt

我正在使用 CheckedTreeSelectionDialog ,我想最初选择一些项目。

如何使用 setInitialSelections 方法选择子项(level2项)而不是level1。

CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(
    this.containerComposite.getShell(), new myLabelProvider(), new
    myContentProvider());

dialog.setContainerMode(true);
dialog.setInput(new MyModel());

Parent p = new Parent("I am a parent");
p.getChildren.add(new Child("I am a child"));
dialog.setInitialSelection(p);

当containerMode为false时,未选中子项,如果为true,则选择所有子项时为

2 个答案:

答案 0 :(得分:1)

只需使用方法SelectionDialog#setInitialElementSelections(List elements)并在List中传递您想要选择的元素:

CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(
this.containerComposite.getShell(), new myLabelProvider(), new myContentProvider());

dialog.setContainerMode(true);
dialog.setInput(new MyModel());

List<Child> list = new ArrayList<Child>();

/* fill your list */

dialog.setInitialElementSelections(list);

答案 1 :(得分:1)

确保你做到了 dialog.setInitialElementSelections(model.getAllElements());
之前打开对话框:dialog.open();
因为否则它将无效。

我遇到了同样的问题 - 我只能标记fisrt级元素。 解决方案是确保这些方法在ITreeContentProvider实现类中起作用:

// this is the object that would get passed into setInput()
private MyModelProvider model; 

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) 
    this.model = (MyModelProvider ) newInput;
}

@Override
public Object getParent(Object element) {
    if (element instanceof Child)
        return model.getCategories().get(0);    // I only have one category 
    return null;
}