【发布时间】:2022-01-10 21:42:59
【问题描述】:
我想在 Eclipse 上下文菜单中添加一个项目仅如果文本被标记为选中
以下是我的 XML 相关 sn-p:
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any">
<command
commandId="com.test.ide.menu.commands.sampleCommand"
icon="icons/sample.png"
id="com.test.ide.menu.toolbars.sampleCommand"
tooltip="Popup test">
<visibleWhen>
<with variable="selection">
<instanceof value="org.eclipse.jface.text.ITextSelection"/>
</with>
</visibleWhen>
</command>
</menuContribution>
我尝试了How do you contribute a command to an editor context menu in Eclipse 中的解决方案,但无法解决问题
我错过了什么?
你能帮忙吗?
谢谢
Avner
请原谅我的详细问题,我是 Eclipse 编程的新手
我试过了:
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.test.ide.TextSelectedPropertyTester"
id="com.test.ide.propertyTester1"
namespace="tested"
properties="textSelected"
type="org.eclipse.jface.text.ITextSelection">
</propertyTester>
</extension>
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any">
<command
commandId="com.test.ide.menu.commands.sampleCommand"
icon="icons/sample.png"
id="com.test.ide.menu.toolbars.sampleCommand"
tooltip="Popup test">
<visibleWhen>
<with variable="selection">
<adapt type="org.eclipse.jface.text.ITextSelection">
<test
property="tested.textSelected">
</test>
</adapt>
</with>
</visibleWhen>
</command>
</menuContribution>
创建了一个新包 - com.test.ide 在其中创建了一个新类 TextSelectedPropertyTester
但是该类从未被调用
package com.test.ide;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.jface.text.ITextSelection;
public class TextSelectedPropertyTester extends PropertyTester
{
@Override
public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
{
System.out.println("testing");
if (receiver instanceof ITextSelection) {
return ((ITextSelection)receiver).getLength() > 0;
}
return false;
}
}
然而该类从未使用过
我错过了什么?
【问题讨论】:
-
问题是编辑器通常总是有一个文本选择集 - 如果没有选择任何字符,它将只是零长度。
-
谢谢,有没有办法检查它的长度?
-
我尝试使用 public void setEnabled(Object context) 但是看起来这只有在第一次使用新菜单项后才会生效
-
您是否在 Eclipse 启动时指定
-clean以确保它始终查找更改的 plugin.xml 详细信息?只有在当前选择是文本选择时才会运行属性测试器 - 只有在文本编辑器中才会出现这种情况。 -
我正在使用 Eclipse Committer 包(版本 2021-06 (4.20))进行开发,并通过右键单击 -> 调试为 -> Eclipse 应用程序重新启动 Eclipse,这就是我完成整个过程的方式开发我的功能并且从未遇到任何问题。在新打开的 Eclipse 中,鼠标在文本编辑器或 Java 编辑器中时,右键单击完成。无论是否标记了字符串,都会显示上下文菜单
标签: eclipse