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();
}
}
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;
}
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));
}
}
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;
}
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"));
}
Aggregations