update copyright
[fedora-idea.git] / java / idea-ui / src / com / intellij / facet / impl / autodetecting / facetsTree / DetectedFacetsTreeComponent.java
blob0d25027c678001a5c6f3e34fd68ed996553bcbc7
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.facet.impl.autodetecting.facetsTree;
19 import com.intellij.facet.*;
20 import com.intellij.facet.autodetecting.FacetDetector;
21 import com.intellij.facet.impl.autodetecting.FacetAutodetectingManager;
22 import com.intellij.facet.impl.ui.FacetDetectionProcessor;
23 import com.intellij.ide.util.importProject.ModuleDescriptor;
24 import com.intellij.openapi.module.Module;
25 import com.intellij.openapi.roots.ModifiableRootModel;
26 import com.intellij.openapi.util.Pair;
27 import com.intellij.openapi.vfs.LocalFileSystem;
28 import com.intellij.openapi.vfs.VirtualFile;
29 import com.intellij.ui.CheckboxTreeBase;
30 import com.intellij.util.ui.tree.TreeUtil;
31 import org.jetbrains.annotations.Nullable;
33 import javax.swing.*;
34 import java.awt.*;
35 import java.io.File;
36 import java.util.*;
37 import java.util.List;
39 /**
40 * @author nik
42 public class DetectedFacetsTreeComponent {
43 private final JPanel myMainPanel;
44 private final List<ModuleDescriptorNode> myModuleNodes = new ArrayList<ModuleDescriptorNode>();
46 public DetectedFacetsTreeComponent() {
47 myMainPanel = new JPanel(new BorderLayout());
50 public JPanel getMainPanel() {
51 return myMainPanel;
54 public void addFacets(ModuleDescriptor moduleDescriptor, Map<File, List<FacetDetectionProcessor.DetectedInWizardFacetInfo>> root2Facets) {
55 for (File root : root2Facets.keySet()) {
56 ModuleDescriptorNode moduleNode = new ModuleDescriptorNode(moduleDescriptor, root);
58 Map<FacetInfo, DetectedFacetsTree.FacetNode> facetInfos = new HashMap<FacetInfo, DetectedFacetsTree.FacetNode>();
59 for (FacetDetectionProcessor.DetectedInWizardFacetInfo detectedFacetInfo : root2Facets.get(root)) {
60 DetectedFacetsTree.FacetNode parent = null;
61 FacetInfo underlyingFacet = detectedFacetInfo.getFacetInfo().getUnderlyingFacet();
62 if (underlyingFacet != null) {
63 parent = facetInfos.get(underlyingFacet);
66 VirtualFile virtualRoot = LocalFileSystem.getInstance().findFileByIoFile(root);
67 DetectedFacetsTree.FacetNode detectedFacet = new FacetInfoNode(detectedFacetInfo, virtualRoot, parent);
68 facetInfos.put(detectedFacetInfo.getFacetInfo(), detectedFacet);
69 if (parent == null) {
70 moduleNode.addRootFacet(detectedFacet);
74 myModuleNodes.add(moduleNode);
78 public void createTree() {
79 final CheckboxTreeBase tree = new DetectedFacetsTree(myModuleNodes);
80 TreeUtil.expandAll(tree);
81 myMainPanel.add(tree, BorderLayout.CENTER);
84 public void clear() {
85 myMainPanel.removeAll();
86 myModuleNodes.clear();
89 public void createFacets(final ModuleDescriptor descriptor, final Module module, final ModifiableRootModel rootModel) {
90 List<Pair<FacetDetector, Facet>> createdFacets = new ArrayList<Pair<FacetDetector, Facet>>();
91 ModifiableFacetModel modifiableModel = FacetManager.getInstance(module).createModifiableModel();
92 for (ModuleDescriptorNode moduleNode : myModuleNodes) {
93 if (moduleNode.myModuleDescriptor.equals(descriptor)) {
94 processFacetsInfos(moduleNode.getRootFacets(), module, rootModel, modifiableModel, null, createdFacets, moduleNode.isChecked());
97 modifiableModel.commit();
98 for (Pair<FacetDetector, Facet> createdFacet : createdFacets) {
99 createdFacet.getFirst().afterFacetAdded(createdFacet.getSecond());
103 private static void processFacetsInfos(final List<DetectedFacetsTree.FacetNode> facets, final Module module, final ModifiableRootModel rootModel,
104 final ModifiableFacetModel facetModel, Facet underlyingFacet,
105 final List<Pair<FacetDetector, Facet>> createdFacets, boolean createFacets) {
106 for (DetectedFacetsTree.FacetNode facetNode : facets) {
107 boolean createFacet = createFacets && facetNode.isChecked();
108 FacetDetectionProcessor.DetectedInWizardFacetInfo detectedFacetInfo = ((FacetInfoNode)facetNode).getDetectedFacetInfo();
109 FacetInfo facetInfo = detectedFacetInfo.getFacetInfo();
110 FacetType type = facetInfo.getFacetType();
111 Facet facet = null;
113 if (createFacet) {
114 //noinspection unchecked
115 facet = FacetManager.getInstance(module).createFacet(type, facetInfo.getName(), facetInfo.getConfiguration(), underlyingFacet);
116 FacetDetector facetDetector = detectedFacetInfo.getFacetDetector();
117 facetDetector.beforeFacetAdded(facet, facetModel, rootModel);
119 facetModel.addFacet(facet);
120 createdFacets.add(Pair.create(facetDetector, facet));
122 else {
123 VirtualFile[] files = facetNode.getFiles();
124 String[] urls = new String[files.length];
125 for (int i = 0; i < files.length; i++) {
126 urls[i] = files[i].getUrl();
128 FacetAutodetectingManager.getInstance(module.getProject()).disableAutodetectionInFiles(type, module, urls);
131 processFacetsInfos(facetNode.getChildren(), module, rootModel, facetModel, facet, createdFacets, createFacet);
135 private static class FacetInfoNode extends DetectedFacetsTree.FacetNode {
136 private final FacetDetectionProcessor.DetectedInWizardFacetInfo myDetectedFacetInfo;
138 private FacetInfoNode(final FacetDetectionProcessor.DetectedInWizardFacetInfo detectedFacetInfo, final VirtualFile root, @Nullable final DetectedFacetsTree.FacetNode parent) {
139 super(detectedFacetInfo.getFacetInfo(), detectedFacetInfo.getFacetInfo().getFacetType(), root, new VirtualFile[]{detectedFacetInfo.getFile()}, parent);
140 myDetectedFacetInfo = detectedFacetInfo;
143 public FacetDetectionProcessor.DetectedInWizardFacetInfo getDetectedFacetInfo() {
144 return myDetectedFacetInfo;
148 private static class ModuleDescriptorNode extends DetectedFacetsTree.ModuleNode {
149 private final ModuleDescriptor myModuleDescriptor;
150 private final File myRoot;
152 private ModuleDescriptorNode(ModuleDescriptor moduleDescriptor, final File root) {
153 super(moduleDescriptor);
154 myModuleDescriptor = moduleDescriptor;
155 myRoot = root;
158 public String getModuleName() {
159 return myModuleDescriptor.getName();
162 public String getModuleDescription() {
163 return " (" + myRoot.getPath() + ")";