Eclipse rcp 使用编程菜单时如何管理命令参数
我使用MenuContribution为菜单项提供信息,实现了E3中的“切换工作区”功能。 在动态菜单贡献中,我正在构建一个包含最近打开的项目的3项列表,后面是“Other..”条目。 从图形上看,我完成了,3个列表、分隔符和“其他…”菜单元素显示出来 但是对于最近的项目,我必须动态地将项目名称/路径传递给使用选择事件的处理程序。 下面是一个类似于我在菜单贡献中创建的3个最近项目项之一的代码:Eclipse rcp 使用编程菜单时如何管理命令参数,eclipse-rcp,e4,Eclipse Rcp,E4,我使用MenuContribution为菜单项提供信息,实现了E3中的“切换工作区”功能。 在动态菜单贡献中,我正在构建一个包含最近打开的项目的3项列表,后面是“Other..”条目。 从图形上看,我完成了,3个列表、分隔符和“其他…”菜单元素显示出来 但是对于最近的项目,我必须动态地将项目名称/路径传递给使用选择事件的处理程序。 下面是一个类似于我在菜单贡献中创建的3个最近项目项之一的代码: @AboutToShow public void aboutToShow(List<MMenuE
@AboutToShow
public void aboutToShow(List<MMenuElement> items, MApplication application) {
MHandledMenuItem dynamicItem = modelService.createModelElement(MHandledMenuItem.class);
dynamicItem.setLabel(projectName);
dynamicItem.setContributorURI("platform:/plugin/com.acme");
MCommand command = modelService.createModelElement(MCommand.class);
command.setElementId(LOAD_PROJECT_COMMAND_ID);
MCommandParameter commandParam = modelService.createModelElement(MCommandParameter.class);
commandParam.setElementId(PROJECT_NAME_PARAMETER_ID);
commandParam.setName(PROJECT_NAME_PARAMETER_ID);
command.getParameters().add(commandParam);
// one of the 3 last used projects
String projectName = "foo";
dynamicItem.setCommand(command);
items.add(dynamicItem);
}
注意:我读了,但没有找到解决方案
---编辑:完整贡献代码--
import java.util.Collections;
导入java.util.List;
导入javax.annotation.PostConstruct;
导入javax.inject.inject;
导入org.eclipse.e4.core.commands.ECommandService;
导入org.eclipse.e4.ui.di.AboutToShow;
导入org.eclipse.e4.ui.model.application.MApplication;
导入org.eclipse.e4.ui.model.application.commands.MCommand;
导入org.eclipse.e4.ui.model.application.commands.mpareter;
导入org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem;
导入org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
导入org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
导入org.eclipse.e4.ui.model.application.ui.menu.MMenuSeparator;
导入org.eclipse.e4.ui.workbench.modeling.EModelService;
导入com.acme.model.platypus.extractionresult.ActivityResultProvider;
@禁止警告(“限制”)
公共类SwitchProjectMenuContribution{
私有静态最终字符串NEW_PROJECT=“其他…”;
私有静态最终字符串LOAD\u PROJECT\u COMMAND\u ID=“gui.rcp4.COMMAND.loadProjectCommand”;
私有静态最终字符串PROJECT\u NAME\u参数\u ID=“gui.rcp4.command.loadProjectCommand.projectName”;
@注入
活动结果提供者活动结果提供者;
@注入
私有EModelService模型服务;
@注入
ECommandService命令服务;
私有MDirectMenuItem其他项目项;
私人分离机分离机;
专用MCommand loadProjectCommand;
@抑制警告(“未选中”)
@施工后
公共void初始化(MapApplication应用程序){
loadProjectCommand=(MCommand)模型服务
.findElements(应用程序、加载\项目\命令\ ID、MCommand.class、集合.空\列表)。get(0);
otherProjectItem=modelService.createModelElement(MDirectMenuItem.class);
otherProjectItem.setLabel(新项目);
otherProjectItem.setContributorURI(“平台:/plugin/com.acme.gui.rcp4”);
otherProjectItem.setContributionURI(
“不ndleclass://com.acme.gui.rcp4/com.acme.gui.rcp4.handlers.OtherProjecthandler");
separatorItem=modelService.createModelElement(MMenuSeparator.class);
}
@关于表演
关于ToShow的公共作废(列表项、地图应用程序){
字符串[]lastProject=ActivityResultProvider.getLastUsed();
新词元;
用于(字符串项目名称:lastProject){
newEntry=createExistingProjectEntry(项目名称);
添加(新条目);
}
如果(lastProject.length>0){
项目。添加(单独项目);
}
项目。添加(其他项目项);
}
私有MHandledMenuItem createExistingProjectEntry(字符串projectPath){
MHandledMenuItem dynamicItem=modelService.createModelElement(MHandledMenuItem.class);
dynamicItem.setLabel(项目路径);
dynamicItem.setContributorURI(“平台:/plugin/com.acme.gui.rcp4”);
MPParameter commandParam=modelService.createModelElement(MPParameter.class);
commandParam.setName(“项目名称”);
commandParam.setElementId(项目名称参数ID);
commandParam.setValue(项目路径);
dynamicItem.getParameters().add(commandParam);
dynamicItem.setCommand(loadProjectCommand);
返回动态项;
}
}
将参数值添加到MHandledMenuItem
而不是命令中
使用MParameter
并调用setName
和setValue
方法来设置名称(必须与命令中的参数名称匹配)和值(在您的案例中为项目名称)
将MParameter
添加到MHandledMenuItem.getParameters()
列表中
注意:您应该只定义一次命令和命令参数,而不是每次调用
aboutToShow
(因此可能在e4xmi文件中)。您确定要重用MPareter吗?如果我对5个不同的菜单项重复使用同一个菜单项,我认为会出现“value”属性冲突(我在真正的“aboutToShow()”主体中有一个循环,您只需要创建一次MCommand和MCommandParameter。MHandledMenuItem和MParameter是每个菜单项的一个。
@Execute
public void execute(ParameterizedCommand command) {
[...]
}
import java.util.Collections;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.eclipse.e4.core.commands.ECommandService;
import org.eclipse.e4.ui.di.AboutToShow;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.commands.MCommand;
import org.eclipse.e4.ui.model.application.commands.MParameter;
import org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem;
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuSeparator;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import com.acme.model.platypus.extractionresult.CampaignResultProvider;
@SuppressWarnings("restriction")
public class SwitchProjectMenuContribution {
private static final String NEW_PROJECT = "Other...";
private static final String LOAD_PROJECT_COMMAND_ID = "gui.rcp4.command.loadProjectCommand";
private static final String PROJECT_NAME_PARAMETER_ID = "gui.rcp4.command.loadProjectCommand.projectName";
@Inject
CampaignResultProvider campaignResultProvider;
@Inject
private EModelService modelService;
@Inject
ECommandService commandService;
private MDirectMenuItem otherProjectItem;
private MMenuSeparator separatorItem;
private MCommand loadProjectCommand;
@SuppressWarnings("unchecked")
@PostConstruct
public void initialize(MApplication application) {
loadProjectCommand = (MCommand) modelService
.findElements(application, LOAD_PROJECT_COMMAND_ID, MCommand.class, Collections.EMPTY_LIST).get(0);
otherProjectItem = modelService.createModelElement(MDirectMenuItem.class);
otherProjectItem.setLabel(NEW_PROJECT);
otherProjectItem.setContributorURI("platform:/plugin/com.acme.gui.rcp4");
otherProjectItem.setContributionURI(
"bundleclass://com.acme.gui.rcp4/com.acme.gui.rcp4.handlers.OtherProjecthandler");
separatorItem = modelService.createModelElement(MMenuSeparator.class);
}
@AboutToShow
public void aboutToShow(List<MMenuElement> items, MApplication application) {
String[] lastProject = campaignResultProvider.getLastUsed();
MMenuElement newEntry;
for (String projectName : lastProject) {
newEntry = createExistingProjectEntry(projectName);
items.add(newEntry);
}
if (lastProject.length > 0) {
items.add(separatorItem);
}
items.add(otherProjectItem);
}
private MHandledMenuItem createExistingProjectEntry(String projectPath) {
MHandledMenuItem dynamicItem = modelService.createModelElement(MHandledMenuItem.class);
dynamicItem.setLabel(projectPath);
dynamicItem.setContributorURI("platform:/plugin/com.acme.gui.rcp4");
MParameter commandParam = modelService.createModelElement(MParameter.class);
commandParam.setName("projectName");
commandParam.setElementId(PROJECT_NAME_PARAMETER_ID);
commandParam.setValue(projectPath);
dynamicItem.getParameters().add(commandParam);
dynamicItem.setCommand(loadProjectCommand);
return dynamicItem;
}
}