Menu Contributions/IFile objectContribution

From Eclipsepedia

< Menu Contributions
Revision as of 11:33, 28 February 2007 by Pwebster.ca.ibm.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

IFile object contribution

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

Menus

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

 <extension point="org.eclipse.ui.menus">
   <menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions">
     <command commandId="org.eclipse.ui.examples.wiki.post"
           mnemonic="%WikiExample.post.mnemonic"
           icon="$nl$/icons/full/elcl16/post_wiki.gif">
       <visibleWhen>
         <with variable="selection">
            <!-- do something with an ISelection -->
         </with>
       </visibleWhen>
     </command>
     <command commandId="org.eclipse.ui.examples.wiki.load"
           mnemonic="%WikiExample.load.mnemonic"
           icon="$nl$/icons/full/elcl16/load_wiki.gif">
        <visibleWhen
              checkEnabled="false">
           <!-- the default variable is a Collection holding the ISelection,
                   or the objects from an IStructuredSelection -->
           <iterate>
              <adapt
                    type="org.eclipse.core.resources.IResource">
              </adapt>
           </iterate>
        </visibleWhen>
     </command>
   </menuContribution>
 </extension>


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

So programmatically it is similar to all other menu contributions.

public static void addFileContribution() {
    final IMenuService menuService = (IMenuService) PlatformUI
            .getWorkbench().getService(IMenuService.class);
    // an expression that walks the selection looking for objectclasses
    final ObjectClassExpression ifileExpression = new ObjectClassExpression(
            "org.eclipse.core.resources.IFile");

    final ImageDescriptor postIcon = AbstractUIPlugin
            .imageDescriptorFromPlugin("org.eclise.ui.tests",
                    "icons/full/elcl16/post_wiki.gif");
    final ImageDescriptor loadIcon = AbstractUIPlugin
            .imageDescriptorFromPlugin("org.eclise.ui.tests",
                    "icons/full/elcl16/load_wiki.gif");
    AbstractContributionFactory factory = new AbstractContributionFactory(
            "popup:org.eclipse.ui.popup.any?after=additions") {
        public void createContributionItems(IMenuService menuService,
                List additions) {
            CommandContributionItem item = new CommandContributionItem(
                    "org.eclipse.ui.examples.wiki.post",
                    "org.eclipse.ui.examples.wiki.post", null, postIcon,
                    null, null, null, "P", null,
                    CommandContributionItem.STYLE_PUSH);
            menuService.registerVisibleWhen(item, ifileExpression);
            additions.add(item);

            item = new CommandContributionItem(
                    "org.eclipse.ui.examples.wiki.load",
                    "org.eclipse.ui.examples.wiki.load", null, loadIcon,
                    null, null, null, "L", null,
                    CommandContributionItem.STYLE_PUSH);
            menuService.registerVisibleWhen(item, ifileExpression);
            additions.add(item);
        }

        public void releaseContributionItems(IMenuService menuService,
                List items) {
        }
    };
    menuService.addContributionFactory(factory);
}


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.