Reputation: 984
I created a custom content provider tha implements ITreeContentProvider
and my custom model similar to this: Vogella Tutorial JFace where it has Categories and Todos and Todos are stored as children of categories as a list in the class Category.
I am using eclipse dialog CheckedTreeSelectionDialog
and it should be the same as any tree dialog and I extended it to be able to get the checked elements in a map but for some reason i can get the treeitem of level1 but not their children and I cannot understand why.
public void createMapOfCheckedItems() {
mapOfCheckedElements = new HashMap<Object, List<Object>>();
for (TreeItem level1 : getTreeViewer().getTree().getItems()) {
if (level1.getChecked()) {
List<Object> checkedChildren = new ArrayList<Object>();
for (TreeItem level2 : level1.getItems()) {
if (level2.getChecked()) {
checkedChildren.add(level2.getData());
}
}
mapOfCheckedElements.put(level1.getData(), checkedChildren);
}
}
}
The code is the above but the level1.getItems()
returns an empty treeitem but the selection dialog shows correctly the parents and childrens.
Upvotes: 2
Views: 1367
Reputation: 13468
Reading the TreeItem#getItems() API Javadoc I found this:
Returns a (possibly empty) array of TreeItems which are the direct item children of the receiver. Note: This is not the actual structure used by the receiver to maintain its list of items, so modifying the array will not affect the receiver.
My knowledge on the SWT API is not too broad, but my guess is that using the TreeViewer#getItems(org.eclipse.swt.widgets.Item) method could fix the problem, as the viewer is suposed to mantain the state for all the nodes.
So, your code should look something like:
if (level1.getChecked()) {
List<Object> checkedChildren = new ArrayList<Object>();
for (Item level2 : getTreeViewer().getItems(level1)) {
if (((TreeItem)level2).getChecked()) {
checkedChildren.add(level2.getData());
}
}
mapOfCheckedElements.put(level1.getData(), checkedChildren);
}
Upvotes: 2