如何在 Eclipse e4 应用程序的菜单/工具栏中创建切换/单选项?

问题描述

在 eclipse e4 rcp 应用程序中创建实现切换或单选状态的菜单项的规范方法是什么?

这似乎是一件很基本的事情,但我找到的所有文档都依赖于 e3 API,并创建了对 org.eclipse.ui 的依赖,这在 e4 中是不允许的。

解决方法

我用于单选按钮菜单的一种可能方式,它将单选按钮状态保存在部件类中。

我使用多个 Direct Menu Item 条目并将类型设置为 Radio

我将 Tag 值(在菜单项的补充页面上)设置为要与菜单项关联的值。

我对所有菜单项使用相同的处理程序。

在处理程序中,我注入了 MPartMItem

@Execute
public void execute(final MPart part,final MItem mitem)
{
  // Only take action on the selected radio item

  if (!mitem.isSelected())
    return;

  // The tag value specifying the radio state

  String tag = mitem.getTags().get(0);

  // Get the part class

  MyPart myPart = (MyPart)part.getObject();

  // tell the part about the state change

  myPart.setState(tag);
}

您还可以使用 Eclipse 上下文中的任何类来代替 MPart - 例如声明为 @Creatable@Singleton 的类。