Search in sources :

Example 1 with ModuleWithNameAlreadyExists

use of com.intellij.openapi.module.ModuleWithNameAlreadyExists in project intellij-plugins by StepicOrg.

the class StepikProjectManager method refreshCourse.

private void refreshCourse() {
    if (project == null || root == null) {
        return;
    }
    root.setProject(project);
    executor.execute(() -> {
        StepikApiClient stepikApiClient = authAndGetStepikApiClient();
        if (isAuthenticated()) {
            root.reloadData(project, stepikApiClient);
        }
        ProgressManager.getInstance().run(new Task.Backgroundable(project, "Synchronize Project") {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                if (project.isDisposed()) {
                    return;
                }
                repairProjectFiles(root);
                repairSandbox();
                ApplicationManager.getApplication().invokeLater(() -> {
                    VirtualFileManager.getInstance().syncRefresh();
                    setSelected(selected, false);
                });
            }

            private void repairSandbox() {
                VirtualFile projectDir = project.getBaseDir();
                if (projectDir != null && projectDir.findChild(EduNames.SANDBOX_DIR) == null) {
                    Application application = ApplicationManager.getApplication();
                    ModifiableModuleModel model = application.runReadAction((Computable<ModifiableModuleModel>) () -> ModuleManager.getInstance(project).getModifiableModel());
                    application.invokeLater(() -> application.runWriteAction(() -> {
                        try {
                            new SandboxModuleBuilder(projectDir.getPath()).createModule(model);
                            model.commit();
                        } catch (IOException | ConfigurationException | JDOMException | ModuleWithNameAlreadyExists e) {
                            logger.warn("Failed repair Sandbox", e);
                        }
                    }));
                }
            }
        });
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) Task(com.intellij.openapi.progress.Task) SandboxModuleBuilder(org.stepik.plugin.projectWizard.idea.SandboxModuleBuilder) ModuleWithNameAlreadyExists(com.intellij.openapi.module.ModuleWithNameAlreadyExists) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) ConfigurationException(com.intellij.openapi.options.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Application(com.intellij.openapi.application.Application) Computable(com.intellij.openapi.util.Computable)

Example 2 with ModuleWithNameAlreadyExists

use of com.intellij.openapi.module.ModuleWithNameAlreadyExists in project intellij-community by JetBrains.

the class RenameProjectHandler method renameProject.

public static boolean renameProject(@NotNull ProjectEx project, @Nullable Module module, String newName) {
    if (shouldRenameProject(project, module, newName)) {
        project.setProjectName(newName);
        project.save();
    }
    if (module != null && !newName.equals(module.getName())) {
        final ModifiableModuleModel modifiableModel = ModuleManager.getInstance(project).getModifiableModel();
        try {
            modifiableModel.renameModule(module, newName);
        } catch (ModuleWithNameAlreadyExists moduleWithNameAlreadyExists) {
            Messages.showErrorDialog(project, IdeBundle.message("error.module.already.exists", newName), IdeBundle.message("title.rename.module"));
            return false;
        }
        final Ref<Boolean> success = Ref.create(Boolean.TRUE);
        CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> modifiableModel.commit()), IdeBundle.message("command.renaming.module", module.getName()), null);
        return success.get().booleanValue();
    }
    return true;
}
Also used : ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) ModuleWithNameAlreadyExists(com.intellij.openapi.module.ModuleWithNameAlreadyExists)

Example 3 with ModuleWithNameAlreadyExists

use of com.intellij.openapi.module.ModuleWithNameAlreadyExists in project intellij-community by JetBrains.

the class AbstractIdeModifiableModelsProvider method newModule.

@NotNull
@Override
public Module newModule(@NotNull final String filePath, final String moduleTypeId) {
    Module module = getModifiableModuleModel().newModule(filePath, moduleTypeId);
    final String moduleName = FileUtil.getNameWithoutExtension(new File(filePath));
    if (!module.getName().equals(moduleName)) {
        try {
            getModifiableModuleModel().renameModule(module, moduleName);
        } catch (ModuleWithNameAlreadyExists exists) {
            LOG.warn(exists);
        }
    }
    // set module type id explicitly otherwise it can not be set if there is an existing module (with the same filePath) and w/o 'type' attribute
    module.setOption(Module.ELEMENT_TYPE, moduleTypeId);
    return module;
}
Also used : ModuleWithNameAlreadyExists(com.intellij.openapi.module.ModuleWithNameAlreadyExists) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ModuleWithNameAlreadyExists

