Eclipse call ViewPart saveState on View close

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

I added saveState code and the corresponding init method, and it works. Unfortunately, saveState is only called when the entire workspace is closed. My view is not so important that I can expect it to be open all the time. Therefore, it would be great if saveState is called when the view is closed.

I found that the listener of the part of the view makes sense to respond to closing the view, but I do not get where IMemento comes IMemento . Where can I get the memmento object that is used when closing the workspace? Or where do I need to save my own memory object so that part of the view uses it in the init method if the view is open (re)?

 @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 say that my look somehow says to call it every time the view opens?

+4
source share
3 answers

Take a look at the question in the Eclipse FAQ:

Saving view state is done in two ways, depending on whether you want to save settings between Workbench sessions or through calls to your gaze. The first of these objects is found directly on IViewPart. When the workplace is disabled, the saveState method is called in all open views.

Another mechanism for maintaining view state is the JFace IDialogSettings Object. The advantage of dialog settings over view save / init is that you can control when the settings were saved. The saveState method is called only if your view is open when the workplace is turned off, so it is not useful to save the view state when the user closes the view. Dialog settings on the other hand can be changed and saved whenever you want.

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

+4
source

Well, it might be a little ugly, but it never crossed my mind: save the memento variable as a field variable, initialize it in your init(IViewSite site, IMemento memento) method init(IViewSite site, IMemento memento) , override dispose() and call saveState(IMemento memento) explicitly.

+2
source

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

0
source

Source: https://habr.com/ru/post/1398689/


All Articles