简体   繁体   中英

Eclipse RCP - How to shutdown before workbench initializes

I have a similar setup to below:

<extension
     id="product"
     point="org.eclipse.core.runtime.products">
  <product
        name="%product.name"
        application="org.eclipse.e4.ui.workbench.swt.E4Application">
      <property
           name="lifeCycleURI"
           value="bundleclass://plugin-id/package.LifeCycle">
     </property>
     .... more properties ...
public class LifeCycle
{
  @PostConstruct
  public void doWork()
  {
    // Show a login screen. If the user cancels out of it, shut down
    // the application. 
  }
}

In the scenario above, what is the correct way to properly shutdown the application? If I do:

PlatformUI.getWorkbench().close()

I get an error since it's not initialized yet. If I do:

System.exit(0)

then I kill all other things on the JVM (Even though it is suggested to do it here http://www.vogella.com/tutorials/Eclipse4LifeCycle/article.html )

Any ideas/suggestions on how this could be done?

PlatformUI is not available in an e4 application, do not try and use it.

@PostConstruct is too early to do anything in the LifeCycle class. The first point you should try and do anything is the @PostContextCreate method.

You can inject org.eclipse.e4.ui.workbench.IWorkbench and call the close method to shutdown the e4 application. However the workbench is not available until the application startup is complete so you need to wait for this event.

public class LifeCycle
{
  @PostContextCreate
  public void postContextCreate(IEclipseContext context, IEventBroker eventBroker)
  {
    ...

    eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE,
                          new AppStartupCompleteEventHandler(eventBroker, context));
  }
}


class AppStartupCompleteEventHandler implements EventHandler
{
 private IEventBroker _eventBroker;
 private IEclipseContext _context;


 AppStartupCompleteEventHandler(IEventBroker eventBroker, IEclipseContext context)
 {
   _eventBroker = eventBroker;
   _context = context;
 }

 @Override
 public void handleEvent(final Event event)
 {
   _eventBroker.unsubscribe(this);

   IWorkbench workbench = _context.get(IWorkbench.class);

   workbench.close();
 }
}

System.exit() is the only way to currently abort the E4 startup if you are using the SWT renderer.

If you use the JavaFX renderer from e(fx)clipse you can return FALSE from @PostContextCreate to shutdown.

For more information see this blog: http://tomsondev.bestsolution.at/2014/11/03/efxclipse-1-1-new-features-api-to-restart-your-e4-app-on-startup/

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