org.openide.explorer.propertysheet.PropertyPanel

Let's use the org.openide.explorer.propertysheet.PropertyPanel component, a generic GUI component for displaying and editing a JavaBeans property:

The window in the lower left above uses the PropertyPanel, in fact all 3 components you see there are PropertyPanels. (So does the Properties window that you see above, but that's a predefined component that uses the PropertyPanel, while in this blog we want to create a custom component that uses PropertyPanel too.) The PropertyPanel is automatically synchronized with the other two windows you see above.

Start by creating a domain object, e.g., "Car", with properties "type" and "year". Make sure to provide PropertyChangeSupport, with firing of property changes in the setters.

Define your Node as follows:

public class CarNode extends BeanNode implements PropertyChangeListener {

    public CarNode(Car bean) throws IntrospectionException {
        super(bean, Children.LEAF, Lookups.singleton(bean));
        setDisplayName(bean.getType());
        addPropertyChangeListener(this);
    }

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

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
       if (evt.getPropertyName().equals("type")){
           String oldDisplayName = evt.getOldValue().toString();
           String newDisplayName = evt.getNewValue().toString();
           fireDisplayNameChange(oldDisplayName, newDisplayName);
       }
    }
    
}

Display the Node above in a BeanTreeView within a TopComponent in the "explorer" mode of the application. Notice that it listens for property changes on the "type" property and then fires a change of the display name.

In the TopComponent on the lower left above, i.e., the one containing the PropertyPanels, set a LookupListener that is sensitive to BeanNodes and then implement the methods below:

private GridBagConstraints gbc = new GridBagConstraints();
private Lookup.Result<BeanNode> result = null;

@Override
public void resultChanged(LookupEvent le) {
    Collection<? extends BeanNode> nodes = result.allInstances();
    if (!nodes.isEmpty()) {
        for (Node node : nodes) {
            PropertySet[] sets = node.getPropertySets();
            for (PropertySet propertySet : sets) {
                Property[] props = propertySet.getProperties();
                removeAll();
                for (Property property : props) {
                    gbc.gridwidth = GridBagConstraints.REMAINDER;
                    gbc.fill = GridBagConstraints.BOTH;
                    property.setValue("suppressCustomEditor", true);
                    PropertyPanel cpp = new PropertyPanel(property);
                    add(cpp, gbc);
                    open();
                    requestActive();
                }
            }
        }
    }
}

@Override
public void componentOpened() {
    result = Utilities.actionsGlobalContext().lookupResult(BeanNode.class);
    result.addLookupListener(this);
    resultChanged(new LookupEvent(result));
}

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

That's all. Now you have created a PropertyPanel for each property retrieved from the current BeanNode.

Comments:

how to add a pop-up menu in the property sheet? Thanks

Posted by guest on November 26, 2013 at 11:28 AM PST #

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