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

Sources of Simple Customer Editor Application on the NetBeans Platform

04.26.2010
| 5209 views |
  • submit to reddit

At the start of the 2nd day of the NetBeans Platform Certified Training, we normally do a workshop that combines many of the topics from the 1st day into a single example application. The aim of the workshop is to (1) create a very simple application that (2) makes use of the standard NetBeans APIs that apply to the scenario.

The nice thing is that the application starts from a business object ("Customer") and builds onwards from there. That makes it a realistic scenario since every serious application starts off with some POJOs that you need to visualize in some way. How to do so on the NetBeans Platform? That's what the workshop is about. At the end of the workshop, the students have wrapped the Customer POJO into a Node and displayed it in an explorer view that is synchronized with the Properties window, as shown below:

I extended the sample slightly from where it was at the end of the training in Krakow, Poland. Now, you can change a property in the Properties window above, which will cause the display name to change (if the "Name" property is changed) or the tooltip to change (if the "City" property is changed). That is possible thanks to the following Node I created:

import java.lang.reflect.InvocationTargetException;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.PropertySupport;
import org.openide.nodes.Sheet;
import org.openide.nodes.Sheet.Set;
import org.openide.util.lookup.Lookups;
import pl.edu.customer.api.Customer;

public class CustomerNode extends AbstractNode {

public CustomerNode(Customer c) {
super(Children.LEAF, Lookups.singleton(c));
setDisplayName(c.getName());
setShortDescription(c.getCity());
}

@Override
protected Sheet createSheet() {
Sheet sheet = Sheet.createDefault();
Set set = Sheet.createPropertiesSet();
set.put(new NameProperty(getLookup().lookup(Customer.class)));
set.put(new CityProperty(getLookup().lookup(Customer.class)));
sheet.put(set);
return sheet;
}

@Override
public String getDisplayName() {
Customer c = getLookup().lookup(Customer.class);
if (null != c.getName()) {
return c.getName();
}
return super.getDisplayName();
}

@Override
public String getShortDescription() {
Customer c = getLookup().lookup(Customer.class);
if (null != c.getCity()) {
return c.getCity();
}
return super.getDisplayName();
}

private class NameProperty extends PropertySupport.ReadWrite {

private final Customer c;

private NameProperty(Customer c) {
super("Name", String.class, "Name", "Description of Name");
this.c = c;
}

@Override
public String getValue() throws IllegalAccessException, InvocationTargetException {
return c.getName();
}

@Override
public void setValue(String t) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String oldDisplayName = c.getName();
c.setName(t);
fireDisplayNameChange(oldDisplayName, t);
}
}

private class CityProperty extends PropertySupport.ReadWrite {

private final Customer c;

private CityProperty(Customer c) {
super("City", String.class, "City", "Description of City");
this.c = c;
}

@Override
public String getValue() throws IllegalAccessException, InvocationTargetException {
return c.getCity();
}

@Override
public void setValue(String t) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String oldDisplayName = c.getCity();
c.setCity(t);
fireShortDescriptionChange(oldDisplayName, t);
}

}

}

If you want the whole sample, i.e., all the source code for the above, simply go here and download the NBM file you find there (then install it into the IDE and you will have a new sample, as shown on the page below):

http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=28000

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