5

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"); ?

greg-449
  • 109,219
  • 232
  • 102
  • 145
Jehan Zeb
  • 155
  • 12

1 Answers1

5

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");
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Thanks for a quick reply. I would like to add that the getObject() method only returns non-null after the part is rendered. So if I want to transfer data that is to be rendered in the editor e.g. the file name in a text field, then it would be created empty then I have to set the text later in the method that is invoked on the mpart object retrieved via getObject(). In this case it is perhaps better to use the 'transient data'. Which approach is better 'transient data' or 'getObject'? or they are the same and it depends on the situation? – Jehan Zeb May 19 '15 at 14:29
  • 1
    Use transient data if you want to set things before rendering. This is what I do when starting editor parts. – greg-449 May 19 '15 at 14:31
  • I am using the following code to get the Object which I have put in transient data (from another Part). Is it correct? MPart part = partService.getActivePart(); Object object = part.getTransientData().get(Constants.INPUT_KEY); Is partService.getActivePart(); like the "this" in case of a part class? – Jehan Zeb May 22 '15 at 10:05
  • Normally you would just inject the MPart in your class. – greg-449 May 22 '15 at 10:13