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:

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
    ITreeContentProvider contentProvider= new StandardJavaElementContentProvider(true);
    // There are more flags defined in class JavaElementLabelProvider
    ILabelProvider labelProvider= new JavaElementLabelProvider(
        JavaElementLabelProvider.SHOW_DEFAULT |
        JavaElementLabelProvider.SHOW_QUALIFIED |
    // Using the Java model as the viewers input present Java projects on the first level.

The example above uses a Java 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());

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 ?

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.