Kap
Kap

Reputation:

Eclipse: Tree View And Table View

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.

Upvotes: 2

Views: 2887

Answers (2)

fgui
fgui

Reputation: 1574

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);
}

Upvotes: 0

Manuel Selva
Manuel Selva

Reputation: 19050

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.

Upvotes: 2

Related Questions