I'm aware of several groups of developers who have recently started porting Swing applications to the NetBeans Platform. In the aftermath of the JSR-296 meltdown, that trend is likely to become even more prominent.
That, in fact, may be the biggest win of the JSR-296 project—it helped developers take the next step in their projects, which they otherwise might not have done. I.e., developers abandoned the JFrame, swapping it for the JSR-296 SingleFrameApplication class. Since it was so easy to get started with JSR-296 (I refuse to call it "Swing Application Framework", simply because the name arrogantly and mistakenly implies that there is no other Swing application framework), especially with the help of the tools in NetBeans IDE, many developers did so, quickly bringing their applications to fruition.
However, it's never been more than a JSR and its development has ceased, as described in the warning you'll see when you try to create these applications in NetBeans IDE 6.9:
And so, now even more developers are in need of the NetBeans Platform, since that's the most logical next step for your Swing application, especially if you've committed yourself to a larger source base, which is what the adoption of JSR-296 implies, i.e., you didn't find the JFrame sufficient, you wanted to focus on your domain rather than its infrastructure, hence you needed lifecycle management, and an Actions system, and Tasks. So, now you still need them, and they're all available in the NetBeans Platform.
Here, based on quite some interaction with developers entering the process of porting applications to the NetBeans Platform, are my top 10 practical tips for getting you started:
- Follow the CRUD Tutorial. Yes, there are many tutorials you can take and screencasts you can watch. However, if there were to be one specific tutorial, which would it be? It would be the NetBeans Platform CRUD Application Tutorial. I was recently in touch with a developer who was pretty confused about many things relating to the NetBeans Platform. After following that tutorial, the kinds of questions he ended up asking were markedly different, far more in line with the idioms of the NetBeans Platform. That specific tutorial covers the most typical scenario on the NetBeans Platform and in the process you learn a LOT about the typical ways of working on the NetBeans Platform, the processes, the APIs, everything you're going to need to learn at some point anyway. So why not follow that tutorial and you'll be off to a flying start. (The completed CRUD application is available in the IDE from 6.9 onwards, both as a Maven-based application and an Ant-based application.)
- Print Out the NetBeans Platform Refcard. A very big problem when confronted with the NetBeans Platform is that of scope. In other words, what does the NetBeans Platform provide, exactly? Why would you use what when? Again, the screencast series helps a lot in that regard, but you should really get (for free) and print out the Essential NetBeans Platform Refcard. It really covers everything on a very global level, as well as drilling down to the finer details, giving you lots of code tips and tricks along the way.
- Prototype. Do a quick prototype. I.e., right after having followed the two steps above, you actually already have everything you need. It's all a lot less complicated than it might seem. Make a back up of your source code, create a new NetBeans Platform application in NetBeans IDE (or use one of the Groovy scripts provided by the IDE for the same purpose), create a new module, and then stick all your code into that single module. Next, create library wrappers for all your third party libraries. Set dependencies as required, create a TopComponent (which you know how to do, since you completed step 1) and then display some of the data in a new window in your new application. That's it, your first prototype is complete.
- Turn External JARs into Modules. A frequent pain point is where developers say: "Really? Do I need to turn all of my libraries into library wrapper modules? Why?" Well, the idea is that you're creating a modular application, which lets you benefit from dependency management, versioning, and information hiding. It is a great benefit to also be able to version and manage the dependencies of your 3rd party libraries. You can even put multiple JARs into the same module (via Ctrl-Click and Shift-Click in the "Library Wrapper Module wizard"). However, you need to think about the wisdom of putting multiple JARs into the same module, as (remember!) you're creating a modular application, so only those things that NEED to be in the same module should be in the same module.
- Think on Module Boundaries. When to create a new NetBeans module (or OSGi bundle)? It is very important that you read the article How to Split an Application into Modules?
- Register Action Classes. Since the 6.7 release of the NetBeans Platform, you typically do not need to subclass the NetBeans Platfor's Action classes anymore, as explained in Which NetBeans Platform Action Class Should I Use? Why is that a good thing? Because now you can simply copy the ActionListeners and AbstractActions from your original application and paste them into your NetBeans modules. Once you've done so, all you need to do is register them correctly and then you're done. That means that porting your Action classes to display them in menus, toolbars, and have them invoked from keyboard shortcuts is a question of registration, rather than recoding the classes.
- Love the Layer. The layer is your best friend. A "layer" is an XML file, of which each module can have at most one. Each layer is added to the virtual filesystem of the application at start up. You can register anything you like in the layer and then, from your Java code, you can read that layer. That sounds very abstract, but look at "Pluggable JXTaskPane" for a very simply and typical example. That's also the magic to turning your application... into a platform! Create a folder in the layer (i.e., simply by typing in an XML file) named "Products" and then each module providing a new product can register a folder below "Products". Then in your code, you can iterate through the Products folder, find all the products, and then display them in a window in the application. Service providers can be registered in the layer too, as explained in How Do NetBeans Extension Points Work?
- Keep the JPanels. You have JPanels in your original application? Wonderful. You can use them without making any changes whatsoever. Create a new TopComponent class (either via the wizard in NetBeans IDE or by simply subclassing "TopComponent"), then call "add(myJPanel)", and then you're done. No more work needed than that. You can do the same thing with the Options window or a wizard. I.e., you might find that a JPanel can be used to configure something (in the Options window or in a wizard), as well as as an edit panel in a window. So you can reuse that same JPanel in multiple different places, simply by calling "add(myJPanel") in whichever NetBeans Platform container you're working on.
- Participate in the Community. You'll definitely get more out of things by participating, which mainly occurs in the email@example.com mailing list. All sorts of questions are asked and answered there and, as you learn more, you should make use of the opportunity of sharing your knowledge with others. You can really have an impact on some very cool applications being developed all over the world!
- Porting is a Process. Remember always that porting is a process. Typically, it takes place over many releases of your product, so plan accordingly, since there should be no regressions for the end user. For example, in the first release after you start porting, you might simply port the JPanels to TopComponents in your new NetBeans Platform application. In the next release, you might want to wrap your business objects into Node classes, so that they can be displayed in explorer views, including the Properties window. Etc. Take it slow and pace yourself. If you have a few weeks or months to work on the porting process, depending on the size of the source base, that should be OK. However, if you have less time, consider working in stages, choosing the features you want to use for each release. You only need to use as much as you need, there's no rule (except for the runtime container, which is mandatory) saying how much of the NetBeans Platform should be part of your application.
OK, that's the end of my list! What do you, reading this article, think? Do you have other tips?