Difference between revisions of "Menu Contributions/IFile objectContribution"

From Eclipsepedia

Jump to: navigation, search
 
(Menus)
 
(4 intermediate revisions by 3 users not shown)
Line 2: Line 2:
  
 
We also have to provide object contributions (which in the past were scoped by objectClass).
 
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:
 +
 +
<source lang="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>
 +
</source>
 +
 +
<b>enablesFor</b> is now a property of the active handler, not the visible GUI element.
  
 
== Menus ==
 
== Menus ==
Line 7: Line 26:
 
There will be a reserved popup ID, "org.eclipse.ui.popup.any" that will allow contributions to any popup menu.
 
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">
+
  <source lang="xml">
    <menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions">
+
  <extension point="org.eclipse.core.expressions.definitions">
      <command commandId="org.eclipse.ui.examples.wiki.post"
+
      <definition id="org.eclipse.ui.example.antFile">
            mnemonic="%WikiExample.post.mnemonic"
+
        <iterate ifEmpty="false">
            icon="$nl$/icons/full/elcl16/post_wiki.gif">
+
            <adapt type="org.eclipse.core.resources.IFile">
        <visibleWhen>
+
              <test property="org.eclipse.core.resources.name"  
          <with variable="selection">
+
                    value="*.xml"/>
            &lt;!-- do something with an ISelection --&gt;
+
              <test property="org.eclipse.core.resources.contentTypeId"
          </with>
+
                    value="org.eclipse.ant.core.antBuildFile"/>
        </visibleWhen>
+
            </adapt>
       </command>
+
        </iterate>
      <command commandId="org.eclipse.ui.examples.wiki.load"
+
       </definition>
            mnemonic="%WikiExample.load.mnemonic"
+
  </extension>
            icon="$nl$/icons/full/elcl16/load_wiki.gif">
+
  <extension point="org.eclipse.ui.menus">
        <visibleWhen
+
      <menuContribution locationURI="popup:org.eclipse.ui.popup.any">
              checkEnabled="false">
+
        <command commandId="org.eclipse.jdt.ui.launchJavadocWizard"
            &lt;!-- the default variable is a Collection holding the ISelection,
+
              id="LaunchJavadocWizard"
                    or the objects from an IStructuredSelection --&gt;
+
              label="Create Javadoc"
            <iterate>
+
              style="push">
              <adapt
+
            <visibleWhen checkEnabled="false">
                     type="org.eclipse.core.resources.IResource">
+
              <or>
              </adapt>
+
                  <with variable="activeMenuSelection">
            </iterate>
+
                    <reference definitionId="org.eclipse.ui.example.antFile"/>
        </visibleWhen>
+
                  </with>
      </command>
+
                  <with variable="activeMenuEditorInput">
    </menuContribution>
+
                     <reference definitionId="org.eclipse.ui.example.antFile"/>
  </extension>
+
                  </with>
 +
              </or>
 +
            </visibleWhen>
 +
        </command>
 +
      </menuContribution>
 +
  </extension>
 +
  </source>
  
  
 
+
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.
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.
+
 
+
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 ==
  
So programmatically it is similar to all other menu contributions.
+
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 85: 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.