Why do we need ContentProviders for JFace Viewers (TableViewers specifically)?
Asked Answered
O

2

8

I have always wondered why we exactly need ContentProviders for JFace TableViewers? I know that getElements() method of a ContentProvider class will return an Array or Collection of Objects which corresponds to rows on the table and getElements will be called when we setInput(input) on a table. But my questions would be more specific..

getElements returns an Array or Collection of objects, instead I could write my own method to return an Array of objects and then do a setInput on the table viewer's instance right?

Can someone give me a not-too-technical but a brief and satisfying answer on why exactly we need ContentProviders? Without ContentProviders if I try to set input on a table then nothing shows up.. so when it comes to JFace a ContentProvider and a LabelProvider is a must..

Offprint answered 29/12, 2015 at 11:9 Comment(0)
A
7

For a JFace (table) viewer to work, a ContentProvider and a LabelProvider is essential.

When creating a viewer, a default LabelProvider (an instance of LabelProvider) is already set. This implementation uses toString() to obtain a textual representation of the element.

A ContentProvider needs to be explicitly set. It is used to translate from the applications domain model to the viewers model. In case of the TableViewer, the viewer expects a flat array of the elements that should be displayed. Each element represents one row of the TableViewer.

If your domain model already uses an array or collection, you can use the predefined ArrayContentProvider.

To show a list of Persons for example you would configure the viewer like this:

Person[] persons = ...
viewer.setContentProvider( ArrayContentProvider.getInstance() );
viewer.setInput( persons );

Suppose that persons are held in a PersonRegistry you would rather configure the viewer with a custom ContentProvider that is able to extract persons from the registry:

PersonRegistry registry = ...
viewer.setContentProvider( new PersonContentProvider() );
viewer.setInput( registry );

class PersonContentProvider implements IStructuredContentProvider {
  PersonRegistry registry;
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    this.registry = ( PersonRegistry )newInput;
  }

  public Object[] getElements(Object inputElement) {
    return registry.getPersons();
  }

  ...
}

Finally, setInput() is meant to supply the application model. The inputChanged() method of the ContentProvider must be able to cope with what was passed to setInput(). Also note, that setInput() must only be called after a content provider was set, otherwise an exception will be thrown.

Adriatic answered 29/12, 2015 at 12:20 Comment(0)
K
0

If your having an array input just set an org.eclipse.jface.viewers.ArrayContentProvider. http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjface%2Fviewers%2FArrayContentProvider.html

Tables and Trees can have differnent inputs for example XML content or database result, the content provider prepares the content for the table.

EDIT: Lars Vogel writes in his tutorial:
As with other JFace viewers a content provider supplies the data which should be displayed in the TableViewer.

Eclipse provides an implementation of this interface via the ArrayContentProvider class. The ArrayContentProvider class supports Arrays or Collections as input, containing the domain data. You can implement your own content provider for a table by implementing the interface IStructuredContentProvider from the org.eclipse.jface.viewers package.

The getElements() method of the content provider is used to translate the input of the viewer into an array of elements. Once the setInput() method on the viewer is called, it uses the content provider to convert it. This is the reason why the content provider must be set before the setInput() method is called.

Each object in the Array returned by the content provider is displayed as individual element by the viewer. In case of the table viewer each object is displayed in an individual row. http://www.vogella.com/tutorials/EclipseJFaceTable/article.html

Kanya answered 29/12, 2015 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.