如何在eclipse rcp中的视图工具栏中初始化切换按钮选择状态

时间:2010-10-27 21:32:37

标签: eclipse eclipse-rcp

我在我的RCP中为我的视图贡献了一个切换式工具栏贡献。现在,我想知道如何从我的视图设置按钮的状态(因为它是一个切换按钮)。或者,至少,如何在加载视图后初始化它的状态(切换状态可以变化,它不是静态的)

我试图从我的视图中调用:getViewSite()。getActionBars()。getMenuManager()。getItems()(返回一个IContributionElements数组),我迭代并查找id。但是数组只包含按钮的模型,并且不可能通过这些对象更改选择。

帮助!!

3 个答案:

答案 0 :(得分:6)

在命令的定义中(在plug-in.xml中)CommandContributionItem 调用,定义一个状态元素,如下所示:

<state class="org.eclipse.ui.handlers.RegistryToggleState:true"
     id="org.eclipse.ui.commands.toggleState">
</state>

以上将根据您在'RegistryToggleState:'部分之后指定的内容将状态(切换开/关)初始化为true / false。

要更改代码中的状态,请首先像以前一样获取ParamterizedCommand的引用。然后从Command获取对underyling ParamaterizedCommmand对象的引用,并调用:

 HandlerUtil.toggleCommandState(command);

答案 1 :(得分:3)

您可以将项目投放到ActionContributionItem,获取Action并致电setChecked()

((ActionContributionItem) item).getAction().setChecked(true);

答案 2 :(得分:0)

要将状态更改为特定的状态,请尝试使用command.getState()和state.setValue()方法,如示例所示:

private void refreshToggleButtonState(String commandID, String constantID) {

    ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);  
    Command command = commandService.getCommand(commandID);  

    State state = command.getState(RegistryToggleState.STATE_ID);
    if (Activator.getDefault().getPreferenceStore().getBoolean(constantID)) {
        state.setValue(Boolean.TRUE);
    } else {
        state.setValue(Boolean.FALSE);
    }
}

constantID是一个首选项存储参数,假设您有一个。