2 * Copyright (c) 2007 JetBrains s.r.o. All Rights Reserved.
5 package com
.intellij
.facet
.impl
.autodetecting
.facetsTree
;
7 import com
.intellij
.facet
.FacetType
;
8 import com
.intellij
.facet
.Facet
;
9 import com
.intellij
.facet
.impl
.autodetecting
.DetectedFacetManager
;
10 import com
.intellij
.facet
.impl
.autodetecting
.model
.DetectedFacetInfo
;
11 import com
.intellij
.facet
.impl
.autodetecting
.model
.FacetInfo2
;
12 import com
.intellij
.facet
.impl
.autodetecting
.model
.FacetInfoBackedByFacet
;
13 import com
.intellij
.openapi
.module
.Module
;
14 import com
.intellij
.openapi
.vfs
.VirtualFile
;
15 import org
.jetbrains
.annotations
.Nullable
;
22 public class ImplicitFacetsTreeComponent
{
23 private DetectedFacetsTree myTree
;
24 private final DetectedFacetManager myDetectedFacetManager
;
25 private Collection
<DetectedFacetsTree
.FacetTypeNode
> myFacetTypeNodes
;
27 public ImplicitFacetsTreeComponent(DetectedFacetManager detectedFacetManager
, Collection
<DetectedFacetInfo
<Module
>> detectedFacets
, HashMap
<DetectedFacetInfo
<Module
>, List
<VirtualFile
>> files
) {
28 myDetectedFacetManager
= detectedFacetManager
;
30 Map
<FacetType
, DetectedFacetsTree
.FacetTypeNode
> facetTypeNodes
= new HashMap
<FacetType
, DetectedFacetsTree
.FacetTypeNode
>();
32 Collection
<FacetInfo2
<Module
>> sortedFacets
= new LinkedHashSet
<FacetInfo2
<Module
>>();
33 for (FacetInfo2
<Module
> facet
: detectedFacets
) {
34 addUnderlying(facet
, sortedFacets
);
37 Map
<FacetInfo2
<Module
>, DetectedFacetsTree
.FacetNode
> facetNodes
= new HashMap
<FacetInfo2
<Module
>, DetectedFacetsTree
.FacetNode
>();
38 for (FacetInfo2
<Module
> facetInfo
: sortedFacets
) {
39 FacetType facetType
= getRootFacetType(facetInfo
);
40 DetectedFacetsTree
.FacetTypeNode facetTypeNode
= facetTypeNodes
.get(facetType
);
41 if (facetTypeNode
== null) {
42 facetTypeNode
= new DetectedFacetsTree
.FacetTypeNode(facetType
);
43 facetTypeNodes
.put(facetType
, facetTypeNode
);
46 Module module
= facetInfo
.getModule();
47 ModuleNodeImpl moduleNode
= findOrCreateModuleNode(facetTypeNode
, module
);
49 DetectedFacetsTree
.FacetNode parentNode
= null;
50 FacetInfo2
<Module
> underlyingFacet
= facetInfo
.getUnderlyingFacetInfo();
51 if (underlyingFacet
!= null) {
52 parentNode
= facetNodes
.get(underlyingFacet
);
55 VirtualFile projectRoot
= facetInfo
.getModule().getProject().getBaseDir();
56 DetectedFacetsTree
.FacetNode facetNode
;
57 if (facetInfo
instanceof DetectedFacetInfo
) {
58 facetNode
= new FacetNodeImpl((DetectedFacetInfo
<Module
>)facetInfo
, projectRoot
, parentNode
, files
.get(facetInfo
));
61 facetNode
= new RealFacetNode((FacetInfoBackedByFacet
)facetInfo
, projectRoot
, parentNode
);
63 facetNodes
.put(facetInfo
, facetNode
);
64 if (parentNode
== null) {
65 moduleNode
.addRootFacet(facetNode
);
69 myFacetTypeNodes
= facetTypeNodes
.values();
70 myTree
= new DetectedFacetsTree(myFacetTypeNodes
);
73 public void createFacets() {
74 for (DetectedFacetsTree
.FacetTypeNode facetTypeNode
: myFacetTypeNodes
) {
75 for (DetectedFacetsTree
.ModuleNode moduleNode
: facetTypeNode
.getModuleNodes()) {
76 boolean accept
= facetTypeNode
.isChecked() && moduleNode
.isChecked();
78 processFacetNodes(moduleNode
.getRootFacets(), true, null);
81 myDetectedFacetManager
.disableDetectionInModule(facetTypeNode
.getFacetType(), ((ModuleNodeImpl
)moduleNode
).myModule
);
87 private void processFacetNodes(final List
<DetectedFacetsTree
.FacetNode
> facetNodes
, final boolean accept
, final Facet underlyingFacet
) {
88 for (DetectedFacetsTree
.FacetNode facetNode
: facetNodes
) {
90 if (facetNode
instanceof FacetNodeImpl
) {
91 DetectedFacetInfo
<Module
> detectedFacetInfo
= ((FacetNodeImpl
)facetNode
).getDetectedFacetInfo();
92 if (accept
&& facetNode
.isChecked()) {
93 facet
= myDetectedFacetManager
.createFacet(detectedFacetInfo
, underlyingFacet
);
96 myDetectedFacetManager
.disableDetectionInFile(detectedFacetInfo
);
100 facet
= ((RealFacetNode
)facetNode
).myFacetInfo
.getFacet();
102 processFacetNodes(facetNode
.getChildren(), accept
&& facetNode
.isChecked(), facet
);
106 private static void addUnderlying(final FacetInfo2
<Module
> facet
, final Collection
<FacetInfo2
<Module
>> sortedFacets
) {
107 FacetInfo2
<Module
> underlying
= facet
.getUnderlyingFacetInfo();
108 if (underlying
!= null && !sortedFacets
.contains(underlying
)) {
109 addUnderlying(underlying
, sortedFacets
);
111 sortedFacets
.add(facet
);
114 private static ModuleNodeImpl
findOrCreateModuleNode(final DetectedFacetsTree
.FacetTypeNode facetTypeNode
, final Module module
) {
115 for (DetectedFacetsTree
.ModuleNode node
: facetTypeNode
.getModuleNodes()) {
116 ModuleNodeImpl moduleNode
= (ModuleNodeImpl
)node
;
117 if (moduleNode
.myModule
.equals(module
)) {
121 ModuleNodeImpl moduleNode
= new ModuleNodeImpl(module
);
122 facetTypeNode
.addModuleNode(moduleNode
);
126 private static FacetType
getRootFacetType(final FacetInfo2
<?
> detectedFacet
) {
127 FacetInfo2
<?
> facet
= detectedFacet
;
128 FacetInfo2
<?
> underlyingFacet
;
129 while ((underlyingFacet
= facet
.getUnderlyingFacetInfo()) != null) {
130 facet
= underlyingFacet
;
132 return facet
.getFacetType();
135 public DetectedFacetsTree
getTree() {
139 private static class FacetNodeImpl
extends DetectedFacetsTree
.FacetNode
{
140 private final DetectedFacetInfo
<Module
> myDetectedFacetInfo
;
142 private FacetNodeImpl(DetectedFacetInfo
<Module
> detectedFacet
, VirtualFile projectRoot
, @Nullable final DetectedFacetsTree
.FacetNode parent
,
143 final List
<VirtualFile
> files
) {
144 super(detectedFacet
, detectedFacet
.getFacetType(), projectRoot
, files
.toArray(new VirtualFile
[files
.size()]), parent
);
145 myDetectedFacetInfo
= detectedFacet
;
148 public DetectedFacetInfo
<Module
> getDetectedFacetInfo() {
149 return myDetectedFacetInfo
;
152 public String
getName() {
153 return myDetectedFacetInfo
.getFacetName();
157 private static class RealFacetNode
extends DetectedFacetsTree
.FacetNode
{
158 private final FacetInfoBackedByFacet myFacetInfo
;
160 private RealFacetNode(final FacetInfoBackedByFacet facetInfo
, final VirtualFile projectRoot
, @Nullable final DetectedFacetsTree
.FacetNode parent
) {
161 super(facetInfo
, facetInfo
.getFacetType(), projectRoot
, VirtualFile
.EMPTY_ARRAY
, parent
);
162 myFacetInfo
= facetInfo
;
165 public String
getDescription() {
169 public String
getName() {
170 return myFacetInfo
.getFacetName();
174 private static class ModuleNodeImpl
extends DetectedFacetsTree
.ModuleNode
{
175 private final Module myModule
;
177 private ModuleNodeImpl(final Module module
) {
182 public String
getModuleName() {
183 return myModule
.getName();
186 public String
getModuleDescription() {