use of com.intellij.openapi.module.ModuleWithNameAlreadyExists in project android by JetBrains.

the class ImportModuleTask method perform.

@Override
public Exception perform() {
    final Module[] moduleWrapper = { null };
    final Exception exception = ApplicationManager.getApplication().runWriteAction(new Computable<Exception>() {

        @Override
        public Exception compute() {
            try {
                moduleWrapper[0] = ModuleManager.getInstance(myProject).loadModule(myModuleFilePath);
            } catch (InvalidDataException e) {
                return e;
            } catch (IOException e) {
                return e;
            } catch (JDOMException e) {
                return e;
            } catch (ModuleWithNameAlreadyExists e) {
                return e;
            }
            return null;
        }
    });
    if (exception != null) {
        return exception;
    }
    assert moduleWrapper[0] != null;
    if (AndroidFacet.getInstance(moduleWrapper[0]) == null) {
        AndroidUtils.addAndroidFacetInWriteAction(moduleWrapper[0], myContentRoot, true);
    }
    AndroidSdkUtils.setupAndroidPlatformIfNecessary(moduleWrapper[0], false);
    setDepModule(moduleWrapper[0]);
    return null;
}
Also used : ModuleWithNameAlreadyExists(com.intellij.openapi.module.ModuleWithNameAlreadyExists) InvalidDataException(com.intellij.openapi.util.InvalidDataException) IOException(java.io.IOException) Module(com.intellij.openapi.module.Module) JDOMException(org.jdom.JDOMException) IOException(java.io.IOException) InvalidDataException(com.intellij.openapi.util.InvalidDataException) JDOMException(org.jdom.JDOMException)

Example 5 with ModuleWithNameAlreadyExists

use of com.intellij.openapi.module.ModuleWithNameAlreadyExists in project intellij-plugins by StepicOrg.

the class ModuleUtils method createStepModule.

static void createStepModule(@NotNull Project project, @NotNull StepNode step, @NotNull ModifiableModuleModel moduleModel) {
    StudyNode lesson = step.getParent();
    if (lesson != null) {
        String moduleDir = String.join("/", project.getBasePath(), lesson.getPath());
        StepModuleBuilder stepModuleBuilder = new StepModuleBuilder(moduleDir, step);
        try {
            stepModuleBuilder.createModule(moduleModel);
        } catch (IOException | ModuleWithNameAlreadyExists | JDOMException | ConfigurationException e) {
            logger.warn("Cannot create step: " + step.getDirectory(), e);
        }
    }
}
Also used : ConfigurationException(com.intellij.openapi.options.ConfigurationException) ModuleWithNameAlreadyExists(com.intellij.openapi.module.ModuleWithNameAlreadyExists) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) StudyNode(org.stepik.core.courseFormat.StudyNode) StepModuleBuilder(org.stepik.plugin.projectWizard.idea.StepModuleBuilder)

Aggregations

ModuleWithNameAlreadyExists (com.intellij.openapi.module.ModuleWithNameAlreadyExists)5 IOException (java.io.IOException)3 JDOMException (org.jdom.JDOMException)3 ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)2 Module (com.intellij.openapi.module.Module)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 Application (com.intellij.openapi.application.Application)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 Computable (com.intellij.openapi.util.Computable)1 InvalidDataException (com.intellij.openapi.util.InvalidDataException)1 File (java.io.File)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 NotNull (org.jetbrains.annotations.NotNull)1 StepikApiClient (org.stepik.api.client.StepikApiClient)1 StudyNode (org.stepik.core.courseFormat.StudyNode)1 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)1 SandboxModuleBuilder (org.stepik.plugin.projectWizard.idea.SandboxModuleBuilder)1 StepModuleBuilder (org.stepik.plugin.projectWizard.idea.StepModuleBuilder)1