fdd4a8c686dcd58296fb04acc225500eadd37eb8
[idea/community.git] / platform / lang-impl / src / com / intellij / facet / impl / ui / FacetDependentToolWindowManager.java
1 // Copyright 2000-2018 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 public class FacetDependentToolWindowManager implements ProjectComponent {
16   private final Project myProject;
17   private final ProjectWideFacetListenersRegistry myFacetListenersRegistry;
18   private final ProjectFacetManager myFacetManager;
19   private final ToolWindowManagerEx myToolWindowManager;
20
21   protected FacetDependentToolWindowManager(Project project,
22                                             ProjectWideFacetListenersRegistry facetListenersRegistry,
23                                             ProjectFacetManager facetManager,
24                                             ToolWindowManagerEx toolWindowManager) {
25     myProject = project;
26     myFacetListenersRegistry = facetListenersRegistry;
27     myFacetManager = facetManager;
28     myToolWindowManager = toolWindowManager;
29   }
30
31   @Override
32   public void projectOpened() {
33     myFacetListenersRegistry.registerListener(new ProjectWideFacetAdapter<Facet>() {
34       @Override
35       public void facetAdded(@NotNull Facet facet) {
36         for (FacetDependentToolWindow extension : getDependentExtensions(facet)) {
37           ensureToolWindowExists(extension);
38         }
39       }
40
41       @Override
42       public void facetRemoved(@NotNull Facet facet) {
43         if (!myFacetManager.hasFacets(facet.getTypeId())) {
44           for (FacetDependentToolWindow extension : getDependentExtensions(facet)) {
45             ToolWindow toolWindow = myToolWindowManager.getToolWindow(extension.id);
46             if (toolWindow != null) {
47               // check for other facets
48               List<FacetType> facetTypes = extension.getFacetTypes();
49               for (FacetType facetType : facetTypes) {
50                 if (myFacetManager.hasFacets(facetType.getId())) return;
51               }
52               myToolWindowManager.unregisterToolWindow(extension.id);
53             }
54           }
55         }
56       }
57     }, myProject);
58
59     loop: for (FacetDependentToolWindow extension : FacetDependentToolWindow.EXTENSION_POINT_NAME.getExtensionList()) {
60       for (FacetType type : extension.getFacetTypes()) {
61         if (myFacetManager.hasFacets(type.getId())) {
62           ensureToolWindowExists(extension);
63           continue loop;
64         }
65       }
66     }
67   }
68
69   private void ensureToolWindowExists(FacetDependentToolWindow extension) {
70     ToolWindow toolWindow = myToolWindowManager.getToolWindow(extension.id);
71     if (toolWindow == null) {
72       myToolWindowManager.initToolWindow(extension);
73     }
74   }
75
76   private static List<FacetDependentToolWindow> getDependentExtensions(final Facet facet) {
77     return ContainerUtil.filter(FacetDependentToolWindow.EXTENSION_POINT_NAME.getExtensionList(), toolWindowEP -> {
78       for (String id : toolWindowEP.getFacetIds()) {
79         if (facet.getType().getStringId().equals(id)) return true;
80       }
81       return false;
82     });
83   }
84 }