简体   繁体   English

Eclipse RCP 4:向MPart添加弹出菜单

[英]Eclipse RCP 4: Adding a Popup Menu to an MPart

I am trying to add a Popup menu for an MPart when it is right clicked, but I am having difficulty doing so. 右键单击时,我试图为MPart添加弹出菜单,但是这样做很难。 It should function the same way it did in Eclipse 3.x (Right click near the title of the view and the popup menu to detach the view/move it appears). 它的功能应与Eclipse 3.x中的功能相同(右键单击视图标题和弹出菜单以分离视图/移动显示的视图)。 I have added a Popup Menu with a Handled Menu Item. 我添加了带有处理菜单项的弹出菜单。 I assumed this would be all that is required to allow the popup menu to appear on right click, but nothing happens when I do. 我以为这是允许在右键单击时出现弹出菜单所需要的全部,但是当我这样做时什么也没有发生。

I did follow Lars Vogel's tutorials, but his tutorial only shows how to add a JFace viewer to a part, and then adding the popup menu to that viewer. 我确实遵循了Lars Vogel的教程,但是他的教程仅显示了如何将JFace查看器添加到零件,然后将弹出菜单添加到该查看器。 What is the proper way to add a popup menu when you right click on an MPart? 右键单击MPart时,添加弹出菜单的正确方法是什么?

MPart在我的应用程序模型中的外观

Thanks for any help you may be able to provide! 感谢您提供的任何帮助! :) :)

EDIT: Example of what I want 编辑:我想要的示例

例

As well as declaring the Popup Menu in the Application.e4xmi (or fragment) you must also use the EMenuService in the code to register the menu with the control that you are going to right click on. 除了在Application.e4xmi(或片段)中声明弹出菜单外,还必须在代码中使用EMenuService来向要右键单击的控件注册菜单。

@Inject
EMenuService menuService;


menuService.registerContextMenu(control, "menu id");

Changing the menu shown when right clicking on the tab for a part is much more complex. 右键单击零件的选项卡时,更改显示的菜单要复杂得多。 To do this you must define a custom renderer for the MPartStack using a custom renderer factory (see here for basic details). 为此,必须使用自定义渲染器工厂为MPartStack定义自定义渲染器(有关基本详细信息,请参见此处 )。

Your renderer can extend the standard StackRenderer class and override the populateTabMenu method. 渲染器可以扩展标准StackRenderer类,并覆盖populateTabMenu方法。

This is an example method I use which reduces the menu to just show 'Close': 这是我使用的示例方法,该方法将菜单缩小为仅显示“关闭”:

@Override
protected void populateTabMenu(final Menu menu, final MPart part)
{
  if (!isClosable(part))
    return;

  // Just add Close menu item and handle enabling it correctly

  final MenuItem menuItemClose = new MenuItem(menu, SWT.NONE);

  menuItemClose.setText(SWTRenderersMessages.menuClose);

  menuItemClose.addListener(SWT.Selection, this::closeSelected);

  menu.removeListener(SWT.Show, _menuListener);
  menu.addListener(SWT.Show, _menuListener);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM