简体   繁体   中英

Eclipse RCP e4 switch workspace programmatically

I would like to switch the workspace in a pure rcp e4 application in the code. I found the old way that uses the org.eclipse.ui.internal.ide.actions.OpenWorkspaceAction .

But this approach doesn't work in a pure e4 app because the application model has changed.

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 . 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 is org.eclipse.e4.ui.workbench.IWorkbench which can be injected.

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.

Workaround: - First implement the IApplication interface (see code below)

 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).

  <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).

 <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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