我的ecp应用程序中有一个视图部分,它只包含一个TreeViewer
。
每个树节点都有一个动作提供程序,当右键单击该节点时,该动作提供程序将对上下文菜单有所贡献:
MenuManager menuManager = new MenuManager();
menuManager.addMenuListener(new IMenuListener(){
@Override
public void menuAboutToShow(IMenuManager manager) {
IStructuredSelection is = (IStructuredSelection)treeViewer.getSelection();
if(is.isEmpty()){
return;
}else{
Node node = (Node)is.getFirstElement();
IActionProvider provider = node.getActionProvider();
IContributionItem[] actions = provider.getActions();
if(actions.length == 0){
return;
}else{
for(IContributionItem action : actions){
manager.add(action);
}
}
}
}
});
Menu menu = menuManager.createContextMenu(treeViewer.getTree());
menuManager.setRemoveAllWhenShown(true);
treeViewer.getTree().setMenu(menu);
getSite().registerContextMenu(menuManager, treeViewer);
getSite().setSelectionProvider(treeViewer);
动作提供者示例:
public class CPSActionProvider implements IActionProvider {
private Object source;
@Override
public void setInitializationData(IConfigurationElement config,
String propertyName, Object data) throws CoreException {
// TODO Auto-generated method stub
}
@Override
public IContributionItem[] getActions() {
CommandContributionItemParameter cp = new CommandContributionItemParameter(
PlatformUI.getWorkbench().getActiveWorkbenchWindow(),
null, "cn.ggfan.dmp.commands.addCP",
CommandContributionItem.STYLE_CHECK);
HashMap<String, Object> paras = new HashMap<String, Object>();
paras.put("source", this.source);
cp.parameters = paras;
CommandContributionItem add = new CommandContributionItem(cp);
//add.setId("cn.ggfan.dmp.commands.addCP");
return new IContributionItem[] { add };
}
@Override
public void setSource(Object o) {
// TODO Auto-generated method stub
this.source = o;
}
}
cn.ggfan.dmp.commands.addCP
是一个已定义的命令,其中defaultHandler
,源字段是右键单击的节点,现在我的问题是如何将源字段传递给处理程序?正如你所看到的,我试图这样做:
HashMap<String, Object> paras = new HashMap<String, Object>();
paras.put("source", this.source);
cp.parameters = paras;
但它不起作用。
答案 0 :(得分:2)
Tree/Table Viewer
设为SelectionProvider
至PartSite
。IWorkbenchSite.java
public void setSelectionProvider(ISelectionProvider provider);
public Object execute(ExecutionEvent event)抛出ExecutionException
使用ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
/////////////////////////////////////////////// /////////////////////////////////////
如果没有工作台提供SourceProvider,还有另一种方法。您可以实现自己的
org.eclipse.ui.ISourceProvider
您有责任更新提供商地图。这是样本参考
http://www.vogella.com/articles/EclipseCommandsAdvanced/article.html