Reputation: 9825
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.
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.
BTW, my view is defined as allowMultiple="false"
but I tried with true
and that didn't make any difference.
Any help will be appreciated.
Upvotes: 9
Views: 17773
Reputation: 19060
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.
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()
.
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.
Moreover, the call hierarchy view told me than the hideView()
should call the 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 ??
Upvotes: 3
Reputation: 129
To close views, opened in different perspective, I overridden perspectiveDeactivated() of org.eclipse.ui.PerspectiveAdapter.
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.
Upvotes: 0
Reputation: 1
In order to dispose ViewPart on closing Perspective we used the next code:
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.
Upvotes: 0
Reputation: 530
I overridden dispose method from IWorkbenchPart and that worked. I had something like this in my overridden dispose method:
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);
}
}
}
}
Upvotes: 0
Reputation: 9825
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.
Upvotes: 9
Reputation: 84068
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.
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.
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);
Upvotes: 0