僅當文本被標記為
以下選定是我的 XML 相關片段時,我才想向 eclipse 背景關系選單添加一個專案:
<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>
我嘗試了如何將命令添加到 Eclipse 中的編輯器背景關系選單中的解決方案,但它沒有解決問題
我錯過了什么?
你能幫我嗎?
由于
阿夫納
請原諒我的詳細問題,我是
我嘗試過的Eclipse 編程的新手:
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
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;
}
}
但是這個類從來沒有被使用過
我錯過了什么?
uj5u.com熱心網友回復:
問題是編輯器通常總是有一個文本選擇集 - 如果沒有選擇任何字符,它的長度將為零。
我看不到使用現有運算式對此進行測驗的方法,因此可能需要使用org.eclipse.core.expressions.propertyTester擴展點定義您自己的屬性測驗器。
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="tested.TextSelectedPropertyTester"
id="tested.propertyTester1"
namespace="tested"
properties="textSelected"
type="org.eclipse.jface.text.ITextSelection">
</propertyTester>
</extension>
像這樣使用:
<visibleWhen>
<with variable="selection">
<adapt type="org.eclipse.jface.text.ITextSelection">
<test
property="tested.textSelected"
forcePluginActivation="true">
</test>
</adapt>
</with>
</visibleWhen>
屬性測驗器非常簡單:
public class TextSelectedPropertyTester extends PropertyTester
{
@Override
public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
{
if (receiver instanceof ITextSelection textSel) {
return textSel.getLength() > 0;
}
return false;
}
}
請注意,該if陳述句使用 Java 16 instanceof 型別模式,如果您需要使用較舊的 Java 運行,請使用:
if (receiver instanceof ITextSelection) {
return ((ITextSelection)receiver).getLength() > 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/377194.html
標籤:蚀
