Issue
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.
Solution
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.
Answered By - John S.