如何在使用编程菜单

时间:2016-01-21 19:38:43

标签: eclipse-rcp e4

我正在使用MenuContribution来提供菜单条目,实现类似E3中的“切换工作区”的功能。 在动态菜单贡献中,我正在构建最近打开的项目的3项列表,后跟“其他...”条目。 从图形上看,我已经完成了3个列表,分隔符和“其他...”菜单元素。

但是在最近的项目中,我必须动态地将项目名称/ Path传递给使用选择事件的处理程序。 下面的代码类似于我在菜单贡献中创建的代码,它创建了3个最近的项目项目之一:

@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);
}

其中LOAD_PROJECT_COMMAND_ID和PROJECT_NAME_PARAMETER_ID是e4xmi命令和命令参数id。 我想知道如何将projectName放入命令中以便能够将其返回到关联的处理程序中,该处理程序包含以下内容:

@Execute
public void execute(ParameterizedCommand command) {
[...]
}

注意:我读过Lars tutorial about menus但没有在那里找到解决方案

---编辑:完整贡献代码---

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;
    }
}

1 个答案:

答案 0 :(得分:1)

您可以将参数值添加到MHandledMenuItem而不是命令。

使用MParameter并调用setNamesetValue方法设置名称(必须与命令中的参数名称匹配)和值(在您的情况下为项目名称)。

MParameter添加到MHandledMenuItem.getParameters()列表。

注意:您应该只定义命令和命令参数一次,而不是每次调用aboutToShow时(因此应该在e4xmi文件中)。