private void setupInitialValues(boolean newlyCreatedModule) {
   if (newlyCreatedModule || myRootModel.getSdk() == null) {
     myRootModel.inheritSdk();
   }
   if (newlyCreatedModule) {
     getCompilerExtension().setExcludeOutput(true);
   }
 }
  public void apply() throws ConfigurationException {
    // validate content and source roots
    final Map<VirtualFile, String> contentRootToModuleNameMap = new HashMap<VirtualFile, String>();
    final Map<VirtualFile, VirtualFile> srcRootsToContentRootMap =
        new HashMap<VirtualFile, VirtualFile>();
    for (final ModuleEditor moduleEditor : myModuleEditors.values()) {
      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 (VfsUtilCore.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<VirtualFile, VirtualFile> entry : srcRootsToContentRootMap.entrySet()) {
      final VirtualFile srcRoot = entry.getKey();
      final VirtualFile correspondingContent = entry.getValue();
      final String expectedModuleName = contentRootToModuleNameMap.get(correspondingContent);

      for (VirtualFile candidateContent = srcRoot;
          candidateContent != null && !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 List<ModifiableRootModel> models =
        new ArrayList<ModifiableRootModel>(myModuleEditors.size());
    for (ModuleEditor moduleEditor : myModuleEditors.values()) {
      moduleEditor.canApply();
    }

    final Map<Sdk, Sdk> modifiedToOriginalMap = new HashMap<Sdk, Sdk>();
    final ProjectSdksModel projectJdksModel =
        ProjectStructureConfigurable.getInstance(myProject).getProjectJdksModel();
    for (Map.Entry<Sdk, Sdk> entry : projectJdksModel.getProjectSdks().entrySet()) {
      modifiedToOriginalMap.put(entry.getValue(), entry.getKey());
    }

    for (final ModuleEditor moduleEditor : myModuleEditors.values()) {
      final ModifiableRootModel model = moduleEditor.apply();
      if (model != null) {
        if (!model.isSdkInherited()) {
          // make sure the sdk is set to original SDK stored in the JDK Table
          final Sdk modelSdk = model.getSdk();
          if (modelSdk != null) {
            final Sdk original = modifiedToOriginalMap.get(modelSdk);
            if (original != null) {
              model.setSdk(original);
            }
          }
        }
        models.add(model);
      }
    }
    myFacetsConfigurator.applyEditors();

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                try {
                  final ModifiableRootModel[] rootModels =
                      models.toArray(new ModifiableRootModel[models.size()]);
                  ModifiableModelCommitter.multiCommit(rootModels, myModuleModel);
                  myModuleModelCommitted = true;
                  myFacetsConfigurator.commitFacets();

                } finally {
                  ModuleStructureConfigurable.getInstance(myProject)
                      .getFacetEditorFacade()
                      .clearMaps(false);

                  myFacetsConfigurator = createFacetsConfigurator();
                  myModuleModel = ModuleManager.getInstance(myProject).getModifiableModel();
                  myModuleModelCommitted = false;
                }
              }
            });

    myModified = false;
  }