Simple tips to utilize the JFace Tree audience

Summary The objective of the article is always to educate you on simple tips to utilize TreeViewers in your Eclipse plug-ins or stand-alone JFace/SWT applications. We’ll focus on a example that is simple progressively include functionality.

By Chris Grindstaff, Applied Reasoning ( chrisg at May 5, 2002


The tree audience can be utilized in Eclipse plug-ins or in a stand alone JFace/SWT application. We’ve chosen to make usage of a plug-in that shows the tree audience functionality.

Here’s a typical example of just what the plugin provides:

You may notice the occasional as you work through the article . These icons suggest points for which you should launch Eclipse and attempt the instance.

Source Code

To operate the examples or see the origin with this article, unzip to your eclipse_root directory and restart Eclipse. In Windows the eclipse_root directory can look something such as d:\apps\eclipse\. As soon as Eclipse is restarted, find the attitude | visit the web site Show View | Other… menu choice. Within the “Other” category choose “Moving Box.”

This short article be best as you read it if you are able to run the plugin we’ve provided.

Big Photo

To comprehend just how to make use of the TreeViewer, it is essential to comprehend where in fact the TreeViewer fits into JFace in general, and exactly how JFace fits into Eclipse.

JFace is a UI toolkit that helps resolve common UI development tasks. JFace additionally will act as a bridge between low-level SWT widgets as well as your domain things. SWT widgets communicate with the host system that is operating as a result don’t have any understanding of your domain things.

One of many real methods JFace bridges the gap between SWT widgets and domain models is by audiences. JFace people include an SWT widget (age.g. Tree, dining table, etc), as well as your domain things. You offer the watchers using the information they require so that you can populate the underlying SWT widget. The audience has the capacity to sort and filter your objects that are domain along with up-date the widget if your domain objects change.

Additional information about JFace and SWT are located in the Javadoc for org.eclipse.jface.viewers, org.eclipse.swt.widgets, org.eclipse.swt.custom and JFace audiences.

Establishing the phase

Due to the fact TreeViewer utilizes domain objects to populate its Tree, we are going to produce a model that is simple be properly used throughout this informative article.

Assume you’ve bought an innovative new household and it’s really time and energy to pack your material for the move. To help keep your entire publications and games organized you determine to use a TreeViewer that will help you keep purchase. (Yes, you are a geek.)

Listed below are our objects that are domain

A moving field can include other going bins, publications, and games. A box that is moving or might not have a name.

  • Guide
  • Game

Each board and book game has both a name and a writer.

Build a straightforward TreeViewer

We’ll focus on an easy TreeViewer example and build upon it through the article.

Here’s the rule that creates this view depicted previous.

TreeViewer Domain Model Interactions

It is vital to know the model/view relationship as employed by JFace audiences. Conceptually, all audiences perform two main tasks:

  • they assist adjust your objects that are domain viewable entities
  • they supply notifications once the entities that are viewable chosen or changed through the UI

More especially, whenever using a tree audience, you utilize your domain things as arguments within the API practices. For instance, you can include a novel to a moving field by calling the TreeViewer.add(aMovingBox, aBook) technique. You don’t need certainly to translate your objects that are domain UI elements; the tree audience does that for you personally. You will be making your “root” domain item avaliable into the tree audience by invoking the TreeViewer’s setInput technique. Your domain item then becomes the tree viewer’s input.

Likewise, whenever you ask the tree audience for the chosen items, it will probably answer aided by the domain things – perhaps not the underlying UI resources.

Content Provider

Whenever using a tree audience, you ought to supply the audience with here is how to transform your domain item into a product within the UI tree. This is the reason for an ITreeContentProvider . One of the domain classes could implement this user interface. In the place of “polluting” your domain objects with graphical user interface rule, you might want to produce another item to satisfy the tree content provider demands.

Let’s take a good look at several of this interface’s methods.

  • public Object[] getElements(Object inputElement)

This is basically the technique invoked by calling the setInput technique in the tree audience. In reality, the getElements technique is known as just as a result to your tree audience’s setInput technique and may respond to utilizing the appropriate domain things of this inputElement. The getElements and getChildren techniques operate in a similar means. According to your objects that are domain you’ve probably the getElements just get back caused by calling getChildren. The 2 techniques are held distinct since it provides a way that is clean distinguish between your root domain item and all other domain objects.