How do I reset perspective for Eclipse e4 RCP application?

前端 未结 2 1395
轮回少年
轮回少年 2020-12-09 23:48

After building a perspective in application.e4xmi file, I am unable to reset perspective by calling IWorkbenchPage.resetPerspective().

2条回答
  •  孤街浪徒
    2020-12-10 00:50

    Reset perspective (when you lunch e4 application without clearing work space, when you switch other perspective to your perceptive).

    Step 1: Add a add-on in your model fragment at application level.

    Step 2: Create Add-on class and implement EventHandler

    Step 3: add following code in the class.

    public class ResetPrespectiveAddOn implements EventHandler {
    
    private static final String MY_PERSPECTIVE_ID = "myPrespectiveId";
    
    @Inject
    private IEventBroker broker;
    
    @PostConstruct
    public void loadPrespective() {
    
        broker.subscribe(UIEvents.ElementContainer.TOPIC_SELECTEDELEMENT, this);
    }
    
    @SuppressWarnings("restriction")
    @Override
    public void handleEvent(Event event) {
    
        //UIEvents.EventTags.ELEMENT is trigger  for all UI activity
        Object property = event.getProperty(UIEvents.EventTags.ELEMENT);
        if (!(property instanceof PerspectiveStackImpl)) {
            return;
    
        }
    
        // Reset perspective logic .
        IEclipseContext serviceContext = E4Workbench.getServiceContext();
        final IEclipseContext appContext = (IEclipseContext) serviceContext.getActiveChild();
        EModelService modelService = appContext.get(EModelService.class);
        MApplication application = serviceContext.get(MApplication.class);
        MWindow mWindow = application.getChildren().get(0);
    
        PerspectiveStackImpl perspectiveStack = (PerspectiveStackImpl) property;
        List children = perspectiveStack.getChildren();
        for (MPerspective myPerspective : children) {
            if (myPerspective.getElementId().equals(MY_PERSPECTIVE_ID)) {
    
                //find active perspective
                MPerspective activePerspective = modelService.getActivePerspective(mWindow);
                if(activePerspective.getElementId().equals(MY_PERSPECTIVE_ID))
    
                //Reseting perspective  e3 way 
                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().resetPerspective();
    
    
                // till now there is no direct e4 way to reset perspective 
                // but u can Add and remove e4 perspective with this code code              
                EPartService partService = serviceContext.get(EPartService.class);
                MPerspectiveStack perspectiveStack = (MPerspectiveStack) (MElementContainer) activePerspective.getParent();
                int indexOf = perspectiveStack.getChildren().indexOf(activePerspective);
                perspectiveStack.getChildren().remove(indexOf);
    
                perspectiveStack.getChildren().add(myPerspective);
                partService.switchPerspective(myPerspective);
            }   
        }
    }}
    

提交回复
热议问题