How to close a ViewPart in Eclipse?

前端 未结 6 1371
时光取名叫无心
时光取名叫无心 2020-12-10 05:52

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 wa

相关标签:
6条回答
  • 2020-12-10 05:59

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

    0 讨论(0)
  • 2020-12-10 06:01

    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.

    0 讨论(0)
  • 2020-12-10 06:03

    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);
    
    0 讨论(0)
  • 2020-12-10 06:10

    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.

    0 讨论(0)
  • 2020-12-10 06:20

    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);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-10 06:22

    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.

    0 讨论(0)
提交回复
热议问题