简体   繁体   English

如何在Eclipse e4的特定零件堆栈中获得有效零件?

[英]How to get the active part in a particular partstack in eclipse e4?

I have a button which creates parts. 我有一个创建零件的按钮。 I need to get the active part that is currently visible in he part stack and I am storing it as a key for some value. 我需要获取当前在零件堆栈中可见的活动零件,并将其存储为某个值的键。 How should I get the active part? 我应该如何获得积极参与? I have used the following code but it is getting all the parts in the 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);
                }
            }

From a MPart you can get its container directly with: MPart可以直接通过以下方式获取其容器:

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

(this will be the MPartStack ) (这将是MPartStack

You can then get the stacks currently selected child with: 然后,您可以使用以下命令获取当前选择的子堆栈:

MUIElement selected = container.getSelectedElement();

Using the parent of the part and its selected element also worked for me. 使用零件的父级及其选定的元素也对我有用。 partService.getActivePart() did not work because in our application we have several part stacks and I needed a part from a part stack which was not in focus at that moment. partService.getActivePart()无法正常工作,因为在我们的应用程序中,我们有几个零件栈,而我需要当时未重点关注的零件栈中的零件。 I also had to cast the MUIElement to a MPart because I needed to return an MPart, that was not a problem, since MPart extends from MUIElement. 我还必须将MUIElement强制转换为MPart,因为我需要返回MPart,这不是问题,因为MPart是从MUIElement扩展的。 Here's my code: 这是我的代码: 在此处输入图片说明

I have found the answer. 我找到了答案。 Its working now. 现在正在工作。

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

We should make use of partservice to check particular stack is visible or not. 我们应该利用partservice来检查特定堆栈是否可见。

This is pretty simple with Eclipse E4: 使用Eclipse E4,这非常简单:

  1. Inject the EPartService 注入EPartService

  2. Then get from partService the active part. 然后从partService获得活动零件。

Hier is a sample of my RefreshHandler. 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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM