Decoration Elements in TreeViewer

I have the following problem: I am preparing an editor in Eclipse, and one of the tabs contains a TreeViewer to display items in the tree. Each element has a name and a value that is editable. The problem is, I have to tell the user that the value is incorrect (for example, exceeds the specified range). My idea is to decorate the wrong cells with a warning or error icon, which will be shown after editing is completed.

Does anyone know how to decorate elements in a tree? I experimented with the ControlDecoration class, but without success.

Thanks in advance,

Marcin

PS. I am limited to Eclipse 3.4

+4
source share
1 answer

There are two ways to do this. If your TreeViewer displays objects that are EObject instances (generated by EMF. If you donโ€™t understand this part, skip to the next paragraph :)), you can change this EObject "XyzItemProvider" so that their getImage method returns a decorated image instead of a "simple" image. .. and that nothing needs to be changed for EMF objects.

If you are showing "classic" Java objects, you will need to modify your LabelProvider TreeViewer to decorate the image. This is done using the TreeViewer # setLabelProvider () method.

Then you need โ€œhow to decorate the imageโ€, which is executed using code, for example:

public class MyLabelProvider extends DecoratingLabelProvider { public Image getImage(Object element) { Image image = super.getImage(element); List<Object> images = new ArrayList<Object>(2); images.add(image); images.add(<Image of the decorator>); labelImage = new ComposedImage(images); // This will put the second of the "images" list (the decorator) above the first (the element image) return decoratedImage; } [...] } 

Then you need to provide your tree viewer with this label provider:

 TreeViewer treeViewer = new TreeViewer(...); treeViewer.setLabelProvider(new MyLabelProvider(new LabelProvider()); // new LabelProvider()... or your previous label provider if you have one. 
+8
source

All Articles