CustomLibraryDescriptorWithDeferredConfig: J2K
This commit is contained in:
+123
-167
@@ -14,232 +14,188 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea.framework;
|
package org.jetbrains.kotlin.idea.framework
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists
|
||||||
import com.intellij.framework.library.LibraryVersionProperties;
|
import com.intellij.framework.library.LibraryVersionProperties
|
||||||
import com.intellij.openapi.module.Module;
|
import com.intellij.openapi.module.Module
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.roots.ModifiableRootModel;
|
import com.intellij.openapi.roots.ModifiableRootModel
|
||||||
import com.intellij.openapi.roots.OrderRootType;
|
import com.intellij.openapi.roots.OrderRootType
|
||||||
import com.intellij.openapi.roots.libraries.Library;
|
import com.intellij.openapi.roots.libraries.Library
|
||||||
import com.intellij.openapi.roots.libraries.LibraryKind;
|
import com.intellij.openapi.roots.libraries.LibraryKind
|
||||||
import com.intellij.openapi.roots.libraries.NewLibraryConfiguration;
|
import com.intellij.openapi.roots.libraries.NewLibraryConfiguration
|
||||||
import com.intellij.openapi.roots.ui.configuration.libraries.CustomLibraryDescription;
|
import com.intellij.openapi.roots.ui.configuration.libraries.CustomLibraryDescription
|
||||||
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager;
|
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager
|
||||||
import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor;
|
import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil
|
||||||
import com.intellij.openapi.vfs.VfsUtil;
|
import com.intellij.openapi.vfs.VfsUtil
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile
|
||||||
import kotlin.jvm.functions.Function1;
|
import org.jetbrains.kotlin.idea.configuration.KotlinWithLibraryConfigurator
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.kotlin.idea.configuration.createConfigureKotlinNotificationCollector
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.kotlin.idea.configuration.getConfiguratorByName
|
||||||
import org.jetbrains.kotlin.idea.configuration.*;
|
import org.jetbrains.kotlin.idea.framework.ui.CreateLibraryDialog
|
||||||
import org.jetbrains.kotlin.idea.framework.ui.CreateLibraryDialog;
|
import org.jetbrains.kotlin.idea.framework.ui.FileUIUtils
|
||||||
import org.jetbrains.kotlin.idea.framework.ui.FileUIUtils;
|
import org.jetbrains.kotlin.idea.util.projectStructure.findLibrary
|
||||||
import org.jetbrains.kotlin.idea.util.projectStructure.ProjectStructureUtilKt;
|
import org.jetbrains.kotlin.idea.util.projectStructure.getModuleDir
|
||||||
|
import org.jetbrains.kotlin.idea.util.projectStructure.replaceFileRoot
|
||||||
|
import java.io.File
|
||||||
|
import java.util.*
|
||||||
|
import javax.swing.JComponent
|
||||||
|
|
||||||
import javax.swing.*;
|
/**
|
||||||
import java.io.File;
|
* @param project null when project doesn't exist yet (called from project wizard)
|
||||||
import java.util.Arrays;
|
*/
|
||||||
import java.util.List;
|
abstract class CustomLibraryDescriptorWithDeferredConfig
|
||||||
import java.util.Set;
|
(
|
||||||
|
project: Project?,
|
||||||
|
private val configuratorName: String,
|
||||||
|
private val libraryName: String,
|
||||||
|
private val dialogTitle: String,
|
||||||
|
private val modulesSeparatorCaption: String,
|
||||||
|
private val libraryKind: LibraryKind,
|
||||||
|
private val suitableLibraryKinds: Set<LibraryKind>
|
||||||
|
) : CustomLibraryDescription() {
|
||||||
|
private val projectBaseDir: VirtualFile?
|
||||||
|
|
||||||
public abstract class CustomLibraryDescriptorWithDeferredConfig extends CustomLibraryDescription {
|
var copyFileRequests: DeferredCopyFileRequests? = null
|
||||||
|
private set
|
||||||
|
|
||||||
private static final String DEFAULT_LIB_DIR_NAME = "lib";
|
init {
|
||||||
|
this.projectBaseDir = project?.baseDir
|
||||||
private final String configuratorName;
|
|
||||||
private final String libraryName;
|
|
||||||
private final String dialogTitle;
|
|
||||||
private final String modulesSeparatorCaption;
|
|
||||||
private final LibraryKind libraryKind;
|
|
||||||
private final Set<? extends LibraryKind> suitableLibraryKinds;
|
|
||||||
private final VirtualFile projectBaseDir;
|
|
||||||
|
|
||||||
private DeferredCopyFileRequests deferredCopyFileRequests;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param project null when project doesn't exist yet (called from project wizard)
|
|
||||||
*/
|
|
||||||
public CustomLibraryDescriptorWithDeferredConfig(
|
|
||||||
@Nullable Project project,
|
|
||||||
@NotNull String configuratorName,
|
|
||||||
@NotNull String libraryName,
|
|
||||||
@NotNull String dialogTitle,
|
|
||||||
@NotNull String modulesSeparatorCaption,
|
|
||||||
@NotNull LibraryKind libraryKind,
|
|
||||||
@NotNull Set<? extends LibraryKind> suitableLibraryKinds
|
|
||||||
) {
|
|
||||||
this.projectBaseDir = project != null ? project.getBaseDir() : null;
|
|
||||||
this.configuratorName = configuratorName;
|
|
||||||
this.libraryName = libraryName;
|
|
||||||
this.dialogTitle = dialogTitle;
|
|
||||||
this.modulesSeparatorCaption = modulesSeparatorCaption;
|
|
||||||
this.libraryKind = libraryKind;
|
|
||||||
this.suitableLibraryKinds = suitableLibraryKinds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
override fun getSuitableLibraryKinds(): Set<LibraryKind> {
|
||||||
public DeferredCopyFileRequests getCopyFileRequests() {
|
return suitableLibraryKinds
|
||||||
return deferredCopyFileRequests;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
fun finishLibConfiguration(module: Module, rootModel: ModifiableRootModel) {
|
||||||
@Override
|
val deferredCopyFileRequests = copyFileRequests ?: return
|
||||||
public Set<? extends LibraryKind> getSuitableLibraryKinds() {
|
|
||||||
return suitableLibraryKinds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void finishLibConfiguration(@NotNull Module module, @NotNull ModifiableRootModel rootModel) {
|
val library = rootModel.orderEntries().findLibrary { library ->
|
||||||
DeferredCopyFileRequests deferredCopyFileRequests = getCopyFileRequests();
|
val libraryPresentationManager = LibraryPresentationManager.getInstance()
|
||||||
if (deferredCopyFileRequests == null) return;
|
val classFiles = Arrays.asList(*library.getFiles(OrderRootType.CLASSES))
|
||||||
|
|
||||||
Library library = ProjectStructureUtilKt.findLibrary(rootModel.orderEntries(), new Function1<Library, Boolean>() {
|
libraryPresentationManager.isLibraryOfKind(classFiles, libraryKind)
|
||||||
@Override
|
} ?: return
|
||||||
public Boolean invoke(@NotNull Library library) {
|
|
||||||
LibraryPresentationManager libraryPresentationManager = LibraryPresentationManager.getInstance();
|
|
||||||
List<VirtualFile> classFiles = Arrays.asList(library.getFiles(OrderRootType.CLASSES));
|
|
||||||
|
|
||||||
return libraryPresentationManager.isLibraryOfKind(classFiles, libraryKind);
|
val model = library.modifiableModel
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (library == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Library.ModifiableModel model = library.getModifiableModel();
|
|
||||||
try {
|
try {
|
||||||
deferredCopyFileRequests.performRequests(module.getProject(), ProjectStructureUtilKt.getModuleDir(module), model);
|
deferredCopyFileRequests.performRequests(module.project, module.getModuleDir(), model)
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
model.commit();
|
model.commit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class DeferredCopyFileRequests {
|
class DeferredCopyFileRequests(private val configurator: KotlinWithLibraryConfigurator) {
|
||||||
private final List<CopyFileRequest> copyFilesRequests = Lists.newArrayList();
|
private val copyFilesRequests = Lists.newArrayList<CopyFileRequest>()
|
||||||
private final KotlinWithLibraryConfigurator configurator;
|
|
||||||
|
|
||||||
public DeferredCopyFileRequests(KotlinWithLibraryConfigurator configurator) {
|
fun performRequests(project: Project, relativePath: String, model: Library.ModifiableModel) {
|
||||||
this.configurator = configurator;
|
val collector = createConfigureKotlinNotificationCollector(project)
|
||||||
}
|
for (request in copyFilesRequests) {
|
||||||
|
val destinationPath = if (FileUtil.isAbsolute(request.toDir))
|
||||||
|
request.toDir
|
||||||
|
else
|
||||||
|
File(relativePath, request.toDir).path
|
||||||
|
|
||||||
public void performRequests(@NotNull Project project, @NotNull String relativePath, Library.ModifiableModel model) {
|
val resultFile = configurator.copyFileToDir(request.file, destinationPath, collector)
|
||||||
NotificationMessageCollector collector = NotificationMessageCollectorKt.createConfigureKotlinNotificationCollector(project);
|
|
||||||
for (CopyFileRequest request : copyFilesRequests) {
|
|
||||||
String destinationPath = FileUtil.isAbsolute(request.toDir) ?
|
|
||||||
request.toDir :
|
|
||||||
new File(relativePath, request.toDir).getPath();
|
|
||||||
|
|
||||||
File resultFile = configurator.copyFileToDir(request.file, destinationPath, collector);
|
if (request.replaceInLib && resultFile != null) {
|
||||||
|
model.replaceFileRoot(request.file, resultFile)
|
||||||
if (request.replaceInLib) {
|
|
||||||
ProjectStructureUtilKt.replaceFileRoot(model, request.file, resultFile);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
collector.showNotification();
|
collector.showNotification()
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCopyWithReplaceRequest(@NotNull File file, @NotNull String copyIntoPath) {
|
fun addCopyWithReplaceRequest(file: File, copyIntoPath: String) {
|
||||||
copyFilesRequests.add(new CopyFileRequest(copyIntoPath, file, true));
|
copyFilesRequests.add(CopyFileRequest(copyIntoPath, file, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class CopyFileRequest {
|
class CopyFileRequest(val toDir: String, val file: File, val replaceInLib: Boolean)
|
||||||
private final String toDir;
|
|
||||||
private final File file;
|
|
||||||
private final boolean replaceInLib;
|
|
||||||
|
|
||||||
public CopyFileRequest(String dir, File file, boolean replaceInLib) {
|
|
||||||
toDir = dir;
|
|
||||||
this.file = file;
|
|
||||||
this.replaceInLib = replaceInLib;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
override fun createNewLibrary(parentComponent: JComponent, contextDirectory: VirtualFile?): NewLibraryConfiguration? {
|
||||||
@Override
|
val configurator = configurator
|
||||||
public NewLibraryConfiguration createNewLibrary(@NotNull JComponent parentComponent, @Nullable VirtualFile contextDirectory) {
|
|
||||||
KotlinWithLibraryConfigurator configurator = getConfigurator();
|
|
||||||
|
|
||||||
deferredCopyFileRequests = new DeferredCopyFileRequests(configurator);
|
copyFileRequests = DeferredCopyFileRequests(configurator)
|
||||||
|
|
||||||
String defaultPathToJarFile = projectBaseDir == null ? DEFAULT_LIB_DIR_NAME
|
val defaultPathToJarFile = if (projectBaseDir == null)
|
||||||
: FileUIUtils.createRelativePath(null, projectBaseDir, DEFAULT_LIB_DIR_NAME);
|
DEFAULT_LIB_DIR_NAME
|
||||||
|
else
|
||||||
|
FileUIUtils.createRelativePath(null, projectBaseDir, DEFAULT_LIB_DIR_NAME)
|
||||||
|
|
||||||
RuntimeLibraryFiles files = configurator.getExistingJarFiles();
|
val files = configurator.existingJarFiles
|
||||||
|
|
||||||
File runtimeJar;
|
val runtimeJar: File
|
||||||
File reflectJar;
|
var reflectJar: File?
|
||||||
File runtimeSrcJar;
|
var runtimeSrcJar: File
|
||||||
|
|
||||||
File stdJarInDefaultPath = files.getRuntimeDestination(defaultPathToJarFile);
|
val stdJarInDefaultPath = files.getRuntimeDestination(defaultPathToJarFile)
|
||||||
if (projectBaseDir != null && stdJarInDefaultPath.exists()) {
|
if (projectBaseDir != null && stdJarInDefaultPath.exists()) {
|
||||||
runtimeJar = stdJarInDefaultPath;
|
runtimeJar = stdJarInDefaultPath
|
||||||
|
|
||||||
reflectJar = files.getReflectDestination(defaultPathToJarFile);
|
reflectJar = files.getReflectDestination(defaultPathToJarFile)
|
||||||
if (reflectJar != null && !reflectJar.exists()) {
|
if (reflectJar != null && !reflectJar.exists()) {
|
||||||
reflectJar = files.getReflectJar();
|
reflectJar = files.reflectJar
|
||||||
assert reflectJar != null : "getReflectDestination != null, but getReflectJar == null";
|
assert(reflectJar != null) { "getReflectDestination != null, but getReflectJar == null" }
|
||||||
deferredCopyFileRequests.addCopyWithReplaceRequest(reflectJar, runtimeJar.getParent());
|
copyFileRequests!!.addCopyWithReplaceRequest(reflectJar!!, runtimeJar.parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
runtimeSrcJar = files.getRuntimeSourcesDestination(defaultPathToJarFile);
|
runtimeSrcJar = files.getRuntimeSourcesDestination(defaultPathToJarFile)
|
||||||
if (!runtimeSrcJar.exists()) {
|
if (!runtimeSrcJar.exists()) {
|
||||||
runtimeSrcJar = files.getRuntimeSourcesJar();
|
runtimeSrcJar = files.runtimeSourcesJar
|
||||||
deferredCopyFileRequests.addCopyWithReplaceRequest(runtimeSrcJar, runtimeJar.getParent());
|
copyFileRequests!!.addCopyWithReplaceRequest(runtimeSrcJar, runtimeJar.parent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
CreateLibraryDialog dialog = new CreateLibraryDialog(defaultPathToJarFile, dialogTitle, modulesSeparatorCaption);
|
val dialog = CreateLibraryDialog(defaultPathToJarFile, dialogTitle, modulesSeparatorCaption)
|
||||||
dialog.show();
|
dialog.show()
|
||||||
|
|
||||||
if (!dialog.isOK()) return null;
|
if (!dialog.isOK) return null
|
||||||
|
|
||||||
String copyIntoPath = dialog.getCopyIntoPath();
|
val copyIntoPath = dialog.copyIntoPath
|
||||||
if (copyIntoPath != null) {
|
if (copyIntoPath != null) {
|
||||||
for (File file : files.getAllJars()) {
|
for (file in files.getAllJars()) {
|
||||||
deferredCopyFileRequests.addCopyWithReplaceRequest(file, copyIntoPath);
|
copyFileRequests!!.addCopyWithReplaceRequest(file, copyIntoPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runtimeJar = files.getRuntimeJar();
|
runtimeJar = files.runtimeJar
|
||||||
reflectJar = files.getReflectJar();
|
reflectJar = files.reflectJar
|
||||||
runtimeSrcJar = files.getRuntimeSourcesJar();
|
runtimeSrcJar = files.runtimeSourcesJar
|
||||||
}
|
}
|
||||||
|
|
||||||
return createConfiguration(Arrays.asList(runtimeJar, reflectJar), runtimeSrcJar);
|
return createConfiguration(Arrays.asList<File>(runtimeJar, reflectJar), runtimeSrcJar)
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
private val configurator: KotlinWithLibraryConfigurator
|
||||||
private KotlinWithLibraryConfigurator getConfigurator() {
|
get() {
|
||||||
KotlinWithLibraryConfigurator configurator =
|
val configurator = getConfiguratorByName(configuratorName) as KotlinWithLibraryConfigurator? ?: error("Configurator with name $configuratorName should exists")
|
||||||
(KotlinWithLibraryConfigurator) ConfigureKotlinInProjectUtilsKt.getConfiguratorByName(configuratorName);
|
return configurator
|
||||||
assert configurator != null : "Configurator with name " + configuratorName + " should exists";
|
}
|
||||||
return configurator;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implements an API added in IDEA 16
|
// Implements an API added in IDEA 16
|
||||||
@Nullable
|
override fun createNewLibraryWithDefaultSettings(contextDirectory: VirtualFile?): NewLibraryConfiguration? {
|
||||||
public NewLibraryConfiguration createNewLibraryWithDefaultSettings(@Nullable VirtualFile contextDirectory) {
|
val files = configurator.existingJarFiles
|
||||||
RuntimeLibraryFiles files = getConfigurator().getExistingJarFiles();
|
return createConfiguration(Arrays.asList<File>(files.runtimeJar, files.reflectJar), files.runtimeSourcesJar)
|
||||||
return createConfiguration(Arrays.asList(files.getRuntimeJar(), files.getReflectJar()), files.getRuntimeSourcesJar());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
protected fun createConfiguration(libraryFiles: List<File>, librarySrcFile: File): NewLibraryConfiguration {
|
||||||
protected NewLibraryConfiguration createConfiguration(@NotNull final List<File> libraryFiles, @NotNull final File librarySrcFile) {
|
return object : NewLibraryConfiguration(libraryName, null, LibraryVersionProperties()) {
|
||||||
return new NewLibraryConfiguration(libraryName, null, new LibraryVersionProperties()) {
|
override fun addRoots(editor: LibraryEditor) {
|
||||||
@Override
|
for (libraryFile in libraryFiles) {
|
||||||
public void addRoots(@NotNull LibraryEditor editor) {
|
|
||||||
for (File libraryFile : libraryFiles) {
|
|
||||||
if (libraryFile != null) {
|
if (libraryFile != null) {
|
||||||
editor.addRoot(VfsUtil.getUrlForLibraryRoot(libraryFile), OrderRootType.CLASSES);
|
editor.addRoot(VfsUtil.getUrlForLibraryRoot(libraryFile), OrderRootType.CLASSES)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
editor.addRoot(VfsUtil.getUrlForLibraryRoot(librarySrcFile), OrderRootType.SOURCES);
|
editor.addRoot(VfsUtil.getUrlForLibraryRoot(librarySrcFile), OrderRootType.SOURCES)
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private val DEFAULT_LIB_DIR_NAME = "lib"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user