diff --git a/platform/projectModel-impl/src/com/intellij/openapi/roots/ModuleRootModificationUtil.java b/platform/projectModel-impl/src/com/intellij/openapi/roots/ModuleRootModificationUtil.java index 81f900a49216..1dea81030f5c 100644 --- a/platform/projectModel-impl/src/com/intellij/openapi/roots/ModuleRootModificationUtil.java +++ b/platform/projectModel-impl/src/com/intellij/openapi/roots/ModuleRootModificationUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2013 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.libraries.Library; +import com.intellij.util.Consumer; import org.jetbrains.annotations.Nullable; import java.util.Collections; @@ -33,27 +34,111 @@ public class ModuleRootModificationUtil { addModuleLibrary(module, libName, classesRoots, sourceRoots, DependencyScope.COMPILE); } - public static void addModuleLibrary(Module module, String libName, List classesRoots, List sourceRoots, + public static void addModuleLibrary(final Module module, + final String libName, + final List classesRoots, + final List sourceRoots, final DependencyScope scope) { - final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); - final Library library = model.getModuleLibraryTable().createLibrary(libName); - final Library.ModifiableModel libraryModel = library.getModifiableModel(); - for (String root : classesRoots) { - libraryModel.addRoot(root, OrderRootType.CLASSES); - } - for (String root : sourceRoots) { - libraryModel.addRoot(root, OrderRootType.SOURCES); - } - model.findLibraryOrderEntry(library).setScope(scope); - doWriteAction(new Runnable() { + updateModel(module, new Consumer() { @Override - public void run() { - libraryModel.commit(); - model.commit(); + public void consume(final ModifiableRootModel model) { + final Library library = model.getModuleLibraryTable().createLibrary(libName); + final Library.ModifiableModel libraryModel = library.getModifiableModel(); + + for (String root : classesRoots) { + libraryModel.addRoot(root, OrderRootType.CLASSES); + } + for (String root : sourceRoots) { + libraryModel.addRoot(root, OrderRootType.SOURCES); + } + + LibraryOrderEntry entry = model.findLibraryOrderEntry(library); + assert entry != null : library; + entry.setScope(scope); + + doWriteAction(new Runnable() { + @Override + public void run() { + libraryModel.commit(); + } + }); } }); } + public static void addModuleLibrary(Module module, String classesRootUrl) { + addModuleLibrary(module, null, Collections.singletonList(classesRootUrl), Collections.emptyList()); + } + + public static void addDependency(Module module, Library library) { + addDependency(module, library, DependencyScope.COMPILE, false); + } + + public static void addDependency(Module module, final Library library, final DependencyScope scope, final boolean exported) { + updateModel(module, new Consumer() { + @Override + public void consume(ModifiableRootModel model) { + LibraryOrderEntry entry = model.addLibraryEntry(library); + entry.setExported(exported); + entry.setScope(scope); + } + }); + } + + public static void setModuleSdk(Module module, @Nullable final Sdk sdk) { + updateModel(module, new Consumer() { + @Override + public void consume(ModifiableRootModel model) { + model.setSdk(sdk); + } + }); + } + + public static void setSdkInherited(Module module) { + updateModel(module, new Consumer() { + @Override + public void consume(ModifiableRootModel model) { + model.inheritSdk(); + } + }); + } + + public static void addDependency(final Module from, final Module to) { + addDependency(from, to, DependencyScope.COMPILE, false); + } + + public static void addDependency(Module from, final Module to, final DependencyScope scope, final boolean exported) { + updateModel(from, new Consumer() { + @Override + public void consume(ModifiableRootModel model) { + ModuleOrderEntry entry = model.addModuleOrderEntry(to); + entry.setScope(scope); + entry.setExported(exported); + } + }); + } + + private static void updateModel(Module module, Consumer task) { + final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); + try { + task.consume(model); + doWriteAction(new Runnable() { + @Override + public void run() { + model.commit(); + } + }); + } + catch (RuntimeException e) { + model.dispose(); + throw e; + } + catch (Error e) { + model.dispose(); + throw e; + } + } + private static void doWriteAction(final Runnable action) { final Application application = ApplicationManager.getApplication(); application.invokeAndWait(new Runnable() { @@ -63,53 +148,4 @@ public class ModuleRootModificationUtil { } }, application.getDefaultModalityState()); } - - public static void addModuleLibrary(Module module, String classesRootUrl) { - addModuleLibrary(module, null, Collections.singletonList(classesRootUrl), Collections.emptyList()); - } - - public static void addDependency(Module module, Library library) { - addDependency(module, library, DependencyScope.COMPILE, false); - } - - public static void addDependency(Module module, Library library, final DependencyScope scope, final boolean exported) { - final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); - final LibraryOrderEntry entry = model.addLibraryEntry(library); - entry.setExported(exported); - entry.setScope(scope); - doCommit(model); - } - - public static void setModuleSdk(Module module, @Nullable Sdk sdk) { - final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); - model.setSdk(sdk); - doCommit(model); - } - - public static void setSdkInherited(Module module) { - final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); - model.inheritSdk(); - doCommit(model); - } - - public static void addDependency(final Module from, final Module to) { - addDependency(from, to, DependencyScope.COMPILE, false); - } - - public static void addDependency(final Module from, final Module to, final DependencyScope scope, final boolean exported) { - final ModifiableRootModel model = ModuleRootManager.getInstance(from).getModifiableModel(); - final ModuleOrderEntry entry = model.addModuleOrderEntry(to); - entry.setScope(scope); - entry.setExported(exported); - doCommit(model); - } - - private static void doCommit(final ModifiableRootModel model) { - doWriteAction(new Runnable() { - @Override - public void run() { - model.commit(); - } - }); - } }