update copyright
[fedora-idea.git] / java / idea-ui / src / com / intellij / ide / util / importProject / ModuleDescriptor.java
blob2f1285068a1ac249e4337cbc03b812844906e93f
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.ide.util.importProject;
18 import com.intellij.openapi.util.text.StringUtil;
19 import com.intellij.openapi.util.InvalidDataException;
20 import com.intellij.ide.highlighter.ModuleFileType;
21 import org.jetbrains.annotations.NonNls;
22 import org.jetbrains.annotations.NotNull;
24 import java.io.File;
25 import java.util.*;
27 /**
28 * @author Eugene Zhuravlev
29 * Date: Jul 13, 2007
31 public class ModuleDescriptor {
32 private String myName;
33 private final Map<File, Set<File>> myContentToSourceRoots = new HashMap<File, Set<File>>();
34 private final Set<File> myLibraryFiles = new HashSet<File>();
35 private final Set<ModuleDescriptor> myDependencies = new HashSet<ModuleDescriptor>();
36 private static final String[] ourModuleNameStoplist = new String[] {
37 "java", "src", "source", "sources", "C:", "D:", "E:", "F:", "temp", "tmp"
40 public ModuleDescriptor(final File contentRoot, final Set<File> sourceRoots) {
41 myName = suggestModuleName(contentRoot);
42 myContentToSourceRoots.put(contentRoot, sourceRoots);
45 private static String suggestModuleName(final File contentRoot) {
46 for (File dir = contentRoot; dir != null; dir = dir.getParentFile()) {
47 final String suggestion = dir.getName();
48 boolean belongsToStopList = false;
49 for (String undesirableName : ourModuleNameStoplist) {
50 if (suggestion.equalsIgnoreCase(undesirableName)) {
51 belongsToStopList = true;
52 break;
55 if (!belongsToStopList) {
56 return StringUtil.capitalize(suggestion);
60 return StringUtil.capitalize(contentRoot.getName());
63 public String getName() {
64 return myName;
67 public void setName(final String name) {
68 myName = name;
71 public Set<File> getContentRoots() {
72 return Collections.unmodifiableSet(myContentToSourceRoots.keySet());
75 public Set<File> getSourceRoots() {
76 final Set<File> allSources = new HashSet<File>();
77 for (Set<File> files : myContentToSourceRoots.values()) {
78 allSources.addAll(files);
80 return allSources;
83 public Set<File> getSourceRoots(File contentRoot) {
84 final Set<File> sources = myContentToSourceRoots.get(contentRoot);
85 return (sources != null) ? Collections.unmodifiableSet(sources) : Collections.<File>emptySet();
88 public void addContentRoot(File contentRoot) {
89 myContentToSourceRoots.put(contentRoot, new HashSet<File>());
92 public Set<File> removeContentRoot(File contentRoot) {
93 return myContentToSourceRoots.remove(contentRoot);
96 public void addSourceRoot(final File contentRoot, File sourceRoot) {
97 Set<File> sources = myContentToSourceRoots.get(contentRoot);
98 if (sources == null) {
99 sources = new HashSet<File>();
100 myContentToSourceRoots.put(contentRoot, sources);
102 sources.add(sourceRoot);
105 public void addDependencyOn(ModuleDescriptor dependence) {
106 myDependencies.add(dependence);
109 public void removeDependencyOn(ModuleDescriptor module) {
110 myDependencies.remove(module);
113 public void addLibraryFile(File libFile) {
114 myLibraryFiles.add(libFile);
117 public Set<File> getLibraryFiles() {
118 return myLibraryFiles;
121 public Set<ModuleDescriptor> getDependencies() {
122 return Collections.unmodifiableSet(myDependencies);
126 * For debug purposes only
128 public String toString() {
129 @NonNls final StringBuilder builder = new StringBuilder();
130 builder.append("[Module: ").append(getContentRoots()).append(" | ");
131 for (File sourceRoot : getSourceRoots()) {
132 builder.append(sourceRoot.getName()).append(",");
134 builder.append("]");
135 return builder.toString();
138 public void clearModuleDependencies() {
139 myDependencies.clear();
142 public void clearLibraryFiles() {
143 myLibraryFiles.clear();
146 @NotNull
147 public String computeModuleFilePath() throws InvalidDataException {
148 final String name = getName();
149 final Set<File> contentRoots = getContentRoots();
150 if (contentRoots.size() > 0) {
151 return contentRoots.iterator().next().getPath() + File.separator + name + ModuleFileType.DOT_DEFAULT_EXTENSION;
153 else {
154 throw new InvalidDataException("Module " + name + " has no content roots and will not be created.");