RCP程序关闭时,关闭所有Editor和View

By | 11月 18, 2015

只需要给workbench添加一个Listener,此逻辑可以添加到ApplicationWorkbenchWindowAdvisor ::postWindowCreate(),

    final IWorkbench workbench = PlatformUI.getWorkbench();
    final IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();

    workbench.addWorkbenchListener(new IWorkbenchListener() {
      @Override
      public boolean preShutdown(IWorkbench workbench, boolean forced) {
        activePage.closeEditors(activePage.getEditorReferences(), true);
        // Tips: View cannot be closed (disposed), because a view can be used in multiple perspective.
        // When a view is hide in all perspective, this view will be disposed automatically.
        for (IViewReference viewReference : activePage.getViewReferences()) {
          activePage.hideView(viewReference);
        }
        return true;
      }

      @Override
      public void postShutdown(IWorkbench workbench) {
        // Do nothing.
      }
    });