我有一个创建零件的按钮。我需要获取当前在部分堆栈中可见的活动部分,并将其存储为某个值的关键字。我该如何获得活跃的部分?我使用了以下代码,但它正在获取partstack中的所有部分。
MPart graphpart = partService
.createPart("com.abc.xyz.project.partDescriptor.1");
MPartStack stack = (MPartStack) modelService.find(
"com.abc.xyz.project.partstack.2", application);
for (int i = 0; i < stack.getChildren().size(); i++) {
if (stack.getChildren().get(i).isVisible()) {
System.out.println("values"
+ ((MPart) stack.getChildren().get(i)).getLabel());
application.getTransientData().put(
((MPart) stack.getChildren().get(i)).getLabel(),
selectedFiles);
}
}
从MPart
您可以直接获得其容器:
final MElementContainer<MUIElement> container = part.getParent();
(这将是MPartStack
)
然后,您可以获取当前所选子堆栈:
MUIElement selected = container.getSelectedElement();
我找到了答案。它现在正在工作。
for (int i = 0; i < stack.getChildren().size(); i++) {
if (partService.isPartVisible((MPart) stack.getChildren().get(i))) {
System.out.println("Storage of values"
+ ((MPart) stack.getChildren().get(i)).getLabel());
application.getTransientData().put(
((MPart) stack.getChildren().get(i)).getLabel(),
selectedFiles);
}
}
我们应该使用partservice来检查特定堆栈是否可见。
Eclipse E4非常简单:
Hier是我的RefreshHandler的一个示例。
public class RefreshHandler {
@Inject
EModelService modelService;
@Inject
MWindow window;
@Inject
IEventBroker broker;
@Inject
EPartService partService;
@Execute
public void execute() {
System.out.println(this.getClass().getSimpleName() + " called");
MPart activePart = partService.getActivePart();
if(activePart != null) {
System.out.println("--->" + activePart.getElementId());
}
}
@CanExecute
public boolean canExecute() {
MPerspective activePerspective = modelService.getActivePerspective(window);
if (activePerspective != null && activePerspective.getElementId()
.equals(IApplicationUIElementID.PERSPECTIVE_WORKINGSTORE_ID)) {
return true;
}
return false;
}
}