Download NetBeans!

20071016 Tuesday October 16, 2007

Quick Code Generation for the NetBeans Platform

A new NetBeans Platform developer wrote to the docs feedback alias, in response to the Recognizing a File Type Tutorial, the following words: "How do we actually change the contents of the new .mf file? what if I don't want it to be in text format? what if I want it to open up a another project X (with its own topcomponent, palette and graphscene) when the user creates a new file type?"

Well, the answer is found in an earlier blog entry entitled Elvis, Einstein, and Advanced NetBeans API Wizards. There you're pointed to two magical wizards that generate exactly the code requested by the developer above! Let's use them to generate the things that the developer asked for above. However, before we do so, some caveats:

  • Generating lots of code is not necessarily a good idea, especially if you don't know what you're doing. If you build everything from the ground up, you are far more likely to learn how things work. Also, what happens when you run into some problem when you begin developing your code? There's no way that you're going to easily be able to figure out where the problem comes from if you didn't create it yourself. Okay, you can debug it, but it's not an ideal situation.

  • A lot of the code that is generated is my own. So, be careful. On the other hand, most of it is pretty basic code. In other words, you get a very basic palette, a very basic graph scene, a very basic OpenSupport implementation. So, all things being equal, the code should be good. On the other hand, all things are never equal.

At the end of this process, whenever you create a new Manifest file in NetBeans IDE, the Manifest file will have two views. The source view will include a palette (Common Palette API), while the design view will include a graph scene (Visual Library API), as shown in the next two screenshots:

You'll be able to drag and drop the palette item into the source view. To do so in the design view, you will need to follow the NetBeans Visual Library 6.0 Tutorial once you have completed this process. Your module's source structure will be like this:

However, again, you've been warned about the problems you might encounter. Still, it's at least fun to do, even though there are one or two transitions that are a bit clunky, as you'll see.

  1. Download the plugin from the Plugin Portal.

  2. Use the Plugin Manager to install the plugin into NetBeans IDE 6.0 Beta 1 or some other 6.0 build. Now go to the Samples | NetBeans Modules section. You will see two new modules, "Enhanced File Type" and "Enhanced Window":

  3. Complete the wizard for one of them. Then complete the wizard for the other one. The order is irrelevant. Then right-click one of them and choose "Install/Reload in Development IDE". Do the same for the other one. Again, the order of doing this is irrelevant.

  4. Create a new module project. You're going to get lots of code, a variety of classes, lots of new layer entries and Bundle keys. Therefore, better to begin with a completely new module than to try the following steps with an existing one where you already have code. So, create a new module project.

  5. In the New File wizard, you will now find two new file templates, "File Type (Enhanced)" and "Window Component (Enhanced)", as you can see here:

  6. Choose the first one, i.e, "File Type (Enhanced)". You will now see this:

  7. Fill it out for your file type, specify some sample content for the palette item that will be created, specify whether the document should open in a TopComponent and name that TopComponent (and just ignore the fact that some parts are greyed out, because they shouldn't be greyed out):

  8. On the next page, you will see everything that the IDE is about to create for you:

  9. Click Finish. Now the IDE creates everything in your module project. However, the TopComponent that you named in the wizard doesn't exist yet! So, you have some red error marks in the class that will handle the opening of the document for you:

    Next, we'll create that TopComponent! But, it won't be a standard TopComponent. No, it will be a multiview component with a graph scene in the visual view. The next steps are all about how to do this via the other enhanced wizard.

  10. Choose the other enhanced wizard:

  11. Fill it out and notice that you cannot select all checkboxes at the same time, because some contradict the others. For our purposes, we'll specify that we want a multiview editor (an implementation of Core MultiView, not XML MultiView, by the way!) and a Visual Library API implementation:

  12. Click Next and then finish the wizard. Now you have two data objects. That's unavoidable, because we needed to create one during the second wizard, to work with the multiview. You also have an error badge over the new one:

    So, now... you're going to have to (1) delete "ManifestDataObject" and then (2) rename "ManifestDataObjectSample", so that it is called "ManifestDataObject" instead. Sorry, this is messy. It would not pass any kind of review process, but they work and they get the job done.

  13. That's it. All the code is complete, all the layer entries are present, all the Bundle keys are there, all the project dependencies have been set. You should see this:

    You might still encounter some error badges, but you'll notice that you can compile the module without a problem. Install it and you'll have a multiview editor with a palette and a graph scene for Manifest files.

Now, you need to learn from those sources. The vast majority comes from reliable places, for example the OpenSupport implementation is completely standard. It handles the opening of your Manifest documents, such that it opens into your new TopComponent. Now go to NetBeans Modules and Rich-Client Applications Learning Trail and look especially at the tutorials under the heading "New! NetBeans IDE 6.0 Beta Tutorials".

Oct 16 2007, 08:09:33 AM PDT Permalink