我正在尝试从e4xmi文件的ID中获取findpart。但它始终是空的。我在这里错过了什么。我是新来的。
package handler;
import java.util.Collection;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import analysis.ProjectAnalyzer;
import view.View;
public class ASTZestHandler {
private static final String SIMPLEZESTVIEW =
"simplezestproject4.partdescriptor.simplezestview4";
@Execute
public void execute(EPartService service){
MPart findPart = service.findPart(SIMPLEZESTVIEW); // it is always null
Collection mpart = service.getParts();
if (findPart != null && findPart.getObject() instanceof View) {
new ProjectAnalyzer().analyze();
((View) findPart.getObject()).update();
}
}
}
我的e4xmi代码。我在我的e4xmi代码中尝试了所有其他Id,但无论我使用任何元素id,查找部分始终为null
<?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:basic
="http://www.eclipse.org/ui/2010/UIModel/application/descriptor/basic" xmlns: commands
="http://www.eclipse.org/ui/2010/UIModel/application/commands" xmlns:fragment
="http://www.eclipse.org/ui/2010/UIModel/fragment" xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu" xmi:id="_oDqLAL46EeWS1vppwIGIuQ">
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_qS0xQL46EeWS1vppwIGIuQ" featurename="descriptors" parentElementId="org.eclipse.e4.legacy.ide.application">
<elements xsi:type="basic:PartDescriptor" xmi:id="_x0-UEL46EeWS1vppwIGIuQ" elementId="simplezestproject4.partdescriptor.simplezestview4" label="Simple Zest View 4" iconURI="platform:/plugin/simpleZestProject4/icons/sample.png" contributionURI="bundleclass://simpleZestProject4/view.View"/>
</fragments>
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_q08m4L46EeWS1vppwIGIuQ" featurename="commands" parentElementId="org.eclipse.e4.legacy.ide.application">
<elements xsi:type="commands:Command" xmi:id="_2gK34L46EeWS1vppwIGIuQ" elementId="simplezestproject4.command.viewastnodes" commandName="View AST Nodes"/>
</fragments>
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_rRyT4L46EeWS1vppwIGIuQ" featurename="handlers" parentElementId="org.eclipse.e4.legacy.ide.application">
<elements xsi:type="commands:Handler" xmi:id="_7WE24L46EeWS1vppwIGIuQ" elementId="simplezestproject4.handler.0" contributionURI="bundleclass://simpleZestProject4/handler.ASTZestHandler" command="_2gK34L46EeWS1vppwIGIuQ"/>
</fragments>
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_r6cPUL46EeWS1vppwIGIuQ" featurename="menuContributions" parentElementId="org.eclipse.e4.legacy.ide.application">
<elements xsi:type="menu:MenuContribution" xmi:id="_DithIL47EeWS1vppwIGIuQ" elementId="simplezestproject4.menucontribution.0" positionInParent="after=window" parentId="org.eclipse.ui.main.menu">
<children xsi:type="menu:Menu" xmi:id="_A_CqgL47EeWS1vppwIGIuQ" elementId="simplezestproject4.menu.mymenu" label="My Menu">
<children xsi:type="menu:HandledMenuItem" xmi:id="_HmhU0L47EeWS1vppwIGIuQ" elementId="simplezestproject4.handledmenuitem.item-showastnodes" label="Item - Show AST Nodes" command="_2gK34L46EeWS1vppwIGIuQ"/>
</children>
</elements>
</fragments>
</fragment:ModelFragments>
答案 0 :(得分:2)
你想要达到什么目的?基于PartDescriptor实例化新零件或查找PartDescriptor的实时实例?
如果您想基于PartDescriptor创建实例,可以这样做(使用EPartService partService
):
MPart part = partService.createPart(SIMPLEZESTVIEW);
part.setLabel(...);
part.setElementId(<partname>);
partService.showPart(part, PartState.CREATE);
partService.activate(part, true);
如果要查找以前创建的零件的实例,请使用这样的EModelSrevice(使用Application app, EModelService modelService
):
MPart part = (MPart) modelService.find(<partname>, app);
if (part !=null) {
partService.showPart(part, PartState.CREATE);
partService.activate(part, true);
}