如何在eclipse e4中获得特定partstack中的活动部分?

问题描述 投票:1回答:4

我有一个创建零件的按钮。我需要获取当前在部分堆栈中可见的活动部分,并将其存储为某个值的关键字。我该如何获得活跃的部分?我使用了以下代码,但它正在获取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);
                }
            }
java eclipse eclipse-rcp e4
4个回答
3
投票

MPart您可以直接获得其容器:

final MElementContainer<MUIElement> container = part.getParent();

(这将是MPartStack

然后,您可以获取当前所选子堆栈:

MUIElement selected = container.getSelectedElement();

2
投票

使用零件的父级及其选定的元素也对我有用。 partService.getActivePart()不起作用,因为在我们的应用程序中,我们有几个部分堆栈,我需要一个零件堆栈的一部分,当时不是焦点。我还必须将MUIElement转换为MPart,因为我需要返回一个MPart,这不是问题,因为MPart从MUIElement扩展。这是我的代码:enter image description here


0
投票

我找到了答案。它现在正在工作。

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来检查特定堆栈是否可见。


0
投票

Eclipse E4非常简单:

  1. 注入EPartService
  2. 然后从partService获取活动部分。

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;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.