有了这个,您可以关闭具有特定标签的 MPart。
看来您必须切换到该部分所在的透视图,否则它不会从上下文中删除,这将导致空指针异常。
@Inject
@Optional
public void doEvent(@EventTopic(EventConstants.EventTopics.CLOSE_PARTS_WITH_TAGS) String tagToClose, MApplication app,
EModelService eModelService, EPartService ePartService) {
MUIElement activePart = ePartService.getActivePart();
EPartService activePeServcie = null;
MPerspective activePerspective = null;
if (activePart instanceof MPart) {
activePeServcie = ((MPart) activePart).getContext().get(EPartService.class);
activePerspective = eModelService.getPerspectiveFor(activePart);
}
List<String> tags = new ArrayList<String>();
tags.add(tagToClose);
List<MPart> elementsWithTags = eModelService.findElements(app, null, MPart.class, tags);
for (MPart part : elementsWithTags) {
try {
logger.info("Closing part " + part.toString());
EPartService peService = part.getContext().get(EPartService.class);
peService.switchPerspective(eModelService.getPerspectiveFor(part));
peService.hidePart(part);
} catch (Exception e) {
logger.error(e);
}
}
if (activePart instanceof MPart && activePeServcie != null && activePerspective != null) {
activePeServcie.switchPerspective(activePerspective);
}
}