fix "IDEA-221944 Deadlock on opening second project" and support preloading for proje...
[idea/community.git] / platform / lang-impl / src / com / intellij / facet / impl / ui / FacetDependentToolWindowManager.java
1 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.facet.impl.ui;
3
4 import com.intellij.facet.*;
5 import com.intellij.facet.ui.FacetDependentToolWindow;
6 import com.intellij.openapi.components.ProjectComponent;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.openapi.wm.ToolWindow;
9 import com.intellij.openapi.wm.ex.ToolWindowManagerEx;
10 import com.intellij.util.containers.ContainerUtil;
11 import org.jetbrains.annotations.NotNull;
12
13 import java.util.List;
14
15 final class FacetDependentToolWindowManager implements ProjectComponent {
16   private final Project myProject;
17
18   private FacetDependentToolWindowManager(@NotNull Project project) {
19     myProject = project;
20   }
21
22   @Override
23   public void projectOpened() {
24     ProjectWideFacetListenersRegistry.getInstance(myProject).registerListener(new ProjectWideFacetAdapter<Facet>() {
25       @Override
26       public void facetAdded(@NotNull Facet facet) {
27         for (FacetDependentToolWindow extension : getDependentExtensions(facet)) {
28           ensureToolWindowExists(extension);
29         }
30       }
31
32       @Override
33       public void facetRemoved(@NotNull Facet facet) {
34         ProjectFacetManager facetManager = ProjectFacetManager.getInstance(myProject);
35         if (facetManager.hasFacets(facet.getTypeId())) {
36           return;
37         }
38
39         ToolWindowManagerEx toolWindowManager = ToolWindowManagerEx.getInstanceEx(myProject);
40         for (FacetDependentToolWindow extension : getDependentExtensions(facet)) {
41           ToolWindow toolWindow = toolWindowManager.getToolWindow(extension.id);
42           if (toolWindow != null) {
43             // check for other facets
44             List<FacetType> facetTypes = extension.getFacetTypes();
45             for (FacetType facetType : facetTypes) {
46               if (facetManager.hasFacets(facetType.getId())) {
47                 return;
48               }
49             }
50             toolWindowManager.unregisterToolWindow(extension.id);
51           }
52         }
53       }
54     }, myProject);
55
56     ProjectFacetManager facetManager = ProjectFacetManager.getInstance(myProject);
57     loop: for (FacetDependentToolWindow extension : FacetDependentToolWindow.EXTENSION_POINT_NAME.getExtensionList()) {
58       for (FacetType type : extension.getFacetTypes()) {
59         if (facetManager.hasFacets(type.getId())) {
60           ensureToolWindowExists(extension);
61           continue loop;
62         }
63       }
64     }
65   }
66
67   private void ensureToolWindowExists(FacetDependentToolWindow extension) {
68     ToolWindow toolWindow = ToolWindowManagerEx.getInstanceEx(myProject).getToolWindow(extension.id);
69     if (toolWindow == null) {
70       ToolWindowManagerEx.getInstanceEx(myProject).initToolWindow(extension);
71     }
72   }
73
74   private static List<FacetDependentToolWindow> getDependentExtensions(final Facet facet) {
75     return ContainerUtil.filter(FacetDependentToolWindow.EXTENSION_POINT_NAME.getExtensionList(), toolWindowEP -> {
76       for (String id : toolWindowEP.getFacetIds()) {
77         if (facet.getType().getStringId().equals(id)) return true;
78       }
79       return false;
80     });
81   }
82 }