void (@NotNull Project project) { ProjectWideFacetListenersRegistry.getInstance(project).registerListener(new ProjectWideFacetAdapter<>() { @Override public void facetAdded(@NotNull Facet facet) { checkIfToolwindowMustBeAdded(facet.getType()); } @Override public void facetRemoved(@NotNull Facet facet) { checkIfToolwindowMustBeRemoved(facet.getType()); } private void checkIfToolwindowMustBeAdded(FacetType<?, ?> facetType) { ToolWindowManager.getInstance(project).invokeLater(() -> { for (FacetDependentToolWindow extension : getDependentExtensions(facetType)) { ensureToolWindowExists(extension, project); } }); } private void checkIfToolwindowMustBeRemoved(FacetType<?, ?> removedFacetType) { ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project); toolWindowManager.invokeLater(() -> { ProjectFacetManager facetManager = ProjectFacetManager.getInstance(project); if (facetManager.hasFacets(removedFacetType.getId())) { return; } for (FacetDependentToolWindow extension : getDependentExtensions(removedFacetType)) { ToolWindow toolWindow = toolWindowManager.getToolWindow(extension.id); if (toolWindow != null) { // check for other facets for (FacetType<?, ?> facetType : extension.getFacetTypes()) { if (facetManager.hasFacets(facetType.getId())) { return; } } toolWindow.remove(); } } }); } }, project); FacetDependentToolWindow.EXTENSION_POINT_NAME.addExtensionPointListener(new ExtensionPointListener<>() { @Override public void extensionAdded(@NotNull FacetDependentToolWindow extension, @NotNull PluginDescriptor pluginDescriptor) { initToolWindowIfNeeded(extension, project); } @Override public void extensionRemoved(@NotNull FacetDependentToolWindow extension, @NotNull PluginDescriptor pluginDescriptor) { ToolWindow window = ToolWindowManager.getInstance(project).getToolWindow(extension.id); if (window != null) { window.remove(); } } }, project); }