Customizing Menus

Multiple menus within the DevOps Modeling Platform can be customized using plug-ins. Pluglets cannot be used to extend menus. In this section we will cover the most common menus to extend.

Menu Bar

Like any Eclipse-based application, menus can be easily contributed by extending the org.eclipse.ui.actionSets extension-point. The following example shows an extension that will add an action to a newly created menu.

     <extension
            point="org.eclipse.ui.actionSets">
      <actionSet
            label="My Action Set"
            description="My action set description."
            visible="true"
            id="com.ibm.examples.actionSet">
         <menu
               label="My Menu"
               id="com.ibm.examples.menu">
         </menu>
         <action
               label="My Action"
               icon="icons/myIcon.gif"
               id="com.ibm.examples.actions.myAction"
               class="com.ibm.examples.actions.MenuActionDelegate"
               menubarPath="com.ibm.examples.menu/additions">
         </action>
      </actionSet>
   </extension>

visible is an optional attribute indicating whether the action set is initially visible when a perspective is open. If the value is unspecified or false, in order to toggle the action set visibility go to Windows > Customize Perspective... and navigate to the Commands tab. Find the action set to toggle in the Available command groups selection box.

The MenuActionDelegate class extends org.eclipse.gmf.runtime.common.ui.action.AbstractActionDelegate and implements IWorkbenchWindowActionDelegate. In this example, the action simply opens a message dialog.

The AbstractActionDelegate provides definitions for the methods that are found in most action delegate interfaces in Eclipse. Therefore adding a new action delegate is simply a matter of subclassing this class and declaring that the new class implements the desired action delegate interface.

public class MenuActionDelegate 
    extends AbstractActionDelegate implements IWorkbenchWindowActionDelegate {

    protected void doRun(IProgressMonitor progressMonitor) {
        Shell shell = new Shell();
        MessageDialog.openInformation(
            shell,
            "My Title",
            "Menu Action Example");
    }
}

Pop-up Menu

Pop-up menus are contributed through the org.eclipse.ui.popupMenus extension-point.

This extension-point is used to add new actions to context menus owned by other plug-ins. Action contributions may be made against a specific object type (objectContribution) or against a specific context menu of a view or editor part (viewerContribution). When using objectContribution, the contribution will appear in all view or editor part context menus where objects of the specified type are selected. In contrast, using viewerContribution, the contribution will only appear in the specified view or editor part context menu, regardless of the selection.

Project Explorer

To contribute actions to the pop-up menus for selectable elements within the Project Explorer, add a viewerContribution to a org.eclipse.ui.popupMenus extension with targetID set to org.eclipse.ui.navigator.ProjectExplorer#PopupMenu.

   <extension
         point="org.eclipse.ui.popupMenus">
      <viewerContribution
            targetID="org.eclipse.ui.navigator.ProjectExplorer#PopupMenu"
            id="com.ibm.examples.viewerContribution.projectExplorer">
         <action
               class="com.ibm.examples.ProjectExplorerActionDelegate"
               label="Project Explorer Example Action"
               menubarPath="additions"
               id="com.ibm.examples.projectExplorerAction">
            <enablement>
               <and>
                  <objectState name="hasStereotype" value="MyProfile::MyStereotype"/>
               </and>
            </enablement>
         </action>               
      </viewerContribution>
   </extension>

The ProjectExplorerActionDelegate class extends org.eclipse.gmf.runtime.common.ui.action.AbstractActionDelegate and implements IViewActionDelegate. In this example, the action simply opens a message dialog.

public class ProjectExplorerActionDelegate 
    extends AbstractActionDelegate implements IViewActionDelegate {

    protected void doRun(IProgressMonitor progressMonitor) {
        Shell shell = new Shell();
        MessageDialog.openInformation(
            shell,
            "My Title",
            "Project Explorer Example Action");
    }
}

Diagram Editor

To contribute actions to the pop-up menus for selectable elements within a diagram editor, add a viewerContribution to a org.eclipse.ui.popupMenus extension with targetID set to org.eclipse.gmf.runtime.diagram.ui.DiagramEditorContextMenu.

