Eclipse RCP:操作VS命令 [英] Eclipse RCP: Actions VS Commands

查看:209
本文介绍了Eclipse RCP:操作VS命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Eclipse RCP的上下文中,动作和命令有什么区别?我知道他们都贡献了菜单项,但哪一个更好?为什么?



在我阅读的所有在线资源中,我无法深入了解两者之间的差异。我没有真正尝试使用它们,但只是想了解他们从较高层次的角度开始。



谢谢

解决方案

您是否阅读过eclipse wiki 详细说明差异



操作




  • UI和处理始终被绑定。没有办法可以分开对方

  • 虽然可以对工作台的不同部分(弹出菜单/工具栏)进行操作,但所有这些都是不同的扩展点,所以你最终在多个地方复制XML。最糟糕的是,并不是所有的延伸点都是预期相同的配置。

  • 在多个地方指定操作是一个维护噩梦。如果你必须改变一个动作的图标,你需要改变所有的地方。

  • 在plugin.xml中重复操作的另一个问题是,在内存中将创建相同操作的多个实例。



命令涉及更多的扩展点,但是:




  • 处理程序可以单独声明从命令。

  • 对所有处理程序的 activeWhen 进行评估,返回true的多个处理程序声明选择最具体的条件。所有这些都是完成,甚至不加载您的处理程序在内存中。即使没有加载您的插件

  • 定义参数是关于返回显示名称& ids。该名称将显示在密钥绑定页面中,当按下密钥序列时,该ID将用于调用该命令。

  • 定义一个IExecutionListener,它只是该命令的观察者执行,所以它既不能否决,也不能对事件进行任何更改


What are differences between Actions and Commands in the context of Eclipse RCP? I know that they both contribute to the menu entries, but which one is better? And why?

Of all the online resources I read, I could not get a firm understanding of the differences between both. I have not actually tried to use them, but just wanted to understand them to start with from higher level point of view.

Thanks

解决方案

Did you read the eclipse wiki FAQ What is the difference between a command and an action?

You probably already understand that Actions and Commands basically do the same thing: They cause a certain piece of code to be executed. They are triggered, mainly, from artificats within the user interface

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.

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.

This article details the differences

Actions:

  • The UI and handling are always tied. There is no way you can separate each other
  • 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.
  • 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.
  • Another issue with duplicating Actions in plugin.xml is that multiple instance of the same Actions will be created in the memory.

Commands involve more extension points, but:

  • Handler can be declared separately from a Command. This enables for multiple handler declarations for the same 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!
  • 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.
  • 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

这篇关于Eclipse RCP:操作VS命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