How to add menus and actions

From Bioclipse
Jump to: navigation, search
Development tutorial
Responsible author:Ola
Bioclipse version:N/A
"N/A" is not a number.
Last updated:2010-07-15
Tags:


A command is the declaration of a behaviour by id. Commands are used to declare semantic behaviour so that action implementations defined elsewhere by handlers, or linked to the action extension points (see Basic workbench extension points using actions) can associate themselves with a particular semantic command.

Add a Command

  <extension
        point="org.eclipse.ui.commands">
        <category
              id="net.bioclipse.ui.dummyCategory"
              name="DummyCategory">
        </category>
     <command
           id="net.bioclipse.ui.commands.dummyCommand"
           categoryId="net.bioclipse.ui.dummyCategory"
           name="Dummy"/>
  </extension>

More info: More info: http://help.eclipse.org/help33/topic/org.eclipse.platform.doc.isv/guide/workbench_cmd_commands.htm

Add a handler to carry out the action

<extension
        point="org.eclipse.ui.handlers">
     <handler
        class="net.bioclipse.ui.actions.DummyActionHandler"
        commandId="net.bioclipse.ui.commands.dummyCommand">
     </handler>
</extension> 

More info: http://help.eclipse.org/help33/topic/org.eclipse.platform.doc.isv/guide/workbench_cmd_handlers.htm

Alternativly, you can also add a default handler directly to the command:

       <command
          id="net.bioclipse.cdk.ui.handlers.create3d"
          defaultHandler="net.bioclipse.cdk.ui.handlers.Create3dHandler"
          name="create3D"/>

defaultHandler is the name of a class extending AbstractHandler.

Bind the command to a key sequence (optional)

  <extension
        point="org.eclipse.ui.bindings">
     <key
           commandId="net.bioclipse.ui.commands.dummyCommand"
           schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
           sequence="M1+D"/>
  </extension>

More info: http://help.eclipse.org/help33/topic/org.eclipse.platform.doc.isv/guide/workbench_cmd_bindings.htm

Create a menu and menu item that uses the Command

<extension 
  id="add.item"
  point="org.eclipse.ui.menus">
 <menuContribution
    locationURI= "menu:org.eclipse.ui.main.menu?after=additions">
    <menu
       id="net.bc.dummyMenu"
       label="DummyMenu">
    </menu>
 </menuContribution>
<menuContribution
    locationURI= "menu:net.bc.dummyMenu?after=additions">
    <command
       commandId="net.bioclipse.ui.commands.dummyCommand"
       icon= "icons/source.gif"
       id= "testCMDItem"
       label= "Dummy Item">
    </command>
</menuContribution>
</extension>

More info: http://help.eclipse.org/help33/topic/org.eclipse.platform.doc.isv/guide/workbench_cmd_menus.htm

Create a popup menu item that uses the Command

Add the following menu contribution:

<menuContribution
           locationURI="popup:org.eclipse.ui.popup.any?after=additions">
        <command
              commandId="net.bioclipse.ui.commands.dummyCommand">
        </command>
     </menuContribution>

Implement a handler class

public class DummyActionHandler extends AbstractHandler{
  @Override
  public Object execute(ExecutionEvent arg0) throws ExecutionException {
  ...
  }
 }

Get Bioclipse selection in a handler

  HandlerUtil.getCurrentSelection( event );

There is a convenience method in plugin net.bioclipse.cdk.business to turn a selection into a list of molecules:

 List<ICDKMolecule> mols=CDKMoleculeSelectionHelper.getMoleculesFromSelection(sel);

Add context menu for a certain Context

Create a contentType by extending the contentTypes extension point in plugin.xml. This associates file-extensions with a contenttype. Example is from net.bioclipse.biojava.ui.

  <extension
        point="org.eclipse.core.runtime.contentTypes">
     <content-type
           id="net.bioclipse.biojava.ui.sequenceType"
           name="sequence"
           base-type="org.eclipse.core.runtime.text"
           file-extensions="gbk,fasta,embl,seq">
     </content-type>

The following adds a menu contribution for a command for any context menu when selection is an IResource of CML or SDF context type.

<menuContribution
          locationURI="popup:org.eclipse.ui.popup.any?after=additions">
       <command
             commandId="net.bioclipse.MYcommand">
         <visibleWhen>
   		<iterate operator="and" ifEmpty="false">
      			<adapt type="org.eclipse.core.resources.IResource">
      			<or>
                            <test property="org.eclipse.core.resources.contentTypeId" 
           	    	value="net.bioclipse.contenttypes.cml"/>
   	      		     <test property="org.eclipse.core.resources.contentTypeId" 
       	        	value="net.bioclipse.contenttypes.sdf"/>
               	</or>
      			</adapt>
   		</iterate>
        </visibleWhen>
       </command>
</menuContribution>

Make context menu visible for file extension and text selection

<visibleWhen>
  <iterate operator="and" ifEmpty="false">
    <or>
      <adapt type="org.eclipse.core.resources.IResource">
         <or>
            <test property="org.eclipse.core.resources.name" value="*.fasta"/> 
            <test property="org.eclipse.core.resources.name" value="*.seq"/> 
         </or>
      </adapt>
      <adapt type="org.eclipse.jface.text.ITextSelection" />
    </or>
   </iterate>
</visibleWhen>

The above did not work in one place, where the following solved it:

<visibleWhen>
 <with variable="selection">
  <iterate ifEmpty="false">
  <instanceof value="org.eclipse.core.resources.IResource"/>
   <test property="org.eclipse.core.resources.name" value="*.smi"/>
  </iterate>
 </with>
</visibleWhen>

Make context menu visible for a molecule-file and ICDKMolecule

It is common to add an action when users click on files containeing one or more molecules, AND if the user clicks one or more ICDKMolecules (for example selecting 2 molecules inside an SDFEditor). This shows is how you accomplish that:

  	<visibleWhen>
 	   <iterate operator="and" ifEmpty="false">
 		<or>
  		    <adapt type="org.eclipse.core.resources.IResource">
   			<or>
                       <test property="org.eclipse.core.resources.contentTypeId" 
       	    	      value="net.bioclipse.contenttypes.mdlMolFile2D"/>
                       <test property="org.eclipse.core.resources.contentTypeId" 
                             value="net.bioclipse.contenttypes.cml.singleMolecule2d"/>
                       </or>
                   </adapt>
                   <adapt type="net.bioclipse.cdk.domain.ICDKMolecule">
                       <test property="net.bioclipse.cdk.ui.has3D" 
                             value="false"/>
                   </adapt>
                </or>
           </iterate>
       </visibleWhen>

This makes use of the PropertyTester (here in plugin net.bioclipse.cdk.ui) which defines the properties has2D and has3D (can easily be extended). This is how the PropertyTester is defined in plugin.xml (see class for implementation).

 <extension point="org.eclipse.core.expressions.propertyTesters">
   <propertyTester
     id="net.bioclipse.cdk.ICDKMoleculeTester"
     type="net.bioclipse.cdk.domain.ICDKMolecule"
     namespace="net.bioclipse.cdk.ui"
     properties="has2D,has3D"
     class="net.bioclipse.cdk.ui.domain.CDKMoleculeTester">
   </propertyTester>
 </extension>


More links