1

我正在使用目标平台 3.7 编写 RCP 应用程序。我喜欢仅在特定视图处于活动状态时才启用 menuItem,否则应将其禁用。我通过如下 plugin.xml 中所示的表达式尝试它,但 menuItem 始终处于活动状态。

 <extension
         point="org.eclipse.ui.commands">
      <command
            defaultHandler="pgui.handler.SaveHandler"
            id="pgui.rcp.command.save"
            name="Save">
      </command>
   </extension>
   <extension
     point="org.eclipse.ui.views">
     <view
          allowMultiple="true"
          class="pgui.view.LogView"
          id="pgui.view.LogView"
          name="logview"
          restorable="true">
     </view>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            allPopups="false"
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               id="fileMenu"
               label="File">
            <command
                  commandId="pgui.rcp.command.save"
                  label="Save"
                  style="push"
                  tooltip="Save the active log file.">
            </command>
         </menu>
      </menuContribution>
    </extension>
    <extension
         point="org.eclipse.ui.handlers">
      <handler
            commandId="pgui.rcp.command.save">
         <activeWhen>
            <with
                  variable="activePart">
               <instanceof
                     value="pgui.view.LogView">
               </instanceof>
            </with>
         </activeWhen>
      </handler>
   </extension>
4

1 回答 1

3

首先,从您的命令中删除defaultHandler 。

接下来,将您的处理程序类添加到您的处理程序扩展点。

基本上,该机制允许您为同一命令定义多个处理程序,使用不同的activeWhen表达式让命令在不同情况下由不同的处理程序类处理。

如果命令的所有已定义处理程序上的所有activeWhen表达式评估为 false,并且为命令本身定义了defaultHandler,则该默认处理程序将用于命令。该命令本质上将始终处于活动状态,因为始终有一个默认处理程序来处理它。

例如,如果您同时拥有现有的 LogView 和另一个充满独角兽的视图,并且您想使用相同的pgui.rcp.command.save命令来处理来自任一视图的项目的保存:

<extension point="org.eclipse.ui.commands">
    <command
        id="pgui.rcp.command.save"
        name="Save">
    </command>
</extension>
:
<extension point="org.eclipse.ui.handlers">
    <handler
        class="pgui.handler.SaveLogHandler"
        commandId="pgui.rcp.command.save">

        <activeWhen>
            <with variable="activePart">
                <instanceof value="pgui.view.LogView">
                </instanceof>
            </with>
        </activeWhen>
    </handler>
</extension>
:
<extension point="org.eclipse.ui.handlers">
    <handler
        class="pgui.handler.SaveUnicornHandler"
        commandId="pgui.rcp.command.save">

        <activeWhen>
            <with variable="activePart">
                <instanceof value="pgui.view.UnicornView">
                </instanceof>
            </with>
        </activeWhen>
    </handler>
</extension>
于 2012-07-23T14:52:57.717 回答