Howto restart an e4 RCP application

安稳与你 提交于 2019-12-22 09:58:11

问题


A 3.* based RCP application will be restarted if IApplication.start returns IApplication.EXIT_RESTART. The class E4Application seems to always return EXIT_OK.

org.eclipse.ui.IWorkbench also has a restart method, org.eclipse.e4.ui.workbench.IWorkbench does not.

So how can I restart an e4 RCP application?


回答1:


The current implementation in Eclipse 4.2 leads to a command with id org.eclipse.ui.file.restartWorkbench which ultimately leads to the handler class RestartWorkbenchHandler. This class is implemented as follows

public Object execute(ExecutionEvent event){

    PlatformUI.getWorkbench().restart();
    return null;
}

Which means, that it is Eclipse 3.x dependent, as the PlatformUI class is not available in a pure Eclipse 4 based application anymore. So if you want to solve this task in your e4 Application, create a command, implement the handler and currently you will have some E3 code dependency!




回答2:


Until this feature is introduced into e4 my workaround goes like this: I use my IApplication implementation as a wrapper to the E4Application. This way, I can return IApplication.EXIT_RESTART from the start method and Equniox will do the restart.

Beware though, that this workaround uses an internal API, which is likely to change in the future!

package de.emsw.gosa.e4.application;

import org.eclipse.e4.ui.internal.workbench.swt.E4Application;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

public class MyE4Application implements IApplication {

    private static MyE4Application instance;

    public static MyE4Application getInstance() {
        return instance;
    }

    private Integer exitRet = IApplication.EXIT_OK;
    private E4Application e4app;

    public void setRestart() {
        exitRet = IApplication.EXIT_RESTART;
    }

    @Override
    public Object start(IApplicationContext context) throws Exception {
        instance = this;
        e4app = new E4Application();
        e4app.start(context);
        return exitRet;
    }

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

When you you want to restart, you can now use the Singleton to set the return value:

@Execute
public void execute(IWorkbench workbench) {
    MyE4Application.getInstance().setRestart();
    workbench.close();
}

I used a Singleton here, because it is easier to explain this way. A better solution is to register your IApplication as an OSGi service and have DI inject it into your handler.




回答3:


org.eclipse.e4.ui.workbench.IWorkbench does now have a restart method in Eclipse Kepler (4.3).



来源:https://stackoverflow.com/questions/12869859/howto-restart-an-e4-rcp-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!