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.
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....
- "File ... New Project". Select "Module Project".
- Give the project a name and location.
- Edit the "Code Name Base" to something that makes sense, and click "Finish".
- Now I want to add a new Action. So, from "File ... New File", select the appropriate template.
- 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.
- 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."
- Give the class a name and a display name, and click "Finish".
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.getCookie(DataObject.class);
if (dobj != null)
URL url = URLMapper.findURL(dobj.getPrimaryFile(), URLMapper.NETWORK);
NotifyDescriptor nd = new NotifyDescriptor.Message("URL: " + url);
As I type this in, certain classes will not be found, even after a "Fix Imports" (Shift-Alt-F). The unknown classes are:
Now, if I right-click the URLMapperTest project node, and select "Properties", there's a section for "Libraries"
When I click "Add...", I'm presented with a new dialog that lets me search for these classes.
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"
...and the result is this:
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 :-)