Skip to main content
Jump to: navigation, search

Menu Contributions/IFile objectContribution

< Menu Contributions
Revision as of 18:56, 8 April 2007 by (Talk | contribs) (IFile object contribution)

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">
           <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"/>

It's probably that the default variable for core expression evaluations would be selection, so you wouldn't need the <with/> clause like in the second item above.

There would probably also be a short-hand to tie the visibility to an active handler. Maybe <visibleWhen handler="true"/> This is still conjecture.

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