data.widthHint = MIN_TREE_WIDTH;
tree.setLayoutData(data);
// Set its content provider and label provider
final IContentProvider contentProvider =
new StyleCategoriesContentProvider();
final ILabelProvider labelProvider = new StyleCategoriesLabelProvider();
categoryTreeViewer.setContentProvider(contentProvider);
categoryTreeViewer.setLabelProvider(labelProvider);
// create the node collapse listener. We need this listener to ensure
// that a node is always selected. If a node is expanded and one of
// its children is the selected node the tree will have no nodes
// selected when the expanded node is collapsed.
collapseListener = new ITreeViewerListener() {
// javadoc inherited
public void treeCollapsed(TreeExpansionEvent event) {
// If the colapse event has resulted in nothing being selected
// then ensure that the node that was collapsed is selected
if (categoryTreeViewer.getSelection().isEmpty()) {
categoryTreeViewer.setSelection(
new StructuredSelection(event.getElement()));
}
}
// javadoc inherited
public void treeExpanded(TreeExpansionEvent event) {
// not interested in expansion events
}
};
// register the listener with the viewer
categoryTreeViewer.addTreeListener(collapseListener);
// Now set the input to the top-level categories: in single-select
// mode, this will automatically cause the first one to be selected
// which is what we want
categoryTreeViewer.setInput(categories);
// Listen for disposal
this.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent event) {
contentProvider.dispose();
labelProvider.dispose();
categoryTreeViewer.removeTreeListener(collapseListener);
}
});