Are you sure the model contains all the data when setInput()
is called?
internalExpandToLevel(Widget widget, int level)
(where expanding takes place) is called on inputChanged(Object input, Object oldInput)
. If at the time setInput
is called the model is empty, no node will be expanded. Even if you later add nodes and call refresh.
To prove this, I changed my snippet from an answer for another question.
Run this code as it is, then run it with the empty
field initialized to true
. You will see the difference.
static boolean empty = false;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
TreeViewer treeViewer = new TreeViewer(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
treeViewer.setContentProvider(new DummyContentProvider());
treeViewer.setAutoExpandLevel(3);
treeViewer.setInput("root");
empty = true;
treeViewer.refresh();
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static class DummyContentProvider implements ITreeContentProvider {
@Override
public Object[] getElements(Object inputElement) {
return this.getChildren(inputElement);
}
@Override
public Object[] getChildren(Object parentElement) {
if (!empty) {
return new Object[0];
}
switch ((String) parentElement) {
case "root":
return new String[]{"a", "b"};
case "a":
return new String[]{"1"};
case "b":
return new Object[]{"1", "2"};
case "1":
return new Object[]{"x", "y"};
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) {
}
}