简体   繁体   English

Eclipse RCP e4以编程方式切换工作区

[英]Eclipse RCP e4 switch workspace programmatically

I would like to switch the workspace in a pure rcp e4 application in the code. 我想在代码中的纯rcp e4应用程序中切换工作区。 I found the old way that uses the org.eclipse.ui.internal.ide.actions.OpenWorkspaceAction . 我发现了使用org.eclipse.ui.internal.ide.actions.OpenWorkspaceAction的旧方法。

But this approach doesn't work in a pure e4 app because the application model has changed. 但是这种方法在纯e4应用程序中不起作用,因为应用程序模型已更改。

It does not appear to be possible to do this. 似乎无法执行此操作。

When you run a pure e4 application the main IApplication is org.eclipse.e4.ui.internal.workbench.swt.E4Application . 运行纯e4应用程序时,主要的IApplicationorg.eclipse.e4.ui.internal.workbench.swt.E4Application This code does not support the special return code used to signal that a change of workspace is required. 该代码不支持用于指示需要更改工作空间的特殊返回码。

You can restart the existing workbench be calling IWorkbench.restart . 您可以通过调用IWorkbench.restart重新启动现有工作台。 IWorkbench is org.eclipse.e4.ui.workbench.IWorkbench which can be injected. IWorkbench是可以注入的org.eclipse.e4.ui.workbench.IWorkbench

I got a solution for this problem: 我有解决此问题的方法:

The big problem with e4 is that the returing code will not be changed and equinox need a IApplication.EXIT_RESTART code. e4的最大问题是重试代码将不会更改,并且春分需要IApplication.EXIT_RESTART代码。

Workaround: - First implement the IApplication interface (see code below) 解决方法:-首先实现IApplication接口(请参见下面的代码)

 public class FrameworkE4Application implements IApplication {
        private static FrameworkE4Application application;
        private Integer exit_code = IApplication.EXIT_OK;
        private E4Application e4Application;

        public static FrameworkE4Application getInstance() {
            return application;
        }

        public void setRestartCode() {
            exit_code = IApplication.EXIT_RESTART;
        }

        @Override
        public Object start(IApplicationContext context) throws Exception {
            application = this;
            e4Application = new E4Application();
            e4Application.start(context);
            return exit_code;
        }

        @Override
        public void stop() {
            e4Application.stop();
        }
    }  

Then you need to register your custom implementation with equinox. 然后,您需要向春分注册您的自定义实现。 For this extend the org.eclipse.core.runtime.applications extension point (see below). 为此,请扩展org.eclipse.core.runtime.applications扩展点(请参见下文)。

  <extension id="FrameworkE4Application"
      point="org.eclipse.core.runtime.applications"> 
      <application> 
         <run  class="FrameworkE4Application"> 
            <parameter name="optimize" value="true"/> 
         </run> 
       </application> 
   </extension> 

If you would like to start this as product you need to overwritte the org.eclipse.core.runtime.products extension point (see below). 如果要将此作为产品启动,则需要改写org.eclipse.core.runtime.products扩展点(请参见下文)。

 <product
    name="YourProduct"
    application="FrameworkE4Application">
    <property
        name="appName"
        value="ProductPath">
    </property>
 </product>

And here is a implementation of an action that restarts your application. 这是重新启动应用程序的操作的实现。

public class RestartApplicationAction {
    @Execute
    public void execute(IWorkbench workbench) {
        FrameworkE4Application.getInstance().setRestartCode();
        workbench.close();
    }
}

Now you can set the new Location and restart the application with a new workspace. 现在,您可以设置新的位置并使用新的工作区重新启动应用程序。

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

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