如何在 Eclipse 中关闭 ViewPart?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1227059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close a ViewPart in Eclipse?
提问by zvikico
I have a view in Eclipse (implemented by a class which extends org.eclipse.ui.part.ViewPart
) which I need to close. I mean completely close, not just hide. I want a new ViewPart instance to be created when the user (or my code) asks to open the view again.
我在 Eclipse 中有一个视图(由一个扩展的类实现org.eclipse.ui.part.ViewPart
),我需要关闭它。我的意思是完全关闭,而不仅仅是隐藏。当用户(或我的代码)要求再次打开视图时,我希望创建一个新的 ViewPart 实例。
The only method I found was IWorkbenchPage.hideView
which hides the view, but does not completely dispose of it. Invoking dispose
on the view has no affect, either.
我发现的唯一方法是IWorkbenchPage.hideView
隐藏视图,但没有完全处理它。调用dispose
视图也没有影响。
BTW, my view is defined as allowMultiple="false"
but I tried with true
and that didn't make any difference.
顺便说一句,我的观点被定义为allowMultiple="false"
但我尝试过true
并且没有任何区别。
Any help will be appreciated.
任何帮助将不胜感激。
回答by zvikico
I found the problem eventually. If the view is open on more than one perspective, hiding it on one perspective will not close it. It is possible to iterate over all the open perspective and look for the view. Hiding it on all perspectives will close it.
我最终发现了问题。如果视图在多个透视图上打开,将它隐藏在一个透视图上不会关闭它。可以遍历所有打开的透视图并查找视图。将它隐藏在所有角度将关闭它。
回答by Manuel Selva
I think the IWorkbenchPage.hideView()
method you mentioned is the only one available to programmaticaly close a view. I also think this method name should be closeView() beacause it really close the view.
我认为IWorkbenchPage.hideView()
您提到的方法是唯一可用于以编程方式关闭视图的方法。我也认为这个方法名称应该是 closeView() 因为它真的关闭了视图。
I have been using this method for a while (with allowMultiple=true
views) and after debugging it seems my view.dispose()
method is invoked each time I invoke hideView()
.
我一直在使用这种方法一段时间(带有allowMultiple=true
视图),调试后似乎view.dispose()
每次调用hideView()
.
Next time I open this view again (I mean from my code and not from user interface), a new one is created by Eclipse and the createPartControl()
method is invoked again.
下次我再次打开这个视图时(我的意思是从我的代码而不是从用户界面),Eclipse 会创建一个新视图并再次createPartControl()
调用该方法。
Moreover, the call hierarchy view told me than the hideView()
should call the dispose method()
....
此外,调用层次结构视图告诉我hideView()
应该调用 dispose method()
....
hideView() >> releaseView() >> partRemoved() >> disposePart() >> dispose() >> doDisposePart() >> doDisposePart() >> dispose()
Hope this can help ....
希望这可以帮助....
One last question, how did you checked that your view was not correctly disposed ??
最后一个问题,你是如何检查你的视图没有正确处理的?
回答by Rich Seller
The org.eclipse.ui.internal.ViewFactory has a method called releaseView that I think closes the view completely (though I'm not certain). It takes an IViewReference.
org.eclipse.ui.internal.ViewFactory 有一个名为 releaseView 的方法,我认为它可以完全关闭视图(尽管我不确定)。它需要一个 IViewReference。
You can access the ViewFactory by calling Perspective.getViewFactory, and you can access the Perspective, you then pass it an IViewReference to release the view.
您可以通过调用 Perspective.getViewFactory 来访问 ViewFactory,您可以访问 Perspective,然后向它传递一个 IViewReference 以释放视图。
IWorkbenchPage page =
Workbench.getInstance().getActiveWorkbenchWindow().getActivePage()
Perspective perspective = page.getPerspective();
String viewId = "myViewId"; //defined by you
//get the reference for your viewId
IViewReference ref = page.findViewReference(viewId);
//release the view
perspective.getViewFactory.releaseView(ref);
回答by Manglesh
I overridden dispose method from IWorkbenchPart and that worked. I had something like this in my overridden dispose method:
我从 IWorkbenchPart 重写了 dispose 方法并且有效。我在我重写的 dispose 方法中有这样的事情:
public void dispose() {
super.dispose();
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
if (page != null) {
IViewReference[] viewReferences = page.getViewReferences();
for (IViewReference ivr : viewReferences) {
if (ivr.getId().equalsIgnoreCase("your view id")
|| ivr.getId().equalsIgnoreCase("more view id if you want to close more than one at a time")) {
page.hideView(ivr);
}
}
}
}
回答by vabank
In order to dispose ViewPart on closing Perspective we used the next code:
为了在关闭 Perspective 时处理 ViewPart,我们使用了下一个代码:
IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (workbenchWindow != null) {
workbenchWindow.addPerspectiveListener(new PerspectiveAdapter() {
@Override
public void perspectiveActivated(IWorkbenchPage page,
IPerspectiveDescriptor perspectiveDescriptor) {
super.perspectiveActivated(page, perspectiveDescriptor);
}
@Override
public void perspectiveDeactivated(IWorkbenchPage page,
IPerspectiveDescriptor perspective) {
super.perspectiveDeactivated(page, perspective);
page.closePerspective(perspective, false, true);
}
});
}
In result of page.closePerspective(perspective, false, true);
, ViewPart that was opened within perspective, will be disposed.
结果page.closePerspective(perspective, false, true);
,在透视中打开的 ViewPart 将被处理。
回答by Priyadarshini
To close views, opened in different perspective, I overridden perspectiveDeactivated() of org.eclipse.ui.PerspectiveAdapter.
为了关闭以不同视角打开的视图,我覆盖了 org.eclipse.ui.PerspectiveAdapter 的perspectiveDeactivated()。
public void perspectiveDeactivated(IWorkbenchPage page,
IPerspectiveDescriptor perspective) {
super.perspectiveDeactivated(page, perspective);
boolean myPerspective = MyPerspective.PERSPECTIVE_ID.equals(perspective.getId());
if(!myPerspective) {
//close Error Log view if it is opened in any perspective except My perspective.
IViewPart errorView = page.findView("org.eclipse.pde.runtime.LogView");
if(errorView != null) {
page.hideView(errorView);
}
}
}
My requirement was to close "Error Log" view. Above code can be modified to close any view.
我的要求是关闭“错误日志”视图。可以修改以上代码以关闭任何视图。