2011-04-08 93 views
8

我使用的是menuContribution內「visibleWhen」表達式的上下文菜單中嘗試的命令配置的知名度。我所試圖做的是使該命令在上下文菜單中僅可見,如果您:visibleWhen的命令出現在上下文菜單

  1. 右鍵單擊資源視圖特定類型的文件(資源)(或包裝查看)
  2. 右擊打開文件類型的適當編輯器。它可以檢測到我的編輯器是打開的,或者編輯器打開了某個資源。

我已經完成了第一個使用「visibleWhen」>「選擇(含)」>「迭代」>「org.eclipse.core.resources.IResource(適應)」,然後檢查資源的文件擴展名。代碼如下所示。但是,我不知道如何得到相同的命令,當你用鼠標右鍵單擊有正確的擴展名打開一個文件正確的編輯只出現 - EXT1,EXT2。

察看我的編輯是主動解決的第二個問題,但似乎並沒有幫助,因爲如果我點擊那個不是我喜歡的類型文件時,它仍然會顯示在上下文菜單中的命令。

任何建議? 的「Eclipse插件(第三版)」顯示了編輯器上下文菜單中的一些例子,但它使用的行爲,我想堅持的命令。

謝謝!


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

回答

1

我能得到它與我碰到一個with變量來完成。使用相同的代碼示例以上:

  • <iterate>塊內添加<or>
  • 放置現有<adapt>塊在新<or>
  • 添加一個新的名爲with可變activeEditorInput

這是新的代碼示例。

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

這是正確的,但我會建議輕微調整優化: – 2011-04-28 12:58:40

1

你可以實現自己的PropertyTester

+0

感謝。我對自定義PropertyTester尚不熟悉,甚至不熟悉插件API。我遇到了另一個「with」變量,它幫助我完成了這個任務! – blissfool 2011-04-20 08:39:52

+0

你介意舉個例子......? – Campa 2017-09-04 13:51:53

9

@blissfool,我會建議稍作重組。你可以把你的基本測試(這是正確的)在org.eclipse.core.expressions.definitions塊:

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

然後在你的visibleWhen移動activeEditorInput測試到頂部:

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

啊。對。感謝那。我讀到org.eclipse.core.expressions.definitions,想着我怎麼能減少重複條件,但並沒有把兩者結合起來。我太專注於讓功能工作。乾杯〜:) – blissfool 2011-04-29 09:05:58