Extreme Java When vanilla Java is not enough

30May/100

Using NetBeans Parser API with Navigator API

So, you created a new language on you NBP-based application and associated it with a new file type. Now, you need to show your parse result on your Navigator panel. I haven't found a standard way to do so, but the best shot so far was adding the parser result to the DataObject's CookieSet. This way you can lookup it in your NavigationPanel's panelActivated. Easy and fast.

10May/101

sed-like function in scala

It was a long time without using Scala, but I had a problem that it solved like a charm. I need to edit a huge file with one XML per line, replacing "b" for "bb" (i.e. duplicate a tag). I could do it with a single SED command, but, on an Windows box, I used this scala script:

def solveMyProblem = {
  import java.io.File
  import scala.io.Source
 
  Source.fromFile(new File("path-to-file"))
    .getLines()
    .map {
      _.replaceAll("(<a>(.*)</a>)", "$1<c>$2</c>");
    }
    .foreach {
      Console.println(_)
    }
}

I'm still using Scala 2.7, but this got my problem solved. It was easy to test - instead of thousands of lines on my Console, I put a ".take(10)" and I got something like Linux's "sed | head".

BTW, I guess it could be improved even more, but I don't have time to do that too.

Tagged as: , , 1 Comment
7May/102

Using NewType on a NetBeans Node

Given any Node on a NetBeans Platform Application, you can create a "new action" by adding "SystemAction.get(NewAction.class)" to your node's actions (overriding Node.getAction). You must also override the Node.getNewTypes.

Everything is simple and obvious - except one detail: be careful when overriding the node's lookup. This works:

public class MyNode extends AbstractNode {
  public MyNode() {
    super(Children.LEAF);
  }
}

But this don't (you will only get a grayed "Add" menu):

public class MyNode extends AbstractNode {
  public MyNode(MyElement a) {
    super(Children.LEAF, Lookups.fixed(a));
  }
}

The answer is not obvious (I took hours to find out) - you must add "this" to the node lookup, using the old InstanceContent recipe:

public class MyNode extends AbstractNode {
  public MyNode(MyElement a) {
    this(a, new InstanceContent());
  }
  public MyNode(MyElement a, InstanceContent ic) {
    super(Children.LEAF, new AbstractLookup(ic));
    ic.add(this);
    ic.add(a);
  }
}
Tagged as: , , 2 Comments