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

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

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today