Eclipse RCP: clonePerspective returns null

点点圈 提交于 2019-12-11 07:16:02

问题


I want to clone a perspective and save that cloned one. However, the call to the PerspectiveRegistry.clonePerspective(...) returns null, and I'm running out of ideas what could possibly be wrong with my parameter values.

public void savePerspectiveAs(String label) {
    IWorkbenchPage page = PlatformUI.getWorkbench()
            .getActiveWorkbenchWindow().getActivePage();
    IPerspectiveDescriptor perspectiveDescriptor = perspectiveRegistry
            .findPerspectiveWithLabel(label);

    // if the perspective doesn't exist, create a new one 
    // by cloning the current perspective
    if (perspectiveDescriptor == null) {
        IPerspectiveDescriptor currentPerspectiveDescriptor = 
                perspectiveRegistry.findPerspectiveWithId(currentPerspective);
        // after this line, perspectiveDescriptor is still null
        perspectiveDescriptor = perspectiveRegistry
                .clonePerspective(currentPerspective + "." 
                         + label.replaceAll(" ", "_"), label, 
                         currentPerspectiveDescriptor);
        // hence I get a NullPointerException here...
        connectedPerspectives.add(perspectiveDescriptor.getId());
    }
    page.savePerspectiveAs(perspectiveDescriptor);

}

All other values seem to be fine. currentPerspectiveDescriptor is a valid object, so is perspectiveRegistry.

label is something like "Connected Perspective 2". And it does not exist yet, neither does my created perspective ID.

Any ideas? What could go wrong here for clonePerspective(...) to return null?


回答1:


clonePerspective is not supported in Eclipse 4.x.

This is the code for PerspectiveRegistry.clonePerspective:

public IPerspectiveDescriptor clonePerspective(String id, String label,
        IPerspectiveDescriptor desc) throws IllegalArgumentException {
    // FIXME: compat clonePerspective. Not called in 3.8
    E4Util.unsupported("clonePerspective"); //$NON-NLS-1$
    return null;
}

The Eclipse bug for this is 382209




回答2:


I was able to make a workaround for "cloning" a perspective without rewriting everything for e4. However, it's a rather dirty hack, and I don't recommend it, because it uses unchecked casts to Eclipse's implementing classes rather then its interfaces.

@SuppressWarnings("restriction")
public void savePerspectiveAs(String label) {
    IWorkbenchPage page = PlatformUI.getWorkbench()
            .getActiveWorkbenchWindow().getActivePage();
    IPerspectiveDescriptor perspectiveDescriptor = perspectiveRegistry
            .findPerspectiveWithLabel(label);

    // if the perspective doesn't exist, create a new one 
    // by cloning the current perspective
    if (perspectiveDescriptor == null) {
        // Note: this is a dirty hack.
        // It is not recommended to cast to PerspectiveDescriptor 
        // or PerspectiveRegistry.
        PerspectiveDescriptor currentPerspectiveDescriptor =
                (PerspectiveDescriptor) perspectiveRegistry.findPerspectiveWithId(currentPerspective);
        perspectiveDescriptor =
                ((PerspectiveRegistry) perspectiveRegistry)
                        .createPerspective(label, currentPerspectiveDescriptor);
        connectedPerspectives.add(perspectiveDescriptor.getId());
    }

    page.savePerspectiveAs(perspectiveDescriptor);
}


来源:https://stackoverflow.com/questions/26384842/eclipse-rcp-cloneperspective-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!