Open source Java projects: NetBeans plugins

Five plugins to enhance your NetBeans development experience

The NetBeans IDE is pretty good on its own, but even handier once you start extending it with plugins specific to your needs. In this installment of Open source Java projects, Jeff Friesen introduces you to NetBeans plugins that let you view the NetBeans filesystem, explore pictures on Windows systems, check code for standards compliance, add properties to Java classes, and make OpenGL development easier. All that is just a start, though, as you'll also learn how to create your own NetBeans plugin for accessing the NetBeans API Javadoc. Level: Intermediate.

Earlier this year, I used NetBeans IDE 6.1 with JavaFX (part of the Sun Microsystems JavaFX Preview SDK) to develop the examples for my Jump into JavaFX series on JavaWorld. While playing with the IDE, I encountered the topic of plugins -- modules that install themselves into NetBeans to extend it with new features such as a Google search toolbar.

This article introduces you to five of the many open source NetBeans plugins that you can download from the NetBeans Plugin Portal. You'll discover each plugin's purpose, its open source license, and the platforms that the plugin targets. You'll also briefly tour each plugin in NetBeans IDE 6.5, which I assume you've installed. Lastly, you will see how to create your own plugin for accessing the NetBeans API Javadoc.

At the end of this article you will have learned about five useful plugins. Moreover, you'll be familiar with the NetBeans plugin architecture and with the steps for creating your own plugins.

File System Displayer: View the NetBeans filesystem

The NetBeans system filesystem is the central repository for NetBeans configuration data. The File System Displayer plugin, created by NetBeans expert Geertjan Wielenga, lets you view the NetBeans system filesystem from within the IDE. This plugin is licensed under the Common Development and Distribution License (CDDL) and targets the Solaris, Linux, Mac OS X, and Windows platforms.

The module foundation

NetBeans is based on modules -- software components for providing applications or the IDE with various features. These components are distributed as NetBeans Module (NBM) files, which are JAR files containing sets of classes that provide the features, as well as manifest information for identifying these archives to NetBeans as modules.

