views:

749

answers:

2

Hello Guys,

I want to know if it's possible to use the same content and label providers for Tree and Table in Eclipse views or they must have separate content and label providers. I am trying to use the content and label providers i wrote for the tree for the table as well but i see nothing on the table view.

Thanks.

+1  A: 

You CAN use the same Label provider.

You CAN'T use the same content provider since the tree content provider must implement ITreeContentProvider which is not "compatible" with the IStructuredContentProvider interface that must be implemented by table content provider.

By not "compatible" I mean that the implementation of IStructuredContentProvider.getElements(Object inputElement) method in TreeContentProvider must return only the "roots" objects whereas it must return all the objects for a list content provider.

Manuel Selva
A: 

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);
}
zesc
If a viewer is disposed, the dispose() method of its label provider is invoked. For example DecoratingLabelProvider implements this method to dispose the images.Though you cannot reuse the same LabelProvider instance in multiple viewers, if this viewers are not disposed at the same time.
ftl