Add Widget via Action in Toolbar (Part 2)
By Geertjan-Oracle on Dec 21, 2012
Updated the "Add Widget via Action in Toolbar" sample so that, when a new object is added to the canvas (via the red dot toolbar button below), a new customer is automatically added to the explorer view, below, after which any node selected in the explorer is automatically highlighted in the canvas, while any object selected in the canvas causes the related node in the explorer to be highlighted:
This is a cool example illustrating communication between multiple modules, with the Visual Library included too.
Also, Lookup is used in different ways:
- Customer Action Loosely Coupled from Canvas. The canvas publishes a Droppable object, which causes the button in the toolbar to be enabled because its Action is sensitive to instances of Droppable. When the user clicks the button, i.e., invokes the Action, a small dialog is shown where the user can specify a text. When OK is pressed in the dialog, a new Customer object is created, with its title set to the text, and passed by the Action to the Droppable implementation published by the canvas, which creates a widget in the canvas. This mechanism is similar to how Savable works.
- Node Hierarchy Displays Customer Objects in Canvas. When a new widget is created by the Droppable in the canvas, the customer object is added to the CentralLookup (thanks Wade), which is what the ChildFactory in the explorer is listening to for new Customer objects. When a new Customer object appears in the CentralLookup, the explorer is updated with new nodes representing the new customers found in the CentralLookup. CentralLookup is used because, in this scenario, the explorer doesn't care about what is currently selected, it simply wants to show all the objects created in the canvas.
- Visual Library Listens for Selected Node. Each node in the explorer publishes its underlying Customer object, when the Node is selected. The canvas is defined by an ObjectScene from the Visual Library, which implements LookupListener, listening for Customer objects in the global Lookup. So, when a different Node is selected, a different Customer object is available in the global Lookup (i.e., Utilities.actionsGlobalContext). The title of that object is compared to the titles of all the objects in the ObjectScene and the widget of whichever object matches the Customer object's title has its foreground turned to red, with all other widgets turned to black. This happens whenever a new Node is selected in the explorer.
- Node Hierarchy in Explorer Window Listens for Selected Widget. When a widget is double-clicked, a SelectProvider in the Visual Library Scene publishes the underlying Customer object into an InstanceContent published into the TopComponent Lookup, i.e., different to the InstanceContent for the Droppable described in point 1 above. The explorer listens to the global Lookup (as well as to the CentralLookup as described in point 2 above) for Customer objects and then finds the matching Node in the ExplorerManager and selects it.