【问题标题】:Populating view menu dynamically using commands使用命令动态填充视图菜单
【发布时间】:2013-01-18 10:06:36
【问题描述】:

我正在使用下面的代码动态(在运行时)创建命令(STYLE_CHECK)并将它们添加到查看菜单。

final IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getService(IMenuService.class);
AbstractContributionFactory viewMenuAddition = new AbstractContributionFactory("menu:show?after=additions",
        null) {
    @Override
    public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
    List<String> hardLabels = ReadVzFile.getHwLabels();
    LinkedHashSet<String> sections = new LinkedHashSet<String>();
    //List will be poulated here
    for (Iterator<String> iterator = sections.iterator(); iterator.hasNext();) {
        String hardLabel = iterator.next();

        ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);
        Command command = commandService.getCommand(hardLabel);
        RegistryToggleState registryToggleState = new RegistryToggleState();
        registryToggleState.setInitializationData(null, null, "true");
        registryToggleState.setShouldPersist(false);
        System.out.println(registryToggleState.getValue());
        command.addState("org.eclipse.ui.commands.toggleState", registryToggleState);
        command.define(hardLabel, hardLabel + "command created dynamically", commandService
            .getCategory("org.eclipse.ui.category.window"));
        IHandlerService handlerService = (IHandlerService) serviceLocator.getService(IHandlerService.class);
        handlerService.activateHandler(command.getId(), new AbstractHandler() {

        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
            System.out.println("Command executed !" + event.getCommand().getId());
            System.out.println(event.getCommand().getState("org.eclipse.ui.commands.toggleState")
                .getValue());
            return null;
        }

        });

        // build a couple of command-based contribution parameters
        CommandContributionItemParameter pAA = new CommandContributionItemParameter(serviceLocator,
            hardLabel + " command menu", hardLabel, CommandContributionItem.STYLE_CHECK);
        pAA.label = hardLabel;
        CommandContributionItem itemAA = new CommandContributionItem(pAA);
        itemAA.setVisible(true);
        additions.addContributionItem(itemAA, null);
    }
    }
};
menuService.addContributionFactory(viewMenuAddition);

但是当我在视图菜单中看到所有菜单项都未选中(已选择)。 这段代码有什么问题?

【问题讨论】:

    标签: eclipse-plugin eclipse-rcp


    【解决方案1】:

    Tru 使用 HandlerUtil,就像那样

    HandlerUtil.toggleCommandState(command);
    

    希望对你有帮助。

    【讨论】:

    • No not working..在“additions.addContributionItem(itemAA, null);”之前添加语句“HandlerUtil.toggleCommandState(command);”之后
    • 您是否尝试删除/注释掉您自己的代码,这会修改状态?此外,您可能希望将您的代码或对 HandlerUtil.toggleCommandState 的调用放入 AbstractHandler 中的“执行”方法中,因为我认为实际上是您想要更改按钮状态的时候了。让我知道这是否有帮助。
    • 否,如果我删除添加状态的代码,即 RegistryToggleState registryToggleState = new RegistryToggleState(); registryToggleState.setInitializationData(null, null, "true"); registryToggleState.setShouldPersist(false); System.out.println(registryToggleState.getValue()); command.addState("org.eclipse.ui.commands.toggleState", registryToggleState);
    • 来自“ HandlerUtil.toggleCommandState(command);"
    • 我希望默认选择所有菜单项,因此如果我在处理程序执行方法中编写代码,那么这将在单击菜单项时执行。目前菜单项显示如下link,但我希望这些必须显示为link
    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2013-06-17
    • 1970-01-01
    相关资源
    最近更新 更多