9

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.

zvikico
  • 9,765
  • 4
  • 38
  • 49

6 Answers6

9

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
  • 9,765
  • 4
  • 38
  • 49
  • 1
    How is it possible to iterate over all views that are open in all perspectives? – Lii Feb 14 '18 at 10:39
3

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

Lii
  • 11,553
  • 8
  • 64
  • 88
Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
  • 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 Aug 04 '09 at 13:51
0

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
  • 83,208
  • 23
  • 172
  • 177
0

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);
            }
        }
    }
}
Manglesh
  • 520
  • 1
  • 13
  • 29
0

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.

vabank
  • 1
  • 1
0

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.

Priyadarshini
  • 129
  • 1
  • 10