Plug-ins Cookbook
From BlogBridge Wiki
Here we collect and share some recipes that come handy when creating a plug-in.
Contents |
The Core
Application Events
How to know when the application finishes initialization?
When the application finishes initialization of the GUI and loading data from the database, it fires an event initializationFinished to all IControllerListeners. During initialization of your plug-in you can subscribe to this events (see GlobalController.addControllerListener()) to receive the notification.
The only suggestion is as follows. Because of this event is delivered only once in the application run, you may want to unsubscribe from it once you finished all necessary processing (see GlobalController.removeControllerListener()). It will not waste application resources.
User Interface
Menus
Adding commands to the popup menus
You can add an action to a popup menu by implementing the IPopupMenuHook interface or extending PopupMenuHookAdapter utility class. Every method in this interface returns the collection of actions to be appended to a specific context menu.
For example, if I need an About Box command to appear in the Feeds list popup menu, I implement my own popup menu hook, like this:
private class PopupMenuHook extends PopupMenuHookAdapter
{
/**
* Returns the collection of actions that should be
* appended to the list in the feeds list context menu.
*
* @return the collection of actions or NULL.
*/
@Override
public Collection<Action> getFeedsListActions()
{
List<Action> actions = new ArrayList<Action>();
actions.add(ActionManager.get(ActionsTable.CMD_BB_ABOUT));
return actions;
}
}
Note: This method of the popup menu hook will be called every time a user wants to see the menu (not once during the initialization). To you this means that you can customize the menu depending on the current state of the application (selected items, options, whatever else).
The next step is to register this hook within the application upon initialization. At the moment of the plug-in method initialize() call there's no MainFrame ready to link this new hook to. It's the very beginning of the application startup yet, and we have to wait until the GUI is ready. See how you can know when the application is initialized in our other recipe.
What you need to do is usually as simple as this:
MainFrame mf = GlobalController.SINGLETON.getMainFrame(); mf.addPopupMenuHook(new PopupMenuHook()); GlobalController.SINGLETON.removeControllerListener(this);
You take the main frame and register your hook. Yes, and I never forget to unsubscribe from the controller events when I don't need them any more to behave well.
