如何重置Eclipse e4 RCP应用程序的透视图?

时间:2013-10-31 21:45:31

标签: eclipse-rcp perspective e4

在application.e4xmi文件中构建透视图后,我无法通过调用IWorkbenchPage.resetPerspective()来重置透视图。

2 个答案:

答案 0 :(得分:4)

我认为这可能会为其他人节省一些时间,并为自己记录。

能够重置e4透视图的技巧如下(假设使用PerspectiveStack元素的基本application.e4xmi):

  1. 在application.e4xmi文件中,在Application / TrimmedWindow节点下找到PerspectiveStack。记录/设置其ID。
  2. 在Eclipse 4模型编辑器中,将Perspective(透视图)从PerspectiveStack下方拖到Application / Snippets。 (这将导致您的透视ID向IPerspectiveRegistry注册,并提供原始状态。)
  3. 创建新的CopyPerspectiveSnippetProcessor。这将在启动时将片段中的透视图复制到PerspectiveStack。这使得您无需在e4xmi文件中维护每个透视元素的两个副本。

    package com.example.application.processors;
    
    import org.eclipse.e4.core.di.annotations.Execute;
    import org.eclipse.e4.ui.model.application.MApplication;
    import org.eclipse.e4.ui.model.application.ui.MUIElement;
    import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
    import org.eclipse.e4.ui.model.application.ui.advanced.MPerspectiveStack;
    import org.eclipse.e4.ui.workbench.modeling.EModelService;
    
    /**
     * Copies all snippet perspectives to perspective stack called "MainPerspectiveStack" In order to register/reset perspective and not have to sync two copies in
     * e4xmi.
     * 
     */
    public class CopyPerspectiveSnippetProcessor {
        private static final String MAIN_PERSPECTIVE_STACK_ID = "MainPerspectiveStack";
    
        @Execute
        public void execute(EModelService modelService, MApplication application) {
            MPerspectiveStack perspectiveStack = (MPerspectiveStack) modelService.find(MAIN_PERSPECTIVE_STACK_ID, application);
    
            // Only do this when no other children, or the restored workspace state will be overwritten.
            if (!perspectiveStack.getChildren().isEmpty())
                return;
    
            // clone each snippet that is a perspective and add the cloned perspective into the main PerspectiveStack
            boolean isFirst = true;
            for (MUIElement snippet : application.getSnippets()) {
                if (snippet instanceof MPerspective) {
                    MPerspective perspectiveClone = (MPerspective) modelService.cloneSnippet(application, snippet.getElementId(), null);
                    perspectiveStack.getChildren().add(perspectiveClone);
                    if (isFirst) {
                        perspectiveStack.setSelectedElement(perspectiveClone);
                        isFirst = false;
                    }
                }
            }
        }
    }
    
  4. 将CopyPerspectiveSnippetProcess注册到plugin.xml文件中。

    <extension id="MainAppModel" point="org.eclipse.e4.workbench.model">
        <processor beforefragment="false" class="com.example.application.processors.CopyPerspectiveSnippetProcessor"/>
    </extension>
    
  5. 正常重置视角。您还需要将透视图堆栈和当前透视图设置为可见,因为这些有时可以设置为不可见。示例处理程序可能如下所示:

    import org.eclipse.e4.core.di.annotations.Execute;
    import org.eclipse.e4.ui.model.application.MApplication;
    import org.eclipse.e4.ui.model.application.ui.advanced.MPerspectiveStack;
    import org.eclipse.e4.ui.workbench.modeling.EModelService;
    import org.eclipse.ui.PlatformUI;
    
    public class ResetPerspectiveHandler {
        private static final String MAIN_PERSPECTIVE_STACK_ID = "MainPerspectiveStack";
    
        @Execute
        public void execute(EModelService modelService, MApplication application) {
             MPerspectiveStack perspectiveStack = (MPerspectiveStack) modelService.find(MAIN_PERSPECTIVE_STACK_ID, application);
             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().resetPerspective();
             perspectiveStack.getSelectedElement().setVisible(true);
             perspectiveStack.setVisible(true);
        }
    }
    

答案 1 :(得分:1)

重置透视图(当您在不清理工作空间的情况下为e4应用程序午餐时,将其他透视图切换为感知角度时)。

第1步:在应用程序级别的模型片段中添加一个附件。 enter image description here

步骤2:创建加载项类并实现EventHandler

第3步:在类中添加以下代码。

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