4

I have a Eclipse plugin that uses a view which extends ViewPart. ViewPart has a saveState method which requires an IMemento.

I added my code to saveState and the corresponding init method and it works. Unfortunately, saveState is only called if the entire workspace is shutting down. My view is not of such great importance that I can expect it to be opened the entire time. Hence, it would be cool if saveState would be called on view closure.

I found a view-part listener as mean to react on view closure, but what I do not get is where the IMemento comes from. Where do I get the memento object that is used on workspace closure? Or where do I have to store my own memento object to make the view part use it in the init method if the view is (re)opened?

@Override
public void saveState(IMemento memento) {
    super.saveState(memento);
    memento = memento.createChild(MEMENTO_GUI_STATE);
    memento.putBoolean(MEMENTO_IS_FLAT, !isHierarchicalModeActive());
    memento.putBoolean(MEMENTO_IS_CATEGORY_MODE_ACTIVE, comboViewer.isVisible());
}

This is my saveState - can I tell my view somehow tell to call it every time the view closes?

Lii
  • 11,553
  • 8
  • 64
  • 88
user867204
  • 265
  • 4
  • 16

3 Answers3

4

Take a look at this question in the Eclipse FAQ:

Storing view state is done in two commons ways, depending on whether you want to store settings between workbench sessions or across invocations of your view. The first of these facilities is found directly on IViewPart. When the workbench is shut down, the method saveState is called on all open views.

Another mechanism for persisting view state is the JFace IDialogSettings facility. The advantage of dialog settings over the view save/init mechanism is that you can control when settings are persisted. The saveState method is called only if your view is open when the workbench shuts down, so it is not useful for storing view state when the view is closed by the user. Dialog settings, on the other hand, can be changed and persisted whenever you want.

Go to this other question or to the Eclipse documentation itself for the settings mechanism.

  • Having just ran into this problem I came to the conclusion that for saving state for a view, the DialogSettings works best, leaving the memento to the Workbench's saving of view size, location etc. The DialogSettings are simple retrieved from the plug-in: Activator.getDefault().getDialogSettings(); – fredrik Aug 28 '13 at 14:54
2

Well this could be "a bit" ugly but nothing else came to my mind: store memento variable as a field variable, initialize it in your init(IViewSite site, IMemento memento) method, override dispose() and call saveState(IMemento memento) explicitely.

Bela Vizer
  • 2,527
  • 22
  • 26
  • `dispose()` of the `View` might be called after the `Widget`s have been disposed, resulting in a `org.eclipse.swt.SWTException`: Widget is disposed when trying to save the state of those widgets – benez Nov 04 '16 at 18:00
0

You can read and write your own XMLMemento from your org.eclipse.core.runtime.Plugin.getStateLocation() at any time you want. As @BelaViser mentioned, you could write your file in your IViewPart#dispose() method and read it in your view constructor.

Paul Webster
  • 10,614
  • 1
  • 25
  • 32