user2992188
user2992188

Reputation: 293

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?

Upvotes: 2

Views: 1098

Answers (2)

christoph.keimel
christoph.keimel

Reputation: 1240

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/

Upvotes: 0

greg-449
greg-449

Reputation: 111142

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

Upvotes: 1

Related Questions