mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
platform: commit/dispose allocated model
This commit is contained in:
+101
-65
@@ -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<String> classesRoots, List<String> sourceRoots,
|
||||
public static void addModuleLibrary(final Module module,
|
||||
final String libName,
|
||||
final List<String> classesRoots,
|
||||
final List<String> 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<ModifiableRootModel>() {
|
||||
@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.<String>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<ModifiableRootModel>() {
|
||||
@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<ModifiableRootModel>() {
|
||||
@Override
|
||||
public void consume(ModifiableRootModel model) {
|
||||
model.setSdk(sdk);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void setSdkInherited(Module module) {
|
||||
updateModel(module, new Consumer<ModifiableRootModel>() {
|
||||
@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<ModifiableRootModel>() {
|
||||
@Override
|
||||
public void consume(ModifiableRootModel model) {
|
||||
ModuleOrderEntry entry = model.addModuleOrderEntry(to);
|
||||
entry.setScope(scope);
|
||||
entry.setExported(exported);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void updateModel(Module module, Consumer<ModifiableRootModel> 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.<String>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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user