Search code examples
javanetbeansnetbeans-platform

BeanTreeView: how to display the properties of the selected tree item in the native "Properties" window?


I need to use BeanTreeView for my project, so I practiced using this component in this guide: Tutorial

Here is the complete source code: Source code

I would like to display the properties of the selected tree item (Event) in the native "Properties" window, which this line of code should provide: associateLookup(ExplorerUtils.createLookup(mgr, getActionMap()));

Unfortunately, the properties of the selected tree item are not displayed in "Properties" window, why?


Solution

  • There's another tutorial that describes what you need:

    https://netbeans.apache.org/tutorial/main/tutorials/nbm-nodesapi2/#_creating_a_node_subclass

    and

    https://netbeans.apache.org/tutorial/main/tutorials/nbm-nodesapi2/#_properties_and_the_property_sheet

    You basically have to make a few changes in the "My Editor" project.

    Introduce a new class EventNode:

    package org.myorg.myeditor;
    
    import java.time.ZonedDateTime;
    import org.myorg.myapi.Event;
    import org.openide.ErrorManager;
    import org.openide.nodes.AbstractNode;
    import org.openide.nodes.Children;
    import org.openide.nodes.PropertySupport;
    import org.openide.nodes.Sheet;
    import org.openide.util.lookup.Lookups;
    
    public class EventNode extends AbstractNode {
    
        public EventNode(Event obj) {
            super(Children.create(new EventChildFactory(), true), Lookups.singleton(obj));
            setDisplayName("Event " + obj.getIndex());
        }
    
        public EventNode() {
            super(Children.create(new EventChildFactory(), true));
            setDisplayName("Root");
        }
    
        @Override
        protected Sheet createSheet() {
            Sheet sheet = Sheet.createDefault();
            Sheet.Set set = Sheet.createPropertiesSet();
            Event obj = getLookup().lookup(Event.class);
    
            try {
                Property indexProp = new PropertySupport.Reflection(obj, Integer.class, "getIndex", null);
                Property dateProp = new PropertySupport.Reflection(obj, ZonedDateTime.class, "getDate", null);
    
                indexProp.setName("index");
                dateProp.setName("date");
    
                set.put(indexProp);
                set.put(dateProp);
            } catch (NoSuchMethodException ex) {
                ErrorManager.getDefault();
            }
    
            sheet.put(set);
            return sheet;
        }
    }
    

    Change the constructor of MyEditor:

    public MyEditor() {
        initComponents();
        Event obj = new Event();
        associateLookup(ExplorerUtils.createLookup(mgr, getActionMap()));
    
        setLayout(new BorderLayout());
        add(new BeanTreeView(), BorderLayout.CENTER);
    
        setDisplayName("MyEditor " + obj.getIndex());
    //        mgr.setRootContext(new AbstractNode(Children.create(new EventChildFactory(), true))); // remove this line
        mgr.setRootContext(new EventNode());    // and add this one instead
    }
    

    And in EventChildFactory alter the createNodeForKey method:

    @Override
    protected Node createNodeForKey(Event key) {
        return new EventNode(key);
    }