X

Geertjan's Blog

  • December 26, 2011

NetBeans Property Editors for java.io.File and String Arrays

Geertjan Wielenga
Product Manager

I drag and drop a few PropertyPanels (which you can get into your palette by right-clicking inside it, choosing 'Palette Manager', then 'Add from JAR', and then browsing to 'platform/modules/org-openide-explorer.jar' in the NetBeans IDE installation directory) onto a TopComponent...

I then run the application and when I switch from one node to another, the property panels automatically update themselves, in the same way as the Properties Window:

The small "..." buttons to the right of each of the property panels are created automatically. The first two, above, are not so interesting, because the property in both cases is of type String. However, the property of the third property panel is java.io.File. For that reason, when you click on the small button... a file chooser opens! And, since the type of the property bound to the fourth property panel is a String Array, this is what you see when you click on the last button above:


I.e., the NetBeans Platform has a predefined property editor for java.io.File and for String[].

Try it yourself. Create a Person object with this content:

public class Person {
String name;
String city;
File photo;
String[] courses;
public Person(String name, String city, File file, String[] courses) {
this.name = name;
this.city = city;
this.photo = file;
this.courses = courses;
}
public String[] getCourses() {return courses;}
public void setCourses(String[] courses) {this.courses = courses;}
public File getPhoto() {return photo;}
public void setPhoto(File photo) {this.photo = photo;}
public String getCity() {return city;}
public void setCity(String city) {this.city = city;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}

Now create a node hierarchy, using BeanNode, and display the node hierarchy in a BeanTreeView, with multiple tutorials that can help you on the NetBeans Platform Learning Trail. Finally, create a TopComponent like the one shown in the first screenshot above, then use a LookupListener to listen for Node classes, and do the following with Nodes:

@Override
public void resultChanged(LookupEvent ev) {
if (personResult.allInstances().iterator().hasNext()) {
Node personNode = personResult.allInstances().iterator().next();
Property<?>[] properties = personNode.getPropertySets()[0].getProperties();
for (Property prop : properties) {
if (prop.getName().equals("name")) {
prop.setValue("oneline", Boolean.TRUE);
namePropertyPanel.setProperty(prop);
} else if (prop.getName().equals("city")) {
cityPropertyPanel.setProperty(prop);
} else if (prop.getName().equals("photo")) {
prop.setValue("directories", Boolean.FALSE);
photoPropertyPanel.setProperty(prop);
} else if (prop.getName().equals("courses")) {
coursesPropertyPanel.setProperty(prop);
}
}
}
}
@Override
public void componentOpened() {
personResult = Utilities.actionsGlobalContext().lookupResult(Node.class);
personResult.addLookupListener(this);
}
@Override
public void componentClosed() {
personResult.removeLookupListener(this);
}

Now whenever a new Node is in the Lookup (you could also check whether the Node has a Person in the Lookup, which would make it the Node that you're interested in) the relevant property is bound to a property panel. Depending on what the property is, the NetBeans Platform automatically assigns one of the default property editors, for the properties that it supports.

For further info: Customizing How Properties Are Displayed

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.