NetBeans Platform Idioms: Pluggable TopComponent (Part 1)
So, I'm resuming my series about the Idioms for the NetBeans Platform, that I suspended just after the first post. Today we see the Pluggable TopComponent. As usual, there's no special invention here, but the use of well-known patterns specifically adapted for the NetBeans Platform context.
The idea, of course, is reuse and modularization. I'll illustrate some real code taken from forceTen that implements the features of the tree component that you can see in this screenshot:
The component with the tree is called "GeoExplorer" and it allows to browse and select nodes representing geographical entities; when you select one, the map renderer (called "GeoViewer") centers on it. The two components are decoupled by means of another Platform Idiom, the "Event Bus": whenever a geo entity is selected in the GeoExplorer, an event is posted to the Event Bus and received by the Geo Viewer.
Since I consider the Event Bus a pretty useful idiom to integrate different components, the design of GeoExplorer makes sense for my projects; but in a wider reuse context, you might want to do something different. There are also a lot of other details that can be done in a slightly different way: for instance, do you want the selection to happen when you single-click on a node? Or when you double-click? Or do you prefer to pop-up a contextual action? Do you need to implement complementary behaviours, such as changing the main window title in function of the selected window, or node, etc? How do you want to load data into a TopComponent, e.g. from a database or a remote service? In eager fashion, that is during initialization, or in lazy fashion, the first time the component is rendered?
Any of these choices make sense in a different context, and a really reusable GeoExplorer should allow a programmer to use it out-of-the-box, only adding integration code and/or configuration changes. The purpose of this article is to find a good design that allows to do that.
This article is the first in a series - this introduction is easily readable by anyone with the very basic knowledge of the NetBeans Platform; next parts will have complete code examples and I'll assume that you have a good knowledge of some fundamendal things:
- the Lookup API
- the Node API
- the Explorer API
- the TopComponent from the Windows API
- the System File System (layer.xml)
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)