2012-07-20 93 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