Download the 1226158890776_org-demo-fsdisplayer.nbm module file by clicking the Download button on the File System Displayer detail page. Then start NetBeans 6.5 (if the IDE isn't already running), and complete the following steps to install this plugin into the IDE:

  1. Select Tools > Plugins from the main menu to open the Plugins dialog box.
  2. Switch to the Downloaded tab on the Plugins dialog box.
  3. Click the Add Plugins button.
  4. In the Add Plugins file chooser, navigate to the folder containing the downloaded 1226158890776_org-demo-fsdisplayer.nbm file. Select this file and click Open. The File System Displayer plugin will show up in the list of plugins to be installed.
  5. Click the Plugins dialog's Install button.
  6. On the resulting NetBeans IDE Install dialog box, click the Next button, followed by the Install button. You should see a Validation Warning dialog box stating that the plugin is unsigned. Skip this warning by clicking the dialog's Continue button.
  7. Once installation completes, click the Finish button on the NetBeans IDE Install dialog, followed by the Close button on the Plugins dialog.

After installation, NetBeans presents the plugin's FsDisplayer Window explorer window component, which displays the hierarchy of configuration data that appears in Figure 1.

FsDisplayer Window appears alongside the Projects, Files, and Services tabs.
Figure 1. FsDisplayer Window appears alongside the Projects, Files, and Services tabs. (Click to enlarge.)

At runtime, the filesystem is composed of information located in each module's XML-based layer file. A layer file defines folders and files to be merged into the system filesystem. Other modules can store files or objects in these folders; the defining module can scan each folder's contents and do whatever it likes with the contents.

PicturesExplorer: View images on Windows

Kunal Modi's PicturesExplorer plugin lets users of Windows XP (and presumably other Windows versions) view the contents of their My Pictures folder within NetBeans; you don't need to leave the IDE to open a picture viewer. This plugin is licensed under version 2 of the GNU General Public License (GPL).

Download PicturesExplorer's 1202697173670_org-modules-sack-picturesexplorer.nbm module file by clicking the Download button on the PicturesExplorer detail page. Then install this plugin via the same steps for installing File System Displayer. Once it's installed, select Pictures from the Window menu to display the Pictures Window with its My Pictures contents.

You can zoom into the currently displayed picture by clicking the zoom bar's plus (+) button and zoom out by clicking the minus (-) button, as shown in Figure 2.

Use the zoom bar to zoom into or out of a picture.
Figure 2. Use the zoom bar to zoom into or out of a picture. (Click to enlarge.)

Additional buttons let you restore the picture to its original size (1:1), select a zoom-out or zoom-in preset (1:3 through 7:1), select a custom zoom level (X:Y), and display a grid when zooming in (#).

Checkstyle 4.3 plugin: Check code for standards compliance/h2>

Checkstyle helps you write Java source code that obeys a given coding standard. This tool automates the task of checking code for standards compliance, relieving you of the tedium of manually performing this task. Checkstyle is available to NetBeans via Andrea Matsunaga's Checkstyle 4.3 plugin, which is licensed under GPL version 2. It targets the Solaris, Linux, Mac OS X, and Windows platforms.

Open source licenses

Each of the open source Java projects covered in this series is subject to a license, which you should understand before integrating the project with your own projects. In the case of this article, each NetBeans plugin has its own license.

From the Checkstyle 4.3 detail page, click the Download button to download the 42267_com-ammatsun-checkstyle4nb-4.3.1.nbm module file. Then install the plugin via the same steps you used for the previous two plugins. Once it's installed, select any Java source file and click the Checkstyle icon (the main toolbar's rightmost icon) to check this code, as shown in Figure 3.

Click the Checkstyle icon to check the style of the current Java source file.
Figure 3. Click the Checkstyle icon to check the style of the current Java source file. (Click to enlarge.)

Although Checkstyle defaults to Sun's Java code conventions, you can make Checkstyle support a different coding standard by selecting Tools > Options, and then selecting the appropriate Checkstyle configuration and properties files from the Options dialog box's Checkstyle tab. For information on developing new coding standards in Checkstyle, take a look at the Checkstyle documentation.

Add Property: Add properties to Java classes

Many developers expect Java SE 7 to introduce new language features, and one possibility is properties. If you perform a Google search for "java 7 property" you'll discover that this topic has been widely discussed, and that ideas abound about what form the syntax should take. While we wait to see what happens with properties support in Java SE 7, check out Sandip V. Chitale's Add Property plugin.

Add Property is licensed under the CDDL and targets the Solaris, Linux, Mac OS X, and Windows platforms. It simplifies adding properties to Java classes. Visit this plugin's detail page, and click the Download button to download the 1198476035439_org-netbeans-modules-java-addproperty.nbm module file. Follow the same steps as before to install this plugin.

Let's play with Add Property. Start the New Project wizard and introduce a new Java application project named Employee. Leave the project's package set to employee and the main class set to Main. The resulting skeletal application code is similar to that shown in Figure 3.

Next you'll introduce a name property for the employee's name. Select Source >Add Property, and then specify name in the Name field of the resulting Add Property dialog box. Figure 4 presents this dialog box, which reveals what the generated property's code looks like.

The property type defaults to String.
Figure 4. The property type defaults to String. (Click to enlarge.)

Click OK to insert the code at the caret position. If the caret position is located outside of the class, the resulting code won't compile. In that case, you'll need to undo the insert via Edit > Undo, reposition the caret, launch the Add Property dialog box, and click its OK button. This dialog box remembers the previous settings.

Add Property lets you perform a wide range of customizations. For example, you can create a bound property. If you require a special customization that's not supported (perhaps you wish to prefix your property declarations with a certain annotation), you'll need to modify the boilerplate template. Follow these steps to access this template:

  1. Select Tools > Templates.
  2. In the resulting Template Manager dialog box, select and expand the org.netbeans.modules.java.addproperty template category folder.
  3. Select the AddProperty.freemarker template under this folder, and click the Open in Editor button.

Figure 5 shows a portion of the template.

The template includes special ${identifier} placeholders and HTML-like <#if> tag blocks.
Figure 5. The template includes special ${identifier} placeholders and HTML-like <#if> tag blocks. (Click to enlarge.)

Figure 5 reveals various ${identifier} placeholders, where identifier names a special variable. Each placeholder's value is inserted into the generated code. For example, the String value in the Type field of Figure 4's dialog box would be inserted into the generated code via the ${type} placeholder.

Figure 5 also reveals various <#if identifier> tags, where identifier names a special variable. The variable's value is used in a Boolean expression to determine if the code between <#if identifier> and </#if> should be inserted into the generated code. For example, <#if static >static </#if> inserts static into the generated code if the static checkbox is selected.

Although Figure 5's template is fairly easy to follow, here are a few more examples to facilitate your understanding:

  • ${access} inserts the selected private, protected, or public radio button's name into the generated code. Nothing is inserted if package is selected.
  • The following example inserts Javadoc into the generated code whenever the Generate javadoc checkbox is clicked. Each of the two occurrences of ${name} within the Javadoc is expanded to the name of the property (as entered into the Name field):
    <#if generateJavadoc >
        /**
         * Get the value of ${name}
         *
         * @return the value of ${name}
         */
    </#if>
    
  • return <#if !static >this.</#if>${name}; inserts the this. prefix (if the static checkbox is not selected), followed by the property's name and a semicolon, into the generated code.

NetBeans OpenGL Pack: Ease OpenGL development

My previous article in this series, "Open source Java projects: Java Binding for OpenGL (JOGL)," introduced 3D graphics by way of Sun's JOGL technology. While writing that article, I developed my JOGL demos at the command line. If I had known about the NetBeans OpenGL Pack plugin, I would have developed them in NetBeans instead.

Michael Bien introduced NetBeans OpenGL Pack to provide an easy-to-use OpenGL development environment that's integrated into NetBeans. This plugin provides several modules, such as an OpenGL Shading Language (GLSL) shader editor and an OpenGL capabilities viewer, It also includes ready-to-run JOGL demos and all OpenGL samples from the OpenGL Programming Guide (also known as the Red Book). NetBeans OpenGL Pack is licensed under the GPLv2 with Classpath Exception license. It targets the Solaris, Linux, Mac OS X, and Windows platforms.

Download NetBeans OpenGL Pack's 227048417356_netbeans-opengl-pack_0.5.3.zip archive by clicking the Download button on the NetBeans OpenGL Pack detail page. After unzipping this archive, you'll discover several NBM files. You must select all these modules before installing them, as shown in Figure 6.

Click the Install button to start installing all of the selected modules.
Figure 6. Click the Install button to start installing all of the selected modules. (Click to enlarge.)

After installing NetBeans OpenGL Pack, you can immediately try out any of the OpenGL demos in NetBeans. For example, start the New Project wizard and (as shown in Figure 7) expand the OpenGL category folder, select the Demos subcategory folder, and select the JOGL Hardware Shadow Mapping Demo project.

Select a JOGL demo and then click the Next button.
Figure 7. Select a JOGL demo and then click the Next button. (Click to enlarge.)

After you click Next and (on the subsequent panel) Finish, NetBeans creates a new project based on the selected demo. As Figure 8 reveals, you can view and edit the demo's source code, and compile and run the demo (by clicking the green triangle button in the main tool bar, or by pressing the F6 function key).

You can easily edit, compile, and run JOGL demos in NetBeans.
Figure 8. You can easily edit, compile, and run JOGL demos in NetBeans. (Click to enlarge.)

Roll your own plugins

After trying out the previously discussed NetBeans plugins and others that you download from the NetBeans Plugins Portal, you'll probably want to create your own. If you're new to this task, you might find it somewhat intimidating. Fortunately, NetBeans greatly simplifies plugin development.

To prove this to you, I'll show you how to create a simple plugin. In response to a menu selection (see Figure 9), the plugin launches a Web browser and has it display the starting page of the NetBeans API documentation. Quick access to this documentation will be helpful when you're ready to create more-sophisticated plugins that take advantage of the various NetBeans APIs.

Select Help > Show NetBeans API Documentation to access the Javadoc for the NetBeans APIs quickly.
Figure 9. Select Help > Show NetBeans API Documentation to access the Javadoc for the NetBeans APIs quickly. (Click to enlarge.)

To create this plugin, you first need to create and configure a new NetBeans project:

  1. Start NetBeans IDE 6.5 if it's not already running.
  2. Select File > New Project. This launches the New Project dialog box.
  3. On the dialog's Choose Project panel, select NetBeans Modules from the Categories list, and Module from the Projects list. Click Next.
  4. On the subsequent Name and Location panel, enter NetBeansAPIDoc into the Project Name text field. Make sure that the Standalone Module radio button is selected. Click Next.
  5. On the final Basic Module Configuration panel, enter org.myorg.netbeansapidoc into the Code Name Base text field. Check the Generate XML Layer checkbox, and click the Finish button.

Next you'll create an action that launches the Web browser, and the menu item that invokes the action:

  1. In the Projects window, right-click the NetBeansAPIDoc project name.
  2. On the resulting pop-up menu, select New > Action. This launches the New Action dialog box.
  3. On the dialog's Action Type panel, make sure that the Always Enabled (Use ActionListener) radio button is selected. Click Next.
  4. On the subsequent GUI Registration panel:
    1. Select Help as the category in the Category combo box.
    2. Make sure that the Global Menu Item checkbox is checked.
    3. Select Help as the menu in the Menu drop-down listbox.
    4. Select "Javadoc References - HERE - <separator>" as the position in the Position drop-down listbox.
    5. Make sure that the Global Toolbar Button and Global Keyboard Shortcut checkboxes are unchecked.
    6. Click Next.
  5. On the final Name, Icon, and Location panel, enter ShowNetBeansAPIDoc into the Class Name text field, enter Show NetBeans API Documentation into the Display Name text field, and click the Finish button. In response, NetBeans creates a ShowNetBeansAPIDoc.java source file whose skeletal contents appear on a new tab, as shown in Figure 10.
The <code>actionPerformed()</code> method is invoked when the Show NetBeans API Documentation menu item (shown in Figure 9) is selected.
Figure 10. The actionPerformed() method is invoked when the Show NetBeans API Documentation menu item is selected. (Click to enlarge.)

To finish the plugin, you need to tell actionPerformed() to launch the browser, and have it display the NetBeans API documentation starting page. Accomplish this task by replacing the // TODO implement action body with the following code fragment:

try
{
    URLDisplayer.getDefault()
                .showURL(new URL("http://bits.netbeans.org/dev/javadoc/"));
}
catch (Exception ex)
{
    return;
}

After you make this replacement, you'll discover that NetBeans cannot find the URLDisplayer and URL classes. It reports this problem by underlining the affected class names in red wavy lines. Complete the following steps to correct this problem:

  1. Right-click NetBeansAPIDoc in the Projects window.
  2. Select the Properties menu item from the resulting popup menu. This launches the Project Properties dialog box.
  3. Select Libraries from the Categories list.
  4. Click the Add button associated with the Module Dependencies list. This launches the Add Module Dependency dialog box.
  5. In the Filter text field, enter URLDisplayer. You might notice that the Module list's selection of returned modules narrows until the only remaining entry is UI Utilities API. Click the OK button.
  6. Click OK to close Project Properties.
  7. Right-click anywhere in ShowNetBeansAPIDoc.java and select Fix Imports from the pop-up menu. This launches the Fix All Imports dialog box.
  8. Click OK. The IDE introduces import java.net.URL; and import org.openide.awt.HtmlBrowser.URLDisplayer; statements into ShowNetBeansAPIDoc.java.

Sharing plugins

You can share a plugin with other developers by creating an NBM file. For example, create an org-myorg-netbeansapidoc.nbm file to distribute this section's plugin by right-clicking the NetBeansAPIDoc project name (in the Projects window) and selecting Create NBM from the popup menu. On my Windows XP platform, C:\Documents and Settings\Jeff Friesen\My Documents\NetBeansProjects\NetBeansAPIDoc\build contains the org-myorg-netbeansapidoc.nbm file.

Because there shouldn't be any further error indicators in ShowNetBeansAPIDoc.java, click the green triangle button on the main toolbar (or press F6). This starts another NetBeans copy to protect the current IDE from a buggy plugin. After you select Show NetBeans API Documentation from this copy's Help menu, your browser should take you to the starting page of the NetBeans API Javadoc.

In conclusion

You can increase your NetBeans productivity by taking advantage of the plugins offered by the NetBeans Plugin Portal. The handful of plugins I briefly introduced in this article offer a tiny glimpse into what's available. If you don't find a plugin that meets your needs, I hope this article helps you get started creating one that does -- and don't forget to share your plugin with the rest of us!

Jeff Friesen is a freelance software developer and educator who specializes in Java technology. Check out his javajeff.mb.ca website to discover all of his published Java articles and more. You might also want to check out his book Beginning Java SE 6 Platform: From Novice to Professional (Apress, 2007), which explores the new features introduced in Java SE 6.

Learn more about this topic

Join the discussion
Be the first to comment on this article. Our Commenting Policies