Eclipse RCP:打开分屏编辑器

问题描述 投票:2回答:1

我正在寻找一种以编程方式在Eclipse RCP应用程序中打开分屏编辑器的方法。从打开的编辑器中,我想打开另一个编辑器。目的是将Editor1的内容与Editor2的内容进行比较。

我拥有以下内容,但这将创建一个包含两次Editor2内容的分屏编辑器:

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

我认为最好在当前编辑器的左侧或下方打开Editor2,因此它具有自己的标签和关闭按钮。

java eclipse eclipse-rcp
1个回答
4
投票

下面的代码通过将一个编辑器插入另一个来拆分一个编辑器。这就是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);

顺便说一句,类SplitDropAgent2中的代码负责编辑器选项卡的DnD功能。

© www.soinside.com 2019 - 2024. All rights reserved.