The Nodes API provides the functionality to present data objects. It includes the functionality to customise the way in which nodes are displayed in explorer views. The focus of this short tutorial is changing the value icon for a node property.

The NetBeans Nodes API Tutorial explains in a lot of detail how to use the Nodes API. It is assumed that the reader is familiar with the basics of that tutorial, and as such some parts of this tutorial are not addressed in great detail.

Goal of the Tutorial

The goal of the tutorial is to create a view displaying the number of users connected to a system. When the number of users drops to 0, the connection status should include a warning icon. Figure 1 shows the desired end result. The view is a TopComponent containing an OutlineView.

Figure 1: Completed Status Window

Step 1: Creating the Projects

Start off by creating a new NetBeans Platform Application Project, and adding a new empty module to it. All of the classes will be created in that module. The code name base I chose for my module is za.co.kitt.demo.nodesdemo – remember to modify the image locations in the code snippets to reflect your code name base.

Add dependencies on the following libraries to your module:

Explorer & Property Sheet API

Nodes API

Utilities API

Add these two icons to your module:

Step 2: Creating the API Object

The next step is to create an API object that will contain the data to be presented. The Nodes API is intended as a presentation layer only, and as such the nodes should not contain the actual data directly. The very simple StatusObject class is shown below.

This class can be mostly recreated by following the NetBeans Nodes API Tutorial, perhaps with the exception of the StatusProperty inner class. It is a very simple extension of the ReadWrite property, that for now only contains the getValue and setValue methods.

The node itself has an icon as returned by the getIcon() method (the # icon shown in Figure 1).

Step 4: Creating the Top Component

Create a new TopComponent in the module. When prompted for the window position, choose Explorer. For convenience, check the Open on Application Start check box.

Create a private field in the TopComponent to contain the status object:

private StatusObject mStatus = new StatusObject();

Change the TopComponent class to implement the interface ExplorerManager.Provider, and add the following to the class:

Up to this point, nothing new or noteworthy has been implemented, and the window should now appear as in Figure 2.

Figure 2: Status window with no value icon

Step 5: Adding an Unchanging Icon

The Customizing how Properties are displayed javadoc page lists all the custom parameters that may be set to instruct editors how to display the property. (I discovered this page only after debugging through the org.openide.explorer source code while in search of something else.)

to the constructor of the StatusProperty class to permanently set a value icon for the property. However, should we wish to remove the icon later,

setValue("valueIcon", null);

would cause a null pointer exception to be thrown, since the underlying collection does not allow null values. If we try

setValue("valueIcon", new ImageIcon());

an unwanted empty space is shown when editing the value, as shown in figure 3.

Figure 3: Unwanted Empty Space

Step 6: Changing the Value Icon Dynamically

To allow the icon to be changed according to the data of the StatusObject, simply add the following method override to the StatusProperty class:

/**
* Gets the value of the named attribute.
* @param AttributeName The name of the attribute.
* @return If the attribute is "valueIcon", the return value is either
* null if there are clients online, or an icon if no clients are
* online.
*/
@Override
public Object getValue(String AttributeName)
{
if ("valueIcon".equals(AttributeName))
{
if (mStatus.getNumberOfClientsOnline() == 0)
{
return ImageUtilities.loadImage(
"za/co/kitt/demo/nodesdemo/error_1.png", true);
}
else
{
return null;
}
}
return super.getValue(AttributeName);
}

Note that the setValue() call added in step 5 no longer has any effect and may be safely removed.

Conclusion

Dynamically changing value icons are a powerful way of giving the user feedback on the status of data objects that are displayed by nodes. By simply overriding the getValue(String) method of a property, icons may be changed with minimal effort.

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.