Search code examples
javaeclipse-plugineclipse-luna

Call method as soon as eclipse plug-in starts


I'm developing a plug-in for Eclipse Luna and I wanted to add some listeners to some view and I know how to do it. My question is where to do it?

I need them to be added as soon as the plug-in starts. So I'm thinking there must be some kind of method that gets called when the plug-in is loaded I just can't find it in docs. So far I tried adding this listeners in public void start(BundleContext context) throws Exception method in Activator class but it didn't work. I think that the ui part is not still loaded at that point and that's why it's failing.


Solution

  • A plug-in's Activator start method is not run until something else in the plug-in is used so this is not a suitable place to put listeners. By default plug-ins are not loaded during Eclipse initialization, they are only loaded when needed.

    You can use the org.eclipse.ui.startup extension point to define a class implementing IStartup that will be run during Eclipse initialization.

    Note that the earlyStartup method defined by this interface may be run before the UI is initialized. Use something like the following to run UI code from earlyStartup:

    Display.getDefault().asyncExec(new Runnable() {
      public void run() {
         // UI code
      }
    });
    

    For example to get the selection service:

    @Override
    public void earlyStartup()
    {
      Display.getDefault().asyncExec(new Runnable() {
       @Override
       public void run() {
         final ISelectionService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
         System.out.println("service " + service);
       }
     });
    }