How to present Java elements in a standard JFace viewer

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

Putting the pieces together is quite simple:
	...
IJavaProject jProject= ...;

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()));
...

In addition to the Java model the StandardJavaElementContentProvider class supports the following input element types: IJavaProject, IPackageFragmentRoot, IPackageFragment and IFolder.

Adding  problem and override decorators

When a viewer is supposed to include problem annotations, then a DecoratingLabelProvider together with the ProblemsLabelDecorator is used. Below is a code snippet that outlines 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 ?
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).

Copyright IBM Corporation and others 2000, 2002. All Rights Reserved.