How to get the active part in a particular partstack in eclipse e4?
Asked Answered
D

4

5

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);
                }
            }
Duodenary answered 3/6, 2014 at 14:8 Comment(0)
P
5

From a MPart you can get its container directly with:

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

(this will be the MPartStack)

You can then get the stacks currently selected child with:

MUIElement selected = container.getSelectedElement();
Project answered 3/6, 2014 at 14:25 Comment(0)
T
3

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. 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. Here's my code: enter image description here

Tutorial answered 2/3, 2016 at 10:26 Comment(0)
O
1

This is pretty simple with Eclipse E4:

  1. Inject the EPartService

  2. Then get from partService the active part.

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

}
Orphaorphan answered 7/4, 2019 at 12:14 Comment(0)
D
0

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.

Duodenary answered 3/6, 2014 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.