0

我正在使用以下代码动态(在运行时)创建命令(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);

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

4

1 回答 1

0

Tru 使用 HandlerUtil,就像那样

HandlerUtil.toggleCommandState(command);

希望能帮助到你。

于 2013-01-18T11:39:23.680 回答