Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 322050 - [api] Inconsistency in AnnotationPainter API : addAnnotationType vs addHighlightAnnotationType
Summary: [api] Inconsistency in AnnotationPainter API : addAnnotationType vs addHighli...
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 3.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.7 M2   Edit
Assignee: Platform-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: needinfo
Depends on:
Blocks:
 
Reported: 2010-08-07 11:55 EDT by Alan Mising name CLA
Modified: 2010-08-10 08:28 EDT (History)
1 user (show)

See Also:


Attachments
Plugin showing condition (14.43 KB, application/octet-stream)
2010-08-09 10:34 EDT, Alan Mising name CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alan Mising name CLA 2010-08-07 11:55:47 EDT
Build Identifier: 20100617-1415

There's an inconsistency in the AnnotationPainter API that makes it harder to use than it need be. One uses the #addAnnotationType(Object annotationType) method to tell the AnnotationPainter to paint an annotation using squigglies. This is typically done during the creation of a new SourceViewer instance, as follows:

SourceViewer sourceViewer = new SourceViewer( /* etc */ );
AnnotationPainter ap = new AnnotationPainter( sourceViewer, new MyAnnotationAccess());
ap.addAnnotationType( annotationType);
ap.setAnnotationTypeColor( annotationType, COLOR_RED);
sourceViewer.addPainter(ap);

That's all it takes to have the annotations of the specified type underlined with red squigglies. But suppose one wants them to be highlighted instead? No problem, one might think, because there's a method AnnotationPainter#addHighlightAnnotationType( Object) that seems to be the exact parallel of #addAnnotationType. So one would expect to be able to use the code snippet above, except of course substituting #addHighlightAnnotationType.

Not so. The code with #addHighlightAnnotationType doesn't work. Unless that is, one adds the additional line

sourceViewer.addTextPresentationListener( ap);

Two methods that look the same, but the second one doesn't work as the first one does unless that extra line of code is inserted. To me that seems like a bug. And that extra line is a well-kept secret. It's not mentioned in the Javadocs. You have to find out about it by trolling through the code of, for example, SourceViewerDecorationSupport. So the bug is a significant issue for the usability of the API of AnnotationPainter.

It's actually a bit worse than that, because all the AnnotationPainter methods that install various kinds of highlighting using an IDrawingStrategy or an ITextStyleStrategy also require that that the AnnotationPainter is a TextPresentationListener to the SourceViewer. So that undocumented and hard-to-discover extra line is needed for those as well.

To fix this bug, I suggest a mod to the implementation of SourceViewer#addPainter so that the AnnotationPainter is installed as a TextPresentationListener of the SourceViewer automatically. This change would be backwards compatible.

Lastly, let me point out that the connection between an AnnotationPainter and a SourceViewer is established 2 or 3 times: in the AnnotationPainter constructor, in SourceViewer#addPainter, and yet again when the AnnotationPainter is installed as a listener on the SourceViewer. It would be nice if this connection had to be established only once, but reducing the count from 3 to 2 would be a start.



Reproducible: Always

Steps to Reproduce:
See description details.
Comment 1 Dani Megert CLA 2010-08-09 04:13:25 EDT
Mmh, I don't think it's true that your snippet simply works without attaching the listener yourself. Can you please attach a sample plug-in that shows this.
Comment 2 Alan Mising name CLA 2010-08-09 10:34:00 EDT
Created attachment 176157 [details]
Plugin showing condition

Plugin with a view (TestAnnotationPainter) that shows highlighting with squigglies is effective without the AnnotationPainter being made a TextPresentationListener
Comment 3 Alan Mising name CLA 2010-08-09 10:36:29 EDT
The attached plugin has a view with a SourceViewer. It opens with text containing an annotation, highlighted with squigglies. The AnnotationPainter is defined in SampleView#createSourceViewer2. However, the AnnotationPainter is not made a TestPresentationListener to the SourceViewer.
Comment 4 Dani Megert CLA 2010-08-10 08:28:40 EDT
OK, I see now. The API is from the old days where we did custom squiggle drawing. This got replaced by a new mechanism that lets the widget do the drawing itself. When we added that new mechanism we also added to feature that clients can quickly turn this on and off by delegating the listener addition/removal to them.

I've deprecated the old method and added a comment that the listener must be added to the corresponding methods.

You should change your code to
1. register your own text style strategy (use new 
      AnnotationPainter.UnderlineStrategy(SWT.UNDERLINE_SQUIGGLE)
2. add your type with the id that you used in step 1
3. add the text presentation listener