You can share the providers. Your ContentProvider will have to implement both IStructuredContentProvider and ITreeContentProvider. I guess that normally you will want to have separate content providers.
In the example the Tree will show only one level with the elements (all elements are roots). The Table will show only one row.
Example:
//ContentProvider for Tree and Table
public static class CommonContentProvider extends ArrayContentProvider
implements ITreeContentProvider {
@Override
public Object[] getChildren(final Object arg0) {
return null;
}
@Override
public Object getParent(final Object arg0) {
return null;
}
@Override
public boolean hasChildren(final Object arg0) {
return false;
}
}
public static void testCommonProviderTreeTable(final Composite c) {
final Collection<String> input = Arrays.asList(new String[] { "hi",
"hola" });
final IContentProvider contentProvider = new CommonContentProvider();
final IBaseLabelProvider labelProvider = new LabelProvider() {
@Override
public String getText(final Object element) {
return element.toString();
}
};
final TreeViewer tree = new TreeViewer(c, SWT.NONE);
tree.setContentProvider(contentProvider);
tree.setLabelProvider(labelProvider);
tree.setInput(input);
final TableViewer table = new TableViewer(c, SWT.NONE);
table.setContentProvider(contentProvider);
table.setLabelProvider(labelProvider);
table.setInput(input);
}