Maven Integrated View for NetBeans IDE

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Sat, 3 Nov 2012 10:09:19 +0000 Indexed on 2012/11/03 11:11 UTC
Read the original article Hit count: 365

Filed under:

Started working on an oft-heard request from Kirk Pepperdine for an integrated view for multimodule builds for Maven projects in NetBeans IDE, as explained here. I suddenly had some kind of brainwave and solved all the remaining problems I had, by delegating to the LogicalViewProvider's node, instead of the project's node, which means I inherit all the icons, actions, package nodes, and anything else that was originally defined within the original project, in this case for the open source JAnnocessor project:

Above, you can see that the Maven submodules can either be edited in-line, i.e., within the parent project, or separately, by opening them in the traditional NetBeans way.

Get the module here:

http://plugins.netbeans.org/plugin/45180/?show=true

Some people out there might be interested in how this is achieved. First, hide the original ModulesNodeFactory in the layer. Then create the following class, which creates what you see in the screenshot above:

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.ChangeListener;
import org.netbeans.api.project.Project;
import org.netbeans.spi.project.SubprojectProvider;
import org.netbeans.spi.project.ui.LogicalViewProvider;
import org.netbeans.spi.project.ui.support.NodeFactory;
import org.netbeans.spi.project.ui.support.NodeList;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;

@NodeFactory.Registration(projectType = "org-netbeans-modules-maven", position = 400)
public class ModulesNodeFactory2 implements NodeFactory {

    @Override
    public NodeList<?> createNodes(Project prjct) {
        return new MavenModulesNodeList(prjct);
    }

    private class MavenModulesNodeList implements NodeList<Project> {

        private final Project project;

        public MavenModulesNodeList(Project prjct) {
            this.project = prjct;
        }

        @Override
        public List<Project> keys() {
            return new ArrayList<Project>(
                    project.getLookup().
                    lookup(SubprojectProvider.class).getSubprojects());
        }

        @Override
        public Node node(final Project project) {
            Node node = project.getLookup().lookup(LogicalViewProvider.class).createLogicalView();
            return new FilterNode(node, new FilterNode.Children(node));
        }

        @Override
        public void addChangeListener(ChangeListener cl) {
        }

        @Override
        public void removeChangeListener(ChangeListener cl) {
        }

        @Override
        public void addNotify() {
        }

        @Override
        public void removeNotify() {
        }
        
    }
    
}

Considering that there's only about 5 actual statements above, it's pretty amazing how much can be achieved with so little code. The NetBeans APIs really are very cool.

Hope you like it, Kirk!

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE