Tuesday Jul 22, 2014

World's Simplest NetBeans Node/TopComponent Implementation

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;
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
« July 2014 »
SunMonTueWedThuFriSat
  
13
17
19
20
  
       
Today