Archive

Archive for January, 2011

Code Complete 2nd Edition e-book – $20

January 12, 2011 Leave a comment

Hi all,

I’ve slowly been transitioning all of my tech book purchases to e-book formats, particularly due to O’Reilly’s daily e-book deal tweets (follow @oreilly if interested; they seemed to be posted less frequently recently, but there are some good deals once in awhile).

Anyways, today’s deal of the day is Code Complete 2nd Edition, which is $20 with discount code DDC2E. I got this as a paperback for Christmas a few years ago and literally read it cover to cover in a week or so. It’s great stuff, and a must read for any programmer. I’m half tempted to buy it again, as it’s a lot more convenient to have a digital version than a 1.5 inch thick tome.

Advertisements

Reset windowing system in NetBeans Platform

January 5, 2011 3 comments
One of the benefits of building programs on top of NetBeans Platform is the powerful windowing and docking framework it provides out of the box.  Part of this functionality is persistence; when a user closes a NetBeans Platform application, the state of all the windows is saved.  The next time the application is started, the windowing state is restored.  This is why the NetBeans IDE (which is built on the NetBeans Platform) remembers which files you had loaded and how you had your windows arranged.
During development, it is sometimes beneficial to do a ‘factory reset’ and reset the windows back to the way they would look when the application is launched for the first time. For instance, you may have inadvertently closed some windows you wanted to have open, or you opened some windows that you wanted closed and don’t feel like manually restoring each window’s open/closed state. Or perhaps you moved some of the windows in a position that made sense for testing part of the application, but now you want to test another part that requires the original windowing setup.

Clean and build

There are two main ways to do this.  The easiest way is to do a full clean and build of the project (right click on the top level project node in NetBeans and choose Clean and build)

This works fine, but it has the drawback of.. cleaning and rebuilding the project.  This can take a long time for complex projects, and is a bit overkill if all you want to do is reset the windowing system.

Remove Windows2Local

The second way of accomplishing this is to remove a folder named “Windows2Local”.  If the root of your NetBeans Platform project is located at /path/to/Foo, then the folder to remove is /path/to/Foo/build/testuserdir/config/Windows2Local
You can delete the folder any way you see fit; I usually do a
rm -rf /path/to/Foo/build/testuserdir/config/Windows2Local
from the terminal.
This folder contains the saved information about the window placement and sizes; by removing it, you force NetBeans to recreate it when the application restarts.
This method is much faster, as nothing needs to be recompiled.  It is my preferred way of resetting the windowing system back to its default state when developing NetBeans Platform applications.

NetBeans Platform – ModuleInstall ClassNotFoundException

January 3, 2011 Leave a comment

In the NetBeans Platform, you can create an ModuleInstall class which handles the lifecycle of your module, and provides methods you can override to handle when your module is loaded and unloaded.  The standard way of creating this class is to use the wizard that NetBeans provides for this purpose, accessible by right clicking on the project and choosing New -> Other -> Module Development -> Module Installer

If you decide that you do not want to use the ModuleInstall mechanism any longer (the NetBeans Platform folks suggest you do NOT use it, as it will increase the startup time of the application), you might think you can just delete the Installer.java file that the wizard created.  Unfortunately, that’s not the case.  When you run the project you’ll get an exception like the following

org.netbeans.InvalidException:  
StandardModule:net.developmentality.moduleexample jarFile: ...
  java.lang.ClassNotFoundException:  net.developmentality.moduleexample.Installer starting from  ModuleCL@482...

The problem is that the wizard modified the manifest.mf file as well, and you need to manually clean up the file before your project will work again.

Open the manifest.mf file and you’ll see a line in the file like the following:

OpenIDE-Module-Install: net/developmentality/moduleexample/Installer.class

delete that line, and rebuild.  You should be ready to go.
In general, you need to be very careful about using any of the wizards that NetBeans provides.  They are extremely useful, but they end up changing XML files that you will need to manually edit later if you decide to refactor.  For instance, if you create an Action instance using the New Action Wizard, the action will be registered in the layer.xml file.  If you rename the action using the refactor command, the entries in the xml file are NOT modified, and you will get an exception at runtime unless you remember to edit that file.
Fortunately, the wizards are smart enough to at least tell you which files they are modifying.  It’s just a matter of remembering this further down the road when you need to refactor, move, or delete files.  Look for the modified files section in the wizard dialog:

 

Convert a FileObject into a File

January 3, 2011 Leave a comment

This will be quick, more for my future reference (and hopefully others’ as well) than any real tutorial.

In NetBeans Platform, most IO etc is done with FileObject instances rather than plain Java File objects.  It was not immediately clear how to convert between the two, which is necessary when some APIs require java.io.File objects, and others require the org.openide.filesystems.FileObject instances.  The answer is to use the FileUtil class, which exposes two methods:

static File toFile(FileObject fo)
Finds appropriate java.io.File to FileObject if possible.
static FileObject toFileObject(File file)
Converts a disk file to a matching file object.

Hope this pops up to the top of the google results for “convert a FileObject into a File”.

Categories: Uncategorized