Lesson 2: Create the CustomMainActionGroup
Java™ class
This lesson guides you through the steps to create a CustomMainActionGroup
class.
This class emulates the functionality of the CARMA default MainActionGroup
class.
However, instead of using the default OpenActionGroup
,
the CustomMainActionGroup
class that you create uses
the CustomOpenActionGroup
class that you created
.
About this task
To create the CustomMainActionGroup
class:
Procedure
- Create a new class by right-clicking on the
menu
package in the Package Explorer view, and selecting New > Class. - In the New Java Class dialog box
that opens, enter CustomMainActionGroup as
the name. Click the Browse button to the right
of the Superclass text field. In the Superclass
Selection dialog box that appears, enter the filter, CarmaBrowserActionGroup. Select
the matching class that is contained in the
com.ibm.carma.ui.view
package, and click OK. - Select the Constructors from superclass check box.
- Click Finish to close the New Java Class dialog and create the class.
- The first thing that you will want to do is create global
variables for a
NewMenuActionGroup
, aNavigationActionGroup
, aOpenActionGroup
, aConnectionActionGroup
, aDisplayActionGroup
, and aPropertyDialogAction
, similar to the following sample code:private NewMenuActionGroup _newMenuActionGroup; private NavigationActionGroup _navigationActionMenu; private OpenActionGroup _openActionGroup; private ConnectionActionGroup _connectionActionGroup; private DisplayActionGroup _displayActionGroup; private PropertyDialogAction _propertyAction;
Tip: These variables should be declared at the top of the class before any method declarations. - Now you want to override the
makeActions
method to instantiate the groups and actions that are needed to fill the menu. With theOpenActionGroup
, be sure to instantiate yourCustomOpenActionGroup
class instead of the default. Use the following source code:protected void makeActions() { _newMenuActionGroup = new NewMenuActionGroup(); _navigationActionMenu = new NavigationActionGroup(getBrowser()); _openActionGroup = new CustomOpenActionGroup(); _connectionActionGroup = new ConnectionActionGroup(getBrowser()); _displayActionGroup = new DisplayActionGroup(getBrowser()); _propertyAction = new PropertyDialogAction(getBrowser().getViewSite(), getViewer()); }
- Override the
fillContextMenu()
method with the updated list of groups, actions, and separators. To make it look like a pop-up menu, use the same order for adding items to the menu using the following example code:public void fillContextMenu(IMenuManager menu) { ActionContext myContext = new ActionContext(getViewer().getSelection()); _newMenuActionGroup.getContext(); _newMenuActionGroup.setContext(myContext); _newMenuActionGroup.fillContextMenu(menu); _navigationActionMenu.setContext(myContext); _navigationActionMenu.fillContextMenu(menu); menu.add(new Separator("open")); _openActionGroup.setContext(myContext); _openActionGroup.fillContextMenu(menu); menu.add(new Separator("refractor")); menu.add(new Separator("connect")); _connectionActionGroup.setContext(myContext); _connectionActionGroup.fillContextMenu(menu); _connectionActionGroup.updateActionBars(); menu.add(new Separator("display")); _displayActionGroup.setContext(myContext); _displayActionGroup.fillContextMenu(menu); menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); menu.add(new Separator("project")); menu.add(new Separator("properties")); menu.add(_propertyAction); }
- Import all needed classes and packages. Ensure that all
of the following are included in the import statements:
import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.Separator; import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.actions.ActionContext; import org.eclipse.ui.dialogs.PropertyDialogAction; import com.ibm.carma.ui.view.BaseCarmaBrowser; import com.ibm.carma.ui.view.CarmaBrowserActionGroup; import com.ibm.carma.ui.view.ConnectionActionGroup; import com.ibm.carma.ui.view.DisplayActionGroup; import com.ibm.carma.ui.view.NavigationActionGroup; import com.ibm.carma.ui.view.NewMenuActionGroup; import com.ibm.carma.ui.view.OpenActionGroup;
- Save the source and debug any errors.