handler执行的时候,eclipse会传入一个ExecutionEvent参数,通过调用该参数的getApplicationContext() 方法,可以获取一个上下文描述对象(Object)。如果这个上下文描述对象是 IEvaluationContext 的实例,则表示这是一个用 eclipse CoreExpression 描述的上下文对象(在command、handler架构中,按钮是通过 CoreExpression 绑定到view上的),在这个对象中,可以通过 getVariable(String name) 方法获取大量的信息,(可用的name列表可以在 org.eclipse.ui.ISource 中查到)例如:获取当前的view就用如下方法:
Object o = getVariable(event, ISources.ACTIVE_PART_NAME)
不过这样通过名称调用还是比较晦涩,还好eclipse提供了 org.eclipse.ui.handlers.HandlerUtil 类,在这个工具类里,将常用的获取上下文的方法全部封装好了。
下面是个例子
public Object execute(ExecutionEvent event) throws ExecutionException {
MessageDialog.openInformation(PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell(), "info", "copy stack");
IWorkbenchPart view = HandlerUtil.getActivePart(event);
if (view != null) {
System.out.println(view.getTitle());
}
return null;
}
posted on 2009-04-08 23:21
Rick Murphy 阅读(306)
评论(0) 编辑 收藏 所属分类:
eclipse插件开发