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 に答える