Rational Developer for System z


Lesson 1: Create the CustomOpenActionGroup Java class

This lesson will guide you through the steps of creating the Java™ class needed to handle the customized open menu.
The context 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 will make up the context menu. You will also create another class, OpenActionGroup that will override the default open group option.

To create these classes:

  1. You will start with the OpenActionGroup class, since the MainActionGroup class will need a custom open group. In the Package Explorer view, right click on thecom.ibm.carma.plugin.view Eclipse plugin project 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 on the menu package, and select New > Class. The New Java Class dialog box will open.
  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.viewpackage, and click OK.
  6. Click Finish to close the New Java Class dialog box and create the class.
  7. You will want to override the fillContextMenu method to provide the custom content of the open section of the context menu. For the open section of the context menu, you will 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.

Feedback