我在 Eclipse 插件(在 Indigo 上开发)中有一个处理程序,我希望每次保存(或打开,理想情况下)文本编辑器中的 Java 文件时运行它。据我所知,有两种方法可以实现这一点:
- 通过
CommandService
API 以编程方式。 - 使用中的配置
plugin.xml
这两种我都试过了。方法1是这样的:
public class Activator extends AbstractUIPlugin {
@Override
public void start(final BundleContext context) throws Exception {
super.start(context);
ICommandService commandService = (ICommandService) PlatformUI
.getWorkbench().getService(ICommandService.class);
commandService.addExecutionListener(new MyListener());
plugin = this;
}
...
}
public class MyListener implements IExecutionListener {
@Override
public void postExecuteSuccess(final String commandId, final Object returnValue) {
System.out.println("PostEventSuccess:" + commandId);
}
@Override
public void preExecute(final String commandId, final ExecutionEvent event) {}
@Override
public void notHandled(String commandId, NotHandledException exception) {}
@Override
public void postExecuteFailure(String commandId, ExecutionException exception) {}
}
和方法2。像这样:
<extension point="org.eclipse.ui.handlers">
<handler
commandId="org.eclipse.ui.file.save"
class="mypackage.MyHandler">
<activeWhen>
<with variable="activeWorkbenchWindow">
<instanceof value="org.eclipse.ui.IWorkbenchWindow"/>
</with>
</activeWhen>
</handler>
</extension>
方法 1. 根本不运行我的侦听器。方法 2. 有效,因为处理程序被执行,但我的处理程序似乎替换了默认处理程序。当用户尝试保存文件时,我的处理程序运行但文件没有保存。处理程序本身似乎很好(我已经通过创建一个运行它的菜单项对其进行了测试)。
方法 1. 现在已弃用还是我错误地实施了该服务?如何获得方法 2. 保存文件?可以从我的处理程序运行默认处理程序吗?