Car Designer on the NetBeans Platform (Part 1)

Let's create a development environment for a car designer. The car designer needs to provide a Java editor, as well as some kind of drawing functionality. Each part of the car will be stored in a different XML file, e.g., "wheel.xml", "bumper.xml", etc. We imagine that each new car project will look as follows on disk:

Within our car designer, this is how the application will appear:

As you can see, no drawing functionality. The point of this blog entry, instead, is to show how to extend NetBeans IDE to provide the above project structure.

It's all pretty trivial. Extend the NetBeans Java project type as follows, i.e., note the class-level annotation, which is what will enable the extension to be registered, via a compile-time generation of META-INF/services:

package org.car.designer;

import org.netbeans.api.project.Project;
import org.netbeans.spi.project.ui.support.NodeFactory;
import org.netbeans.spi.project.ui.support.NodeFactorySupport;
import org.netbeans.spi.project.ui.support.NodeList;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.util.Exceptions;

@NodeFactory.Registration(projectType = "org-netbeans-modules-java-j2seproject")
public class CarNodeFactory implements NodeFactory {
    @Override
    public NodeList createNodes(Project project) {
        if (project.getProjectDirectory().getFileObject("car-artifacts") != null) {
            CarNode nd;
            try {
                nd = new CarNode(project);
                return NodeFactorySupport.fixedNodeList(nd);
            } catch (DataObjectNotFoundException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        return NodeFactorySupport.fixedNodeList();
    }
}

And here's the car node referred to above, i.e., this is the folder that contains the XML files:

package org.car.designer;

import java.awt.Image;
import org.netbeans.api.project.Project;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.nodes.FilterNode;
import org.openide.util.ImageUtilities;

public class CarNode extends FilterNode {

    private static Image smallImage =
            ImageUtilities.loadImage("/org/car/designer/resources/car8.png"); // NOI18N

    public CarNode(Project proj) throws DataObjectNotFoundException {
        super(DataObject.find(proj.getProjectDirectory().getFileObject("car-artifacts")).getNodeDelegate());
    }

    @Override
    public String getDisplayName() {
        return "Car";
    }

    @Override
    public Image getIcon(int type) {
        DataFolder root = DataFolder.findFolder(FileUtil.getConfigRoot());
        Image original = root.getNodeDelegate().getIcon(type);
        return ImageUtilities.mergeImages(original, smallImage, 7, 7);
    }

    @Override
    public Image getOpenedIcon(int type) {
        DataFolder root = DataFolder.findFolder(FileUtil.getConfigRoot());
        Image original = root.getNodeDelegate().getIcon(type);
        return ImageUtilities.mergeImages(original, smallImage, 7, 7);
    }
    
}

Now, for each XML file, i.e., "car.xml", "wheel.xml", etc, create a new file type via the File Type Integration Tutorial.

In the end, you'll end up with a project structure for your plugin something like this:

Next time, we'll look at how to open a car into a window, make some changes, and store those changes back into the files.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
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
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today