Search in sources :

Example 1 with ProjectRootManagerEx

use of com.intellij.openapi.roots.ex.ProjectRootManagerEx in project intellij-elixir by KronicDeth.

the class MixProjectImportBuilder method fixProjectSdk.

@Nullable
private static Sdk fixProjectSdk(@NotNull Project project) {
    final ProjectRootManagerEx projectRootMgr = ProjectRootManagerEx.getInstanceEx(project);
    Sdk selectedSdk = projectRootMgr.getProjectSdk();
    if (selectedSdk == null || selectedSdk.getSdkType() != ElixirSdkType.getInstance()) {
        final Sdk moreSuitableSdk = ProjectJdkTable.getInstance().findMostRecentSdkOfType(ElixirSdkType.getInstance());
        ApplicationManager.getApplication().runWriteAction(new Runnable() {

            @Override
            public void run() {
                projectRootMgr.setProjectSdk(moreSuitableSdk);
            }
        });
        return moreSuitableSdk;
    }
    return selectedSdk;
}
Also used : ProjectRootManagerEx(com.intellij.openapi.roots.ex.ProjectRootManagerEx) Sdk(com.intellij.openapi.projectRoots.Sdk) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ProjectRootManagerEx

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

the class NewProjectUtil method applyJdkToProject.

public static void applyJdkToProject(@NotNull Project project, @NotNull Sdk jdk) {
    ProjectRootManagerEx rootManager = ProjectRootManagerEx.getInstanceEx(project);
    rootManager.setProjectSdk(jdk);
    JavaSdkVersion version = JavaSdk.getInstance().getVersion(jdk);
    if (version != null) {
        LanguageLevel maxLevel = version.getMaxLanguageLevel();
        LanguageLevelProjectExtension extension = LanguageLevelProjectExtension.getInstance(ProjectManager.getInstance().getDefaultProject());
        LanguageLevelProjectExtension ext = LanguageLevelProjectExtension.getInstance(project);
        if (extension.isDefault() || maxLevel.compareTo(ext.getLanguageLevel()) < 0) {
            ext.setLanguageLevel(maxLevel);
        }
    }
}
Also used : ProjectRootManagerEx(com.intellij.openapi.roots.ex.ProjectRootManagerEx) JavaSdkVersion(com.intellij.openapi.projectRoots.JavaSdkVersion) LanguageLevel(com.intellij.pom.java.LanguageLevel) LanguageLevelProjectExtension(com.intellij.openapi.roots.LanguageLevelProjectExtension)

Example 3 with ProjectRootManagerEx

use of com.intellij.openapi.roots.ex.ProjectRootManagerEx in project intellij-leiningen-plugin by derkork.

the class ModuleCreationUtils method importModule.

/**
     * This method imports a leiningen module from a leiningen project file and imports it into the idea project.
     * <p/>
     * Notes:
     * <p/>
     * Each of the IDEA components has a getModifiableModel on it.  This method returns a new instance each time you
     * invoke it.  Once you have a modifiable model of the component you wish to update, you mutate it to the state
     * you wish.  Once you're done, you call commit() on the modifiable model and it updates the component it came from.
     * <p/>
     * Since a lot of the components are persisted in files, commit() updates these files as well.  Therefore you need
     * to make any calls to commit() from within a WriteAction.
     *
     * @param ideaProject The IDEA project to add the leiningen module to.
     * @param leinProjectFile  The leiningen project file
     * @return The leiningen project map.
     */
public Map importModule(Project ideaProject, VirtualFile leinProjectFile) {
    ClassPathUtils.getInstance().switchToPluginClassLoader();
    Map projectMap = LeiningenAPI.loadProject(leinProjectFile.getPath());
    String name = (String) projectMap.get(LEIN_PROJECT_NAME);
    final ModifiableModuleModel moduleManager = createModuleManager(ideaProject);
    final ModifiableRootModel module = createModule(moduleManager, leinProjectFile.getParent().getPath(), name);
    initializeModulePaths(projectMap, module, leinProjectFile.getParent());
    ProjectRootManagerEx rootManager = ProjectRootManagerEx.getInstanceEx(ideaProject);
    module.setSdk(rootManager.getProjectSdk());
    //Setup the dependencies
    // Based loosely on org.jetbrains.idea.maven.importing.MavenRootModelAdapter#addLibraryDependency
    //We could use the module table here, but then the libraries wouldn't be shared across modules.
    final LibraryTable.ModifiableModel projectLibraries = ProjectLibraryTable.getInstance(ideaProject).getModifiableModel();
    //Load all the dependencies from the project file
    List dependencyMaps = LeiningenAPI.loadDependencies(leinProjectFile.getCanonicalPath());
    final List<LibraryInfo> dependencies = initializeDependencies(module, projectLibraries, dependencyMaps);
    new WriteAction() {

        @Override
        protected void run(Result result) throws Throwable {
            for (LibraryInfo library : dependencies) {
                library.modifiableModel.commit();
            }
            //Save the project libraries
            projectLibraries.commit();
            //Save the module itself to the module file.
            module.commit();
            //Save the list of modules that are in this project to the IDEA project file
            moduleManager.commit();
        }
    }.execute();
    return projectMap;
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) Result(com.intellij.openapi.application.Result) ProjectRootManagerEx(com.intellij.openapi.roots.ex.ProjectRootManagerEx) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 4 with ProjectRootManagerEx

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

