2012-07-20 90 views
1

我正在用目标平台3.7编写RCP应用程序。 我喜欢只在特定视图处于活动状态时启用menuItem,否则应禁用它。 我通过如下面的plugin.xml中所示的表达式尝试它,但menuItem始终处于活动状态。启用/禁用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> 

回答

3

首先,从你的命令删除的DefaultHandler

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

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

如果所有的一切都是为了一个命令定义的操作的activeWhen表达式的值设置为false,且有的DefaultHandler的命令本身定义,那么默认的处理程序将被用于命令。该命令本质上始终处于活动状态,因为总是有一个默认处理程序来处理它。

举例来说,如果你有两个现有日志查看,和另一种观点认为充满独角兽,而你想用同一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> 
+0

谢谢。事实上,defaultHandler是错误的。我删除它并将其作为类标记添加到处理程序扩展中。 – 2012-07-24 07:00:11