简体   繁体   中英

Converting object contribution to menu contribution in eclipse

I have a context menu written using org.eclipse.ui.popupMenus. Since it is deprecated now I want to use org.eclipse.ui.menus for the same.

Earlier implementation looks like this

 <extension
     point="org.eclipse.ui.popupMenus">
  <objectContribution
        adaptable="true"
        id="com.xyz.plm.analysis.func.gui.popupOnComponentForCA"
        objectClass="com.xyz.plm.componentmodel.IComponent">
     <action
           class="com.xyz.plm.analysis.my.gui.MyAction"
           enablesFor="1"
           id="com.xyz.plm.analysis.my.gui.MyActionion2"
           label="Run My Action"
           menubarPath="com.xyz.plm.ide.ui.myToolsPopupMenu/myToolsGroup"
           tooltip="Run My Action">
     </action>

What I tried so far is below

<extension
     point="org.eclipse.ui.commands">
  <command
        id="com.xyz.plm.analysis.my.commandmygui"
        name="Run My Action">
  </command>
</extension>

<extension
     point="org.eclipse.ui.handlers">
  <handler
        class="com.xyz.plm.analysis.my.gui.MyAnalysisGui"
        commandId="com.xyz.plm.analysis.my.commandmygui">
     <enabledWhen>
        <with
              variable="selection">
           <iterate
                 ifEmpty="false"
                 operator="or">
              <instanceof
                    value="com.xyz.plm.componentmodel.IComponent">
              </instanceof>
           </iterate>
        </with>
     </enabledWhen>
  </handler>
</extension>

<extension
      point="org.eclipse.ui.menus">
  <menuContribution
        locationURI="popup:com.xyz.plm.ide.ui.myToolsPopupMenu">
     <command
           commandId="com.xyz.plm.analysis.my.commandmygui"
           label="RunMyACtion"
           style="push">
     </command>
  </menuContribution>
 </extension>

But with this I am seeing MyAction subMenu under MyTools context menu. But MyAction is disabled. Also I am seeing MyACtion is appearing in projectExplorer window as well as in editor when I right click select MyTools.

What I want is MyAction submenu should appear only under MyTools popupmenu and only when I right click on a object of type IComponent this menu should appear.

You should use visibleWhen on the menu contribution and no enabledWhen for the handler. You should probably use adapt rather than instanceof .

So something like:

<extension
     point="org.eclipse.ui.handlers">
  <handler
        class="com.xyz.plm.analysis.my.gui.MyAnalysisGui"
        commandId="com.xyz.plm.analysis.my.commandmygui">
  </handler>
</extension>

<extension
      point="org.eclipse.ui.menus">
  <menuContribution
        locationURI="popup:com.xyz.plm.ide.ui.myToolsPopupMenu">
     <command
           commandId="com.xyz.plm.analysis.my.commandmygui"
           label="RunMyACtion"
           style="push">
      <visibleWhen>
        <with
              variable="selection">
           <iterate
                 ifEmpty="false"
                 operator="or">
              <adapt
                    value="com.xyz.plm.componentmodel.IComponent">
              </adapt>
           </iterate>
        </with>
      </visibleWhen> 
    </command>
  </menuContribution>
 </extension>

The equivalent is to test the activeMenuSelection and the activeMenuEditorInput , set when you right-click, and check for the equivalent conditions. The values are iterables, so you need to iterate over them to perform your test. See the example on the Eclipse Wiki .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM