Contribute Actions

From PATSWiki

It is possible to contribute items in the context menu of the model viewer via an extension point.

  <requires>
     <import plugin="org.eclipse.ui"/>
     <import plugin="org.eclipse.core.runtime"/>
     <import plugin="org.eclipse.core.resources"/>
     <import plugin="org.eclipse.uml2"/>
  </requires>
  <extension point="org.eclipse.ui.popupMenus">
     <objectContribution
         objectClass="org.eclipse.uml2.StateMachine"
                  id="stateMachineAction">
        <action
           label="My Action Label"
           icon="icons/ramses.gif"
           class="no.ntnu.item.ramses....."
           menubarPath="additions"
           enablesFor="1"
           id="no.ntnu.item.ramses.....">
        </action>
     </objectContribution>
  </extension>

The entry in plug-in-xml above simply registers an action for a state machine (see objectClass="org.eclipse.uml2.StateMachine"). This action calls a class that you have to write and that has to implement IObjectActionDelegate. From the selection provided via this interface, you can get the state machine. Remember to set the "class" and the "id" attribute to the fully qualified class name of this action, and adjust the label and the icon. Do not change menubarPath="additions".

Read also the documentation about the Eclipse extension point org.eclipse.ui.popupMenus and the interface IObjectActionDelegate.


 public class MyAction extends Action implements IObjectActionDelegate {
     private IWorkbenchPart targetPart;
     private ISelection selection;
           
     public void selectionChanged(IAction action, ISelection selection) {
         this.selection = selection;
         ...(1)...
     }
     
     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
         this.targetPart = targetPart;
     }
      
     public void run(IAction action) {
         ... (2)...
     }
 }
Table of contents

1 Menu Path
2 Contributing an entire Sub-Menu

Obtaining the Selected Element

In method selectionChanged we saved the selection. Usually the selection is an IStructuredSelection, and you can cast to this class. If you just want the first element, call the getFirstElement() method on that casted selection.

Enabling or Disabling an Action

By default, the action appears for all the objects it is declared for and is enabled. In some cases, you might want to make an action dependent on more criteria on an object. To do so, add code to method selectionChanged that checks on the selection if the action should be enabled or not. You can disabling the action by calling the setEnabled() method of the IAction that is given as argument with the method.


Obtaining the Shell

To open a dialog you will need a shell. Obtain it via the targetPart object that we have saved.

 Shell shell = targetPart.getSite().getShell()


Starting a Wizard

 Wizard wizard = new ... (your Wizard)
 WizardDialog dialog = new WizardDialog(targetPart.getSite().getShell(), wizard);
 dialog.open();

Menu Path

You can control where in the Pop-up menu your action appears. Ramses declares the following menu:

  • "New"
    • use this if you contribute an action that starts a wizard to create a new element
    • specify menubarPath="new/new" in plugin.xml
  • "Open With"
    • use this if you contribute an action that opens an editor
    • specify menubarPath="openwith/openwith" in plugin.xml
  • "Edit Actions"
    • this is not a menu, just a placeholder in the main menu
    • use this if you contribute an action that "edits" an element (Copy, paste, delete etc.)
    • so far only delete actions are being put here.
    • specify menubarPath="editActions" in plugin.xml
  • "Source Code"
    • use this if you contribute an action that shows or alters the code directly
    • specify menubarPath="sourcecode/sourcecode" in plugin.xml
  • "Refactor"
    • use this if you contribute an action that refactors an element
    • specify menubarPath="refactor/refactor" in plugin.xml
  • additional actions
    • specify menubarPath="additions" in plugin.xml
  • "Experimental (alpha)"
    • use this if you contribute an experimental action which might still be buggy
    • specify menubarPath="experimental/experimental" in plugin.xml
  • "Inspector Actions"
    • this is not a menu, just a placeholder in the main menu
    • use this if you contribute an action that starts an inspector
    • specify menubarPath="inspectorActions" in plugin.xml

Contributing an entire Sub-Menu

The following code show how to add an entire sub-menu in plugin.xml:

    <extension point="org.eclipse.ui.popupMenus">
    <objectContribution objectClass="org.eclipse.uml2.Package" id="addCollaborationAction">
        <menu
           id="com.xyz.xyzMenu"
           path="additions"
           label="My New Menu">
			<separator name="group1"/>
       </menu>
       <action
             class="no.ntnu.item.ramses.ui.internal.actions.add.AddCollaborationAction"
             enablesFor="1"
             icon="icons/ramses.gif"
             id="no.ntnu.item.ramses.ui.internal.actions.add.AddCollaborationAction"
             label="New Collaboration"
             menubarPath="com.xyz.xyzMenu/group1"
             tooltip="Action Tooltip">
       </action>
    </objectContribution>
 </extension>