给每个编辑器自己的视角

时间:2016-12-29 13:57:51

标签: java eclipse e4

我有几个编辑器,每个编辑器都有一些与其他编辑器无关的详细视图。事实上,客户并不希望他们在那里,因为他们会混淆他们。

所以我想切换视图的可见性,并认为Eclipse中的内置透视图可能会起作用。我像这样创建了IPartListener

public class ToEachTheirOwnPartListener implements IPartListener {

    private final Map<IWorkbenchPart, String> partToPerspectiveId = new IdentityHashMap<>(5);

    @Override
    public void partOpened(IWorkbenchPart part) {
        if (hasOwnPerspective(part)) {
            String perspectiveId = cloneEditorPerspective(part);
            this.partToPerspectiveId.put(part, perspectiveId);
        }
    }

    private static boolean hasOwnPerspective(IWorkbenchPart part) {
        return part instanceof IEditorPart;
    }

    String cloneEditorPerspective(IWorkbenchPart part) {
        final IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
        IWorkbenchPage page = part.getSite().getPage();
        // more later
    }

    @Override
    public void partBroughtToTop(IWorkbenchPart part) {
        if (hasOwnPerspective(part)) {
            final IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
            part.getSite().getPage().setPerspective(registry.findPerspectiveWithId(this.partToPerspectiveId.get(part)));
        }
    }

    @Override
    public void partClosed(IWorkbenchPart part) {
        if (hasOwnPerspective(part)) {
            final IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
            registry.deletePerspective(registry.findPerspectiveWithId(this.partToPerspectiveId.get(part)));
            this.partToPerspectiveId.remove(part);
        }
    }
}

我正在使用cloneEditorPerspective()方法,因为所有方法都没有在E4或普通车中实现。

我想要做的就是克隆这样的现有视角(老实说,我宁愿使用IPerspectiveRegistry#clonePerspective但是...... E4):

page.savePerspectiveAs(page.getPerspective());

String defaultPerspectiveId = PlatformUI.getWorkbench().getPerspectiveRegistry().getDefaultPerspective();
final IPerspectiveDescriptor defaultPerspective = registry.findPerspectiveWithId(defaultPerspectiveId);
IPerspectiveDescriptor clone = ((PerspectiveRegistry) registry).createPerspective(UUID.randomUUID().toString(),
        (PerspectiveDescriptor) defaultPerspective);

但是抛出了以下异常(显然,对于克隆的透视,字段PerspectiveDescriptor.configElement为null):

  

显示java.lang.NullPointerException     at org.eclipse.ui.internal.registry.PerspectiveDescriptor.createFactory(PerspectiveDescriptor.java:74)     在org.eclipse.ui.internal.WorkbenchPage.setPerspective(WorkbenchPage.java:4053)     在ToEachTheirOwnPartListener.partBroughtToTop(ToEachTheirOwnPartListener.java:65)

我可以添加page.savePerspectiveAs(clone)来删除异常,但是由于完全超出我的原因,不会克隆默认透视图,而是克隆当前活动的透视图。

再试一次;我们将应用程序设置为空透视图,然后克隆这个:

IPerspectiveDescriptor perspective = registry.findPerspectiveWithId("org.acme.plugin.perspective");
window.getActivePage().setPerspective(perspective);
IPerspectiveDescriptor clone = ((PerspectiveRegistry) registry).createPerspective(UUID.randomUUID().toString(),
        (PerspectiveDescriptor) page.getPerspective());
page.savePerspectiveAs(clone);

该代码有效,但clones all views

如果我手动关闭视图,那么所有地狱都会失败:

  

显示java.lang.NullPointerException     at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.canExecuteItem(HandledContributionItem.java:808)     at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access $ 1(HandledContributionItem.java:802)     在org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem $ 3.run(HandledContributionItem.java:167)     在org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)

我尝试的所有变化似乎都归结为这三种:NPE,克隆错误观点或复制观点。

有没有办法让每个编辑都有自己的观点?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:0)

显然,诀窍是只克隆当前活动的视角。这意味着切换到空透视然后克隆:

    // we save the current perspective
    IPerspectiveDescriptor currentPerspective = page.getPerspective();
    if (currentPerspective != null) {
        // E4 sometimes lacks perspective
        page.savePerspectiveAs(currentPerspective);
    }

    // switch to the default one (should never have views)
    String defaultPerspectiveId = PlatformUI.getWorkbench().getPerspectiveRegistry().getDefaultPerspective();
    final IPerspectiveDescriptor defaultPerspective = registry.findPerspectiveWithId(defaultPerspectiveId);
    page.setPerspective(defaultPerspective);

    // clone it
    IPerspectiveDescriptor clone = ((PerspectiveRegistry) registry).createPerspective(String.valueOf(this.nextId++),
            (PerspectiveDescriptor) defaultPerspective);
    page.savePerspectiveAs(clone);

仍有一堆随机NPE,因为视角为空,但这就是E4如何滚动。