From de1823dce2a7350708544ec087056c0884e8f79a Mon Sep 17 00:00:00 2001 From: Eugene Zhuravlev Date: Thu, 10 Dec 2009 18:08:01 +0300 Subject: [PATCH] root model validations --- .../ui/configuration/ModulesConfigurator.java | 59 +++++++++++++++++++++- .../src/messages/ProjectBundle.properties | 3 ++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ModulesConfigurator.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ModulesConfigurator.java index b79a068cef..093f1d8920 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ModulesConfigurator.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ModulesConfigurator.java @@ -34,6 +34,7 @@ import com.intellij.openapi.options.ShowSettingsUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectBundle; import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.openapi.roots.ContentEntry; import com.intellij.openapi.roots.LibraryOrderEntry; import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.roots.ModuleRootModel; @@ -47,6 +48,8 @@ import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Disposer; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.packaging.artifacts.Artifact; import com.intellij.packaging.artifacts.ModifiableArtifactModel; import com.intellij.projectImport.ProjectImportBuilder; @@ -225,6 +228,60 @@ public class ModulesConfigurator implements ModulesProvider, ModuleEditor.Change } public void apply() throws ConfigurationException { + // validate content and source roots + final Map contentRootToModuleNameMap = new com.intellij.util.containers.HashMap(); + final Map srcRootsToContentRootMap = new com.intellij.util.containers.HashMap(); + for (final ModuleEditor moduleEditor : myModuleEditors) { + final ModifiableRootModel rootModel = moduleEditor.getModifiableRootModel(); + final ContentEntry[] contents = rootModel.getContentEntries(); + for (ContentEntry contentEntry : contents) { + final VirtualFile contentRoot = contentEntry.getFile(); + if (contentRoot == null) { + continue; + } + final String moduleName = moduleEditor.getName(); + final String previousName = contentRootToModuleNameMap.put(contentRoot, moduleName); + if (previousName != null && !previousName.equals(moduleName)) { + throw new ConfigurationException( + ProjectBundle.message("module.paths.validation.duplicate.content.error", contentRoot.getPresentableUrl(), previousName, moduleName) + ); + } + for (VirtualFile srcRoot : contentEntry.getSourceFolderFiles()) { + final VirtualFile anotherContentRoot = srcRootsToContentRootMap.put(srcRoot, contentRoot); + if (anotherContentRoot != null) { + final String problematicModule; + final String correctModule; + if (VfsUtil.isAncestor(anotherContentRoot, contentRoot, true)) { + problematicModule = contentRootToModuleNameMap.get(anotherContentRoot); + correctModule = contentRootToModuleNameMap.get(contentRoot); + } + else { + problematicModule = contentRootToModuleNameMap.get(contentRoot); + correctModule = contentRootToModuleNameMap.get(anotherContentRoot); + } + throw new ConfigurationException( + ProjectBundle.message("module.paths.validation.duplicate.source.root.error", problematicModule, srcRoot.getPresentableUrl(), correctModule) + ); + } + } + } + } + // additional validation: directories marked as src roots must belong to the same module as their corresponding content root + for (Map.Entry entry : srcRootsToContentRootMap.entrySet()) { + final VirtualFile srcRoot = entry.getKey(); + final VirtualFile correspondingContent = entry.getValue(); + final String expectedModuleName = contentRootToModuleNameMap.get(correspondingContent); + + for (VirtualFile candidateContent = srcRoot; !candidateContent.equals(correspondingContent); candidateContent = candidateContent.getParent()) { + final String moduleName = contentRootToModuleNameMap.get(candidateContent); + if (moduleName != null && !moduleName.equals(expectedModuleName)) { + throw new ConfigurationException( + ProjectBundle.message("module.paths.validation.source.root.belongs.to.another.module.error", srcRoot.getPresentableUrl(), expectedModuleName, moduleName) + ); + } + } + } + final ProjectRootManagerImpl projectRootManager = ProjectRootManagerImpl.getInstanceImpl(myProject); final ConfigurationException[] ex = new ConfigurationException[1]; @@ -249,7 +306,7 @@ public class ModulesConfigurator implements ModulesProvider, ModuleEditor.Change catch (ConfigurationException e) { ex[0] = e; } - finally { + finally { ModuleStructureConfigurable.getInstance(myProject).getFacetEditorFacade().clearMaps(false); for (final ModuleEditor moduleEditor : myModuleEditors) { diff --git a/platform/platform-resources-en/src/messages/ProjectBundle.properties b/platform/platform-resources-en/src/messages/ProjectBundle.properties index d025714d5d..6e9ff3b218 100644 --- a/platform/platform-resources-en/src/messages/ProjectBundle.properties +++ b/platform/platform-resources-en/src/messages/ProjectBundle.properties @@ -122,6 +122,9 @@ module.paths.add.content.already.exists.error=Content root \"{0}\" already exist module.paths.add.content.intersect.error=Content root being added \"{0}\"\nis located below existing content root \"{1}\".\nContent entries should not intersect. module.paths.add.content.dominate.error=Content root being added \"{0}\"\ndominates existing content root \"{1}\".\nContent entries should not intersect. module.paths.add.content.duplicate.error=Content root \"{0}\" already defined for module \"{1}\".\nTwo modules in a project cannot share the same content root. +module.paths.validation.duplicate.content.error=Content root \"{0}\" is defined for modules \"{1}\" and \"{2}\".\nTwo modules in a project cannot share the same content root. +module.paths.validation.duplicate.source.root.error=Module \"{0}\"\nmust not contain source root \"{1}\".\nThe root already belongs to module \"{2}\" +module.paths.validation.source.root.belongs.to.another.module.error=Source root \"{0}\"\ncannot be defined in module \"{1}\" because it belongs to content of nested module \"{2}\" module.paths.remove.content.prompt=Remove content root \"{0}\"? module.paths.remove.content.title=Remove Content Root module.paths.empty.node= -- 2.11.4.GIT