2

I'm looking for a way to open a split screen editor in an Eclipse RCP application programmatically.
From an open Editor I want to open another Editor. The purpose is to compare the content of Editor1 with the content of Editor2.

What I have is the following, but this creates a split screen Editor containing the content of Editor2 twice:

MPart editorPart = editor.getSite().getService(MPart.class);
if (editorPart == null) {
    return;
}
editorPart.getTags().add(IPresentationEngine.SPLIT_HORIZONTAL);

I think best would be opening Editor2 left or below the current editor, so it has its own tab and close button.

5
  • 1
    Are you looking for something like the Compare Editor? Feb 3, 2016 at 17:20
  • The answer posted below, nails it. Just open an editor next or below the current editor. The Compare Editor would be second choise. Feb 5, 2016 at 8:34
  • I don't know your actual use case, but if the purpose is to compare the content, then a compare editor is the natural choice when using Eclipse. Feb 5, 2016 at 9:16
  • I agree, but the compare editor comes with a lot of dependencies. The "org.eclipse.compare" bundle depends also on "org.eclipse.ui.ide", which is not suited for my RCP application. Feb 5, 2016 at 9:59
  • Yes, that's reasonable. I was under the wrong impression that you were targeting the IDE. Feb 5, 2016 at 10:35

1 Answer 1

4

The code below splits an editor by inserting one editor into another. This is what DnD for editor tabs does in Eclipse.

   /**
     * Inserts the editor into the container editor.
     * 
     * @param ratio
     *            the ratio
     * @param where
     *            where to insert ({@link EModelService#LEFT_OF},
     *            {@link EModelService#RIGHT_OF}, {@link EModelService#ABOVE} or
     *            {@link EModelService#BELOW})
     * @param containerEditor
     *            the container editor
     * @param editorToInsert
     *            the editor to insert
     */
    public void insertEditor(float ratio, int where, MPart containerEditor, MPart editorToInsert) {
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        EModelService service = window.getService(EModelService.class);
        MPartStack toInsert = getPartStack(editorToInsert);

        MArea area = getArea(containerEditor);
        MPartSashContainerElement relToElement = area.getChildren().get(0);
        service.insert(toInsert, (MPartSashContainerElement) relToElement, where, ratio);
    }

    private MPartStack getPartStack(MPart childPart) {
        MStackElement stackElement = childPart;
        MPartStack newStack = BasicFactoryImpl.eINSTANCE.createPartStack();
        newStack.getChildren().add(stackElement);
        newStack.setSelectedElement(stackElement);
        return newStack;
    }

    private MArea getArea(MPart containerPart) {
        MUIElement targetParent = containerPart.getParent();
        while (!(targetParent instanceof MArea))
            targetParent = targetParent.getParent();
        MArea area = (MArea) targetParent;
        return area;
    }

Examples of the use the insert method are below:

insertEditor(0.5f, EModelService.LEFT_OF, containerPart, childPart);
insertEditor(0.5f, EModelService.BELOW, containerPart, childPart);

In passing, code in class SplitDropAgent2 is responsible for the DnD capability of the editor tabs.

1
  • Great, this does exactly what I was looking for. Thanks! Feb 5, 2016 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.