Add an ability to create libraries from standalone compiler

This commit is contained in:
goodwinnk
2013-02-26 23:55:04 +04:00
committed by Nikolay Krasko
parent 9b9f9a50d3
commit eed215893f
10 changed files with 454 additions and 110 deletions
@@ -24,6 +24,9 @@ public interface KotlinPaths {
@NotNull
File getHomePath();
@NotNull
File getBuildVersionFile();
@NotNull
File getLibPath();
@@ -34,6 +34,12 @@ public class KotlinPathsFromHomeDir implements KotlinPaths {
return homePath;
}
@NotNull
@Override
public File getBuildVersionFile() {
return new File(homePath, PathUtil.BUILD_VERSION_NAME);
}
@Override
@NotNull
public File getLibPath() {
@@ -21,17 +21,23 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.io.URLUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
/*
* This code is essentially copied from IntelliJ IDEA's VfsUtil, and slightly restructured
*/
public class KotlinVfsUtil {
private static final String FILE = "file";
private static final String JAR = "jar";
private static final String PROTOCOL_DELIMITER = ":";
/*
* This code is essentially copied from IntelliJ IDEA's VfsUtil, and slightly restructured
*/
@NotNull
public static String convertFromUrl(@NotNull URL url) throws MalformedURLException {
String protocol = url.getProtocol();
@@ -55,5 +61,16 @@ public class KotlinVfsUtil {
return protocol + "://" + path;
}
public static List<File> getFilesInDirectoryByPattern(@NotNull File dir, @NotNull final Pattern pattern) {
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(@NotNull File dir, @NotNull String name) {
return pattern.matcher(name).matches();
}
});
return files != null ? Arrays.asList(files) : Collections.<File>emptyList();
}
private KotlinVfsUtil() {}
}
@@ -21,9 +21,12 @@ import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.List;
import java.util.regex.Pattern;
public class PathUtil {
@@ -31,9 +34,52 @@ public class PathUtil {
public static final String JS_LIB_JS_NAME = "kotlinEcma3.js";
public static final String JDK_ANNOTATIONS_JAR = "kotlin-jdk-annotations.jar";
public static final String KOTLIN_JAVA_RUNTIME_JAR = "kotlin-runtime.jar";
public static final String BUILD_VERSION_NAME = "build.txt";
public static final String HOME_FOLDER_NAME = "kotlinc";
private static final File NO_PATH = new File("<no_path>");
public static final Function<String, File> KOTLIN_HOME_DIRECTORY_ADAPTER = new Function<String, File>() {
private final Pattern homeDirPattern = Pattern.compile(HOME_FOLDER_NAME);
private final Pattern buildFilePattern = Pattern.compile(BUILD_VERSION_NAME);
@Override
public File fun(String path) {
if (path == null) {
return null;
}
File directory = new File(path);
if (!(directory.exists() || directory.isDirectory())) {
return null;
}
if (checkIsHomeDirectory(directory)) {
return directory;
}
List<File> homeSubfolders = KotlinVfsUtil.getFilesInDirectoryByPattern(directory, homeDirPattern);
if (!homeSubfolders.isEmpty()) {
assert homeSubfolders.size() == 1;
File homeNamedDir = homeSubfolders.get(0);
if (checkIsHomeDirectory(homeNamedDir)) {
return homeNamedDir;
}
}
File parentDirectory = directory.getParentFile();
if (parentDirectory != null && checkIsHomeDirectory(parentDirectory)) {
return parentDirectory;
}
return null;
}
private boolean checkIsHomeDirectory(File dir) {
return dir.getName().equals(HOME_FOLDER_NAME) && !KotlinVfsUtil.getFilesInDirectoryByPattern(dir, buildFilePattern).isEmpty();
}
};
private PathUtil() {}
@NotNull
@@ -65,7 +111,17 @@ public class PathUtil {
@NotNull
public static KotlinPaths getKotlinPathsForDistDirectory() {
return new KotlinPathsFromHomeDir(new File("dist/kotlinc"));
return new KotlinPathsFromHomeDir(new File("dist", HOME_FOLDER_NAME));
}
@NotNull
public static KotlinPaths getKotlinStandaloneCompilerPaths(@NotNull String path) {
File homePath = KOTLIN_HOME_DIRECTORY_ADAPTER.fun(path);
if (homePath == null) {
throw new IllegalArgumentException(String.format("Can't get home path from '%s'", path));
}
return new KotlinPathsFromHomeDir(homePath);
}
@NotNull
@@ -90,7 +146,7 @@ public class PathUtil {
if (jar.getName().equals("kotlin-jps-plugin.jar")) {
File pluginHome = jar.getParentFile().getParentFile().getParentFile();
return new File(pluginHome, "kotlinc");
return new File(pluginHome, HOME_FOLDER_NAME);
}
return NO_PATH;
@@ -106,7 +162,7 @@ public class PathUtil {
File lib = jar.getParentFile();
File pluginHome = lib.getParentFile();
return new File(pluginHome, "kotlinc");
return new File(pluginHome, HOME_FOLDER_NAME);
}
return NO_PATH;
@@ -31,7 +31,9 @@ import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.plugin.JetPluginUtil;
import org.jetbrains.jet.plugin.framework.ui.CreateLibrarySourceDialog;
import org.jetbrains.jet.plugin.framework.ui.FileUIUtils;
import org.jetbrains.jet.utils.KotlinPaths;
import org.jetbrains.jet.utils.PathUtil;
import javax.swing.*;
@@ -54,41 +56,42 @@ public class JSLibraryDescription extends CustomLibraryDescription {
@Nullable
@Override
public NewLibraryConfiguration createNewLibrary(@NotNull JComponent parentComponent, @Nullable VirtualFile contextDirectory) {
return createFromPlugin(contextDirectory);
}
CreateLibrarySourceDialog dialog = new CreateLibrarySourceDialog(null, "Create Kotlin JavaScript Library", contextDirectory);
dialog.show();
private NewLibraryConfiguration createFromPlugin(VirtualFile contextDirectory) {
File runtimePath = PathUtil.getKotlinPathsForIdeaPlugin().getJsLibJarPath();
if (dialog.isOK()) {
String standaloneCompilerPath = dialog.getStandaloneCompilerPath();
KotlinPaths paths = standaloneCompilerPath == null ?
PathUtil.getKotlinPathsForIdeaPlugin() :
PathUtil.getKotlinStandaloneCompilerPaths(standaloneCompilerPath);
if (!runtimePath.exists()) {
Messages.showErrorDialog("JavaScript standard library was not found. Make sure plugin is installed properly.",
JAVA_SCRIPT_LIBRARY_CREATION);
return null;
}
String directoryPath = FileUIUtils.selectDestinationFolderDialog(
null, contextDirectory, "Select folder where Kotlin JavaScript header should be copied");
if (directoryPath == null) {
return null;
}
final File targetFile;
try {
targetFile = FileUIUtils.copyWithOverwriteDialog(directoryPath, runtimePath);
copyJsRuntimeFile(directoryPath);
}
catch (IOException e) {
Messages.showErrorDialog("Error during file copy", JAVA_SCRIPT_LIBRARY_CREATION);
return null;
}
return new NewLibraryConfiguration(LIBRARY_NAME + "-" + JetPluginUtil.getPluginVersion(), getDownloadableLibraryType(), new LibraryVersionProperties()) {
@Override
public void addRoots(@NotNull LibraryEditor editor) {
editor.addRoot(VfsUtil.getUrlForLibraryRoot(targetFile), OrderRootType.SOURCES);
File libraryFile = paths.getJsLibJarPath();
if (!libraryFile.exists()) {
Messages.showErrorDialog(String.format("JavaScript standard library was not found in %s", paths.getLibPath()),
JAVA_SCRIPT_LIBRARY_CREATION);
return null;
}
};
String copyIntoPath = dialog.getCopyIntoPath();
if (copyIntoPath != null) {
libraryFile = FileUIUtils.copyWithOverwriteDialog(parentComponent, copyIntoPath, libraryFile, JAVA_SCRIPT_LIBRARY_CREATION);
if (libraryFile == null) {
return null;
}
copyJsRuntimeFile(copyIntoPath);
}
final String libraryFileUrl = VfsUtil.getUrlForLibraryRoot(libraryFile);
return new NewLibraryConfiguration(LIBRARY_NAME + "-" + dialog.getVersion(), getDownloadableLibraryType(), new LibraryVersionProperties()) {
@Override
public void addRoots(@NotNull LibraryEditor editor) {
editor.addRoot(libraryFileUrl, OrderRootType.SOURCES);
}
};
}
return null;
}
private static void copyJsRuntimeFile(@NotNull String directoryPath) {
@@ -102,7 +105,7 @@ public class JSLibraryDescription extends CustomLibraryDescription {
LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetFile);
}
catch (IOException e) {
// Don't do nothing. This is temp code and should be removed.
// Do nothing. This is a very temp code and should be removed.
}
}
}
@@ -28,13 +28,13 @@ import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.plugin.JetPluginUtil;
import org.jetbrains.jet.plugin.framework.ui.CreateLibrarySourceDialog;
import org.jetbrains.jet.plugin.framework.ui.FileUIUtils;
import org.jetbrains.jet.utils.KotlinPaths;
import org.jetbrains.jet.utils.PathUtil;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.Set;
public class JavaRuntimeLibraryDescription extends CustomLibraryDescription {
@@ -52,40 +52,42 @@ public class JavaRuntimeLibraryDescription extends CustomLibraryDescription {
@Nullable
@Override
public NewLibraryConfiguration createNewLibrary(@NotNull JComponent parentComponent, @Nullable VirtualFile contextDirectory) {
return createFromPlugin(contextDirectory);
}
CreateLibrarySourceDialog dialog = new CreateLibrarySourceDialog(null, "Create Kotlin Java Runtime Library", contextDirectory);
dialog.show();
private NewLibraryConfiguration createFromPlugin(VirtualFile contextDirectory) {
File runtimePath = PathUtil.getKotlinPathsForIdeaPlugin().getRuntimePath();
if (dialog.isOK()) {
String standaloneCompilerPath = dialog.getStandaloneCompilerPath();
KotlinPaths paths = standaloneCompilerPath == null ?
PathUtil.getKotlinPathsForIdeaPlugin() :
PathUtil.getKotlinStandaloneCompilerPaths(standaloneCompilerPath);
if (!runtimePath.exists()) {
Messages.showErrorDialog("Java Runtime library was not found. Make sure plugin is installed properly.",
JAVA_RUNTIME_LIBRARY_CREATION);
return null;
}
String directoryPath = FileUIUtils.selectDestinationFolderDialog(
null, contextDirectory, "Select folder where bundled Kotlin java runtime library should be copied");
if (directoryPath == null) {
return null;
}
final File targetFile;
try {
targetFile = FileUIUtils.copyWithOverwriteDialog(directoryPath, runtimePath);
}
catch (IOException e) {
Messages.showErrorDialog("Error during file copy", JAVA_RUNTIME_LIBRARY_CREATION);
return null;
}
return new NewLibraryConfiguration(LIBRARY_NAME + "-" + JetPluginUtil.getPluginVersion(), getDownloadableLibraryType(), new LibraryVersionProperties()) {
@Override
public void addRoots(@NotNull LibraryEditor editor) {
editor.addRoot(VfsUtil.getUrlForLibraryRoot(targetFile), OrderRootType.CLASSES);
editor.addRoot(VfsUtil.getUrlForLibraryRoot(targetFile) + "src", OrderRootType.SOURCES);
File libraryFile = paths.getRuntimePath();
if (!libraryFile.exists()) {
Messages.showErrorDialog(
parentComponent,
String.format("Java Runtime library was not found in '%s'." , paths.getLibPath()),
JAVA_RUNTIME_LIBRARY_CREATION);
return null;
}
};
String copyIntoPath = dialog.getCopyIntoPath();
if (copyIntoPath != null) {
libraryFile = FileUIUtils.copyWithOverwriteDialog(parentComponent, copyIntoPath, libraryFile, JAVA_RUNTIME_LIBRARY_CREATION);
if (libraryFile == null) {
return null;
}
}
final String libraryFileUrl = VfsUtil.getUrlForLibraryRoot(libraryFile);
return new NewLibraryConfiguration(LIBRARY_NAME + "-" + dialog.getVersion(), getDownloadableLibraryType(), new LibraryVersionProperties()) {
@Override
public void addRoots(@NotNull LibraryEditor editor) {
editor.addRoot(libraryFileUrl, OrderRootType.CLASSES);
editor.addRoot(libraryFileUrl + "src", OrderRootType.SOURCES);
}
};
}
return null;
}
}
@@ -16,7 +16,6 @@
package org.jetbrains.jet.plugin.framework.ui;
import com.intellij.ide.util.projectWizard.ProjectWizardUtil;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.fileChooser.FileChooserFactory;
@@ -72,9 +71,7 @@ class ChoosePathDialog extends DialogWrapper {
@Override
protected void doOKAction() {
if (ProjectWizardUtil.createDirectoryIfNotExists("Destination folder", getPath(), false)) {
super.doOKAction();
}
super.doOKAction();
}
@NotNull
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.jet.plugin.framework.ui.CreateLibrarySourceDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="48" y="54" width="436" height="297"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="730af" class="javax.swing.JRadioButton" binding="useStandaloneKotlinRadioButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="false"/>
<text value="Use &amp;standalone Kotlin compiler installation"/>
</properties>
</component>
<vspacer id="bb37c">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="46726" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="15" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="edb3" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="copyIntoDirectoryField">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="90315" class="javax.swing.JCheckBox" binding="copyLibraryCheckbox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Copy library files into project"/>
</properties>
</component>
<component id="1f83b" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="2" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Folder"/>
</properties>
</component>
</children>
</grid>
<component id="89e9f" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="kotlinStandalonePathField">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="3" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="c709f" class="javax.swing.JRadioButton" binding="useBundledKotlinRadioButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Use compiler &amp;bundled in plugin"/>
</properties>
</component>
</children>
</grid>
<buttonGroups>
<group name="sourceGroup">
<member id="730af"/>
<member id="c709f"/>
</group>
</buttonGroups>
</form>
@@ -0,0 +1,175 @@
package org.jetbrains.jet.plugin.framework.ui;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.DocumentAdapter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.plugin.JetPluginUtil;
import org.jetbrains.jet.utils.KotlinPaths;
import org.jetbrains.jet.utils.PathUtil;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class CreateLibrarySourceDialog extends DialogWrapper {
private JPanel contentPane;
private JRadioButton useStandaloneKotlinRadioButton;
private JRadioButton useBundledKotlinRadioButton;
private JCheckBox copyLibraryCheckbox;
private TextFieldWithBrowseButton copyIntoDirectoryField;
private TextFieldWithBrowseButton kotlinStandalonePathField;
private String version = null;
private final String initialStandaloneLabelText;
public CreateLibrarySourceDialog(@Nullable Project project, @NotNull String title, VirtualFile contextDirectory) {
super(project);
setTitle(title);
init();
contentPane.setMinimumSize(new Dimension(380, 180));
useBundledKotlinRadioButton.setText(useBundledKotlinRadioButton.getText() + " - " + JetPluginUtil.getPluginVersion());
initialStandaloneLabelText = useStandaloneKotlinRadioButton.getText();
kotlinStandalonePathField.setEditable(false);
kotlinStandalonePathField.addBrowseFolderListener(
"Kotlin Compiler", "Choose folder with Kotlin compiler installation", project,
new FileChooserDescriptor(false, true, false, false, false, false) {
@Override
public boolean isFileSelectable(VirtualFile file) {
if (!super.isFileSelectable(file)) {
return false;
}
return PathUtil.KOTLIN_HOME_DIRECTORY_ADAPTER.fun(com.intellij.util.PathUtil.getLocalPath(file)) != null;
}
});
kotlinStandalonePathField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(final DocumentEvent e) {
updateStandaloneVersion();
updateComponentVersion();
}
});
updateStandaloneVersion();
copyIntoDirectoryField.addBrowseFolderListener(
"Copy Into...", "Choose folder where files will be copied", project,
FileChooserDescriptorFactory.createSingleFolderDescriptor());
copyIntoDirectoryField.getTextField().setText(FileUIUtils.getDefaultLibraryFolder(project, contextDirectory));
copyLibraryCheckbox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(@NotNull ActionEvent e) {
updateComponents();
}
});
useStandaloneKotlinRadioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(@NotNull ActionEvent e) {
updateComponents();
}
});
useBundledKotlinRadioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(@NotNull ActionEvent e) {
updateComponents();
}
});
updateComponents();
}
@Nullable
public String getStandaloneCompilerPath() {
if (useStandaloneKotlinRadioButton.isSelected()) {
return kotlinStandalonePathField.getText().trim();
}
return null;
}
@Nullable
public String getCopyIntoPath() {
if (copyLibraryCheckbox.isSelected()) {
return copyIntoDirectoryField.getText().trim();
}
return null;
}
@NotNull
public String getVersion() {
if (useBundledKotlinRadioButton.isSelected()) {
return JetPluginUtil.getPluginVersion();
}
else {
assert version != null: "It shouldn't be possible to finish dialog with invalid version";
return version;
}
}
private void updateStandaloneVersion() {
if (useStandaloneKotlinRadioButton.isSelected()) {
KotlinPaths paths = PathUtil.getKotlinStandaloneCompilerPaths(kotlinStandalonePathField.getTextField().getText().trim());
try {
version = FileUtilRt.loadFile(paths.getBuildVersionFile());
return;
}
catch (IOException e) {
// Do nothing. Version will be set to null.
}
}
version = null;
}
private void updateComponentVersion() {
if (useStandaloneKotlinRadioButton.isSelected()) {
if (version != null) {
useStandaloneKotlinRadioButton.setForeground(Color.BLACK);
useStandaloneKotlinRadioButton.setText(initialStandaloneLabelText + " - " + version);
setOKActionEnabled(true);
}
else {
useStandaloneKotlinRadioButton.setForeground(Color.RED);
useStandaloneKotlinRadioButton.setText(initialStandaloneLabelText + " - " + "Invalid Version");
setOKActionEnabled(false);
}
}
else {
useStandaloneKotlinRadioButton.setForeground(Color.BLACK);
useStandaloneKotlinRadioButton.setText(initialStandaloneLabelText);
setOKActionEnabled(true);
}
}
private void updateComponents() {
kotlinStandalonePathField.setEnabled(useStandaloneKotlinRadioButton.isSelected());
copyIntoDirectoryField.setEnabled(copyLibraryCheckbox.isSelected());
updateComponentVersion();
}
@Nullable
@Override
protected JComponent createCenterPanel() {
return contentPane;
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.jet.plugin.framework.ui;
import com.intellij.ide.util.projectWizard.ProjectWizardUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.io.FileUtil;
@@ -26,6 +27,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
@@ -33,39 +35,50 @@ public class FileUIUtils {
private FileUIUtils() {
}
@NotNull
public static File copyWithOverwriteDialog(@NotNull String destinationFolder, @NotNull File file) throws IOException {
@Nullable
public static File copyWithOverwriteDialog(
@NotNull Component parent,
@NotNull String destinationFolder,
@NotNull File file,
@NotNull String messagesTitle) {
if (!ProjectWizardUtil.createDirectoryIfNotExists("Destination folder", destinationFolder, false)) {
Messages.showErrorDialog(String.format("Error during folder creating '%s'", destinationFolder), messagesTitle + ". Error");
return null;
}
File folder = new File(destinationFolder);
File targetFile = new File(folder, file.getName());
if (!folder.exists()) {
throw new IllegalArgumentException(String.format("Destination folder '%s' should exist", folder));
}
assert folder.exists();
if (!targetFile.exists()) {
FileUtil.copy(file, targetFile);
}
else {
int replaceIfExist = Messages.showYesNoDialog(
String.format("File \"%s\" is already exist in %s.\nDo you want to rewrite it?", targetFile.getName(),
folder.getAbsolutePath()),
"Replace File", Messages.getWarningIcon());
if (replaceIfExist == JOptionPane.YES_OPTION) {
try {
if (!targetFile.exists()) {
FileUtil.copy(file, targetFile);
}
else {
int replaceIfExist = Messages.showYesNoDialog(
parent,
String.format("File \"%s\" is already exist in %s.\nDo you want to rewrite it?", targetFile.getName(), folder.getAbsolutePath()),
messagesTitle + ". Replace File",
Messages.getWarningIcon());
if (replaceIfExist == JOptionPane.YES_OPTION) {
FileUtil.copy(file, targetFile);
}
}
LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetFile);
return targetFile;
}
catch (IOException e) {
Messages.showErrorDialog("Error during file copy", messagesTitle + ". Error");
return null;
}
LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetFile);
return targetFile;
}
@Nullable
public static String selectDestinationFolderDialog(
@Nullable Project project,
@Nullable VirtualFile contextDirectory,
@Nullable String description) {
@NotNull
public static String getDefaultLibraryFolder(@Nullable Project project, @Nullable VirtualFile contextDirectory) {
String path = null;
if (contextDirectory != null) {
path = PathUtil.getLocalPath(contextDirectory);
@@ -81,14 +94,6 @@ public class FileUIUtils {
else {
path = "";
}
ChoosePathDialog dialog = new ChoosePathDialog(project, "Copy Bundled Library", path, description);
dialog.show();
if (dialog.isOK()) {
return dialog.getPath();
}
return null;
return path;
}
}