Difference between revisions of "Menu Contributions/IFile objectContribution"

From Eclipsepedia

Jump to: navigation, search
(Menus)
(Menus)
 
(2 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
Here's an example from one of our plugin.xml:
 
Here's an example from one of our plugin.xml:
  
<objectContribution adaptable="true“
+
<source lang="xml">
      objectClass="org.eclipse.core.resources.IFile“
+
<objectContribution adaptable="true"
      nameFilter="*.xml“
+
    objectClass="org.eclipse.core.resources.IFile"
      id="org.eclipse.jdt.internal.ui.javadocexport.JavadocWizard">
+
    nameFilter="*.xml"
  <visibility>
+
    id="org.eclipse.jdt.internal.ui.javadocexport.JavadocWizard">
       <objectState name="contentTypeId“
+
  <visibility>
            value="org.eclipse.ant.core.antBuildFile" />
+
       <objectState name="contentTypeId"
  </visibility>
+
          value="org.eclipse.ant.core.antBuildFile" />
  <action label=“Create Javadoc“
+
  </visibility>
        class="o.e.jdt.i.ui.jt.CreateJavadocActionDelegate“
+
  <action label="Create Javadoc"
        enablesFor="1" id="LaunchJavadocWizard"/>
+
      class="org.eclipse.jdt.internal.ui.CreateJavadocActionDelegate"
</objectContribution>
+
      enablesFor="1" id="LaunchJavadocWizard"/>
 +
</objectContribution>
 +
</source>
  
 
<b>enablesFor</b> is now a property of the active handler, not the visible GUI element.
 
<b>enablesFor</b> is now a property of the active handler, not the visible GUI element.
Line 59: Line 61:
  
  
It's probably that the default variable for core expression evaluations would be <b>selection</b>, so you wouldn't need the <with/> clause like in the second item above.
+
The default variable for visibleWhen/activeWhen/enabledWhen expressions is '''selection'''But it's better to be specific and use &lt;with variable="selection".../&gt; if that's what you need.
 
+
There would probably also be a short-hand to tie the visibility to an active handlerMaybe <visibleWhen handler="true"/>  This is still conjecture.
+
  
 
== Menus API ==
 
== Menus API ==
Line 67: Line 67:
 
Here is a similar example programmatically.
 
Here is a similar example programmatically.
  
 +
<source lang="java">
 
  public static void addFileContribution() {
 
  public static void addFileContribution() {
 
     final IMenuService menuService = (IMenuService) PlatformUI
 
     final IMenuService menuService = (IMenuService) PlatformUI
Line 107: Line 108:
 
     menuService.addContributionFactory(factory);
 
     menuService.addContributionFactory(factory);
 
  }
 
  }
 
+
</source>
 
+
  
 
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.
 
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.

Latest revision as of 08:15, 29 November 2010

[edit] 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"
    objectClass="org.eclipse.core.resources.IFile"
    nameFilter="*.xml"
    id="org.eclipse.jdt.internal.ui.javadocexport.JavadocWizard">
  <visibility>
      <objectState name="contentTypeId"
          value="org.eclipse.ant.core.antBuildFile" />
  </visibility>
  <action label="Create Javadoc"
      class="org.eclipse.jdt.internal.ui.CreateJavadocActionDelegate"
      enablesFor="1" id="LaunchJavadocWizard"/>
</objectContribution>

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

[edit] 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.core.expressions.definitions">
      <definition id="org.eclipse.ui.example.antFile">
         <iterate ifEmpty="false">
            <adapt type="org.eclipse.core.resources.IFile">
               <test property="org.eclipse.core.resources.name" 
                     value="*.xml"/>
               <test property="org.eclipse.core.resources.contentTypeId"
                     value="org.eclipse.ant.core.antBuildFile"/>
            </adapt>
         </iterate>
      </definition>
   </extension>
   <extension point="org.eclipse.ui.menus">
      <menuContribution locationURI="popup:org.eclipse.ui.popup.any">
         <command commandId="org.eclipse.jdt.ui.launchJavadocWizard"
               id="LaunchJavadocWizard"
               label="Create Javadoc"
               style="push">
            <visibleWhen checkEnabled="false">
               <or>
                  <with variable="activeMenuSelection">
                     <reference definitionId="org.eclipse.ui.example.antFile"/>
                  </with>
                  <with variable="activeMenuEditorInput">
                     <reference definitionId="org.eclipse.ui.example.antFile"/>
                  </with>
               </or>
            </visibleWhen>
         </command>
      </menuContribution>
   </extension>


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.

[edit] Menus API

Here is a similar example programmatically.

 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.