## Presenting Java elements in a JFace viewer

The JDT UI API provides classes that allow you to present the Java model or parts of it in a standard JFace viewer. This functionality is provided primarily by:

- StandardJavaElementContentProvider - translates the Java element hierarchy into a data structure accessible by a tree, table or list viewer
- JavaElementLabelProvider - provides corresponding images and labels for a standard JFace viewer

Content and label providers for JFace viewers are described in detail in JFace viewers.

If you understand the basic platform mechanism, then putting the Java content and label providers together is quite simple:

        ...
        TreeViewer viewer= new TreeViewer(parent);
        // Provide members of a compilation unit or class file, but no working copy elements
        ITreeContentProvider contentProvider= new StandardJavaElementContentProvider(true, false);
        viewer.setContentProvider(contentProvider);
        // There are more flags defined in class JavaElementLabelProvider
        ILabelProvider labelProvider= new JavaElementLabelProvider(
            JavaElementLabelProvider.SHOW_DEFAULT |
            JavaElementLabelProvider.SHOW_QUALIFIED |
            JavaElementLabelProvider.SHOW_ROOT);
        viewer.setLabelProvider(labelProvider);
        // Using the Java model as the viewers input present Java projects on the first level.
        viewer.setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()));
        ...

The example above uses aJava model (IJavaModel) as the input element for the viewer. The StandardJavaElementContentProvider also supports IJavaProject, IPackageFragmentRoot, IPackageFragment, and IFolder as input elements:

### Overlaying images with Java information

JavaElementImageDescriptor can be used to create an image based on an arbitrary base image descriptor and a set of flags specifying which Java specific adornments (e.g. static, final, synchronized, ....) are to be superimposed on the image.

### Adding problem and override decorators

When a viewer is supposed to include problem annotations, the JFace DecoratingLabelProvider together with the ProblemsLabelDecorator is used. The snippet below illustrates the use of a problem label decorator.

        ...
        DecoratingLabelProvider decorator= new DecoratingLabelProvider(labelProvider, new ProblemsLabelDecorator());
        viewer.setLabelProvider(decorator);
        ...

In the same way the OverrideIndicatorLabelDecorator can be used to decorate a normal label provider to show the implement and override indicators for methods.

### Updating the presentation on model changes

Neither the OverrideIndicatorLabelDecorator nor the ProblemsLabelDecorator listen to model changes. Hence, the viewer doesn't update its presentation if the Java or resource marker model changes. The reason for pushing the update onto the client for these classes is that there isn't yet a generic implementation that fulfills all performance concerns. Handling Java model delta inspection and viewer refreshing in each label decorator or provider would lead to multiple delta inspections and unnecessary viewer updates.

So what does the client need to do in order to update their viewers ?

- OverrideIndicatorLabelDecorator: the client must listen to Java model changes (see Responding to changes in Java elements) and decide if the change(s) described by the delta invalidates the override indicator of elements presented in the viewer. If so, the class inspecting the delta should trigger a repaint of the corresponding Java elements using the standard JFace viewer API (see update methods on StructuredViewer).
- ProblemsLabelDecorator: the client should listen to changes notified by the decorator via a ProblemsLabelChangedEvent (see also ProblemsLabelDecorator.addListener ). Since the marker model is resource based, the listener has to map the resource notifications to its underlying data model. For an example showing how to do this for viewers presenting Java elements see the internal classes `ProblemTreeViewer.handleLabelProviderChanged.`

For the same reasons enumerated for label decorators the StandardJavaElementContentProvider doesn't listen to model changes. If the viewer needs to update its presentation according to Java model changes, then the client should add a corresponding listener to JavaCore. If the change described by the delta invalidates the structure of the elements presented in the viewer then the client should update the viewer using the standard JFace API (see refresh methods on StructuredViewer, and the add and remove methods on TableViewer and AbstractTreeViewer).

### Sorting the viewer

JavaElementSorter can be plugged into a JFace viewer to sort Java elements according to the Java UI sorting style.