Geertjan is a DZone Zone Leader and has posted 453 posts at DZone. You can read more from them at their website. View Full User Profile

Data-Aware Component for NetBeans Platform

09.20.2011
| 3177 views |
  • submit to reddit

A data-aware component has the ability to read and display the value of a field from the property assigned to it at runtime. This is different from a standard component, which has content assigned when you write the component during development.

The NetBeans Platform has always had its very own data-aware component, org.openide.explorer.propertysheet.PropertyPanel. However, until this issue was fixed, during the last few days, it was impossible to use that component within Matisse:

http://netbeans.org/bugzilla/show_bug.cgi?id=196723

Now that the issue is fixed, you can add that component to the Palette in NetBeans IDE and then drag it onto your JPanel or TopComponent in your NetBeans Plaform application.

And here is an example usage:

Result<PersonNode> results = null;

@Override
public void componentOpened() {
    results = Utilities.actionsGlobalContext().lookupResult(PersonNode.class);
    results.addLookupListener(this);
}

@Override
public void componentClosed() {
    results.removeLookupListener(this);
}


@Override
public void resultChanged(LookupEvent le) {
    if (!results.allInstances().isEmpty()) {
        PersonNode node = results.allInstances().iterator().next();
        Property[] properties = node.getPropertySets()[0].getProperties();
        for (Property property : properties) {
            if (property.getName().equals("name")) {
                namePanel.setProperty(property);
            }
            if (property.getName().equals("color")) {
                colorPanel.setProperty(property);
            }
        }
    }
}
 

As you can see above, we're listening for a PersonNode. When a PersonNode is available in the Lookup, we get the its properties. Then we assign the properties that we're interested in to the PropertyPanels that we've created for them. In the case of the Color property, since we're using java.awt.Color, we'll see the color displayed in the PropertyPanel (and the color chooser will be displayed when we click on the "..." button to the right of the panel):

In other words, now that the PropertyPanel can be dragged and dropped into the Matisse GUI Builder, we have a great data-aware component that can be used very easily and that functions exactly as one would expect.

Published at DZone with permission of its author, Geertjan Wielenga.