Friday, September 28, 2007

How to save the Eclipse UI (workbench state) when YOU like


At yesterday's Portland BarCamp Meetup, I was confronted with an interesting problem: David's Eclipse hangs / crashes frequently (probably that pesky PermGen issue) and when that happens the state of the Workbench is lost (i.e. which editors, views, windows are open) as it is only saved on shutdown. So we wrote a plug-in that periodically saves the UI state.

Download the plug-in here (source code here). Eclipse 3.3 only!

Here is what we did:

  1. Since the UI state is saved when Eclipse shutdowns cleanly, we started by looking at the QuitAction. From here we went on to PlatformUI.getWorkbench().close(). A bit of exploration revealed that we need the methods in the snippet below.
    // org.eclipse.ui.internal.Workbench
    XMLMemento memento
    = XMLMemento.createWriteRoot( IWorkbenchConstants.TAG_WORKBENCH );
    saveState( memento );
    saveMementoToFile( memento )

  2. As this code is internal, we copied it into an action which we added to the coolbar. Clicking on the "lock" icon will save the state of the workbench. BTW copying internal code can come back and bite you, but we ignored that in this case.

  3. Then we made the plug-in save the UI state automatically. We added an IResourceChangeListener that is notified on POST_BUILD events. BTW this does not depend on the "auto-build" setting. Our listener will save the workbench state every 5 minutes. The help topic "Tracking Resource Changes" has more details. The listener is installed / removed in the plug-in activator:

    public void start( BundleContext context ) throws Exception {
    super.start( context );
    // ...
    rcListener = new ResourceChangeListener();
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    workspace.addResourceChangeListener(
    rcListener,
    IResourceChangeEvent.POST_BUILD );
    }

  4. To make sure the plug-in is activated on start-up, we added a dummy contribution to the org.eclipse.ui.startup extension point.

  5. If you want to monitor the plug-in's activities on the console, you can do that through a tracing option. Copy the following lines into a file called .options, place it in your eclipse directory and launch eclipse -debug
    # file .options in eclipse directory
    example.saveuistate/debug = true
    After doing a build you should see some output like this:

    Fri Sep 28 08:54:17 PDT 2007 Starting example.saveuistate
    Fri Sep 28 08:54:30 PDT 2007 POST_BUILD resource change
    Fri Sep 28 08:54:30 PDT 2007 Saving state.

Happy hacking,
Elias.

Image: (c) Copyright 2007, table/photocase.

8 comments:

Chris Aniszczyk (zx) said...

Maybe this is something that should be contributed to Eclipse?

If the icon was parachute, it would be even funnier ;)

Elias Volanakis said...

Chris,

I'm open to contribute the code back if there is enough interest.

BTW, having a parachute was my first thought, but I'm a horrible graphic artist ;-).

Elias.

Willian Mitsuda said...

Excellent idea! I'm tired of losing my workbench layout due to crashes.

Elias Volanakis said...

Thanks Willian, I'm glad you like it.

Regards, Elias.

Mike Long said...

+1 for inclusion in the eclipse default install. Firefox restore session was one of the best features they added, I think it would great to have this in eclipse.

Alexander Schlarb said...

Hi,

I'm trying to get this plugin running under Eclipse 3.5 but it's always telling me "No projects are found to import". I'd love to use this plugin but I'm not so much into Eclipse/Java coding. Does anyone have any advice on howto get this thing working in 3.5?

Alexander

Anonymous said...

Great little plugin - definitely would like to see in Eclipse core.

Aaron Digulla said...

I've attached an updated version of the plugin (works with Eclipse 3.3 and up) to this bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=337593

 

Content: © Copyright 2007 - 2009, Elias Volanakis. All rights reserved. Datenschutzhinweis / Privacy Policy