我的 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;
但它不起作用。