Eclipse call ViewPart saveState on View close

守給你的承諾、 提交于 2019-12-12 11:37:53

问题


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?


回答1:


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.




回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/9472279/eclipse-call-viewpart-savestate-on-view-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!