Previous Page
Next Page

5.1.2. Content providers

A content provider is another common adapter type used in list viewers. This provider is used to map between a domain model object or a collection of domain model objects used as the input to the viewer and the internal list structure needed by the viewer itself.

The two most common types of content providers are IStructuredContentProvider, used in lists and tables, and ITreeContentProvider, used in trees (see Figure 5-4). The former maps a domain model input into an array while the latter adds support for retrieving an item's parent or children within a tree. A content provider is associated with a viewer using the setContentProvider() method. A domain model input is associated with a viewer using the setInput() method.

Figure 5-4. ContentProvider hierarchy.

Useful APIs defined by IStructuredContentProvider include:

getElements(Object) Returns the elements to display in the viewer when its input is set to the given element.

inputChanged(Viewer, Object, Object) Notifies this content provider that the given viewer's input has been switched to a different element.

Useful APIs added by ITreeContentProvider include:

Object[] getChildren(Object) Returns the child elements of the given parent element. The difference between this method and the previously listed getElements(Object) method is that it is called to obtain the tree viewer's root elements, whereas getChildren(Object) is used to obtain the children of a given parent element in the tree (including a root).

getParent(Object) Returns either the parent for the given element or null, indicating that the parent can't be computed.

hasChildren(Object) Returns whether the given element has children.

For an example of content providers, see Section 5.1.6, ListViewer class, on page 192.

Previous Page
Next Page