簡體   English   中英

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

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

我有一個創建零件的按鈕。 我需要獲取當前在零件堆棧中可見的活動零件,並將其存儲為某個值的鍵。 我應該如何獲得積極參與? 我使用了以下代碼,但它獲取了部分堆棧中的所有部分。

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

使用零件的父級及其選定的元素也對我有用。 partService.getActivePart()無法正常工作,因為在我們的應用程序中,我們有幾個零件棧,而我需要當時未重點關注的零件棧中的零件。 我還必須將MUIElement強制轉換為MPart,因為我需要返回MPart,這不是問題,因為MPart是從MUIElement擴展的。 這是我的代碼: 在此處輸入圖片說明

我找到了答案。 現在正在工作。

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,這非常簡單:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM