[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[整理]Eclipse RCP Plugin 开发,如何使剪切、复制、粘贴、删除、全选等快捷键可用。

上一篇:[整理]Eclipse RCP 开发,Actions与Commands的区别
下一篇:[整理]Eclipse RCP 开发--预定义的Command。-------Eclipse RCP Command Framework -- Predefined Commands。

添加日期:2011/4/2 11:17:49 快速返回   返回列表 阅读7267次
写了个Editor的小插件,发现剪切、复制、粘贴、删除等快捷键是灰的,不能用。
即Ctrl+X、Ctrl+C、Ctrl+V、Del这些快捷键。

在Action里
this.setAccelerator(SWT.CTRL | 'X');
这样是不好使的。


搜索半天,解决方法如下:

(1)在你的Action里这样设置一下。

this.setActionDefinitionId("org.eclipse.ui.edit.cut"); //剪切
this.setActionDefinitionId("org.eclipse.ui.edit.copy"); //复制
this.setActionDefinitionId("org.eclipse.ui.edit.paste"); //粘贴
this.setActionDefinitionId("org.eclipse.ui.edit.delete"); //删除
this.setActionDefinitionId("org.eclipse.ui.edit.selectAll"); //全选

当然是用其中某一个了,这句话的作用就是,设置Action对应的CommandID。
setActionDefinitionId其实改名叫setCommandId更合适。

其他的一些内置Command的ID,请看
http://www.mytju.com/classcode/news_readNews.asp?newsID=319

(2)在合适的地方,比如Activator.java之类的地方,注册一下Action与CommandID的关系。

先做几个Action的实例,这些Action就是你自己写的Action了,Action类名和这里的变量名都是无所谓的。。

    private CutAction cut = new CutAction();

    private CopyAction copy = new CopyAction();

    private PasteAction paste = new PasteAction();

    private DeleteAction delete = new DeleteAction();

    private SelectAllAction selectAll = new SelectAllAction();

然后绑定一下:
    IHandlerService handlerService = (IHandlerService) editor.getSite().getService(IHandlerService.class);
    handlerService.activateHandler("org.eclipse.ui.edit.cut",new ActionHandler(cut));
    handlerService.activateHandler("org.eclipse.ui.edit.copy",new ActionHandler(copy));
    handlerService.activateHandler("org.eclipse.ui.edit.paste",new ActionHandler(paste));
    handlerService.activateHandler("org.eclipse.ui.edit.delete",new ActionHandler(delete));
    handlerService.activateHandler("org.eclipse.ui.edit.selectAll",new ActionHandler(selectAll));

我这里的editor变量是我正在写的编辑器插件的实例,你们看着办吧,反正就是把IHandlerService取到。

(3)这就行了。
=========================================
大概原理:

快捷键是和Command绑定的,"org.eclipse.ui.edit.cut"这些字符串是Command的ID。

一个Command可以和多个处理程序绑定,但,同一时刻,只会有一个处理程序被激活。

当你的插件程序当前是激活的时候,那么,你按下快捷键,就会调用Command,然后就会调用你的对应处理程序。

前提是,你得把你的处理程序与Command绑定起来,即上面所说的工作。

感觉这种机制比Action里直接写死快捷键好得多……多个插件之间就可以共用快捷键了。
============================================
Command可以自定义,在Plugin.xml里写就行,如下例:


<extension point="org.eclipse.ui.commands">
     <category name="User Sessions"
               description="Management of user sessions"
               id="com.mountainminds.eclipse.examples.sessions" />
     <command name="Login"
              description="Login a new user"
              id="com.mountainminds.eclipse.examples.login"
              categoryId="com.mountainminds.eclipse.examples.sessions" />
     <command name="Logout"
              description="Logout the current user"
              id="com.mountainminds.eclipse.examples.logout"
              categoryId="com.mountainminds.eclipse.examples.sessions" />
  </extension>

<extension point="org.eclipse.ui.bindings">
    <key
              commandId="com.mountainminds.eclipse.examples.login"
              schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
              sequence="CTRL+T">
        </key>
</extension>


扩展点要用org.eclipse.ui.commands,其中category就是command的分组名,随便写。
对于command来说,就id比较重要,其他都没啥。

然后再用org.eclipse.ui.bindings扩展点,给command绑定快捷键。

也可以在代码里声明Command,如:


  // Retrieve the Command Service
  ICommandService cs = (ICommandService) serviceLocator.getService(ICommandService.class);

  // Define Our Category
  Category category = cs.getCategory("com.mountainminds.eclipse.examples.sessions");
  category.define("User Sessions", "Management of user sessions");

  // Define Login Command
  Command login = cs.getCommand("com.mountainminds.eclipse.examples.login");
  login.define("Login", "Login a new user", category);

  // Define Logout Command
  Command logout = cs.getCommand("com.mountainminds.eclipse.examples.logout");
  logout.define("Logout", "Logout the current user", category);

  //下面几句是释放的写法。
  cs.getCommand("com.mountainminds.eclipse.examples.login").undefine();
  cs.getCommand("com.mountainminds.eclipse.examples.logout").undefine();
  cs.getCategory("com.mountainminds.eclipse.examples.sessions").undefine();


==============================================================
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved