Extreme Java When vanilla Java is not enough

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