X

Geertjan's Blog

  • August 16, 2015

Simplified Reflection for AbstractNode

Geertjan Wielenga
Product Manager

When using BeanNode, you don't need to create a PropertySheet because the NetBeans Platform uses reflection to build that for you. However, when using AbstractNode, you don't have that built-in reflection support. Instead, you need to override createSheet() in your AbsrtractNode and then, in the simplest cases, you can populate your PropertySheet by creating Properties that extend PropertySupport.Reflection.

I've come up with a simplified way of doing that:

@Override
protected Sheet createSheet() {
Sheet sheet = Sheet.createDefault();
Sheet.Set basicSet = Sheet.createPropertiesSet();
Planet planet = getLookup().lookup(Planet.class);
try {
basicSet.put(new PropSupport.StringProp(planet, "name", "Name"));
basicSet.put(new PropSupport.IntegerProp(planet, "size", "Size"));
basicSet.put(new PropSupport.ColorProp(planet, "color", "Color"));
basicSet.put(new PropSupport.BooleanProp(planet, "hasWater", "Has Water"));
basicSet.put(new PropSupport.BooleanProp(planet, "hasLife", "Has Life"));
basicSet.put(new PropSupport.PlanetTypeProp(planet, "type", "Type"));
} catch (NoSuchMethodException ex) {
Exceptions.printStackTrace(ex);
}
sheet.put(basicSet);
return sheet;
}

And here's PropSupport, i.e., my own class, essentially an API, that hides the below via the method calls above:

public class PropSupport {
public static class StringProp
extends PropertySupport.Reflection<String> {
public StringProp(
Planet bean,
String propName,
String displayName) throws NoSuchMethodException {
super(bean, String.class, propName);
setDisplayName(displayName);
}
}
public static class ColorProp
extends PropertySupport.Reflection<Color> {
public ColorProp(
Planet bean,
String propName,
String displayName) throws NoSuchMethodException {
super(bean, Color.class, propName);
setDisplayName(displayName);
}
}
public static class IntegerProp
extends PropertySupport.Reflection<Integer> {
public IntegerProp(
Planet bean,
String propName,
String displayName) throws NoSuchMethodException {
super(bean, int.class, propName);
setDisplayName(displayName);
}
}
public static class BooleanProp
extends PropertySupport.Reflection<Boolean> {
public BooleanProp(
Planet bean,
String propName,
String displayName) throws NoSuchMethodException {
super(bean, boolean.class, propName);
setDisplayName(displayName);
}
}
public static class PlanetTypeProp
extends PropertySupport.Reflection<PlanetType> {
public PlanetTypeProp(
Planet bean,
String propName,
String displayName) throws NoSuchMethodException {
super(bean, PlanetType.class, propName);
setDisplayName(displayName);
}
}
}

Now I can pass in a few arguments and get back via reflection my underlying properties very easily.

Be the first to comment

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