Lesson 1: Create the CustomOpenActionGroup Java class

This lesson guides you through the steps of creating the Java™ class that is needed to handle the customized open menu.

About this task

The menu you will create is a collection of actions, separators, and action groups that contain a smaller collection of actions. For this exercise, you will create a class, MainActionGroup that makes up the menu. You will also create another class, OpenActionGroup that overrides the default open group option.

To create these classes:

Procedure

  1. You start with the OpenActionGroup class, since the MainActionGroup class needs a custom open group. In the Package Explorer view, right-click on thecom.ibm.carma.plugin.view Eclipse plug-in project that you have modified from Exercise 5, and select New > Package.
  2. In the New Java Package dialog box that opens enter, menu, as the name of the package, and click Finish.
  3. Now, create the Java class. Right click the menu package, and select New > Class. The New Java Class dialog box opens.
  4. Enter CustomOpenActionGroup as the name of the class.
  5. Click the Browse button to the right of the Superclass text field. In the Superclass Selection dialog box that opens, enter the filter text, OpenActionGroup. Select the class that is part of the com.ibm.carma.ui.view package, and click OK.
  6. Click Finish to close the New Java Class dialog box and create the class.
  7. You want to override the fillContextMenu method to provide the custom content of the open section of the menu. For the open section of the menu, you need to display everything but the Open and Open With menu selections. The easiest way to do this is to get the default from OpenActionGroup section and remove the Open and Open With menu selections.

    The following pseudocode demonstrates this:

    get the default items;
    for each item
    {
       if(item is open or open with) 
       {
          remove it from the list;
       }
    }
    Use the following sample code to implement this functionality:
    public void fillContextMenu(IMenuManager menu)
    {
       super.fillContextMenu(menu);
       IContributionItem[] myItems = menu.getItems();
       for(int i = 0; i < myItems.length; i++)
       {
          IContributionItem item = myItems[i];
          if(item.getId() != null)
          {
             if(item.getId().equals(OpenAction.ID) || item.getId().equals("com.ibm.carma.ui.openWithSubMenu"))
             {
                menu.remove(item);
             }
          }
       }
    }
  8. Automatically import any needed packages and classes. Ensure that all of the following are imported:
    import org.eclipse.jface.action.IContributionItem;
    import org.eclipse.jface.action.IMenuManager;
    
    import com.ibm.carma.ui.action.OpenAction;
    import com.ibm.carma.ui.view.OpenActionGroup;
  9. Save the source and debug any errors.