0

我有一个 Eclipse 插件,它有两个视角。有一个视图在两个视角之一中扩展了 ViewPart。在这个视图中,我重写了 ViewPart 的 saveState 方法来保存我的数据。

首先,我打开有这种观点的观点。然后我在视图中添加一些数据,这些数据应该保存在 saveState 中。接下来,我导航到没有此视图的另一个透视图。最后,我关闭了 eclipse 的工作区。

在 eclipse 4.2(juno) 中,视图的 saveState 方法没有被调用。我的数据丢失了。在 eclipse 3.6(Helios) 中,视图的 saveState 方法已被调用。我的约会一直坚持。

有谁知道原因?如何确保在所有版本的 eclipse 上关闭工作区时将调用 saveState?

4

2 回答 2

0

With joy, this problem persists into 4.4.2.

At its base, the issue is the compatibility layer WorkbenchPart.getViewReferences() only searches the currently active perspective. This behavior is different than the 3.x. The relevant code from the 4.4.2 Eclipse WorkbenchPart is here (notice the call to getCurrentPerspective()).

    public IViewReference[] getViewReferences() {
    MPerspective perspective = getCurrentPerspective();
    if (perspective != null) {
        List<MPlaceholder> placeholders = modelService.findElements(window, null,
                MPlaceholder.class, null, EModelService.PRESENTATION);
        List<IViewReference> visibleReferences = new ArrayList<IViewReference>();
        for (ViewReference reference : viewReferences) {
            for (MPlaceholder placeholder : placeholders) {
                if (reference.getModel() == placeholder.getRef()
                        && placeholder.isToBeRendered()) {
                    // only rendered placeholders are valid view references
                    visibleReferences.add(reference);
                }
            }
        }
        return visibleReferences.toArray(new IViewReference[visibleReferences.size()]);
    }
    return new IViewReference[0];
}

Therefore, if one has a view open and then changes to a perspective where that view is not shown, the saveState() method will not be called.

We have added an OSGI event listener for the UIEvents.UILifeCycle.appShutdownStarted and made a call to the saveState(). However, it is necessary to obtain the IMemento manually, since it is not present. Example code is in the org.eclipse.ui.internal.ViewReference (http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/4.2.2/org.eclipse.ui/workbench/3.104.0/org/eclipse/ui/internal/ViewReference.java#ViewReference).

One could also add a part close listener with the IPartListener class to potentially persist settings if the user closes the view rather than application shutting down.

We have not found an OSGI event for the part being closed, but there may be one.

This discussion (Eclipse call ViewPart saveState on View close) suggested using IDialogSettings rather the IMemento. This discussion also proposed perhaps adding something into the dispose() method, but it is unclear how many resources are necessarily still available at the point of the dispose() being called.

于 2015-03-26T23:40:26.980 回答
0

Eclipse e4 没有 ApplicationWorkbenchAdvisor 类,并且应用程序模型没有设置它的属性,这与 Eclipse 3.x 完全不同。
您可以从wikivogella 教程博客中获得更多信息。

于 2013-05-20T03:29:07.350 回答