Download NetBeans!

20070423 Monday April 23, 2007

Adding Nodes to an Existing Project Type's Logical View

Since a few weeks, it's been possible to add new nodes to an existing NetBeans project type's logical view. Thanks to Milos Kleint, who created this functionality, I found out how to implement it. Take the steps below, as a kind of "hello world" experience.

  1. Create a new project called "foo" with code name base "org.netbeans.foo".

  2. In the layer, specify where you want the node to appear, i.e., which project you want to extend.

    <folder name="Projects">
         <folder name="org-netbeans-modules-java-j2seproject">
            <folder name="Nodes">
                <file name="org-netbeans-foo-NodeFactoryImpl.instance"/>
            </folder>
         </folder>
    </folder>

    Note that when you expand the Important Files node, then the XML Layer node, and then the "this layer in context" node, you can see a long list of new nodes under the "Projects" node, each representing a different project type. In 5.5, only the "Actions" node was shown under the "Projects" node.

    The above view is extremely useful, because without needing any documentation you can now immediately see the required names of the other project types for use within the XML layer. As always in this visual representation, our own changes, coming from our XML layer, are shown in bold. So, above it is clear we will be adding a new node to the org-netbeans-modules-java-j2seproject project type, which is the Java SE project type.

    Notice also that there are two other nodes, aside from "Nodes", one called "Customizer" and one called "Lookup". This means you can extend an existing project's customizer (Project Properties dialog box) and Lookup. Both will be discussed in future blog entries.

  3. Set source level to 1.5 and declare dependencies on Nodes API, Project API, Project UI API, and Utilities API.

    Create the class referred to above in the layer and fill it out as follows:

    public class NodeFactoryImpl implements NodeFactory {
    
        public NodeFactoryImpl() {
        }
    
        public NodeList createNodes(Project arg0) {
            
            AbstractNode nd = new AbstractNode(Children.LEAF);
            nd.setDisplayName("Hello World!");
            
            return NodeFactorySupport.fixedNodeList(nd);
         }
    
    }

    For details on the above, see NodeFactory in the NetBeans Javadoc.

    Install the module and see your new node:

  4. You want the node to appear somewhere else? Go back to the visual representation of the XML file and drag the node that you created in the XML layer somewhere else:

    Install the module again... and now the node has been moved:

It's as simple as that...

Apr 23 2007, 02:21:31 AM PDT Permalink