X

Geertjan's Blog

Car Designer on the NetBeans Platform (Part 1)

Geertjan Wielenga
Product Manager
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.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.