【问题标题】:Eclipse RCP: Open Split Screen EditorEclipse RCP:打开分屏编辑器
【发布时间】:2016-02-03 16:26:37
【问题描述】:

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

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

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

我认为最好在当前编辑器的左侧或下方打开 Editor2,这样它就有自己的选项卡和关闭按钮。

【问题讨论】:

  • 您在寻找类似Compare Editor 的东西吗?
  • 下面贴出的答案,一针见血。只需在当前编辑器旁边或下方打开一个编辑器。比较编辑器将是第二选择。
  • 我不知道你的实际用例,但如果目的是比较内容,那么比较编辑器是使用 Eclipse 时的自然选择。
  • 我同意,但是比较编辑器带有很多依赖项。 “org.eclipse.compare”包还依赖于“org.eclipse.ui.ide”,它不适合我的 RCP 应用程序。
  • 是的,这很合理。我错误地认为您的目标是 IDE。

标签: java eclipse eclipse-rcp


【解决方案1】:

下面的代码通过将一个编辑器插入另一个编辑器来拆分一个编辑器。这就是 Eclipse 中编辑器选项卡的 DnD 所做的。

   /**
     * 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 功能。

【讨论】:

  • 太好了,这正是我想要的。谢谢!
猜你喜欢
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 2012-12-31
  • 2016-02-06
  • 2018-10-30
  • 1970-01-01
相关资源
最近更新 更多