[nbdev] Re: how to avoid duplicate org.openide.text.Annotations

  • From: Miloslav Metelka < >
  • To:
  • Subject: [nbdev] Re: how to avoid duplicate org.openide.text.Annotations
  • Date: Sat, 13 Oct 2012 09:05:39 +0200

Hi Thomas,
the clients attaching the annotations typically hold all annotations that they attach to a given document and typically upon e.g. recompilation they deattach and reattach all the annotations. If you want to inspect a list of your annotations then I would suggest holding them in a list sorted according to their positions in the document so that you can binary-search it. You may then possibly check
whether the annotation is inside the given line by using line elements (line root element is returned by Document.getDefaultRootElement()).
If you would just want to highlight fractions of line (without having icon) you may consider using HighlightingLayer in editor.lib2 too.


On 10/08/2012 02:19 PM, 

Dear List,

I'm dealing with custom org.openide.text.Annotations. Currently my main
goals are to add annotations to Line and Line.Part, to delete
annotations when appropriate (line has been edited) and to avoid having
the same Annotations added twice.

I have had some trouble with it and after several hours of trial/error,
I feel stuck (again). So if you feel able to give a hint or if you have
a best-practice for that, please, just skip reading this long mail at
any time and shout it out. I'd really appreciate any hint.

I used org.openide.text.Annotation#attach(Annotatable) in the first
Place. (e.g. myAnnotation.attach(myLinePart))

Main problem is to avoid duplicate Annotations (same message, same

I noticed that developers life could be very much easier, if
org.openide.text.Annotatable would provide a public method like
attachIfNotPresent(Annotatable) or getAnnotations(). The latter one to
enable the developer to check quickly if this annotation has already
been set.

Because Annotatable manages the Annotations as a List, _and_ doesn't
provide a method to retrieve the collection of Annotations already
stored in this Annotatable, I think I'm required to manage mappings of
Line.Parts and annotations if want to avoid duplicates. This would
include listening to disposal of Documents and stuff, in order to avoid
memory leaks. I feel like I have to write a whole lot of code, just to
manage what is already managed multiple times.

After several hours of searching and trial error, this is currently my
last approach (which doesn't work either due to the problems described

I noticed that NbEditorDocument maps Annotations to AnnotationDescs
using Annotations as keys in a HashMap. It also throws
IllegalStateExceptions when trying to "add an Annotation that has
already been added" or when trying to "remove an Annotation that has not
been added" (problem: what _is_ an Annotation that has already been

I wanted to make use of this IllegalStateException to determine whether
an Annotation has already been set.

Back to the problem: How would you tell if an Annotation equals another
Annotation or how would you calculate a valid and reasonable hashCode?

AnnotationType, ShortMessage and Position should be the same. As the
offset can change, Position would be required to be an Object whose
offset-property gets updated as the position changes. So I tried to use
hashCode of Line and Line.Part before I noticed that Line.Part-Instances
will always be created freshly, not reused even if they describe the
same location.

The original offset is not a valid criteria when comparing one
Annotation with another (that's what the hashCode is actually generated
for, remember the NbEditorDocument HashMap<Annotation,AnnotationDesc>),
nor is the current offset of a Position Instance hashable, cause this
could change the hashValue of an object that is already stored in a
HashMap, which wouldn't work either.

I'm really stuck again. Is there any way (besides creating a big fat
Annotation-Management-Cluster) to deal with these problems?

I'm pretty sure I missed a point.

Thanks in advance,


[nbdev] how to avoid duplicate org.openide.text.Annotations


[nbdev] Re: how to avoid duplicate org.openide.text.Annotations

Miloslav Metelka 10/13/2012

[nbdev] Re: how to avoid duplicate org.openide.text.Annotations


[nbdev] Re: how to avoid duplicate org.openide.text.Annotations

Jaroslav Tulach 10/14/2012

[nbdev] Re: how to avoid duplicate org.openide.text.Annotations


Project Features

About this Project

www was started in November 2009, is owned by jpirek, and has 25 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20150226.965aeb8). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
Please Confirm