<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE type PUBLIC
"-//NetBeans//DTD annotation type 1.1//EN"
"http://www.netbeans.org/dtds/annotation-type-1_1.dtd">
<type name="org-netbeans-modules-yourfirstannotation"
description_key="yourfirstannotation"
localizing_bundle="org.netbeans.modules.yourfirstannotation.Bundle"
visible="true"
glyph="nbresloc:/org/netbeans/modules/java/resources/error-glyph.gif"
highlight="#66CC33"
type="line"
browseable="false"
severity="ok"
/>
Note 1: The value of visible determines whether the annotation will be visible in the Advanced section of the Options window. In the Advanced section, the value of description_key determines the display name of the annotation. The display name is defined in the bundle specified by localizing_bundle.
Note 2: Here we reuse the error glyph, even though it isn't really relevant to what we're doing. We're just adding a highlight to the changed line, so that isn't really an error. There are various other glyphs we could use instead. The highlight here is green. Instead of, or in addition to highlight, we could use foreground (to change the font) and waveunderline (to create the squiggly red underline marking typical to the NetBeans editors).
Note 3: Here we're not creating an error, despite the misleading glyph, and therefore the severity is set to ok, which will add a green mark to the right sidebar. Alternatives are warning (yellow mark), error (red mark), and none (no mark).
Further reading: http://www.netbeans.org/dtds/annotation-type-1_1.dtd
<folder name="Editors">
<folder name="AnnotationTypes">
<file name="org-netbeans-modules-yourfirstannotation.xml"
url="HTMLAnnotation.xml">
</file>
</folder>
</folder>
private static final class InsertAnnotation extends Annotation {
static final InsertAnnotation DEFAULT = new InsertAnnotation();
public String getAnnotationType() {
return "org-netbeans-modules-yourfirstannotation";
}
public String getShortDescription() {
return "Bold tags were added around \\"" + selection + "\\"!";
}
}
Further reading: org.openide.text.Annotation
So, lets set things up by following the steps in Module development for web developers. At the end, you'll be able to select text in a JSP file and use a menu item to add bold tags around the selected text. We're going to enhance that functionality by also highlighting the changed line.
protected void performAction(Node[] activatedNodes) {
JTextComponent editor = Registry.getMostActiveComponent();
selection = editor.getSelectedText();
if (selection.length() > 0) {
editor.replaceSelection("<b>" + selection + "</b>");
//We use the very powerful NbEditorUtilities.getLine()
//to extract a line from the current document and offset,
//for which we need a DocumentListener, as described in the next step:
Line myLine = NbEditorUtilities.getLine(doc, myDocumentListener.getOffset(), false);//Here we attach our annotation to the line:
InsertAnnotation.DEFAULT.attach(myLine);
}
}
We could also get the line from the JTextComponent, but we need the DocumentListener anyway, for detaching the annotation when the user undoes the insertion.
Two global variable are needed:
MyDocumentListener myDocumentListener = new MyDocumentListener();
static String selection;
Further reading: Annotation.attach, NbEditorUtilities.getLine, org.openide.text.Line.
private class MyDocumentListener implements DocumentListener {
int offset;
/\*\* Creates a new instance of MyDocumentListener \*/
public MyDocumentListener() {
}//We get the offset when inserting the changed text:
public void insertUpdate(DocumentEvent e) {
offset = e.getOffset();
}//When/if we remove the inserted text,
//we detach the annotation:
public void removeUpdate(DocumentEvent e) {
InsertAnnotation.DEFAULT.detach();
}
public void changedUpdate(DocumentEvent e) {
}
public int getOffset() {
return offset;
}
}
Further reading: Annotation.detach
Now play around with the definition of the annotation in the XML fle you defined at the start. Also go to the Advanced section of the Options window and look in the Annotation Types section (within "Editing") and notice that the user can change some of the attributes that you defined in the XML file.
Further reading: http://www.netbeans.org/dtds/annotation-type-1_1.dtd
The following link to longer works
http://blogs.sun.com/geertjan/entry/module_development_for_web_developers