X

Geertjan's Blog

  • July 22, 2014

World's Simplest NetBeans Node/TopComponent Implementation

Geertjan Wielenga
Product Manager

In 2008, here, I documented the world's simplest NetBeans Node implementation. Here's the updated implementation, using ChildFactory, together with the code needed for connecting it into a TopComponent:

public final class ViewerTopComponent extends TopComponent implements ExplorerManager.Provider {
    private ExplorerManager em = new ExplorerManager();
    public ViewerTopComponent() {
        setLayout(new BorderLayout());
        add(new BeanTreeView(), BorderLayout.CENTER);
        Children kids = Children.create(new SystemPropertyChildFactory(), true);
        AbstractNode rootNode = new AbstractNode(kids);
        em.setRootContext(rootNode);
        associateLookup(ExplorerUtils.createLookup(em, getActionMap()));
    }
    @Override
    public ExplorerManager getExplorerManager() {
        return em;
    }
    class SystemPropertyChildFactory extends ChildFactory<Object> {
        @Override
        protected boolean createKeys(List<Object> list) {
            list.addAll(System.getProperties().keySet());
            return true;
        }
        @Override
        protected Node createNodeForKey(Object key) {
            BeanNode node = null;
            try {
                node = new BeanNode(key);
                node.setDisplayName(key.toString());
            } catch (IntrospectionException ex) {
                Exceptions.printStackTrace(ex);
            }
            return node;
        }
    }
}

Here are the class annotations for registering the above in the Window System:

@TopComponent.Description(
preferredID = "ViewerTopComponent",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(
mode = "explorer",
openAtStartup = true)
@ActionID(
category = "Window",
id = "org.demo.viewer.ViewerTopComponent")
@ActionReference(
path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
displayName = "#CTL_ViewerAction",
preferredID = "ViewerTopComponent"
)
@Messages({
"CTL_ViewerAction=Viewer",
"CTL_ViewerTopComponent=Viewer Window"
})

And here are the import statements:

import java.awt.BorderLayout;
import java.beans.IntrospectionException;
import java.util.List;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.BeanNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

Join the discussion

Comments ( 3 )
  • nkoding Thursday, September 11, 2014

    Hi Geertjan,

    i tried this code and get an java.lang.IllegalStateException when calling accociateLookup in the TopComponent's constructor.

    i'm using NetBeans Platform 8.

    Is this already known? i found some info on that error but no solutions. Could you please help?

    regards,

    nk


  • Geertjan Thursday, September 11, 2014

    It's not known because it's not true. Send your code in a ZIP file to geertjan dot wielenga at oracle dot com and I'll try to fix your code.


  • guest Thursday, September 11, 2014

    I found my mistake. I entered the wrong ID in the action registration annotation.

    thanks for your reply and efforts!

    regards

    nk


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services