Defining the action is similar to that of the Project Explorer, except that the class should implement org.eclipse.ui.IEditorActionDelegate. Also, in this example the enablement criteria differs so that the action will be enabled only if the selected element is the diagram.

   <extension
         point="org.eclipse.ui.popupMenus">
      <viewerContribution
            targetID="org.eclipse.gmf.runtime.diagram.ui.DiagramEditorContextMenu"
            id="com.ibm.examples.viewerContribution.diagram">
         <action
               class="com.ibm.examples.DiagramEditorActionDelegate"
               label="Diagram Editor Example Action"
               menubarPath="additions"
               id="com.ibm.examples.menus.diagramAction">
            <enablement>
               <and>
                  <objectState name="notationType" value="Diagram"/>
               </and>
            </enablement>
         </action>
      </viewerContribution>
   </extension>

The DiagramEditorActionDelegate class extends org.eclipse.gmf.runtime.common.ui.action.AbstractActionDelegate and implements IEditorActionDelegate. In this example, the action simply opens a message dialog.

public class DiagramEditorActionDelegate 
    extends AbstractActionDelegate implements IEditorActionDelegate {

    protected void doRun(IProgressMonitor progressMonitor) {
        Shell shell = new Shell();
        MessageDialog.openInformation(
            shell,
            "My Title",
            "Diagram Editor Action Example");
    }
}

Object Contribution

To contribute actions to the pop-up menus for selectable objects regardless of in what editor or viewer the selection is made, add an objectContribution to a org.eclipse.ui.popupMenus extension with objectClass set to the name of a class or interface that all objects in the selection must subclass or implement. In this example, IAdaptableSelection is used for the objectClass. GraphicalEditParts and UML Project Explorer tree elements implement this interface.

Defining the action is similar to that of the Project Explorer, except that the class should implement org.eclipse.ui.IObjectActionDelegate.

   <extension
         point="org.eclipse.ui.popupMenus">
      <objectContribution
            objectClass="org.eclipse.gmf.runtime.common.core.util.IAdaptableSelection"
            id="com.ibm.examples.objectContribution">
         <action
               class="com.ibm.examples.ObjectActionDelegate"
               label="Object Example Action"
               menubarPath="additions"
               id="com.ibm.examples.objectAction"
               enablesFor="1"/>
      </objectContribution>
   </extension>

The ObjectActionDelegate class extends org.eclipse.gmf.runtime.common.ui.action.AbstractActionDelegate and implements IObjectActionDelegate. In this example, the action simply opens a message dialog.

public class ObjectActionDelegate 
    extends AbstractActionDelegate implements IObjectActionDelegate {

    protected void doRun(IProgressMonitor progressMonitor) {
        Shell shell = new Shell();
        MessageDialog.openInformation(
            shell,
            "My Title",
            "Object Action Example"");
    }
}

Executing Transactional Commands in an Action Delegate

In many cases the menu actions described above may wish to modify the model. Model modifications should be done within an AbstractTransactionalCommand executed on the correct operation history to automatically support undo and redo of the command. The AbstractTransactionalCommand requires only implementing the doExecuteWithResult method.

In this example, the action delegate will extend the org.eclipse.gmf.runtime.emf.ui.action.AbstractModelActionDelegate class. It is an abstract parent for all concrete action delegates that execute model commands. The only methods that are required to be implemented are getEditingDomain() and doRun(IProgressMonitor progressMonitor). Use the execute method to execute the transactional command.

public class MyToggleAbstractActionDelegate
    extends AbstractModelActionDelegate implements IViewActionDelegate {

    protected TransactionalEditingDomain getEditingDomain() {
        return UMLModeler.getEditingDomain();
    }
    
    protected void doRun(IProgressMonitor progressMonitor) {
        ICommand command = getCommand();
        
        EObject selection = (EObject) ((IAdaptable) getStructuredSelection()
            .getFirstElement()).getAdapter(EObject.class);
        
        if (selection instanceof Class) {
            final Class clazz = (Class)selection;
            
            execute(new AbstractTransactionalCommand(getEditingDomain(), "Toggle Is Abstract", null) {
            
                protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) {
                    clazz.setIsAbstract(!clazz.isAbstract());
                    return CommandResult.newOKCommandResult();
                }
                
            }, progressMonitor, null);
        }
    }
}

Legal notices