可见当命令出现在上下文菜单中 [英] visibleWhen for command to appear in context menu
问题描述
- 右键单击某些文件类型(资源)在资源视图(或程序包视图)中
- 右键单击打开文件类型的相应编辑器。它可以检测到我的编辑器是开放的,或者编辑器有一定的资源打开。
我已经完成了第一次使用'visibleWhen '>'selection(with)'>'iterate'>'org.eclipse.core.resources.IResource(adapt)',然后检查资源的文件扩展名。代码如下。但是,我不知道如何获得相同的命令,只有当您右键单击正确的编辑器,打开一个文件,扩展名为ext1,ext2。
检查我的编辑器是否处于活动状态,解决了第二个问题,但似乎没有帮助,因为如果我点击不是我的类型的文件,它仍将在上下文菜单中显示该命令。
任何建议?
Eclipse插件(第3版)显示了编辑器上下文菜单的一些示例,但它使用动作,我想坚持命令。
谢谢!!
< menuContribution
allPopups =false$ X- 20045 X-454545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045
< separator
name =com.test.ide.separator1
visible =true>
< / separator>
< menu
icon =icons / sample.gif
label =测试菜单>
< command
commandId =com.test.commands.testCommand1
icon =icons / sample.gif
label =testCommand1
style = push
tooltip =这是一个测试命令>
< visibleWhen
checkEnabled =false>
< with
variable =selection>
< iterate
ifEmpty =false
operator =or>新新旗200 200 200 200 200 200 200 X- 20045 CEEC X-
<或>
< test
property =org.eclipse.core.resources.extension
value =ext1>
< / test>
< test
property =org.eclipse.core.resources.extension
value =ext2>
< / test>
< /或>
< / adapt>
< / iterate>
< / with>
< / visibleWhen>
< / command>
< / menu>
< / menuContribution>
我可以使用与
变量,我遇到了。使用上面相同的代码示例:
- 添加一个
<或>
< iterate>
块 - 将现有的
< adapt>
阻止新的<或>
块 - 添加一个新的
与
变量名为activeEditorInput
这是新的代码示例。
< iterate ifEmpty =falseoperator =or>
<或> X-45454545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045:
<或>
...测试扩展
< /或>
< / adapt>
< with variable =activeEditorInput>
< adapt type =org.eclipse.core.resources.IResource>
<或>
...测试扩展
< /或>
< / adapt>
< / with>
< /或>
< / iterate>
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:
- Right-click certain file types(resources) in the resource view (or package view)
- 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>
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 calledactiveEditorInput
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>
这篇关于可见当命令出现在上下文菜单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!