开发者

How to initialize a toggle button's selection state in a view's toolbarin an eclipse rcp

开发者 https://www.devze.com 2023-01-22 12:02 出处:网络
I contributed a toggle-style toolbar contribution to my view in my RCP. Now, i want to know how to set the button\'s state (since it\'开发者_如何学Pythons a toggle button) from my view. Or, at least,

I contributed a toggle-style toolbar contribution to my view in my RCP. Now, i want to know how to set the button's state (since it'开发者_如何学Pythons a toggle button) from my view. Or, at least, how to initialize it's state after the view is loaded (the toggle state can vary, its not static)

I tried to call from my view: getViewSite().getActionBars().getMenuManager().getItems() (returns an array of IContributionElements), which i iterated over and looked for the id. but the array only contains the models of the buttons, and there is no possibility to change the selection through these objects.

Help!!


In the definition of your command (in plug-in.xml) that the CommandContributionItem calls into, define a state element like the following:

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

The above will initialise the state (toggle on/off) to either true/false depending on what you specify after the 'RegistryToggleState:' section.

To change the state within your code, first get the reference to your ParamterizedCommand like you did before. Then get the reference to the underyling Commandobject from the ParamaterizedCommmand and call:

 HandlerUtil.toggleCommandState(command);


You can cast the item to ActionContributionItem, get the Action and call setChecked():

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


To change the state to a specific one, try command.getState(), and state.setValue() methods, as shown in example:

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 is a preference store parameter, assuming you have one.

0

精彩评论

暂无评论...
验证码 换一张
取 消