Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Menu Contributions/IFile objectContribution"

 
(IFile object contribution)
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:
 +
 +
<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="o.e.jdt.i.ui.jt.CreateJavadocActionDelegate“
 +
        enablesFor="1" id="LaunchJavadocWizard"/>
 +
</objectContribution>
 +
 +
<b>enablesFor</b> is now a property of the active handler, not the visible GUI element.
  
 
== Menus ==
 
== Menus ==
Line 7: Line 24:
 
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">
+
  <extension point="org.eclipse.core.expressions.definitions">
    <menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions">
+
      <definition id="org.eclipse.ui.example.antFile">
      <command commandId="org.eclipse.ui.examples.wiki.post"
+
        <iterate>
            mnemonic="%WikiExample.post.mnemonic"
+
            <adapt type="org.eclipse.core.resources.IFile">
            icon="$nl$/icons/full/elcl16/post_wiki.gif">
+
              <test property="org.eclipse.core.resources.name"  
        <visibleWhen>
+
                    value="*.xml"/>
          <with variable="selection">
+
              <test property="org.eclipse.core.resources.contentTypeId"
            &lt;!-- do something with an ISelection --&gt;
+
                    value="org.eclipse.ant.core.antBuildFile"/>
          </with>
+
            </adapt>
        </visibleWhen>
+
        </iterate>
       </command>
+
       </definition>
      <command commandId="org.eclipse.ui.examples.wiki.load"
+
  </extension>
            mnemonic="%WikiExample.load.mnemonic"
+
  <extension point="org.eclipse.ui.menus">
            icon="$nl$/icons/full/elcl16/load_wiki.gif">
+
      <menuContribution locationURI="popup:org.eclipse.ui.popup.any">
        <visibleWhen
+
        <command commandId="org.eclipse.jdt.ui.launchJavadocWizard"
              checkEnabled="false">
+
              id="LaunchJavadocWizard"
            &lt;!-- the default variable is a Collection holding the ISelection,
+
              label="Create Javadoc"
                    or the objects from an IStructuredSelection --&gt;
+
              style="push">
            <iterate>
+
            <visibleWhen checkEnabled="false">
              <adapt
+
              <or>
                     type="org.eclipse.core.resources.IResource">
+
                  <with variable="activeMenuSelection">
              </adapt>
+
                    <reference definitionId="org.eclipse.ui.example.antFile"/>
            </iterate>
+
                  </with>
        </visibleWhen>
+
                  <with variable="activeMenuEditorInput">
      </command>
+
                     <reference definitionId="org.eclipse.ui.example.antFile"/>
    </menuContribution>
+
                  </with>
  </extension>
+
              </or>
 
+
            </visibleWhen>
 +
        </command>
 +
      </menuContribution>
 +
  </extension>
  
  
Line 43: Line 63:
 
== Menus API ==
 
== Menus API ==
  
So programmatically it is similar to all other menu contributions.
+
Here is a similar example programmatically.
  
 
  public static void addFileContribution() {
 
  public static void addFileContribution() {

Revision as of 18:56, 8 April 2007

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="o.e.jdt.i.ui.jt.CreateJavadocActionDelegate“
        enablesFor="1" id="LaunchJavadocWizard"/>
</objectContribution>

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

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


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
            .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.

Back to the top