The Source for Java Technology Collaboration
User: Password:



Rich Unger's Blog

September 2005 Archives


A simple NetBeans module, written in NB 5.0

Posted by richunger on September 22, 2005 at 05:14 PM | Permalink | Comments (1)

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 :-)

Real, supported, module development has arrived

Posted by richunger on September 12, 2005 at 12:57 PM | Permalink | Comments (3)

Other folks have been blogging about the new support for NetBeans module development in the 5.0 stream. However, as the guy who put together the "cluster build harness" for (unofficially) supporting module development in 4.1, I just thought I'd chime in with my own words of praise.

If I were starting a project based on the netbeans platform now, I would not use my cluster build harness. I'd use 5.0 builds, and I'd begin with Geertjan's (most finished) tutorial.

The new UI for module development is very effective. How effective? Well, when I get a few moments I'll write another entry about how I was able to create a module in about 5 minutes to test my fix for http://www.netbeans.org/issues/show_bug.cgi?id=64012

It would have been a lot more work with the cluster build harness!

I prefer my JARs sunny-side up: redux

Posted by richunger on September 05, 2005 at 02:16 PM | Permalink | Comments (3)

In a previous entry, I vented my frustration at scrambled JARs in the netbeans build process.

Well, they're gone.

I'd like to think I had some small part to play in that. I probably didn't, but I'll go on thinking I did :)



Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds