X

Geertjan's Blog

  • May 31, 2006

One day you'll wake up and...

Geertjan Wielenga
Product Manager
...find that you're in the middle of writing some very complex Java code... in... an... HTML file. Either you didn't sleep much or there's some other reason. Wouldn't it be cool if you could, in this admittedly bizarre corner case scenario, change the extension from ".html" to ".java" inside the IDE? Well, now you can...

This is the performAction method in a CookieAction class that is registered in the layer.xml file under Editors/Popup (so that all files can change their extensions to any file extension, which is not something that is useful in all cases, and very damaging in many):

protected void performAction(Node[] activatedNodes) {
DataObject c = (DataObject) activatedNodes[0].getCookie(DataObject.class);
FileObject fo = c.getPrimaryFile();
String foNameString = fo.getName();
String foExtensionString = fo.getExt();
String newext = JOptionPane.showInputDialog(null,"Change extension to: ");
if (newext != null) {
FileLock fileLock = null;
try {
fileLock = fo.lock();fo.rename(fileLock, foNameString, newext);
} catch (IOException ex) {
ErrorManager.getDefault().notify(ex);
} finally{
if (fileLock != null)
fileLock.releaseLock();
}
}
}

The line that does all the work is in bold above. Theoretically, if this were a useful action (rather than a potentially dangerous one), I would have taken the time to try to implement this within the refactoring framework. But, since this doesn't do anything other than change the extension, which you almost never would want to do (but for which there are probably uses cases anyhow), I just created it in the simplest way possible.

Join the discussion

Comments ( 3 )
  • Geertjan Wednesday, May 31, 2006
    "If you build it, they will come," a famous man (in a movie) once said. Have a look at John's blog today if you want a perfect use case:

    http://blogs.sun.com/roller/page/johnc?entry=xhtml_facelets_and_netbeans_ide

  • Martin Thursday, February 21, 2008

    Thanks Geertjan, this exactly what I was looking for. I have a file type that I'd like to toggle between extensions / mimetype so that different modules can work on the file.

    The issue I have is after changing the file extension. Netbeans is not seeing the file as it's new filetype. Until I restart NB.

    Is there a way to force a refresh or fire an event of some sort?

    BTW: NB 5.5.1


  • Martin Thursday, February 21, 2008

    Think I got it working...

    Just need to call the setValid(false) on the dataobject and all is good.


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