Reputation: 984
I am using a CheckedTreeSelectionDialog and I want to select initially some items.
How do I use the method setInitialSelections to select children (level2 items) and not 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);
The child is not selected when containerMode is false and when is true like the example it selects all the children.
Upvotes: 4
Views: 397
Reputation: 1402
Make sure that you do your
dialog.setInitialElementSelections(model.getAllElements());
before you open your dialog: dialog.open();
because otherwise it won't work.
I had the same problem - I could only mark the fisrt level element.
The solution was to make sure that these methods are working in the ITreeContentProvider
implementation class:
// 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;
}
Upvotes: 1
Reputation: 36884
Just use the method SelectionDialog#setInitialElementSelections(List elements)
and pass the elements you want to be selected in a 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);
Upvotes: 1