Eclipse RCP:操作 VS 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/552435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:32:19  来源:igfitidea点击:

Eclipse RCP: Actions VS Commands

javaeclipsecommandactionrcp

提问by VonC

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?

Eclipse RCP 上下文中的操作和命令之间有什么区别?我知道它们都有助于菜单条目,但哪个更好?为什么?

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

谢谢

回答by VonC

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

您是否阅读了 eclipse wiki FAQ 命令和操作之间的区别是什么?

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 Actionsis 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.

Commandspretty 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.

您可能已经了解 Actions 和 Commands 基本上做同样的事情:它们导致执行某段代码。它们主要由用户界面中的人工制品触发

Actions的主要问题是表现形式和代码都存储在 Action 中
尽管 Action Delegates 中有一些分离,但它们仍然与底层操作相关联。选择事件传递给动作,以便它们可以根据当前选择(以编程方式)更改其启用状态。这不是很优雅。此外,要在某个工作台部件上放置操作,您必须使用多个扩展点。

命令几乎可以解决所有这些问题。基本思想是命令只是一些要执行的代码的抽象思想。代码的实际处理是由 handlers 完成的。处理程序由工作台的特定状态激活。该状态由平台核心表达式查询。这意味着我们只需要一个全局 Save 命令,它的行为会根据当前处于活动状态的处理程序而有所不同。

properties of a command

命令的属性

This articledetails 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 pointsand 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.
  • UI和处理总是并列。没有办法彼此分开
  • 虽然操作可以用于工作台的不同部分(弹出菜单/工具栏),但它们都是不同的扩展点,因此您最终会在多个位置复制 XML。最糟糕的是,并非所有扩展点都期望相同的配置。
  • 在多个地方指定操作是维护的噩梦。如果你必须改变一个动作的图标,你需要在所有的地方都改变。
  • 在 plugin.xml 中复制 Actions 的另一个问题是将在内存中创建相同 Actions 的多个实例

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 activeWhenfor 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
  • 处理程序可以与命令分开声明。这为同一命令启用多个处理程序声明
  • 对所有处理程序的activeWhen进行评估,并选择对最特定条件返回 true 的处理程序。所有这些事情都是在没有将处理程序加载到内存中的情况下完成的。即使没有加载你的插件
  • 定义参数就是返回显示名称和 id 的映射。名称将显示在键绑定页面中,而 id 将用于在按下键序列时调用命令。
  • 定义一个 IExecutionListener,它只是命令执行的观察者,因此它既不能否决它,也不能对事件进行任何更改

回答by Mario Ortegón

Just adding to VonC's excellent answer, commands might be a little overkill if your application is relatively small. They are relatively harder to setup, and they shine the most when you have multiple perspectives, editors and views.

只是添加到 VonC 的优秀答案中,如果您的应用程序相对较小,则命令可能有点过分。它们相对更难设置,当您有多个视角、编辑器和视图时,它们最闪耀。

For something simple, I would go with actions.

对于简单的事情,我会采取行动。

回答by nanda

And remember that Action may be deprecated on later version of Eclipse. I'd suggest you to use Command from the beginning.

请记住,在更高版本的 Eclipse 中可能会弃用 Action。我建议你从一开始就使用命令。