Geertjan's Blog

  • May 19, 2008

Customized Rendering of Class Members in the NetBeans Java Editor

Geertjan Wielenga
Product Manager
Some small experiments with the Highlighting SPI, which I discussed yesterday, in response to Casper Bang's comments at the end of that blog entry:
I am curious Geertjan, would this be the API to use, if one wanted to implement a feature which could filter away the noise of non-essential code.

Say I wanted to be able to toggle a button up in the editor toolbar, which would cause all System.out.print/Logger statements as well as catch blocks to be rendered almost invisible (until my caret is on that line).

I often find myself using too few logging statements and uniting exception handling into one catch, just to avoid this "noise" which obscures what my code is actually trying to do.

In the first experiment, I have changed the background color of all methods in my class (but not the methods of inner classes), as can be seen here:

Notice that the constructor is not treated as a method. And here, the same class is shown, this time with the methods greyed out (i.e., I changed their foreground color, which is different to commenting out the code, because here we are only interested in rendering of the code, not in adding the // to the front of the lines):

Here is the AttributeSet for the above:

private static final AttributeSet defaultColors =
new Color(150, 150, 150));

The change (i.e., the new background color or foreground color) is performed when the caret changes (see the CaretListener in yesterday's blog entry), but could be done in other ways too, such as via the ModuleInstall class, for example, although that would affect performance at start up, so be careful.

The pieces that make all this possible are the aforementioned Highlighting SPI, together with the Java Sources API, and other supporting APIs which, in NetBeans terms, are collectively referred to as the "Retouche APIs".

Starting from the code presented in yesterday's blog entry, I only changed the scheduleUpdate() method, as follows:

public void scheduleUpdate() {
if (task == null) {
task = RequestProcessor.getDefault().create(new Runnable() {
public void run() {
Document doc = comp.getDocument();//Get the JavaSource object from the java.swing.text.Document,
//which, in turn, is obtained from the JTextComponent that
//represents the editor (see the constructor of
//MarkOccurrencesHighlighter class in yesterday's blog entry):

JavaSource javaSource = JavaSource.forDocument(doc);
try {
javaSource.runUserActionTask(new Task<CompilationController>() {
public void run(CompilationController compilationController) throws Exception {
Document document = compilationController.getDocument();
if (document != null) {
new MemberVisitor(compilationController).scan(compilationController.getCompilationUnit(), null);
}, true);
} catch (IOException ex) {
}, true);

Here's the MemberVisitor:

private static class MemberVisitor extends TreePathScanner {
private CompilationInfo info;
public MemberVisitor(CompilationInfo info) {
this.info = info;
public Void visitClass(ClassTree t, Void v) {
Element el = info.getTrees().getElement(getCurrentPath());
TypeElement te = (TypeElement) el;
List enclosedElements = te.getEnclosedElements();
for (int i = 0; i < enclosedElements.size(); i++) {
Element encEl = (Element) enclosedElements.get(i);
Trees trees = info.getTrees();
CompilationUnitTree cUnit = info.getCompilationUnit();
if (encEl.getKind() == ElementKind.METHOD) {//Get a new Tree, specifically for our found elements:
Tree tree = trees.getTree(encEl);
int startPosition = (int) info.getTrees().getSourcePositions().getStartPosition(cUnit, tree);
int endPosition = (int) info.getTrees().getSourcePositions().getEndPosition(cUnit, tree);//Add the start position and end position,
//together with colors, to the Highlighting SPI's OffsetsBag:

bag.addHighlight(startPosition, endPosition, defaultColors);
return null;

So, those are my first small steps in using the Highlighting SPI to render the class members in the Java editor. Other experiments will follow, but this should give an idea of what is needed if you want to explore this part of the NetBeans APIs. "Simply" identify the elements you'd like to handle differently and then add them to the Highlighting SPI's highlighting bag.

Essential reading for programmatically customizing the Java editor in NetBeans IDE:

Join the discussion

Comments ( 2 )
  • Sandip Monday, May 19, 2008

    Cool stuff.

    I can imagine a notion of code layers:

    - static vs. non static

    - private vs. protected vs. public vs. package

    - synchronized vs. not

    which can be highlighted based on some check boxes. By assigning same foreground as background you can effectively hide those layers.

    I wonder if you could add an ability to color the elements in your project that can be selected using Jackpot query language:


  • Casper Monday, May 19, 2008

    Jackpot, Schlieman, Retouche, NetBeans Lexer, Java Compilation API... it is a bit confusing and seems to overlap?!

    Anyway, awesome info. I am now filtering SOP and logging statements via the following method, a slightly modified version of your code. I still have some issues getting the outer-most parenthesizes pair highlighted and not really satisfied with lag and flickering.


    public Object visitMethodInvocation(MethodInvocationTree methodInvocationTree, Object obj)


    Trees trees = compilationInfo.getTrees();

    SourcePositions sourcePos = trees.getSourcePositions();

    CompilationUnitTree compUnit = compilationInfo.getCompilationUnit();

    int startPos = (int) sourcePos.getStartPosition(compUnit, methodInvocationTree.getMethodSelect() );

    int endPos = (int) sourcePos.getEndPosition(compUnit, methodInvocationTree.getMethodSelect() );

    Element element = trees.getElement( getCurrentPath() );

    if(element != null && element.getKind() == ElementKind.METHOD )


    if(startPos != javax.tools.Diagnostic.NOPOS && endPos != javax.tools.Diagnostic.NOPOS)


    // Highlight (suppress) the method call

    bag.addHighlight(startPos, endPos, defaultColors);

    // Highlight (suppress) everything in the argument list

    for(ExpressionTree tree : methodInvocationTree.getArguments())


    startPos = (int) sourcePos.getStartPosition(compUnit, tree);

    endPos = (int) sourcePos.getEndPosition(compUnit, tree);

    bag.addHighlight(startPos, endPos, defaultColors);





Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.