Search in sources :

Example 1 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.

the class MavenIdeaPluginConfigurer method configureJdk.

private static void configureJdk(Element cfg, @NotNull Module module) {
    String jdkName = cfg.getChildTextTrim("jdkName");
    if (StringUtil.isEmptyOrSpaces(jdkName))
        return;
    ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
    String currentSdkName = null;
    Sdk sdk = rootManager.getSdk();
    if (sdk != null) {
        currentSdkName = sdk.getName();
    }
    if (!jdkName.equals(currentSdkName)) {
        ModifiableRootModel model = rootManager.getModifiableModel();
        if (jdkName.equals(ProjectRootManager.getInstance(model.getProject()).getProjectSdkName())) {
            model.inheritSdk();
        } else {
            Sdk jdk = ProjectJdkTable.getInstance().findJdk(jdkName);
            if (jdk != null) {
                model.setSdk(jdk);
            } else {
                model.setInvalidSdk(jdkName, JavaSdk.getInstance().getName());
            }
        }
        model.commit();
    }
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 2 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.

the class GroovyFacetUtil method tryToSetUpGroovyFacetOnTheFly.

public static boolean tryToSetUpGroovyFacetOnTheFly(final Module module) {
    final Project project = module.getProject();
    GroovyConfigUtils utils = GroovyConfigUtils.getInstance();
    final Library[] libraries = utils.getAllSDKLibraries(project);
    if (libraries.length > 0) {
        final Library library = libraries[0];
        int result = Messages.showOkCancelDialog(GroovyBundle.message("groovy.like.library.found.text", module.getName(), library.getName(), utils.getSDKLibVersion(library)), GroovyBundle.message("groovy.like.library.found"), JetgroovyIcons.Groovy.Groovy_32x32);
        if (result == Messages.OK) {
            WriteAction.run(() -> {
                ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
                LibraryOrderEntry entry = model.addLibraryEntry(libraries[0]);
                LibrariesUtil.placeEntryToCorrectPlace(model, entry);
                model.commit();
            });
            return true;
        }
    }
    return false;
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) Project(com.intellij.openapi.project.Project) Library(com.intellij.openapi.roots.libraries.Library) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry)

Example 3 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.

the class UsageInModuleClasspath method replaceElement.

@Override
public void replaceElement(final ProjectStructureElement newElement) {
    final ModuleEditor editor = myContext.getModulesConfigurator().getModuleEditor(myModule);
    if (editor != null) {
        final ModifiableRootModel rootModel = editor.getModifiableRootModelProxy();
        OrderEntryUtil.replaceLibrary(rootModel, ((LibraryProjectStructureElement) mySourceElement).getLibrary(), ((LibraryProjectStructureElement) newElement).getLibrary());
        myContext.getDaemonAnalyzer().queueUpdate(new ModuleProjectStructureElement(myContext, myModule));
    }
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ModuleEditor(com.intellij.openapi.roots.ui.configuration.ModuleEditor)

Example 4 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.

the class EclipseEmlTest method doLoadModule.

private static Module doLoadModule(@NotNull String path, @NotNull Project project) throws IOException, JDOMException, InvalidDataException {
    Module module = WriteAction.compute(() -> ModuleManager.getInstance(project).newModule(path + '/' + EclipseProjectFinder.findProjectName(path) + ModuleManagerImpl.IML_EXTENSION, StdModuleTypes.JAVA.getId()));
    replaceRoot(path, EclipseXml.DOT_CLASSPATH_EXT, project);
    ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
    new EclipseClasspathConverter(module).readClasspath(rootModel);
    WriteAction.run(() -> rootModel.commit());
    return module;
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) EclipseClasspathConverter(org.jetbrains.idea.eclipse.config.EclipseClasspathConverter) Module(com.intellij.openapi.module.Module)

Example 5 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.

the class EclipseImlTest method doTest.

protected static void doTest(final String relativePath, final Project project) throws Exception {
    final String path = project.getBaseDir().getPath() + relativePath;
    final File classpathFile = new File(path, EclipseXml.DOT_CLASSPATH_EXT);
    String fileText = FileUtil.loadFile(classpathFile).replaceAll("\\$ROOT\\$", project.getBaseDir().getPath());
    if (!SystemInfo.isWindows) {
        fileText = fileText.replaceAll(EclipseXml.FILE_PROTOCOL + "/", EclipseXml.FILE_PROTOCOL);
    }
    String communityLib = FileUtil.toSystemIndependentName(PathManagerEx.findFileUnderCommunityHome("lib").getAbsolutePath());
    fileText = fileText.replaceAll("\\$" + JUNIT + "\\$", communityLib);
    final Element classpathElement = JdomKt.loadElement(fileText);
    final Module module = WriteCommandAction.runWriteCommandAction(null, (Computable<Module>) () -> ModuleManager.getInstance(project).newModule(new File(path) + File.separator + EclipseProjectFinder.findProjectName(path) + ModuleManagerImpl.IML_EXTENSION, StdModuleTypes.JAVA.getId()));
    final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
    EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, project, null);
    classpathReader.init(rootModel);
    classpathReader.readClasspath(rootModel, classpathElement);
    ApplicationManager.getApplication().runWriteAction(rootModel::commit);
    final Element actualImlElement = new Element("root");
    ((ModuleRootManagerImpl) ModuleRootManager.getInstance(module)).getState().writeExternal(actualImlElement);
    PathMacros.getInstance().setMacro(JUNIT, communityLib);
    PathMacroManager.getInstance(module).collapsePaths(actualImlElement);
    PathMacroManager.getInstance(project).collapsePaths(actualImlElement);
    PathMacros.getInstance().removeMacro(JUNIT);
    assertThat(actualImlElement).isEqualTo(Paths.get(project.getBaseDir().getPath(), "expected", "expected.iml"));
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) Element(org.jdom.Element) EclipseClasspathReader(org.jetbrains.idea.eclipse.conversion.EclipseClasspathReader) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)122 Module (com.intellij.openapi.module.Module)52 VirtualFile (com.intellij.openapi.vfs.VirtualFile)36 ContentEntry (com.intellij.openapi.roots.ContentEntry)30 File (java.io.File)23 Library (com.intellij.openapi.roots.libraries.Library)20 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)19 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)11 Sdk (com.intellij.openapi.projectRoots.Sdk)9 ModuleManager (com.intellij.openapi.module.ModuleManager)8 OrderEntry (com.intellij.openapi.roots.OrderEntry)8 IOException (java.io.IOException)8 NotNull (org.jetbrains.annotations.NotNull)8 Nullable (org.jetbrains.annotations.Nullable)8 Project (com.intellij.openapi.project.Project)7 ConfigurationException (com.intellij.openapi.options.ConfigurationException)6 ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)5 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)5 THashMap (gnu.trove.THashMap)5 FlexProjectConfigurationEditor (com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor)4