Previous Page
Next Page

9.5. Delayed Changed Events

Eclipse uses lazy initializationonly load a plug-in when it is needed. Lazy initialization presents a problem for plug-ins that need to track changes. How does a plug-in track changes when it is not loaded?

Eclipse solves this problem by queuing change events for a plug-in that is not loaded. When the plug-in is loaded, it receives a single resource change event containing the union of the changes that have occurred during the time it was not active. To receive this event, your plug-in must register to be a save participant when it is started up, as follows.

public static void addSaveParticipant() {
   ISaveParticipant saveParticipant = new ISaveParticipant(){
      public void saving(ISaveContext context)
         throws CoreException
      {
         // Save any model state here.
         context.needDelta();
      }
      public void doneSaving(ISaveContext context) {}
      public void prepareToSave(ISaveContext context)
        throws CoreException {}
      public void rollback(ISaveContext context) {}
   };

   ISavedState savedState;
   try {
      savedState = ResourcesPlugin
         .getWorkspace()
         .addSaveParticipant  (
            FavoritesPlugin.getDefault(),
            saveParticipant);
   }
   catch (CoreException e) {
      FavoritesLog.logError(e);
      // Recover if necessary.
      return;
   }

   if (savedState != null)
      savedState.processResourceChangeEvents(
         FavoritesManager.getManager());
}

Tip

Even though Eclipse is based on lazy plug-in initialization, it does provide a mechanism for plug-ins to start when the workbench itself starts. To activate at startup, the plug-in must extend the org.eclipse.ui.startup extension point and implement the org.eclipse.ui. IStartup interface. Once the plug-in is started, the workbench will call the plug-in's earlyStartup() method (see Section 3.4.2, Early plug-in startup, on page 114). A workbench preference option gives the user the ability to prevent a plug-in from starting early, so make sure that if your plug-in takes advantage of this extension point, it degrades gracefully in the event that it is not started early.



Previous Page
Next Page