I develop a Neon-based compatibility mode Eclipse RCP.
In a plugin, I add a new perspective via fragment.e4xmi
as follows.
<?xml version="1.0" encoding="ASCII"?>
<fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:advanced="http://www.eclipse.org/ui/2010/UIModel/application/ui/advanced" xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/ui/basic" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmi:id="_BxaXACerEeWxCPrV0pAZQQ">
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_j2XU0CE8EeicB9o_IvsRpg" featurename="snippets" parentElementId="org.eclipse.e4.legacy.ide.application">
<elements xsi:type="advanced:Perspective" xmi:id="_-awZgCE8EeicB9o_IvsRpg" elementId="net.my.editor.ui.perspective.editing" selectedElement="_HyeW0CE9EeicB9o_IvsRpg" label="Editing Perspective">
<children xsi:type="basic:PartSashContainer" xmi:id="_HyeW0CE9EeicB9o_IvsRpg" elementId="net.my.editor.ui.partsashcontainer.0" selectedElement="_KOMLUCE9EeicB9o_IvsRpg" horizontal="true">
<children xsi:type="basic:PartStack" xmi:id="_iprjYCH5EeilmepNhpTrFQ" elementId="net.my.editor.ui.partstack.navigation" containerData="20">
<children xsi:type="advanced:Placeholder" xmi:id="_RcRHECH3EeilmepNhpTrFQ" elementId="net.my.plugin.views.navigation" ref="_F0GUoCH4EeilmepNhpTrFQ" closeable="true"/>
</children>
<children xsi:type="basic:PartStack" xmi:id="_KOMLUCE9EeicB9o_IvsRpg" elementId="net.my.editor.ui.partstack.editing" containerData="80" selectedElement="_0KJQ0CH2EeilmepNhpTrFQ">
<children xsi:type="basic:Part" xmi:id="_0KJQ0CH2EeilmepNhpTrFQ" elementId="net.my.editor.ui.part.splitter" contributionURI="bundleclass://net.my.editor.ui/net.my.editor.ui.parts.SplitterView" label="splitter" iconURI="platform:/plugin/net.my.editor.ui/icons/Sample.png" closeable="true"/>
<children xsi:type="basic:Part" xmi:id="_LXR6UCE9EeicB9o_IvsRpg" elementId="net.my.editor.ui.parts.MyEditor.static" contributionURI="bundleclass://net.my.editor.ui/net.my.editor.ui.parts.MyEditor" label="Editor" iconURI="platform:/plugin/net.my.editor.ui/icons/Sample.png">
</children>
</children>
</children>
</elements>
</fragments>
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_HSMw4FS4Eeacy4vmBL0tEQ" featurename="sharedElements" parentElementId="IDEWindow">
<elements xsi:type="basic:Part" xmi:id="_F0GUoCH4EeilmepNhpTrFQ" elementId="net.my.plugin.views.navigation" contributionURI="bundleclass://org.eclipse.ui.workbench/org.eclipse.ui.internal.e4.compatibility.CompatibilityView"/>
</fragments>
</fragment:ModelFragments>
The perspective works fine and I can open it from the Perspective Toolbar.
I want to provide a menu item in the main menu which opens the perspective programmatically. It is here I'm running into issues.
I've added a menu item, command and handler to programmatically open the perspective. In it, the EModelService
cannot find the perspective unless it has been opened beforehand.
I.e., modelService.find(perspectiveId, application)
returns null
, unless the perspective has been previously opened via the Perspective Toolbar (at which point its icon also appears in the toolbar for itself).
Hence my question: Is there a way that I can "pre-load" the perspective so that it is available in the model at the time the menu entry is handled, even if the perspective hasn't been opened before?
Managed to make it work by adding the perspective programmatically to the children of the PerspectiveStack
parent of the currently active perspective.
Smells of brute force, so I'd still be interested in a better solution!
MPerspective myPerspective = null;
List<MUIElement> snips = application.getSnippets();
for (MUIElement snip : snips) {
if (snip.getElementId().equals(myPerspectiveId)) {
if (snip instanceof MPerspective) {
myPerspective = (MPerspective) snip;
}
}
}
if (myPerspective != null) {
MPerspective activePerspective = modelService.getActivePerspective(window);
MPerspectiveStack perspectiveStack = (MPerspectiveStack) (MElementContainer<?>) activePerspective
.getParent();
perspectiveStack.getChildren().add(myPerspective);
partService.switchPerspective(myPerspective);
}