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

[整理]Eclipse RCP 开发,Actions与Commands的区别

上一篇:[整理]RCP插件开发,Propertes 视图中的属性项目的顺序问题
下一篇:[整理]Eclipse RCP Plugin 开发,如何使剪切、复制、粘贴、删除、全选等快捷键可用。

添加日期:2011/4/2 11:16:46 快速返回   返回列表 阅读5285次

==================================================================
Actions:

The UI and handling are always tied. There is no way you can separate each other.
UI和处理程序是捆绑在一起的,没法分开。

While Actions can be contributed to different parts of the workbench (popup menu/tool bar), all of them were different extension points and so you end up duplicating the XML in multiple places. The worst of it is that not all the extension points expect the same configuration.
Action可以部署到工作台的不同部分,如菜单、工具栏,它们是不同的扩展点,所以你得复制XML到多个地方。最郁闷的是,不是所有的扩展点都一样。

Specifying Actions in multiple places is a maintenance nightmare. If you have to change the icon of an action, you need to change in all the places.

在多处指定Action,对维护来说,是场噩梦。比如你要变更一个Action的图标,你得替换所有的地方。

Another issue with duplicating Actions in plugin.xml is that multiple instance of the same Actions will be created in the memory.

在plugin.xml中复制Action带来的另一个问题是,同样的Action会被创建多个实例。

----------------------
Commands involve more extension points, but:
Command需要更多的扩展点,但是:

Handler can be declared separately from a Command. This enables for multiple handler declarations for the same command.
处理程序可以单独声明了,就这允许一个Command对应多个处理。

The activeWhen for all the handlers are evaluated and the one that returns true for the most specific condition is selected. All these things are done without even loading your handler in the memory. Even without loading your plugin!
所有处理程序的activeWhen属性被判断,最符合条件的那个被选择。做这些事情,不需要载入你的处理程序,甚至不需要载入你的插件。

Defining the parameters is all about returning a map of display names & the ids. The name would be displayed in the key bindings page and the id would be used to invoke the command when the key sequence is pressed.
要定义的参数只是名字和ID,名字只是在按键绑定页面显示用的,ID是用来调用Command的,当按键序列被按下时。

Define an IExecutionListener, which is merely an observer of the command execution so it can neither veto on it nor make any changes to the event
可以定义一个IExecutionListener,它只是command执行的观察者,它不会否决命令,也不会改变事件。
==================================================================

==================================================================
Actions

The main concern with Actions is that the manifestation and the code is all stored in the Action. Although there is some separation in Action Delegates, they are still connected to the underlying action. Selection events are passed to Actions so that they can change their enabled state (programmatically) based on the current selection. This is not very elegant. Also to place an action on a certain workbench part you have to use several extension points :
org.eclipse.ui.viewActions
org.eclipse.ui.popupMenus
org.eclipse.ui.editorActions

大意差不多,UI和处理程序是绑定的。一般得实现上面3个扩展点。

------------------------------------------------
Commands

Commands pretty much solve all these issues. The basic idea is that the Command is just the abstract idea of some code to be executed. The actual handling of the code is done by, well, handlers. Handlers are activated by a certain state of the workbench. This state is queried by the platform core expressions. This means that we only need one global Save command which behaves differently based on which handler is currently active. Although this specific Command could also be retargeted by a global action, this still has to be done programmatically and not declaratively. To place a Command on a certain workbench part (including the trim area) you have to use only one extension point:
org.eclipse.ui.menus
大意:Command只是一个抽象的想法,就是这个命令要做什么事。而具体如何实现,由处理程序去做。
当前选择哪个处理程序,是平台核心的工作。

Besides this, Handlers can be activated by a powerful expression syntax in the manifest. This means less code and more declarations which will lead to a smoother running workbench or RCP application.

If you look in the General > Keys preference page, you will see a list of all commands known to the platform, including what context (the "When" column) and configuration (the "Scheme" drop-down list?) they belong to. Key bindings are hooked to commands, and then commands are hooked to handlers. This extra level of indirection allows for added flexibility in the implementation. The user can change key bindings for a command without the associated handlers knowing about it, and the handler for a command can be dynamically changed in different circumstances.
你可以看Eclipse菜单的属性的General > Keys这里,里面列出了所有Command,激活的条件,绑定的快捷键等等。
你可以给一个Command改变快捷键,而具体处理程序根本无需关心这个。
==================================================================
 

评论 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