Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

visibleWhen for command to appear in context menu

I am trying configure visibility of a command within the context menu using 'visibleWhen' expression within a menuContribution. What I am trying to do is make the command visible in the context menu only if you:

  1. Right-click certain file types(resources) in the resource view (or package view)
  2. Right-click the appropriate editor that has the file type open. It can detect that my editor is open or that the editor has a certain resource open.

I've accomplished the first using 'visibleWhen'>'selection(with)'>'iterate'>'org.eclipse.core.resources.IResource(adapt)' then checking the file extension for the resource. The code is listed below. However, I'm not sure how to get the same command to only appear when you right-click the correct editor that has a file open with the correct extensions - ext1, ext2.

Checking if my editor is active resolves the second issue but doesn't seem to help since if I click on files that are not my type, it will still show the command in the context menu.

Any recommendations? The "Eclipse Plug-ins (3rd Edition)" shows some example for editor context menu but it uses actions and I want to stick with commands.

Thanks!!


  <menuContribution
        allPopups="false"
        locationURI="popup:org.eclipse.ui.popup.any?before=additions">
     <separator
           name="com.test.ide.separator1"
           visible="true">
     </separator>
     <menu
           icon="icons/sample.gif"
           label="Test Menu">
        <command
              commandId="com.test.commands.testCommand1"
              icon="icons/sample.gif"
              label="testCommand1"
              style="push"
              tooltip="This is a test command">
           <visibleWhen
                 checkEnabled="false">
              <with
                    variable="selection">
                 <iterate
                       ifEmpty="false"
                       operator="or">
                    <adapt
                          type="org.eclipse.core.resources.IResource">
                       <or>
                          <test
                                property="org.eclipse.core.resources.extension"
                                value="ext1">
                          </test>
                          <test
                                property="org.eclipse.core.resources.extension"
                                value="ext2">
                          </test>
                       </or>
                    </adapt>
                 </iterate>
              </with>
           </visibleWhen>
        </command>
     </menu>
  </menuContribution>
like image 568
blissfool Avatar asked Apr 08 '11 07:04

blissfool


3 Answers

@blissfool, I would suggest a slight restructuring. You can put your basic test (which is correct) in a org.eclipse.core.expressions.definitions block:

<extension point="org.eclipse.core.expressions.definitions">
   <definition id="org.eclipse.example.testExtension">
      <adapt type="org.eclipse.core.resources.IResource">
         <or>
             <test property="org.eclipse.core.resources.extension"
                   value="ext1">
             </test>
             <test property="org.eclipse.core.resources.extension"
                   value="ext2">
             </test>
         </or>
      </adapt>
   </definition>
</extension>

Then in your visibleWhen move the activeEditorInput test up to the top:

<visibleWhen>
   <or>
      <with variable="selection">
         <iterate ifEmtpy="false">
           <reference definitionId="org.eclipse.example.testExtension"/>
         </iterate>
      </with>
      <with variable="activeEditorInput">
        <reference definitionId="org.eclipse.example.testExtension"/>
      </with>
   </or>
</visibleWhen>
like image 170
Paul Webster Avatar answered Oct 17 '22 01:10

Paul Webster


You could implement your own PropertyTester.

like image 29
Matthias Kempka Avatar answered Oct 17 '22 00:10

Matthias Kempka


I was able to get it done with a with variable that I came across. Using the same code example above:

  • Add an <or> block within the <iterate> block
  • Place the existing <adapt> block in the new <or> block
  • Add a new with variable called activeEditorInput

Here is the new code example.

<iterate ifEmpty="false" operator="or">
  <or>
    <adapt type="org.eclipse.core.resources.IResource">
      <or>
        ...test extensions
      </or>
    </adapt>
    <with variable="activeEditorInput">
      <adapt type="org.eclipse.core.resources.IResource">
        <or>
          ...test extensions
        </or>
      </adapt>
    </with>
  </or>
</iterate>
like image 40
blissfool Avatar answered Oct 17 '22 01:10

blissfool