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:


  1. Create a new class by right-clicking on the menu package in the Package Explorer view, and selecting New > Class.
  2. 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.
  3. Select the Constructors from superclass check box.
  4. Click Finish to close the New Java Class dialog and create the class.
  5. The first thing that you will want to do is create global variables for a NewMenuActionGroup, a NavigationActionGroup, a OpenActionGroup, a ConnectionActionGroup, a DisplayActionGroup, and a PropertyDialogAction, 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.
  6. Now you want to override the makeActions method to instantiate the groups and actions that are needed to fill the menu. With the OpenActionGroup, be sure to instantiate your CustomOpenActionGroup 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());
  7. 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());
       menu.add(new Separator("open"));		
       menu.add(new Separator("refractor"));
       menu.add(new Separator("connect"));
       menu.add(new Separator("display"));
       menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
       menu.add(new Separator("project"));
       menu.add(new Separator("properties"));
  8. 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;
  9. Save the source and debug any errors.