子非鱼

BlogJava 首页 新随笔 联系 聚合 管理
  21 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks
转自:http://lemon.javaeye.com/blog/51480
        http://www.blogjava.net/fhawk/archive/2007/01/16/28993.html
利用IKeyBindingService接口为Action绑定快捷键:

1、
设置commands extension

   <extension
           
point = "org.eclipse.ui.commands">
           
<!-- activeKeyConfiguration项用来说明所绑定快捷键的初始设置 -->
           
<activeKeyConfiguration value="org.eclipse.ui.defaultAcceleratorConfiguration"/>
           
<!-- 如果快捷键设置有多套,可以添加多个类别 -->
           
<category
               
name="intelliPlatform.Category1"
               description
="Test description"
               id
="intelliPlatform.Category1"/>
           
<!-- 其中id为这个command的ID,相关的action通过这个ID标志找到这个command -->
           
<command
             
name="intelliPlatform.command.DataSource"
             category
="intelliPlatform.Category1"
             description
="数据源配置"
             id
="com.longtop.intelliplatform.ide.project.commands.DataSource"/>
        
<!-- 具体的快捷键设置,其中command指定实际的coomand的ID -->
        
<keyBinding
             
command="com.longtop.intelliplatform.ide.project.commands.DataSource"
             configuration
="org.eclipse.ui.defaultAcceleratorConfiguration"
             keySequence
="Ctrl+Shift+D"/>
   
</extension>
 以上是设置了plugin.xml中command extension,并指定了keybinding,在keybinding中
 的keysequence中的字符串是设置的快捷键。 

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

在具体的Action配置中,只要在其属性definitionId设置成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>

注意:当给相关的action设置完definitionID后,必须保证其中设置的command是有的,而且是正确的,否则有可能导致该action显示不出来。
更具体的信息请参考eclipse开发参考中关于扩展点org.eclipse.ui.commands的详细描述。

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

2、
 建立Acion,在此建立的action可以是实现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、
在创建CopyAction的instance之后,将copyActionInstance用IKeyBindingService绑定到
指定的command。
获得IKeyBinddingservice的一种简单方式为:
IKeyBindingService keyBindingService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getKeyBindingService();
keyBindingService.registerAction(copyActionInstance);


注意:
1、action的definitionid和command定义的id必须一致。
2、当指定的keySequence与系统默认的冲突时,如:在窗体的菜单栏中
指定了Edit->Copy(默认的快捷键为Ctrl+C),若将上面的keySequence改为
M1+C(Ctrl+C)则系统默认的快捷键(Ctrl+C)将更改为Ctrl+Insert。即RCP默认
的为用户指定的优先,系统动态更新。
3、IKeyBindingService指定的快捷键是有作用范围的。




为主菜单绑定快捷键

主菜单的快捷键即为 Alt + 菜单名称中带下划线的字母
定义主菜单快捷键只要在主菜单lable中确定的字母前面加上&字符即可
如:
plugin.properties  menulabel = &Intelliplatform
plugin_zh.properties menulabel = 平台(&I)
(注意:在该label引用的properties国际化文件中加,直接在plugin.xml中加好像无效,此处存疑)
posted on 2008-10-06 15:18 子非鱼 阅读(1224) 评论(0)  编辑  收藏 所属分类: eclipse