我负责重构公司 Eclipse 插件中的弹出菜单,以使用新的命令/处理程序/菜单扩展点方案。我们有一些关于何时启用或不启用某些菜单项的业务逻辑,这是通过应用于操作的 SelectionListener 的 onSelectionChanged 方法来处理的。我现在尝试使用自定义 PropertyTester 来控制启用,但似乎我的自定义 PropertyTester 尚未加载。我已经遵循了 Eclipse 的文档以及我可以在网上找到的任何教程,但由于某种原因它只是不想工作。即使我只是将“测试”方法设置为返回 true,它也不起作用,因为测试器甚至从未加载过(我放入了一些断点,但它们从未被击中)。我尝试在 xml 的测试中设置“forcePluginActivation='true'”标志,但这也无法解决这种情况。这是一些代码,其中公司名称替换为“company”,产品替换为“product”...
命令 XML
<extension
point="org.eclipse.ui.commands">
<command
categoryId="com.company.product.android.core.category2"
id="com.company.product.android.core.commands.convertToProduct"
name="Convert to product Android Project"
description="The resources, jars and native library to begin developing an product for Android project application will be added to the Android project">
</command>
<category
id="com.company.product.android.core.category2"
name="Product Tools">
</category>
</extension>
处理程序 XML
<extension
point="org.eclipse.ui.handlers">
<handler
class="com.company.product.android.core.commands.ConvertToProduct"
commandId="com.company.product.android.core.commands.convertToProduct">
<enabledWhen>
<reference
definitionId="com.company.product.android.core.expressions.productProject">
</reference>
</enabledWhen>
</handler>
</extension>
定义 XML
<extension>
point="org.eclipse.core.expressions.definitions">
<definition
id="com.company.product.android.core.expressions.androidProjectNature">
<with variable="selection">
<iterate ifEmpty="false">
<test
property="org.eclipse.core.resources.projectNature"
value="com.android.ide.eclipse.adt.AndroidNature">
</test>
</iterate>
</with>
</definition>
<definition
id="com.company.product.android.core.expressions.productProject">
<with variable="selection">
<iterate ifEmpty="false">
<test
property="com.company.product.android.expressions.productProjectProperty"
forcePluginActivation="true">
</test>
</iterate>
</with>
</definition>
</extension>
菜单 XML
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions">
<menu
icon="icons/wht_ws.gif"
id="com.company.product.android.core.tools"
label="Product Tools">
<visibleWhen>
<reference
definitionId="com.company.product.android.core.expressions.androidProjectNature">
</reference>
</visibleWhen>
<command
commandId="com.company.product.android.core.commands.convertToProduct"
label="Convert to Product Android Project"
tooltip="The resources, jars and native library to begin developing an product for Android project application will be added to the Android project.">
</command>
</menu>
</menuContribution>
</extension>
属性测试器 XML
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.company.product.android.core.expressions.ProductProjectTester"
id="com.company.product.android.core.expressions.productProjectTester"
namespace="com.company.product.android.core.expressions"
properties="productProjectProperty"
type="java.lang.Object">
</propertyTester>
</extension>
性能测试仪JAVA
package com.company.product.android.core.expressions;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.PlatformUI;
import com.company.product.android.core.ProjectUtils;
public class ProductProjectTester extends PropertyTester {
public ProductProjectTester() {
System.out.println("I AM PRODUCT PROJECT TESTER, AND I HAVE BEEN CREATED");
}
@Override
public boolean test(Object receiver, String property, Object[] args,
Object expectedValue) {
//IJavaProject project = null;
//ISelectionService selectionService =
// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
//ISelection selection = selectionService.getSelection();
//boolean enabled = ProjectUtils.isSingleProjectSelected(selection);
System.out.println("I entered the test method");
boolean enabled = false;
System.out.println("The receiver is a " + receiver.getClass().toString());
IJavaProject project = (IJavaProject)receiver;
//if (enabled && selection instanceof StructuredSelection) {
//project = ProjectUtils.getJavaProject();
if (project != null) {
enabled = ProjectUtils.hasNeedVersion(project)
&& (!ProjectUtils.isProductAndroidProject(project.getProject()));
}
//}
return enabled;
}
}
对于java代码,我尝试了当前的实现以及注释掉的内容。我尝试了很多不同的变体,我在教程中看到这些变体对其他人有效,但对我无效。这些消息永远不会打印到控制台,如果我在构造函数或测试方法中设置断点,它们永远不会被命中。如果其他信息有用,请告诉我...
最佳答案
我在我的项目中使用 PropertyTester,它运行良好
在您的 DEFINITIONS XML 中 您没有正确引用 PropertyTester 定义:
<definition
id="com.company.product.android.core.expressions.productProject">
<with variable="selection">
<iterate ifEmpty="false">
<test
property="com.company.product.android.**core**.expressions.productProjectProperty"
forcePluginActivation="true">
</test>
</iterate>
</with>
</definition>
然后在你的 MENU.XML 中 您应该引用正确的定义(productProject 而不是 androidProjectNature)
<menu
icon="icons/wht_ws.gif"
id="com.company.product.android.core.tools"
label="Product Tools">
<visibleWhen>
<with variable="activeMenuSelection">
<iterate ifEmpty="false">
<reference
definitionId="com.company.product.android.core.expressions.productProject"/>
</iterate>
</with>
</visibleWhen>
<command
commandId="com.company.product.android.core.commands.convertToProduct"
label="Convert to Product Android Project"
tooltip="The resources, jars and native library to begin developing an product for Android project application will be added to the Android project.">
</command>
</menu>
最后,您不需要 HANDLER.XML 中的 EnableWhen 标签
希望这有帮助
关于popup - 尽管有forcePluginActivation,但属性测试器不会加载。用于弹出菜单启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17458972/