From eed215893faa4891029cd2cfb5c8a26c15a38a3d Mon Sep 17 00:00:00 2001 From: goodwinnk Date: Tue, 26 Feb 2013 23:55:04 +0400 Subject: [PATCH] Add an ability to create libraries from standalone compiler --- .../org/jetbrains/jet/utils/KotlinPaths.java | 3 + .../jet/utils/KotlinPathsFromHomeDir.java | 6 + .../jetbrains/jet/utils/KotlinVfsUtil.java | 23 ++- .../src/org/jetbrains/jet/utils/PathUtil.java | 62 ++++++- .../framework/JSLibraryDescription.java | 69 +++---- .../JavaRuntimeLibraryDescription.java | 70 +++---- .../plugin/framework/ui/ChoosePathDialog.java | 5 +- .../ui/CreateLibrarySourceDialog.form | 80 ++++++++ .../ui/CreateLibrarySourceDialog.java | 175 ++++++++++++++++++ .../jet/plugin/framework/ui/FileUIUtils.java | 71 +++---- 10 files changed, 454 insertions(+), 110 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibrarySourceDialog.form create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibrarySourceDialog.java diff --git a/compiler/util/src/org/jetbrains/jet/utils/KotlinPaths.java b/compiler/util/src/org/jetbrains/jet/utils/KotlinPaths.java index c5063f5e8ba..328811682dd 100644 --- a/compiler/util/src/org/jetbrains/jet/utils/KotlinPaths.java +++ b/compiler/util/src/org/jetbrains/jet/utils/KotlinPaths.java @@ -24,6 +24,9 @@ public interface KotlinPaths { @NotNull File getHomePath(); + @NotNull + File getBuildVersionFile(); + @NotNull File getLibPath(); diff --git a/compiler/util/src/org/jetbrains/jet/utils/KotlinPathsFromHomeDir.java b/compiler/util/src/org/jetbrains/jet/utils/KotlinPathsFromHomeDir.java index 40a324e8a7f..41e3d935a23 100644 --- a/compiler/util/src/org/jetbrains/jet/utils/KotlinPathsFromHomeDir.java +++ b/compiler/util/src/org/jetbrains/jet/utils/KotlinPathsFromHomeDir.java @@ -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() { diff --git a/compiler/util/src/org/jetbrains/jet/utils/KotlinVfsUtil.java b/compiler/util/src/org/jetbrains/jet/utils/KotlinVfsUtil.java index d0fa9c247b4..c83749a2428 100644 --- a/compiler/util/src/org/jetbrains/jet/utils/KotlinVfsUtil.java +++ b/compiler/util/src/org/jetbrains/jet/utils/KotlinVfsUtil.java @@ -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 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.emptyList(); + } + + private KotlinVfsUtil() {} } diff --git a/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java b/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java index 7e2af59607f..3dec81527f8 100644 --- a/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java +++ b/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java @@ -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(""); + public static final Function KOTLIN_HOME_DIRECTORY_ADAPTER = new Function() { + 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 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; diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JSLibraryDescription.java b/idea/src/org/jetbrains/jet/plugin/framework/JSLibraryDescription.java index db104c1fbea..ef9f428cd55 100644 --- a/idea/src/org/jetbrains/jet/plugin/framework/JSLibraryDescription.java +++ b/idea/src/org/jetbrains/jet/plugin/framework/JSLibraryDescription.java @@ -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. } } } diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JavaRuntimeLibraryDescription.java b/idea/src/org/jetbrains/jet/plugin/framework/JavaRuntimeLibraryDescription.java index e2a5815eee1..d13e9a1313c 100644 --- a/idea/src/org/jetbrains/jet/plugin/framework/JavaRuntimeLibraryDescription.java +++ b/idea/src/org/jetbrains/jet/plugin/framework/JavaRuntimeLibraryDescription.java @@ -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; } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/ChoosePathDialog.java b/idea/src/org/jetbrains/jet/plugin/framework/ui/ChoosePathDialog.java index 3f006d21979..c8dc4b75a69 100644 --- a/idea/src/org/jetbrains/jet/plugin/framework/ui/ChoosePathDialog.java +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/ChoosePathDialog.java @@ -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 diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibrarySourceDialog.form b/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibrarySourceDialog.form new file mode 100644 index 00000000000..cf4e02f34ed --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibrarySourceDialog.form @@ -0,0 +1,80 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibrarySourceDialog.java b/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibrarySourceDialog.java new file mode 100644 index 00000000000..e32803f4a58 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibrarySourceDialog.java @@ -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; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/FileUIUtils.java b/idea/src/org/jetbrains/jet/plugin/framework/ui/FileUIUtils.java index c39e959f07c..065e3267b78 100644 --- a/idea/src/org/jetbrains/jet/plugin/framework/ui/FileUIUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/FileUIUtils.java @@ -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; } }