我负责重构我公司的 Eclipse 插件中的弹出菜单,以使用新的命令/处理程序/菜单扩展点方案。我们有一些关于何时启用或不启用某些菜单项的业务逻辑,这是通过他们应用于操作的 SelectionListener 的 onSelectionChanged 方法来处理的。我现在正在尝试使用自定义 PropertyTester 来控制启用,但似乎我的自定义 PropertyTesters 没有被加载。我已经关注了 Eclipse 的文档以及我可以在网上找到的任何教程,但由于某种原因它只是不想工作。即使我简单地将“测试”方法设置为返回 true,它也不起作用,因为测试器甚至从未加载过(我在其中设置了一些断点并且它们从未被击中)。我试过设置“forcePluginActivation='true'” 标记在 xml 中的测试,但这也不能补救这种情况。这是一些代码,将公司名称替换为“公司”,将产品替换为“产品”...
命令 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 代码,我尝试了当前的实现以及注释掉的内容。我尝试了很多不同的变体,我在教程中看到了这些变体,这些变体适用于其他人,但不适用于我。消息永远不会打印到控制台,如果我在构造函数或测试方法中设置断点,它们永远不会被命中。请让我知道其他信息是否有用...