子非魚

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            21 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks
          轉(zhuǎn)自:http://lemon.javaeye.com/blog/51480
                  http://www.aygfsteel.com/fhawk/archive/2007/01/16/28993.html
          利用IKeyBindingService接口為Action綁定快捷鍵:

          1、
          設(shè)置commands extension

             <extension
                     
          point = "org.eclipse.ui.commands">
                     
          <!-- activeKeyConfiguration項用來說明所綁定快捷鍵的初始設(shè)置 -->
                     
          <activeKeyConfiguration value="org.eclipse.ui.defaultAcceleratorConfiguration"/>
                     
          <!-- 如果快捷鍵設(shè)置有多套,可以添加多個類別 -->
                     
          <category
                         
          name="intelliPlatform.Category1"
                         description
          ="Test description"
                         id
          ="intelliPlatform.Category1"/>
                     
          <!-- 其中id為這個command的ID,相關(guān)的action通過這個ID標(biāo)志找到這個command -->
                     
          <command
                       
          name="intelliPlatform.command.DataSource"
                       category
          ="intelliPlatform.Category1"
                       description
          ="數(shù)據(jù)源配置"
                       id
          ="com.longtop.intelliplatform.ide.project.commands.DataSource"/>
                  
          <!-- 具體的快捷鍵設(shè)置,其中command指定實(shí)際的coomand的ID -->
                  
          <keyBinding
                       
          command="com.longtop.intelliplatform.ide.project.commands.DataSource"
                       configuration
          ="org.eclipse.ui.defaultAcceleratorConfiguration"
                       keySequence
          ="Ctrl+Shift+D"/>
             
          </extension>
           以上是設(shè)置了plugin.xml中command extension,并指定了keybinding,在keybinding中
           的keysequence中的字符串是設(shè)置的快捷鍵。 

          ------------

          在具體的Action配置中,只要在其屬性definitionId設(shè)置成command的ID即可,示例如下:
          <action
           
          label="Sample Action"
           icon
          ="icons/sample.gif"
           class
          ="cli.bacchus.portal.ui.actions.BacchusAction"
           tooltip
          ="Hello, Eclipse world"
           menubarPath
          ="sampleMenu/sampleGroup"
           toolbarPath
          ="sampleGroup"
           id
          ="bacchus.portal.ui.actions.BacchusAction"
           definitionId
          ="com.longtop.intelliplatform.ide.project.commands.datesource">
          </action>

          注意:當(dāng)給相關(guān)的action設(shè)置完definitionID后,必須保證其中設(shè)置的command是有的,而且是正確的,否則有可能導(dǎo)致該action顯示不出來。
          更具體的信息請參考eclipse開發(fā)參考中關(guān)于擴(kuò)展點(diǎn)org.eclipse.ui.commands的詳細(xì)描述。

          ------------

          2、
           建立Acion,在此建立的action可以是實(shí)現(xiàn)IAction接口的任何類。比較方便的是繼承
           org.eclipse.jface.Action,然后在新類中覆蓋父類的run() 方法.

           public class CopyAction extends Action{
             
          public CopyAction(){
              setId(
          "org.example.copyaction");
              setActionDefinitionId(
          "com.longtop.intelliplatform.ide.project.commands.DataSource");
             }

           }

          3、
          在創(chuàng)建CopyAction的instance之后,將copyActionInstance用IKeyBindingService綁定到
          指定的command。
          獲得IKeyBinddingservice的一種簡單方式為:
          IKeyBindingService keyBindingService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getKeyBindingService();
          keyBindingService.registerAction(copyActionInstance);


          注意:
          1、action的definitionid和command定義的id必須一致。
          2、當(dāng)指定的keySequence與系統(tǒng)默認(rèn)的沖突時,如:在窗體的菜單欄中
          指定了Edit->Copy(默認(rèn)的快捷鍵為Ctrl+C),若將上面的keySequence改為
          M1+C(Ctrl+C)則系統(tǒng)默認(rèn)的快捷鍵(Ctrl+C)將更改為Ctrl+Insert。即RCP默認(rèn)
          的為用戶指定的優(yōu)先,系統(tǒng)動態(tài)更新。
          3、IKeyBindingService指定的快捷鍵是有作用范圍的。




          為主菜單綁定快捷鍵

          主菜單的快捷鍵即為 Alt + 菜單名稱中帶下劃線的字母
          定義主菜單快捷鍵只要在主菜單lable中確定的字母前面加上&字符即可
          如:
          plugin.properties  menulabel = &Intelliplatform
          plugin_zh.properties menulabel = 平臺(&I)
          (注意:在該label引用的properties國際化文件中加,直接在plugin.xml中加好像無效,此處存疑)
          posted on 2008-10-06 15:18 子非魚 閱讀(1252) 評論(0)  編輯  收藏 所屬分類: eclipse
          主站蜘蛛池模板: 阜阳市| 万荣县| 澄江县| 盱眙县| 庐江县| 石柱| 连州市| 临江市| 东明县| 青龙| 沛县| 凉城县| 文登市| 东乌| 商南县| 白银市| 沛县| 杭锦后旗| 淮阳县| 平原县| 邹平县| 滕州市| 锡林浩特市| 恩施市| 武夷山市| 金山区| 山丹县| 萨迦县| 建始县| 登封市| 两当县| 庄河市| 仁怀市| 苏尼特左旗| 萨嘎县| 应用必备| 谷城县| 德阳市| 若尔盖县| 宁城县| 武穴市|