Eclipse RCP:打开分屏编辑器
Eclipse RCP: Open Split Screen Editor
我正在寻找一种以编程方式在 Eclipse RCP 应用程序中打开分屏编辑器的方法。
我想从一个打开的编辑器打开另一个编辑器。目的是比较Editor1的内容和Editor2的内容。
我有以下内容,但这会创建一个包含两次 Editor2 内容的分屏编辑器:
MPart editorPart = editor.getSite().getService(MPart.class);
if (editorPart == null) {
return;
}
editorPart.getTags().add(IPresentationEngine.SPLIT_HORIZONTAL);
我认为最好在当前编辑器的左侧或下方打开 Editor2,这样它就有自己的选项卡和关闭按钮。
下面的代码通过将一个编辑器插入另一个编辑器来拆分一个编辑器。这就是编辑器选项卡的 DnD 在 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;
}
使用 insert
方法的示例如下:
insertEditor(0.5f, EModelService.LEFT_OF, containerPart, childPart);
insertEditor(0.5f, EModelService.BELOW, containerPart, childPart);
顺便说一下,class SplitDropAgent2
中的代码负责编辑器选项卡的 DnD 功能。
我正在寻找一种以编程方式在 Eclipse RCP 应用程序中打开分屏编辑器的方法。
我想从一个打开的编辑器打开另一个编辑器。目的是比较Editor1的内容和Editor2的内容。
我有以下内容,但这会创建一个包含两次 Editor2 内容的分屏编辑器:
MPart editorPart = editor.getSite().getService(MPart.class);
if (editorPart == null) {
return;
}
editorPart.getTags().add(IPresentationEngine.SPLIT_HORIZONTAL);
我认为最好在当前编辑器的左侧或下方打开 Editor2,这样它就有自己的选项卡和关闭按钮。
下面的代码通过将一个编辑器插入另一个编辑器来拆分一个编辑器。这就是编辑器选项卡的 DnD 在 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;
}
使用 insert
方法的示例如下:
insertEditor(0.5f, EModelService.LEFT_OF, containerPart, childPart);
insertEditor(0.5f, EModelService.BELOW, containerPart, childPart);
顺便说一下,class SplitDropAgent2
中的代码负责编辑器选项卡的 DnD 功能。