the class InheritedJdkTest method test1.

public void test1() throws Exception {
    final Sdk jdk = IdeaTestUtil.getMockJdk17("java 1.4");
    ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(jdk));
    final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
    ApplicationManager.getApplication().runWriteAction(() -> {
        final ProjectRootManagerEx rootManagerEx = ProjectRootManagerEx.getInstanceEx(myProject);
        rootManagerEx.setProjectSdkName(jdk.getName());
        ModuleRootModificationUtil.setSdkInherited(myModule);
    });
    assertTrue("JDK is inherited after explicit inheritSdk()", rootManager.isSdkInherited());
    assertEquals("Correct jdk inherited", jdk, rootManager.getSdk());
    ModuleRootModificationUtil.setModuleSdk(myModule, null);
    assertFalse("JDK is not inherited after setJdk(null)", rootManager.isSdkInherited());
    assertNull("No JDK assigned", rootManager.getSdk());
    final Sdk jdk1 = IdeaTestUtil.getMockJdk17("jjj");
    ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(jdk1));
    ModuleRootModificationUtil.setModuleSdk(myModule, jdk1);
    assertFalse("JDK is not inherited after setJdk(jdk1)", rootManager.isSdkInherited());
    assertEquals("jdk1 is assigned", jdk1, rootManager.getSdk());
}
Also used : ProjectRootManagerEx(com.intellij.openapi.roots.ex.ProjectRootManagerEx) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 5 with ProjectRootManagerEx

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

the class InheritedJdkTest method test2.

public void test2() throws Exception {
    final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
    ModuleRootModificationUtil.setSdkInherited(myModule);
    assertTrue("JDK is inherited after inheritSdk()", rootManager.isSdkInherited());
    assertNull("No JDK assigned", rootManager.getSdk());
    final Sdk mockJdk = IdeaTestUtil.getMockJdk17("mock 1.4");
    ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(mockJdk));
    final ProjectRootManagerEx projectRootManager = ProjectRootManagerEx.getInstanceEx(myProject);
    ApplicationManager.getApplication().runWriteAction(() -> projectRootManager.setProjectSdk(mockJdk));
    assertTrue(rootManager.isSdkInherited());
    assertEquals("mockJdk inherited", mockJdk, rootManager.getSdk());
    ApplicationManager.getApplication().runWriteAction(() -> projectRootManager.setProjectSdkName("jdk1"));
    assertTrue(rootManager.isSdkInherited());
    Assert.assertEquals("Correct non-existing JDK inherited", "jdk1", rootManager.orderEntries().process(new RootPolicy<String>() {

        @Override
        public String visitInheritedJdkOrderEntry(InheritedJdkOrderEntry inheritedJdkOrderEntry, String s) {
            return inheritedJdkOrderEntry.getJdkName();
        }
    }, null));
    assertNull("Non-existing JDK", rootManager.getSdk());
    final Sdk jdk1 = IdeaTestUtil.getMockJdk17("jdk1");
    ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(jdk1));
    assertTrue(rootManager.isSdkInherited());
    assertNotNull("JDK appeared", rootManager.getSdk());
    assertEquals("jdk1 found", jdk1, rootManager.getSdk());
}
Also used : ProjectRootManagerEx(com.intellij.openapi.roots.ex.ProjectRootManagerEx) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Sdk(com.intellij.openapi.projectRoots.Sdk) RootPolicy(com.intellij.openapi.roots.RootPolicy) InheritedJdkOrderEntry(com.intellij.openapi.roots.InheritedJdkOrderEntry)

Aggregations

ProjectRootManagerEx (com.intellij.openapi.roots.ex.ProjectRootManagerEx)9 Sdk (com.intellij.openapi.projectRoots.Sdk)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)2 Result (com.intellij.openapi.application.Result)1 WriteAction (com.intellij.openapi.application.WriteAction)1 ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)1 JavaSdkVersion (com.intellij.openapi.projectRoots.JavaSdkVersion)1 InheritedJdkOrderEntry (com.intellij.openapi.roots.InheritedJdkOrderEntry)1 LanguageLevelProjectExtension (com.intellij.openapi.roots.LanguageLevelProjectExtension)1 RootPolicy (com.intellij.openapi.roots.RootPolicy)1 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)1 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (org.jetbrains.annotations.Nullable)1