从代码更新 Eclipse 菜单单选状态

Update Eclipse menu radio state from code

在无线电状态菜单项的命令处理程序中,我提示用户是否要继续。如果他们选择否,我需要将所选菜单项重置为之前的值。

我正在使用 HandlerUtil.updateRadioState(event.getCommand(), oldScope) 但菜单保持新值。

有什么建议吗?

命令处理程序

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    if (HandlerUtil.matchesRadioState(event))
        return null;

    String scope = event.getParameter(RadioState.PARAMETER_ID);

    if (MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm restart",
            "Switching source of Motors requires a restart.  Continue?")) {
        int connection = MotorDbPreferencesPage.DATABASE_ACCESS_LOCAL;
        if ("Cloud".compareToIgnoreCase(scope) == 0) {
            connection = MotorDbPreferencesPage.DATABASE_ACCESS_CLOUD;
        }
        Activator.getDefault().getPreferenceStore().setValue(MotorDbPreferencesPage.DATABASE_ACCESS, connection);
        PlatformUI.getWorkbench().restart();
    } else {
        String oldScope = "Local";
        // switch it back to the way it was before the user selected it
        if ("Local".compareToIgnoreCase(scope) == 0) {
            oldScope = "Cloud";
        }
        HandlerUtil.updateRadioState(event.getCommand(), oldScope);
    }

    return null;
}

命令定义在plugin.xml

      <command
        id="com.client.eclipse.connection"
        name="Connection">
     <commandParameter
           id="org.eclipse.ui.commands.radioStateParameter"
           name="State"
           optional="false">
     </commandParameter>
     <state
           class="org.eclipse.ui.handlers.RadioState:Cloud"
           id="org.eclipse.ui.commands.radioState">
     </state>
  </command>

以及来自 plugin.xml

的菜单定义
            <menu
              label="Connection">
           <command
                 commandId="com.client.eclipse.connection"
                 label="Local"
                 style="radio">
              <parameter
                    name="org.eclipse.ui.commands.radioStateParameter"
                    value="Local">
              </parameter>
           </command>
           <command
                 commandId="com.client.eclipse.connection"
                 label="Cloud"
                 style="radio">
              <parameter
                    name="org.eclipse.ui.commands.radioStateParameter"
                    value="Cloud">
              </parameter>
           </command>
        </menu>

您有责任致电

HandlerUtil.updateRadioState(event.getCommand(), scope);

使 GUI 显示新状态。不调用,广播状态不会改变

意思是在用户中止的情况下,根本不要调用这个方法。