Monday, March 5, 2012

Eclipse Plugins that need early startup

A pattern for Eclipse plugins that need early startup, and also have views (which may or may not be part of the active perspective on startup):
  1. Implement the org.eclipse.ui.startup extension point in a separate class, as the docs recommend, but don't actually do any work in the earlyStartup() method.
  2. Define a finishInit() method in your AbstractUIPlugin which is going to do the actual work of initializing your plugin. Also define an initComplete boolean field. Check this field at the start of finishInit(), and set it at the end, to ensure that the code inside finishInit() is only executed once.
  3. In the start() method of your AbstractUIPlugin, first initialize your static singleton instance field.  Then invoke finishInit() on the UI thread with PlatformUI.getWorkbench().getDisplay().asyncExec(). (Why? If your view is not in the active perspective, then start() will be called by a non-UI thread. The effect of this is that the workbench is not yet fully initialized when your start() method is called.)
  4. In the createPartControl() method of your views, first call finishInit() on the singleton AbstractUIPlugin instance. In the case where your view is present in the active perspective, createPartControl() will be called before the async call to finishInit() is processed on the UI thread. So you call it at the start of createPartControl() to make sure plugin initialization completes before you begin creating your views.