如何在 Eclispe e4 RCP 中将对象从一个部分传递到另一部分?

Jeh*_*Zeb 5 eclipse eclipse-rcp e4

我正在使用 eclipse e4 RCP 构建一个应用程序。我有一个导航器(类似于 eclipse IDE 中的导航器),我想将其链接到编辑器(类似于 eclipse IDE 中的导航器中的文件链接到编辑器的方式)。目前,当用户双击导航器树中的文件时,我正在使用 EPartService 打开编辑器部分(通过创建新实例)。但我想向它传递一个参数(字符串或对象),让它知道要在编辑器中打开哪个文件。我希望能够为导航树的不同节点打开多个编辑器。我在互联网上做了很多研究,但找不到解决方案。我认为这是一个常见问题,e4 框架应该提供一种机制将此类参数从一个部分传递到另一个部分。当前代码如下:

viewer.addDoubleClickListener(event -> {
        final IStructuredSelection selection = (IStructuredSelection) event.getSelection();
        FileNode file = null;
        boolean partExists = false;
        if (selection.getFirstElement() instanceof FileNode ) {
            file = (FileNode ) selection.getFirstElement();
            for (MPart part1 : partService.getParts()) {
                if (part1.getLabel().equals(file.getName())) {

                    partService.showPart(part1, PartState.ACTIVATE);
                    partExists = true;
                    break;
                }
            }
            if (!partExists) {
                MPart part2 = partService
                        .createPart("com.parts.partdescriptor.fileeditor");
                part2.setLabel(file.getName());
                partService.showPart(part2, PartState.ACTIVATE);
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

是否可以这样说:part2.setParameter("PARAM_NAME", "FILE_NAME"); ?

gre*_*449 5

当您有一个时,MPart您可以致电:

MPart mpart = ...

MyClass myClass = (MyClass)mpart.getObject();
Run Code Online (Sandbox Code Playgroud)

获取该部件的类(在 Application.e4xmi 中该部件的“类 URI”中定义的类)。然后,您可以调用在零件类中定义的任何方法。

您还可以在零件的“瞬态数据”区域中设置数据:

mpart.getTransientData().put("key", "data");

Object data = mpart.getTransientData().get("key");
Run Code Online (Sandbox Code Playgroud)