Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an object from one part to another part in Eclispe e4 RCP?

I am building an application with eclipse e4 RCP. I have a navigator (similar to Navigator in eclipse IDE) and I would like to link it to an editor (similar to how a file in Navigator in eclipse IDE is linked to an editor). Currently I am using EPartService to open up my editor Part (by creating a new instance) when the user double clicks on a file in the Navigator tree. But I would like to pass it a parameter (a String or an Object) to let it know which file to open in the editor. I want to be able to open multiple editors for different nodes of the Navigator tree. I have done a lot of research on internet but could not find a solution. I think its a common problem and the e4 framework should provide an mechanism to pass such parameters from one Part to another Part. Current code is as below:

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);
            }
        }
    });

Is it possible to say something like part2.setParameter("PARAM_NAME", "FILE_NAME"); ?

like image 811
Jehan Zeb Avatar asked May 19 '15 14:05

Jehan Zeb


1 Answers

When you have an MPart you can call:

MPart mpart = ...

MyClass myClass = (MyClass)mpart.getObject();

to get your class for the part (the class defined in the 'Class URI' for the part in the Application.e4xmi). You can then call any methods you have defined on your part class.

You can also set data in the 'transient data' area of a part:

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

Object data = mpart.getTransientData().get("key");
like image 71
greg-449 Avatar answered Oct 16 '22 15:10

greg-449