public static boolean showFacetSettingsDialog(
     @NotNull final Facet facet, @Nullable final String tabNameToSelect) {
   final Project project = facet.getModule().getProject();
   final ProjectStructureConfigurable config = ProjectStructureConfigurable.getInstance(project);
   return ShowSettingsUtil.getInstance()
       .editConfigurable(
           project,
           config,
           new Runnable() {
             @Override
             public void run() {
               final ModuleStructureConfigurable modulesConfig = config.getModulesConfig();
               config
                   .select(facet, true)
                   .doWhenDone(
                       new Runnable() {
                         @Override
                         public void run() {
                           if (tabNameToSelect != null) {
                             FacetEditorImpl facetEditor =
                                 modulesConfig.getFacetConfigurator().getOrCreateEditor(facet);
                             facetEditor.setSelectedTabName(tabNameToSelect);
                           }
                         }
                       });
             }
           });
 }
 @Nullable
 public List<Module> addModule(Component parent, boolean anImport) {
   if (myProject.isDefault()) return null;
   final ProjectBuilder builder = runModuleWizard(parent, anImport);
   if (builder != null) {
     final List<Module> modules = new ArrayList<Module>();
     final List<Module> commitedModules;
     if (builder instanceof ProjectImportBuilder<?>) {
       final ModifiableArtifactModel artifactModel =
           ProjectStructureConfigurable.getInstance(myProject)
               .getArtifactsStructureConfigurable()
               .getModifiableArtifactModel();
       commitedModules =
           ((ProjectImportBuilder<?>) builder)
               .commit(myProject, myModuleModel, this, artifactModel);
     } else {
       commitedModules = builder.commit(myProject, myModuleModel, this);
     }
     if (commitedModules != null) {
       modules.addAll(commitedModules);
     }
     ApplicationManager.getApplication()
         .runWriteAction(
             new Runnable() {
               @Override
               public void run() {
                 for (Module module : modules) {
                   getOrCreateModuleEditor(module);
                 }
               }
             });
     return modules;
   }
   return null;
 }
 public void moduleRenamed(Module module, final String oldName, final String name) {
   ModuleEditor moduleEditor = myModuleEditors.get(module);
   if (moduleEditor != null) {
     moduleEditor.setModuleName(name);
     moduleEditor.updateCompilerOutputPathChanged(
         ProjectStructureConfigurable.getInstance(myProject)
             .getProjectConfig()
             .getCompilerOutputUrl(),
         name);
     myContext
         .getDaemonAnalyzer()
         .queueUpdate(new ModuleProjectStructureElement(myContext, module));
   }
 }
 public static boolean showArtifactSettings(
     @NotNull Project project, @Nullable final Artifact artifact) {
   final ProjectStructureConfigurable configurable =
       ProjectStructureConfigurable.getInstance(project);
   return ShowSettingsUtil.getInstance()
       .editConfigurable(
           project,
           configurable,
           new Runnable() {
             @Override
             public void run() {
               configurable.select(artifact, true);
             }
           });
 }
 public static boolean showDialog(
     Project project,
     @Nullable final String moduleToSelect,
     @Nullable final String editorNameToSelect) {
   final ProjectStructureConfigurable config = ProjectStructureConfigurable.getInstance(project);
   return ShowSettingsUtil.getInstance()
       .editConfigurable(
           project,
           config,
           new Runnable() {
             @Override
             public void run() {
               config.select(moduleToSelect, editorNameToSelect, 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());
    }

    final Ref<ConfigurationException> exceptionRef = Ref.create();
    DumbService.getInstance(myProject)
        .allowStartingDumbModeInside(
            DumbModePermission.MAY_START_BACKGROUND,
            new Runnable() {
              public void run() {
                ApplicationManager.getApplication()
                    .runWriteAction(
                        new Runnable() {
                          @Override
                          public void run() {
                            try {
                              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();
                            } catch (ConfigurationException e) {
                              exceptionRef.set(e);
                              return;
                            }

                            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;
                            }
                          }
                        });
              }
            });

    if (!exceptionRef.isNull()) {
      throw exceptionRef.get();
    }

    myModified = false;
  }