Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: for the plan.

Jump to: navigation, search

Menu Contributions/IFile objectContribution

< Menu Contributions
Revision as of 08:15, 29 November 2010 by Unnamed Poltroon (Talk) (Menus)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

IFile object contribution

We also have to provide object contributions (which in the past were scoped by objectClass).

Here's an example from one of our plugin.xml:

<objectContribution adaptable="true"
      <objectState name="contentTypeId"
          value="org.eclipse.ant.core.antBuildFile" />
  <action label="Create Javadoc"
      enablesFor="1" id="LaunchJavadocWizard"/>

enablesFor is now a property of the active handler, not the visible GUI element.


There will be a reserved popup ID, "org.eclipse.ui.popup.any" that will allow contributions to any popup menu.

   <extension point="org.eclipse.core.expressions.definitions">
      <definition id="org.eclipse.ui.example.antFile">
         <iterate ifEmpty="false">
            <adapt type="org.eclipse.core.resources.IFile">
               <test property="" 
               <test property="org.eclipse.core.resources.contentTypeId"
   <extension point="org.eclipse.ui.menus">
      <menuContribution locationURI="popup:org.eclipse.ui.popup.any">
         <command commandId="org.eclipse.jdt.ui.launchJavadocWizard"
               label="Create Javadoc"
            <visibleWhen checkEnabled="false">
                  <with variable="activeMenuSelection">
                     <reference definitionId="org.eclipse.ui.example.antFile"/>
                  <with variable="activeMenuEditorInput">
                     <reference definitionId="org.eclipse.ui.example.antFile"/>

The default variable for visibleWhen/activeWhen/enabledWhen expressions is selection. But it's better to be specific and use <with variable="selection".../> if that's what you need.

Menus API

Here is a similar example programmatically.

 public static void addFileContribution() {
     final IMenuService menuService = (IMenuService) PlatformUI
     // an expression that walks the selection looking for objectclasses
     final ObjectClassExpression ifileExpression = new ObjectClassExpression(
     final ImageDescriptor postIcon = AbstractUIPlugin
     final ImageDescriptor loadIcon = AbstractUIPlugin
     AbstractContributionFactory factory = new AbstractContributionFactory(
             "popup:org.eclipse.ui.popup.any?after=additions") {
         public void createContributionItems(IMenuService menuService,
                 List additions) {
             CommandContributionItem item = new CommandContributionItem(
                     "", null, postIcon,
                     null, null, null, "P", null,
             menuService.registerVisibleWhen(item, ifileExpression);
             item = new CommandContributionItem(
                     "", null, loadIcon,
                     null, null, null, "L", null,
             menuService.registerVisibleWhen(item, ifileExpression);
         public void releaseContributionItems(IMenuService menuService,
                 List items) {

The location of org.eclipse.ui.popup.any specifies any context menu, and the expression ties it to a specific objectClass. Using the new expression syntax you can make your conditions more complex.

You can set your visibleWhen expression on each item as you create it.

In 3.3M6 registerVisibleWhen(*) method might be changing.

Back to the top