EventBus: How to Publish & Subscribe on the NetBeans Platform
The best solution is to have neither of the two depending on each other. You can achieve that with my favorite decoupling pattern, "publish and subscribe": define a set of messages ("topics"), define portions of code that register interest in those topics and receive an asynchronous notification ("subscribers") , and define portions of code that publish and unpublish those topics ("publishers"). Then you're done. All the components can now communicate without knowing about each other, while only depending on the messaging infrastructure and on the topics.
Indeed, this is one of the first parts that I developed in blueMarine when I ported it to the NetBeans Platform more than two years ago, and it was a pretty good improvement from the previous design. The implementation was focused on the NetBeans Platform's Lookup class, one of the most powerful in the NetBeans Platform. While it is mostly used for looking up services registered in the application, it is also used by the NetBeans Platform runtime to store relevant objects (such as selected items in a list) and to notify listeners (such context sensitive actions). So I basically used a global Lookup provided by the NetBeans Platform, "Utilities.actionsGlobalContext()", to listen for selections that were made in TopComponents. It seemed a pretty logical way to work, since TopComponents usually contain an ExplorerManager that automatically receives notification of selected Nodes in trees or lists that are placed inside the TopComponent. So, the flow of events is:
select a node in a tree -> notification published to ExplorerManager -> notification published to the actionsGlobalContext() -> notification received by my listener.
With this approach, you can use the class information as the 'topic': when you select a Node, everything that is inside its private Lookup (for instance, the DataObject) gets published. DataObject is obviously one of the most used topics by blueMarine (representing photos) and delivers information about selecting a single object: to notify that a set of DataObjects has been prepared, I defined a specific class named DataObjectSelection; to notify that there is a selection task running in the background, another specific object (DataObjectSelectionInProgress) is used, so viewers can display a "Please wait" message.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)