tags:

views:

1528

answers:

3

Hello,

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.

A: 

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);
Rich Seller
the releaseView Method is called from the hideView's one. Thus I think it's not required to call it.
Manuel Selva
A: 

Hi,

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 ??

Manuel Selva
My problem was not with disposing the view, but rather with its' creation. At some point, I had to completely close the view and open a fresh instance. My indication was simply a breakpoint in the view createPartControl method. It seems to be a new problem which started in Eclipse 3.5 (I'm on OS X, not sure regarding other platforms). Worked just fine before. The dispose is not happening either. I was just wondering if there's another way before opening a bug.
zvikico
Ok. I understand now ;o)
Manuel Selva
+1  A: 

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.

zvikico