Reputation: 2180
I have a tree viewer next to a specialized viewer. When something is selected in the tree viewer, details about this object are shown in the specialized viewer. TreeViewer tree
, Composite control
, and MySpecializedViewer viewer
are instance variables.
public TheEverythingViewer(Composite parent) {
control = new Composite(parent, SWT.NONE);
control.setLayout(new GridLayout(2, false));
tree = new TreeViewer(control);
tree.setContentProvider(new MyContentProvider());
tree.setLabelProvider(new MyLabelProvider());
tree.setUseHashlookup(true);
tree.getControl().setLayoutData(new GridData(GridData.BEGINNING, GridData.FILL, false, true, 1, 1));
tree.addSelectionChangedListener(new ISelectionChangedListener() {
@Override public void selectionChanged(SelectionChangedEvent event) {
try {
IStructuredSelection sel = (IStructuredSelection) event.getSelection();
MyClass myInput = (MyClass) sel.getFirstElement();
if (viewer != null)
if (!viewer.getControl().isDisposed())
viewer.getControl().dispose();
viewer = new MySpecializedViewer(control, table);
control.getShell().layout();
} catch (Exception e) {
if (viewer != null)
if (!viewer.getControl().isDisposed())
viewer.getControl().dispose();
viewer = null;
}
}
});
}
Am I doing something wrong? I just want:
+--------------+--------------------------------------------+
| + Node | |
| - Node | |
| + Node | My |
| - Node | |
| - Node | Specialized |
| | Viewer |
| | |
| | |
| | |
| | |
| | |
| | |
| | +--------+ |
| | | | |
| | | | |
| | | | |
| | +--------+ |
| | |
| | |
| | |
| | |
+--------------+--------------------------------------------+
The specialized viewer has tables that need to consume more or less space depending on the selected node. And currently, creating a new instance of the specialized viewer is much, much simpler than changing it's input (that wouldn't work ATM).
Upvotes: 0
Views: 3099
Reputation: 20003
Yes, you shouldn't be recreating the viewer every time selection changes in your tree, you should just be sending the tree's selection to the existing viewer as its input, at which point it can do whatever you want it to do with the new input. You're also never setting the layout data on your specialized viewer's control, and then forcing the entire shell to re-layout is wasteful.
Upvotes: 2