Skip to main content

A simple NetBeans module, written in NB 5.0

Posted by richunger on September 22, 2005 at 5:14 PM PDT

As promised, here's a recap of a simple module I built to help me test a bugfix.

There is a feature in NetBeans which allows any FileObject (including files in your project) to be served up by an embedded HTTP server. It is exposed by the API method URLMapper.findURL(FileObject, URL_TYPE). URL_TYPE may be one of three values:

  • INTERNAL: results in nbres://foo
  • EXTERNAL: results in file:/foo
  • NETWORK: results in http://foo

I noticed that if I passed NETWORK into the function, I was getting back a file URL instead of an HTTP URL. I decided to create a simple module to help me debug the problem.

The module would add an Action to the context menu of HTML files which would perform an URLMapper.findURL() and display the result in a dialog box. What surprised me, as a long-time module developer on previous versions of NetBeans, was how easy it was in 5.0....


  1. "File ... New Project". Select "Module Project".

    1.gif

  2. Give the project a name and location.

    2.gif

  3. Edit the "Code Name Base" to something that makes sense, and click "Finish".

    3.gif

  4. Now I want to add a new Action. So, from "File ... New File", select the appropriate template.

    4.gif

  5. In the next screen, I want to conditionally enable this Action, so that I end up with a CookieAction that is resposive to DataObject.class.
    5.gif

  6. The Category isn't particularly important. That controls where this item shows up in the Keyboard Shortcuts editor. The important thing to change here is to uncheck "Global Menu Item" and check "File Type Context Menu Item."
    6.gif

  7. Give the class a name and a display name, and click "Finish".

    7.gif

There. That's almost everything. Now all I have to do is implement the performAction() method. The implementation will look like this:

        DataObject dobj = (DataObject)activatedNodes[0].getCookie(DataObject.class);
        if (dobj != null)
        {
            URL url = URLMapper.findURL(dobj.getPrimaryFile(), URLMapper.NETWORK);
            NotifyDescriptor nd = new NotifyDescriptor.Message("URL: " + url);
            DialogDisplayer.getDefault().notify(nd);
        }

As I type this in, certain classes will not be found, even after a "Fix Imports" (Shift-Alt-F). The unknown classes are:

  • NotifyDescriptor
  • DialogDisplayer
  • URLMappper

Now, if I right-click the URLMapperTest project node, and select "Properties", there's a section for "Libraries"

8.gif

When I click "Add...", I'm presented with a new dialog that lets me search for these classes.

9.gif

In this way, I can add dependencies on the appropriate modules (Dialogs API and File System API). Once again, I can perform "Fix Imports" in my source window, and I'm done coding!

Now, when I run my project, and browse to an HTML file, I can right-click that file and select "Show URL"

10.gif

...and the result is this:

11.gif

Of course, that's the result before I fixed the bug! With my patched version, the URL starts with "http://"


Now, I don't know about you, but I was very impressed by how easy this was. I know how to do it the hard way, because I've been writing NetBeans modules for years. I'm very envious of the folks who are just starting now :-)

Related Topics >>