update copyright
[fedora-idea.git] / java / idea-ui / src / com / intellij / facet / impl / autodetecting / EnableAutodetectionWorker.java
blobcf6117ba10a38530cb71a680212d1de6b384a790
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.
16 package com.intellij.facet.impl.autodetecting;
18 import com.intellij.facet.FacetType;
19 import com.intellij.openapi.module.Module;
20 import com.intellij.openapi.module.ModuleManager;
21 import com.intellij.openapi.progress.ProgressManager;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.project.ProjectBundle;
24 import com.intellij.openapi.roots.ModuleRootManager;
25 import com.intellij.openapi.roots.ContentIterator;
26 import com.intellij.openapi.util.MultiValuesMap;
27 import com.intellij.openapi.vfs.VirtualFile;
28 import com.intellij.openapi.vfs.VirtualFileManager;
29 import gnu.trove.THashSet;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 import org.jetbrains.annotations.TestOnly;
34 import java.util.*;
36 /**
37 * @author nik
39 public class EnableAutodetectionWorker {
40 private final MultiValuesMap<FacetType<?,?>, Module> myModulesToProcess = new MultiValuesMap<FacetType<?,?>, Module>();
41 private final MultiValuesMap<FacetType<?,?>, VirtualFile> myFilesToProcess = new MultiValuesMap<FacetType<?,?>, VirtualFile>();
42 private final Project myProject;
43 private final FacetAutodetectingManagerImpl myFacetAutodetectingManager;
45 public EnableAutodetectionWorker(@NotNull Project project, final FacetAutodetectingManagerImpl facetAutodetectingManager) {
46 myProject = project;
47 myFacetAutodetectingManager = facetAutodetectingManager;
50 public void addFile(@NotNull FacetType<?, ?> type, @NotNull String url) {
51 VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(url);
52 if (file != null) {
53 myFilesToProcess.put(type, file);
57 public void queueChanges(final @NotNull FacetType<?, ?> facetType, final @Nullable DisabledAutodetectionByTypeElement oldElement,
58 final @Nullable DisabledAutodetectionByTypeElement newElement) {
59 if (oldElement == null || newElement != null && newElement.getModuleElements().isEmpty()) return;
61 ModuleManager moduleManager = ModuleManager.getInstance(myProject);
62 List<Module> modulesToProcess = new ArrayList<Module>();
64 List<DisabledAutodetectionInModuleElement> moduleElements = oldElement.getModuleElements();
65 for (DisabledAutodetectionInModuleElement moduleElement : moduleElements) {
66 if (moduleElement.isDisableInWholeModule()) {
67 Module module = moduleManager.findModuleByName(moduleElement.getModuleName());
68 if (module != null) {
69 modulesToProcess.add(module);
72 else {
73 for (String url : moduleElement.getFiles()) {
74 if (newElement == null || !newElement.isDisabled(moduleElement.getModuleName(), url)) {
75 addFile(facetType, url);
78 for (String directoryUrl : moduleElement.getDirectories()) {
79 if (newElement == null || !newElement.isDisabled(moduleElement.getModuleName(), directoryUrl)) {
80 addFile(facetType, directoryUrl);
86 if (moduleElements.isEmpty()) {
87 modulesToProcess.addAll(Arrays.asList(moduleManager.getModules()));
89 if (newElement != null) {
90 Set<String> toRemove = new THashSet<String>();
91 for (DisabledAutodetectionInModuleElement moduleElement : newElement.getModuleElements()) {
92 if (moduleElement.isDisableInWholeModule()) {
93 toRemove.add(moduleElement.getModuleName());
97 Iterator<Module> iterator = modulesToProcess.iterator();
98 while (iterator.hasNext()) {
99 Module module = iterator.next();
100 if (toRemove.contains(module.getName())) {
101 iterator.remove();
106 if (!modulesToProcess.isEmpty()) {
107 myModulesToProcess.putAll(facetType, modulesToProcess);
111 public void redetectFacets() {
112 ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
113 public void run() {
114 for (FacetType<?, ?> type : myModulesToProcess.keySet()) {
115 detectFacetsInModules(type, myModulesToProcess.get(type));
117 myModulesToProcess.clear();
119 for (FacetType<?, ?> type : myFilesToProcess.keySet()) {
120 detectFacetsInFiles(type, myFilesToProcess.get(type));
122 myFilesToProcess.clear();
125 }, ProjectBundle.message("progress.text.detecting.facets"), false, myProject);
126 myFacetAutodetectingManager.getDetectedFacetManager().showDetectedFacetsDialog();
129 private void detectFacetsInModules(final FacetType<?, ?> type, final Collection<Module> modules) {
130 for (Module module : modules) {
131 ModuleRootManager.getInstance(module).getFileIndex().iterateContent(new ContentIterator() {
132 public boolean processFile(final VirtualFile file) {
133 detectFacetsInFile(type, file);
134 return true;
140 private void detectFacetsInFile(final FacetType<?, ?> type, final VirtualFile file) {
141 //todo[nik] detect only facets of specified type
142 if (file.isDirectory()) {
143 for (VirtualFile child : file.getChildren()) {
144 detectFacetsInFile(type, child);
147 else {
148 myFacetAutodetectingManager.processFile(file);
152 private void detectFacetsInFiles(final FacetType<?, ?> type, final Collection<VirtualFile> virtualFiles) {
153 for (VirtualFile file : virtualFiles) {
154 detectFacetsInFile(type, file);
158 @TestOnly
159 @Nullable
160 public Collection<Module> getModulesToProcess(FacetType<?, ?> facetType) {
161 return myModulesToProcess.get(facetType);
164 @TestOnly
165 @Nullable
166 public Collection<VirtualFile> getFilesToProcess(FacetType<?, ?> facetType) {
167 return myFilesToProcess.get(facetType);