【问题标题】:Update Eclipse menu radio state from code从代码更新 Eclipse 菜单单选状态
【发布时间】:2023-03-29 14:12:01
【问题描述】:

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

我正在使用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>

【问题讨论】:

    标签: eclipse eclipse-plugin eclipse-rcp


    【解决方案1】:

    打电话是你的责任

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

    使 GUI 显示新的状态。不调用它,无线电状态不会改变。

    表示在用户正在中止的情况下,根本不要调用这个方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 2022-01-27
      • 1970-01-01
      • 2019-06-12
      相关资源
      最近更新 更多