Fabrizio Giudici is a Senior Java Architect with a long Java experience in the industrial field. He runs Tidalwave, his own consultancy company, and has contributed to Java success stories in a number of fields, including Formula One. Fabrizio often appears as a speaker at international Java conferences such as JavaOne and Devoxx and is member of JUG Milano and the NetBeans Dream Team. Fabrizio is a DZone MVB and is not an employee of DZone and has posted 67 posts at DZone. You can read more from them at their website. View Full User Profile

NetBeans Platform Idioms: Pluggable TopComponent (Part 1)

10.13.2009
| 11593 views |
  • submit to reddit
With part of my NetBeans Platform project mavenized, I have been able to restart coding and refactoring - there are lots of things that I wrote in the past year for blueMarine that, after a clean up, will be the subject of my future posts.

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.

 

 

The EventBus

 

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.

Prerequisites

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:

 

Published at DZone with permission of Fabrizio Giudici, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)