From 9f640e7f139d723c13cad1cd87d34eef25ed0d60 Mon Sep 17 00:00:00 2001 From: goodwinnk Date: Tue, 26 Feb 2013 15:55:59 +0400 Subject: [PATCH] Framework initial commit --- idea/src/META-INF/plugin.xml | 3 + .../org/jetbrains/jet/plugin/JetIcons.java | 2 + .../framework/JavaScriptFrameworkType.java | 48 ++++ .../framework/JetFrameworkConfigurator.java | 31 +++ .../JetJavaFrameworkSupportProvider.java | 90 +++++++ .../framework/JetJavaFrameworkType.java | 48 ++++ .../JetJavaRuntimeLibraryDescription.java | 83 ++++++ ...JetJavaScriptFrameworkSupportProvider.java | 90 +++++++ .../JetJavaScriptLibraryDescription.java | 104 ++++++++ .../plugin/framework/ui/ChoosePathDialog.java | 72 ++++++ .../jet/plugin/framework/ui/CopyFileUtil.java | 58 +++++ .../ui/CreateLibraryFromBundledPanel.form | 61 +++++ .../ui/CreateLibraryFromBundledPanel.java | 237 ++++++++++++++++++ .../framework/ui/FrameworkSourcePanel.form | 42 ++++ .../framework/ui/FrameworkSourcePanel.java | 38 +++ .../jet/plugin/quickfix/JsModuleSetUp.java | 36 +-- .../KotlinLibrariesNotificationProvider.java | 88 +------ .../versions/KotlinRuntimeLibraryUtil.java | 148 +++++------ 18 files changed, 1096 insertions(+), 183 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/JavaScriptFrameworkType.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/JetFrameworkConfigurator.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/JetJavaFrameworkSupportProvider.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/JetJavaFrameworkType.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/JetJavaRuntimeLibraryDescription.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/JetJavaScriptFrameworkSupportProvider.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/JetJavaScriptLibraryDescription.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/ui/ChoosePathDialog.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/ui/CopyFileUtil.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibraryFromBundledPanel.form create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibraryFromBundledPanel.java create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/ui/FrameworkSourcePanel.form create mode 100644 idea/src/org/jetbrains/jet/plugin/framework/ui/FrameworkSourcePanel.java diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 14330649bbe..cd591fe9e9f 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -214,6 +214,9 @@ + + + diff --git a/idea/src/org/jetbrains/jet/plugin/JetIcons.java b/idea/src/org/jetbrains/jet/plugin/JetIcons.java index 2adc692104a..8da9364cf2e 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetIcons.java +++ b/idea/src/org/jetbrains/jet/plugin/JetIcons.java @@ -24,6 +24,8 @@ import javax.swing.*; public interface JetIcons { Icon SMALL_LOGO = IconLoader.getIcon("/org/jetbrains/jet/plugin/icons/kotlin.png"); + Icon SMALL_LOGO_13 = IconLoader.getIcon("/org/jetbrains/jet/plugin/icons/kotlin_13.png"); + Icon CLASS = IconLoader.getIcon("/org/jetbrains/jet/plugin/icons/class_kotlin.png"); Icon ENUM = IconLoader.getIcon("/org/jetbrains/jet/plugin/icons/enum_kotlin.png"); Icon FILE = IconLoader.getIcon("/org/jetbrains/jet/plugin/icons/kotlin_file.png"); diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JavaScriptFrameworkType.java b/idea/src/org/jetbrains/jet/plugin/framework/JavaScriptFrameworkType.java new file mode 100644 index 00000000000..94b7845dc20 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/JavaScriptFrameworkType.java @@ -0,0 +1,48 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.framework; + +import com.intellij.framework.FrameworkTypeEx; +import com.intellij.framework.addSupport.FrameworkSupportInModuleProvider; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.plugin.JetIcons; + +import javax.swing.*; + +public class JavaScriptFrameworkType extends FrameworkTypeEx { + public JavaScriptFrameworkType() { + super("kotlin-js-framework-id"); + } + + @NotNull + @Override + public FrameworkSupportInModuleProvider createProvider() { + return new JetJavaScriptFrameworkSupportProvider(); + } + + @NotNull + @Override + public String getPresentableName() { + return "Kotlin (JavaScript)"; + } + + @NotNull + @Override + public Icon getIcon() { + return JetIcons.SMALL_LOGO_13; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JetFrameworkConfigurator.java b/idea/src/org/jetbrains/jet/plugin/framework/JetFrameworkConfigurator.java new file mode 100644 index 00000000000..d437669a877 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/JetFrameworkConfigurator.java @@ -0,0 +1,31 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.framework; + +import com.intellij.openapi.module.Module; + +public class JetFrameworkConfigurator { + private JetFrameworkConfigurator() {} + + public static boolean configureAsJavaModule(Module module) { + return false; + } + + public static boolean configureAsJavaScriptModule(Module module) { + return false; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JetJavaFrameworkSupportProvider.java b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaFrameworkSupportProvider.java new file mode 100644 index 00000000000..a4ab39f0821 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaFrameworkSupportProvider.java @@ -0,0 +1,90 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.framework; + +import com.intellij.framework.FrameworkTypeEx; +import com.intellij.framework.addSupport.FrameworkSupportInModuleConfigurable; +import com.intellij.framework.addSupport.FrameworkSupportInModuleProvider; +import com.intellij.ide.util.frameworkSupport.FrameworkSupportModel; +import com.intellij.openapi.module.JavaModuleType; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleType; +import com.intellij.openapi.roots.ModifiableModelsProvider; +import com.intellij.openapi.roots.ModifiableRootModel; +import com.intellij.openapi.roots.ui.configuration.libraries.CustomLibraryDescription; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.plugin.framework.ui.FrameworkSourcePanel; + +import javax.swing.*; + +public class JetJavaFrameworkSupportProvider extends FrameworkSupportInModuleProvider { + @NotNull + @Override + public FrameworkTypeEx getFrameworkType() { + return FrameworkTypeEx.EP_NAME.findExtension(JetJavaFrameworkType.class); + } + + @NotNull + @Override + public FrameworkSupportInModuleConfigurable createConfigurable(@NotNull FrameworkSupportModel model) { + return new FrameworkSupportInModuleConfigurable() { + public FrameworkSourcePanel panel = null; + + @Nullable + @Override + public CustomLibraryDescription createLibraryDescription() { + return new JetJavaRuntimeLibraryDescription(panel); + } + + @Nullable + @Override + public JComponent createComponent() { + return getConfigurationPanel().getPanel(); + } + + @Override + public boolean isOnlyLibraryAdded() { + return true; + } + + @Override + public void onFrameworkSelectionChanged(boolean selected) { + getConfigurationPanel().onFrameworkSelectionChanged(selected); + } + + @Override + public void addSupport( + @NotNull Module module, + @NotNull ModifiableRootModel rootModel, + @NotNull ModifiableModelsProvider modifiableModelsProvider) { + } + + private FrameworkSourcePanel getConfigurationPanel() { + if (panel == null) { + panel = new FrameworkSourcePanel(); + } + return panel; + } + }; + } + + @Override + public boolean isEnabledForModuleType(@NotNull ModuleType moduleType) { + return moduleType instanceof JavaModuleType; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JetJavaFrameworkType.java b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaFrameworkType.java new file mode 100644 index 00000000000..af8fad70699 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaFrameworkType.java @@ -0,0 +1,48 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.framework; + +import com.intellij.framework.FrameworkTypeEx; +import com.intellij.framework.addSupport.FrameworkSupportInModuleProvider; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.plugin.JetIcons; + +import javax.swing.*; + +public class JetJavaFrameworkType extends FrameworkTypeEx { + public JetJavaFrameworkType() { + super("kotlin-java-framework-id"); + } + + @NotNull + @Override + public FrameworkSupportInModuleProvider createProvider() { + return new JetJavaFrameworkSupportProvider(); + } + + @NotNull + @Override + public String getPresentableName() { + return "Kotlin (Java)"; + } + + @NotNull + @Override + public Icon getIcon() { + return JetIcons.SMALL_LOGO_13; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JetJavaRuntimeLibraryDescription.java b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaRuntimeLibraryDescription.java new file mode 100644 index 00000000000..0ef1aac4ee0 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaRuntimeLibraryDescription.java @@ -0,0 +1,83 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.framework; + +import com.google.common.collect.Sets; +import com.intellij.framework.library.LibraryVersionProperties; +import com.intellij.openapi.fileChooser.FileChooser; +import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.roots.OrderRootType; +import com.intellij.openapi.roots.libraries.LibraryKind; +import com.intellij.openapi.roots.libraries.NewLibraryConfiguration; +import com.intellij.openapi.roots.ui.configuration.libraries.CustomLibraryDescription; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.plugin.framework.ui.FrameworkSourcePanel; +import org.jetbrains.jet.plugin.versions.KotlinRuntimeLibraryUtil; + +import javax.management.openmbean.InvalidOpenTypeException; +import javax.swing.*; +import java.util.Set; + +public class JetJavaRuntimeLibraryDescription extends CustomLibraryDescription { + public static final LibraryKind KOTLIN_KIND = LibraryKind.create("kotlin-java-runtime"); + private final FrameworkSourcePanel frameworkSourcePanel; + + public JetJavaRuntimeLibraryDescription(FrameworkSourcePanel frameworkSourcePanel) { + this.frameworkSourcePanel = frameworkSourcePanel; + } + + + @NotNull + @Override + public Set getSuitableLibraryKinds() { + return Sets.newHashSet(KOTLIN_KIND); + } + + @Nullable + @Override + public NewLibraryConfiguration createNewLibrary(@NotNull JComponent parentComponent, @Nullable VirtualFile contextDirectory) { + if (frameworkSourcePanel.isConfigureFromBundled()) { + final FileChooserDescriptor descriptor = new FileChooserDescriptor(false, true, false, false, false, false); + + descriptor.setTitle("Select Folder"); + descriptor.setDescription("Select folder where bundled Kotlin java runtime library should be copied"); + + final VirtualFile[] files = FileChooser.chooseFiles(descriptor, parentComponent, null, contextDirectory); + if (files.length == 0) { + return null; + } + + assert files.length == 1: "Only one folder is expected"; + + final VirtualFile directory = files[0]; + assert directory.isDirectory(); + + return new NewLibraryConfiguration(KotlinRuntimeLibraryUtil.LIBRARY_NAME, getDownloadableLibraryType(), new LibraryVersionProperties()) { + @Override + public void addRoots(@NotNull LibraryEditor editor) { + editor.addRoot(directory.getUrl() + "/" + KotlinRuntimeLibraryUtil.KOTLIN_RUNTIME_JAR, OrderRootType.CLASSES); + } + }; + } + else { + throw new InvalidOpenTypeException("Isn't supported yet"); + } + } +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JetJavaScriptFrameworkSupportProvider.java b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaScriptFrameworkSupportProvider.java new file mode 100644 index 00000000000..c8dedb0696e --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaScriptFrameworkSupportProvider.java @@ -0,0 +1,90 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.framework; + +import com.intellij.framework.FrameworkTypeEx; +import com.intellij.framework.addSupport.FrameworkSupportInModuleConfigurable; +import com.intellij.framework.addSupport.FrameworkSupportInModuleProvider; +import com.intellij.ide.util.frameworkSupport.FrameworkSupportModel; +import com.intellij.openapi.module.JavaModuleType; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleType; +import com.intellij.openapi.roots.ModifiableModelsProvider; +import com.intellij.openapi.roots.ModifiableRootModel; +import com.intellij.openapi.roots.ui.configuration.libraries.CustomLibraryDescription; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.plugin.framework.ui.FrameworkSourcePanel; + +import javax.swing.*; + +public class JetJavaScriptFrameworkSupportProvider extends FrameworkSupportInModuleProvider { + @NotNull + @Override + public FrameworkTypeEx getFrameworkType() { + return FrameworkTypeEx.EP_NAME.findExtension(JavaScriptFrameworkType.class); + } + + @NotNull + @Override + public FrameworkSupportInModuleConfigurable createConfigurable(@NotNull FrameworkSupportModel model) { + return new FrameworkSupportInModuleConfigurable() { + private FrameworkSourcePanel sourcePanel = null; + + @Nullable + @Override + public CustomLibraryDescription createLibraryDescription() { + return new JetJavaScriptLibraryDescription(getConfigurationPanel()); + } + + @Nullable + @Override + public JComponent createComponent() { + return getConfigurationPanel().getPanel(); + } + + @Override + public void onFrameworkSelectionChanged(boolean selected) { + getConfigurationPanel().onFrameworkSelectionChanged(selected); + } + + @Override + public boolean isOnlyLibraryAdded() { + return true; + } + + @Override + public void addSupport( + @NotNull Module module, + @NotNull ModifiableRootModel rootModel, + @NotNull ModifiableModelsProvider modifiableModelsProvider) { + } + + private FrameworkSourcePanel getConfigurationPanel() { + if (sourcePanel == null) { + sourcePanel = new FrameworkSourcePanel(); + } + return sourcePanel; + } + }; + } + + @Override + public boolean isEnabledForModuleType(@NotNull ModuleType moduleType) { + return moduleType instanceof JavaModuleType; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JetJavaScriptLibraryDescription.java b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaScriptLibraryDescription.java new file mode 100644 index 00000000000..9808f286aea --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/JetJavaScriptLibraryDescription.java @@ -0,0 +1,104 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.framework; + +import com.google.common.collect.Sets; +import com.intellij.framework.library.LibraryVersionProperties; +import com.intellij.openapi.fileChooser.FileChooser; +import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.roots.OrderRootType; +import com.intellij.openapi.roots.libraries.LibraryKind; +import com.intellij.openapi.roots.libraries.NewLibraryConfiguration; +import com.intellij.openapi.roots.ui.configuration.libraries.CustomLibraryDescription; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.plugin.framework.ui.FrameworkSourcePanel; +import org.jetbrains.jet.plugin.versions.KotlinRuntimeLibraryUtil; +import org.jetbrains.jet.utils.PathUtil; + +import javax.swing.*; +import java.io.File; +import java.io.IOException; +import java.util.Set; + +public class JetJavaScriptLibraryDescription extends CustomLibraryDescription { + public static final LibraryKind KOTLIN_JAVASCRIPT_KIND = LibraryKind.create("kotlin-js-stdlib"); + + private final FrameworkSourcePanel configurationPanel; + + public JetJavaScriptLibraryDescription(FrameworkSourcePanel configurationPanel) { + this.configurationPanel = configurationPanel; + } + + @NotNull + @Override + public Set getSuitableLibraryKinds() { + return Sets.newHashSet(KOTLIN_JAVASCRIPT_KIND); + } + + @Nullable + @Override + public NewLibraryConfiguration createNewLibrary(@NotNull JComponent parentComponent, @Nullable VirtualFile contextDirectory) { + if (configurationPanel.isConfigureFromBundled()) { + // Select folder where to copy bundled library + final FileChooserDescriptor descriptor = new FileChooserDescriptor(false, true, false, false, false, false); + descriptor.setTitle("Select Folder"); + descriptor.setDescription("Select folder where bundled runtime should be placed"); + + final VirtualFile[] files = FileChooser.chooseFiles(descriptor, parentComponent, null, contextDirectory); + if (files.length == 0) { + return null; + } + + assert files.length == 1: "Only one folder is expected"; + + final VirtualFile directory = files[0]; + assert directory.isDirectory(); + + File runtimePath = PathUtil.getKotlinPathsForIdeaPlugin().getRuntimePath(); + if (!runtimePath.exists()) { + return null; + } + + File targetJar = new File(com.intellij.util.PathUtil.getLocalPath(directory), KotlinRuntimeLibraryUtil.KOTLIN_RUNTIME_JAR); + try { + FileUtil.copy(runtimePath, targetJar); + VirtualFile jarVfs = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetJar); + if (jarVfs != null) { + jarVfs.refresh(false, false); + } + } + catch (IOException e) { + throw new RuntimeException(e); + } + + return new NewLibraryConfiguration(KotlinRuntimeLibraryUtil.LIBRARY_NAME, getDownloadableLibraryType(), new LibraryVersionProperties()) { + @Override + public void addRoots(@NotNull LibraryEditor editor) { + editor.addRoot(directory.getUrl() + "/" + KotlinRuntimeLibraryUtil.KOTLIN_RUNTIME_JAR, OrderRootType.CLASSES); + } + }; + } + else { + throw new IllegalStateException("Feature isn't ready yet"); + } + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/ChoosePathDialog.java b/idea/src/org/jetbrains/jet/plugin/framework/ui/ChoosePathDialog.java new file mode 100644 index 00000000000..aebcfa40950 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/ChoosePathDialog.java @@ -0,0 +1,72 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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; +import com.intellij.openapi.fileChooser.FileTextField; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.TextFieldWithBrowseButton; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; + +class ChoosePathDialog extends DialogWrapper { + private final Project myProject; + private final String defaultPath; + private TextFieldWithBrowseButton myPathField; + + public ChoosePathDialog( + Project project, + String title, + String defaultPath) { + super(project); + myProject = project; + this.defaultPath = defaultPath; + + setTitle(title); + init(); + } + + @Override + protected JComponent createCenterPanel() { + FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); + FileTextField field = FileChooserFactory.getInstance().createFileTextField(descriptor, myDisposable); + field.getField().setColumns(25); + + myPathField = new TextFieldWithBrowseButton(field.getField()); + myPathField.addBrowseFolderListener("Choose Destination Folder", "Choose folder", myProject, descriptor); + myPathField.setText(defaultPath); + + return myPathField; + } + + @Override + protected void doOKAction() { + if (ProjectWizardUtil.createDirectoryIfNotExists("Destination folder", getPath(), false)) { + super.doOKAction(); + } + } + + @NotNull + public String getPath() { + return myPathField.getText().trim(); + } +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/CopyFileUtil.java b/idea/src/org/jetbrains/jet/plugin/framework/ui/CopyFileUtil.java new file mode 100644 index 00000000000..0a7ac7845f6 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/CopyFileUtil.java @@ -0,0 +1,58 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.framework.ui; + +import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; +import java.io.File; +import java.io.IOException; + +public class CopyFileUtil { + private CopyFileUtil() { + } + + @NotNull + public static File copyWithOverwriteDialog(@NotNull String destinationFolder, @NotNull File file) throws IOException { + File folder = new File(destinationFolder); + File targetFile = new File(folder, file.getName()); + + // TODO: create folder if it is not present yet + assert folder.exists(); + + if (!targetFile.exists()) { + FileUtil.copy(file, targetFile); + } + else { + int replaceIfExist = Messages.showYesNoDialog( + String.format("File \"%s\" already exist in %s. Do you want to rewrite it?", targetFile.getName(), + folder.getAbsolutePath()), + "Replace File", Messages.getWarningIcon()); + + if (replaceIfExist == JOptionPane.YES_OPTION) { + FileUtil.copy(file, targetFile); + } + } + + LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetFile); + + return targetFile; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibraryFromBundledPanel.form b/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibraryFromBundledPanel.form new file mode 100644 index 00000000000..fe1fdb518d8 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibraryFromBundledPanel.form @@ -0,0 +1,61 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibraryFromBundledPanel.java b/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibraryFromBundledPanel.java new file mode 100644 index 00000000000..c3d4444c598 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/CreateLibraryFromBundledPanel.java @@ -0,0 +1,237 @@ +/* + * Copyright 2000-2012 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.jet.plugin.framework.ui; + +import com.intellij.facet.impl.ui.libraries.EditLibraryDialog; +import com.intellij.facet.impl.ui.libraries.LibraryCompositionSettings; +import com.intellij.facet.impl.ui.libraries.LibraryDownloadSettings; +import com.intellij.framework.library.DownloadableLibraryDescription; +import com.intellij.framework.library.DownloadableLibraryType; +import com.intellij.framework.library.FrameworkLibraryVersion; +import com.intellij.framework.library.FrameworkLibraryVersionFilter; +import com.intellij.ide.util.frameworkSupport.OldCustomLibraryDescription; +import com.intellij.openapi.Disposable; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManager; +import com.intellij.openapi.roots.libraries.Library; +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.libraryEditor.NewLibraryEditor; +import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer; +import com.intellij.openapi.ui.TextFieldWithBrowseButton; +import com.intellij.openapi.util.Disposer; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.ui.components.JBLabel; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class CreateLibraryFromBundledPanel implements Disposable { + private static final Logger LOG = Logger.getInstance("#com.intellij.framework.impl.ui.libraries.CreateLibraryFromBundledPanel"); + + private JBLabel myMessageLabel; + private JButton myConfigureButton; + private JPanel myConfigurationPanel; + + private TextFieldWithBrowseButton destinationFolder; + private JPanel myRootPanel; + private JLabel descriptionLabel; + + private LibraryCompositionSettings mySettings; + private final LibrariesContainer myLibrariesContainer; + private boolean myDisposed; + + public CreateLibraryFromBundledPanel( + @NotNull final CustomLibraryDescription libraryDescription, + @NotNull final String baseDirectoryPath, + @NotNull final FrameworkLibraryVersionFilter versionFilter, + @NotNull final LibrariesContainer librariesContainer, + final boolean showDoNotCreateOption + ) { + myLibrariesContainer = librariesContainer; + showSettingsPanel( + libraryDescription, baseDirectoryPath, versionFilter, showDoNotCreateOption, + new ArrayList()); + + destinationFolder.addBrowseFolderListener("Choose Destination Folder", "Choose folder for file", getProject(), + FileChooserDescriptorFactory.createSingleFolderDescriptor()); + + VirtualFile baseDir = getProject().getBaseDir(); + if (baseDir != null) { + destinationFolder.getTextField().setText(baseDir.getPath().replace('/', File.separatorChar) + File.separatorChar + "lib"); + } + } + + public void setDescription(String description) { + descriptionLabel.setText(description); + } + + @Nullable + private static DownloadableLibraryDescription getDownloadableDescription(CustomLibraryDescription libraryDescription) { + final DownloadableLibraryType type = libraryDescription.getDownloadableLibraryType(); + if (type != null) return type.getLibraryDescription(); + if (libraryDescription instanceof OldCustomLibraryDescription) { + return ((OldCustomLibraryDescription) libraryDescription).getDownloadableDescription(); + } + return null; + } + + private void showSettingsPanel( + CustomLibraryDescription libraryDescription, + String baseDirectoryPath, + FrameworkLibraryVersionFilter versionFilter, + boolean showDoNotCreateOption, final List versions + ) { + mySettings = new LibraryCompositionSettings(libraryDescription, baseDirectoryPath, versionFilter, versions); + Disposer.register(this, mySettings); + List libraries = calculateSuitableLibraries(); + + myConfigureButton.addActionListener(new ActionListener() { + public void actionPerformed(final ActionEvent e) { + doConfigure(); + } + }); + + updateState(); + } + + private void doConfigure() { + NewLibraryEditor editor = new NewLibraryEditor(); + editor.setName("Testing"); + + EditLibraryDialog dialog = new EditLibraryDialog(myConfigurationPanel, mySettings, editor); + dialog.show(); + + updateState(); + } + + public void changeBaseDirectoryPath(@NotNull String directoryForLibrariesPath) { + if (mySettings != null) { + mySettings.changeBaseDirectoryPath(directoryForLibrariesPath); + updateState(); + } + } + + public void setVersionFilter(@NotNull FrameworkLibraryVersionFilter versionFilter) { + if (mySettings != null) { + mySettings.setVersionFilter(versionFilter); + updateState(); + } + } + + private void doCreate() { + //final NewLibraryConfiguration libraryConfiguration = mySettings.getLibraryDescription().createNewLibrary(myPanel, getBaseDirectory()); + //if (libraryConfiguration != null) { + // final NewLibraryEditor libraryEditor = new NewLibraryEditor(libraryConfiguration.getLibraryType(), libraryConfiguration.getProperties()); + // libraryEditor.setName(myLibrariesContainer.suggestUniqueLibraryName(libraryConfiguration.getDefaultLibraryName())); + // libraryConfiguration.addRoots(libraryEditor); + //} + } + + private List calculateSuitableLibraries() { + final CustomLibraryDescription description = mySettings.getLibraryDescription(); + List suitableLibraries = new ArrayList(); + for (Library library : myLibrariesContainer.getAllLibraries()) { + if (description instanceof OldCustomLibraryDescription && + ((OldCustomLibraryDescription) description).isSuitableLibrary(library, myLibrariesContainer) + || + LibraryPresentationManager.getInstance() + .isLibraryOfKind(library, myLibrariesContainer, description.getSuitableLibraryKinds())) { + suitableLibraries.add(library); + } + } + return suitableLibraries; + } + + @Nullable + private VirtualFile getBaseDirectory() { + String path = mySettings.getBaseDirectoryPath(); + VirtualFile dir = LocalFileSystem.getInstance().findFileByPath(path); + if (dir == null) { + path = path.substring(0, path.lastIndexOf('/')); + dir = LocalFileSystem.getInstance().findFileByPath(path); + } + return dir; + } + + private void updateState() { + myMessageLabel.setIcon(null); + myConfigureButton.setVisible(true); + final LibraryDownloadSettings settings = mySettings.getDownloadSettings(); + + //String message = IdeBundle.message( + // "label.library.will.be.created.description.text", + // mySettings.getNewLibraryLevel(), + // libraryEditor.getName(), libraryEditor.getFiles(OrderRootType.CLASSES).length); + // + //((CardLayout)myConfigurationPanel.getLayout()).show(myConfigurationPanel, showConfigurePanel ? "configure" : "empty"); + //myMessageLabel.setText("" + message + ""); + } + + public LibraryCompositionSettings getSettings() { + return mySettings; + } + + @Nullable + public LibraryCompositionSettings apply() { + if (mySettings == null) return null; + + //final Choice option = myButtonEnumModel.getSelected(); + //mySettings.setDownloadLibraries(option == Choice.DOWNLOAD); + // + //final Object item = myExistingLibraryComboBox.getSelectedItem(); + //if (option == Choice.USE_LIBRARY && item instanceof ExistingLibraryEditor) { + // mySettings.setSelectedExistingLibrary(((ExistingLibraryEditor)item).getLibrary()); + //} + //else { + // mySettings.setSelectedExistingLibrary(null); + //} + // + //if (option == Choice.USE_LIBRARY && item instanceof NewLibraryEditor) { + // mySettings.setNewLibraryEditor((NewLibraryEditor)item); + //} + //else { + // mySettings.setNewLibraryEditor(null); + //} + return mySettings; + } + + public JComponent getMainPanel() { + return myRootPanel; + } + + @Override + public void dispose() { + myDisposed = true; + } + + private Project getProject() { + Project project = myLibrariesContainer.getProject(); + if (project == null) { + project = ProjectManager.getInstance().getDefaultProject(); + } + return project; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/FrameworkSourcePanel.form b/idea/src/org/jetbrains/jet/plugin/framework/ui/FrameworkSourcePanel.form new file mode 100644 index 00000000000..08e8098a683 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/FrameworkSourcePanel.form @@ -0,0 +1,42 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/idea/src/org/jetbrains/jet/plugin/framework/ui/FrameworkSourcePanel.java b/idea/src/org/jetbrains/jet/plugin/framework/ui/FrameworkSourcePanel.java new file mode 100644 index 00000000000..235bc59bf32 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/framework/ui/FrameworkSourcePanel.java @@ -0,0 +1,38 @@ +/* + * Copyright 2010-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.framework.ui; + +import javax.swing.*; + +public class FrameworkSourcePanel { + private JRadioButton configureFromStandaloneKotlinRadioButton; + private JRadioButton configureFromFilesBundledRadioButton; + private JPanel contentPanel; + + public JPanel getPanel() { + return contentPanel; + } + + public boolean isConfigureFromBundled() { + return configureFromFilesBundledRadioButton.isSelected(); + } + + public void onFrameworkSelectionChanged(boolean selected) { + // Don't enable for now + configureFromStandaloneKotlinRadioButton.setEnabled(false); + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/JsModuleSetUp.java b/idea/src/org/jetbrains/jet/plugin/quickfix/JsModuleSetUp.java index 9946be0f829..299d801631c 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/JsModuleSetUp.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/JsModuleSetUp.java @@ -20,15 +20,13 @@ import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; import com.intellij.notification.Notification; import com.intellij.notification.NotificationType; import com.intellij.notification.Notifications; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.module.Module; -import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.roots.OrderRootType; import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.roots.libraries.LibraryTable; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor; import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer; import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainerFactory; -import com.intellij.openapi.vfs.JarFileSystem; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiManager; @@ -66,33 +64,25 @@ public final class JsModuleSetUp { if (!copyJsLibFiles(rootDir)) return; setUpK2JSModuleComponent(module); - setUpLibraryAsSourceLibrary(module, rootDir); + createJSLibrary(module, LibrariesContainer.LibraryLevel.MODULE, rootDir); + + // FacetUtil.addFacet(module, JetFacetType.getInstance()); restartHighlightingInTheWholeProject(module); refreshRootDir(module, continuation); } - private static void setUpLibraryAsSourceLibrary(final Module module, @NotNull File rootDir) { + public static Library createJSLibrary(Module module, LibrariesContainer.LibraryLevel level, @NotNull File rootDir) { File libJarFile = new File(rootDir, "lib/" + PathUtil.JS_LIB_JAR_NAME); - final VirtualFile libFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(libJarFile); + VirtualFile libFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(libJarFile); - if (libFile != null) { - ApplicationManager.getApplication().runWriteAction(new Runnable() { - @Override - public void run() { - ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); - LibraryTable table = model.getModuleLibraryTable(); - Library jsLib = table.getLibraryByName(PathUtil.JS_LIB_JAR_NAME); - if (jsLib == null) { - VirtualFile libRoot = JarFileSystem.getInstance().getJarRootForLocalFile(libFile); - LibrariesContainerFactory.createContainer(model).createLibrary(PathUtil.JS_LIB_JAR_NAME, - LibrariesContainer.LibraryLevel.MODULE, new VirtualFile[0], new VirtualFile[] { libRoot }); - model.commit(); - } - } - }); - } + NewLibraryEditor editor = new NewLibraryEditor(); + editor.setName(PathUtil.JS_LIB_JAR_NAME); + editor.addRoot(libFile, OrderRootType.SOURCES); + + LibrariesContainer container = LibrariesContainerFactory.createContainer(module); + return container.createLibrary(editor, level); } private static void setUpK2JSModuleComponent(@NotNull Module module) { diff --git a/idea/src/org/jetbrains/jet/plugin/versions/KotlinLibrariesNotificationProvider.java b/idea/src/org/jetbrains/jet/plugin/versions/KotlinLibrariesNotificationProvider.java index 9e14d8a286e..2cbd34f4036 100644 --- a/idea/src/org/jetbrains/jet/plugin/versions/KotlinLibrariesNotificationProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/versions/KotlinLibrariesNotificationProvider.java @@ -20,10 +20,6 @@ import com.intellij.ProjectTopics; import com.intellij.icons.AllIcons; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.compiler.CompilerManager; -import com.intellij.openapi.fileChooser.FileChooserDescriptor; -import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; -import com.intellij.openapi.fileChooser.FileChooserFactory; -import com.intellij.openapi.fileChooser.FileTextField; import com.intellij.openapi.fileEditor.FileEditor; import com.intellij.openapi.fileEditor.OpenFileDescriptor; import com.intellij.openapi.module.Module; @@ -34,10 +30,6 @@ import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ModuleRootAdapter; import com.intellij.openapi.roots.ModuleRootEvent; -import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.ui.DialogWrapper; -import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.ui.popup.ListPopup; import com.intellij.openapi.ui.popup.PopupStep; @@ -52,12 +44,10 @@ import com.intellij.util.messages.MessageBusConnection; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.plugin.JetFileType; -import org.jetbrains.jet.plugin.quickfix.JsModuleSetUp; +import org.jetbrains.jet.plugin.framework.JetFrameworkConfigurator; import javax.swing.*; import java.awt.*; -import java.io.File; -import java.io.IOException; import java.text.MessageFormat; import java.util.Collection; @@ -135,31 +125,24 @@ public class KotlinLibrariesNotificationProvider extends EditorNotifications.Pro answer.createActionLabel("Set up module '" + module.getName() + "' as JVM Kotlin module", new Runnable() { @Override public void run() { - setUpJavaModule(module); + if (JetFrameworkConfigurator.configureAsJavaModule(module)) { + updateNotifications(); + } } }); answer.createActionLabel("Set up module '" + module.getName() + "' as JavaScript Kotlin module", new Runnable() { @Override public void run() { - setUpJSModule(module); + if (JetFrameworkConfigurator.configureAsJavaScriptModule(module)) { + updateNotifications(); + } } }); return answer; } - private void setUpJavaModule(Module module) { - Library library = KotlinRuntimeLibraryUtil.findOrCreateRuntimeLibrary(myProject, new UiFindRuntimeLibraryHandler()); - if (library == null) return; - - KotlinRuntimeLibraryUtil.setUpKotlinRuntimeLibrary(module, library, updateNotifications); - } - - private void setUpJSModule(@NotNull Module module) { - JsModuleSetUp.doSetUpModule(module, updateNotifications); - } - private EditorNotificationPanel createUnsupportedAbiVersionNotificationPanel(Collection badRoots) { EditorNotificationPanel answer = new ErrorNotificationPanel(); @@ -231,63 +214,6 @@ public class KotlinLibrariesNotificationProvider extends EditorNotifications.Pro }); } - private static class ChoosePathDialog extends DialogWrapper { - private final Project myProject; - private TextFieldWithBrowseButton myPathField; - - protected ChoosePathDialog(Project project) { - super(project); - myProject = project; - - setTitle("Local Kotlin Runtime Path"); - init(); - } - - @Override - protected JComponent createCenterPanel() { - FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); - FileTextField field = FileChooserFactory.getInstance().createFileTextField(descriptor, myDisposable); - field.getField().setColumns(25); - myPathField = new TextFieldWithBrowseButton(field.getField()); - myPathField.addBrowseFolderListener("Choose Destination Folder", "Choose folder for file", myProject, descriptor); - - VirtualFile baseDir = myProject.getBaseDir(); - if (baseDir != null) { - myPathField.setText(baseDir.getPath().replace('/', File.separatorChar) + File.separatorChar + "lib"); - } - - return myPathField; - } - - public String getPath() { - return myPathField.getText(); - } - } - - private class UiFindRuntimeLibraryHandler extends KotlinRuntimeLibraryUtil.FindRuntimeLibraryHandler { - @Override - public void runtimePathDoesNotExist(@NotNull File path) { - Messages.showErrorDialog(myProject, - "kotlin-runtime.jar is not found at " + path + ". Make sure plugin is properly installed.", - "No Runtime Found"); - } - - @Override - public File getRuntimeJarPath() { - ChoosePathDialog dlg = new ChoosePathDialog(myProject); - dlg.show(); - if (!dlg.isOK()) return null; - String path = dlg.getPath(); - return new File(path, "kotlin-runtime.jar"); - } - - @Override - public void ioExceptionOnCopyingJar(@NotNull IOException e) { - Messages.showErrorDialog(myProject, "Error copying jar: " + e.getLocalizedMessage(), "Error Copying File"); - } - - } - private static void navigateToLibraryRoot(Project project, @NotNull VirtualFile root) { new OpenFileDescriptor(project, root).navigate(true); } diff --git a/idea/src/org/jetbrains/jet/plugin/versions/KotlinRuntimeLibraryUtil.java b/idea/src/org/jetbrains/jet/plugin/versions/KotlinRuntimeLibraryUtil.java index c588b7a4e77..f24b12f3d03 100644 --- a/idea/src/org/jetbrains/jet/plugin/versions/KotlinRuntimeLibraryUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/versions/KotlinRuntimeLibraryUtil.java @@ -25,16 +25,16 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.projectRoots.SdkModificator; -import com.intellij.openapi.roots.*; -import com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable; +import com.intellij.openapi.roots.AnnotationOrderRootType; +import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.roots.OrderRootType; +import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.libraries.LibraryTable; -import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.JarFileSystem; import com.intellij.openapi.vfs.LocalFileSystem; -import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.packaging.impl.elements.ManifestFileUtil; import com.intellij.psi.JavaPsiFacade; @@ -45,6 +45,7 @@ import com.intellij.util.Processor; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.indexing.FileBasedIndex; import com.intellij.util.indexing.ID; +import com.intellij.util.text.UniqueNameGenerator; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.resolve.java.AbiVersionUtil; @@ -55,7 +56,8 @@ import org.jetbrains.jet.utils.PathUtil; import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.Collection; +import java.util.Set; import java.util.jar.Attributes; import java.util.jar.JarFile; @@ -173,76 +175,64 @@ public class KotlinRuntimeLibraryUtil { return null; } - static void setUpKotlinRuntimeLibrary( - @NotNull final Module module, - @NotNull final Library library, - @NotNull final Runnable afterSetUp - ) { - ApplicationManager.getApplication().runWriteAction(new Runnable() { - @Override - public void run() { - ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); - if (model.findLibraryOrderEntry(library) == null) { - model.addLibraryEntry(library); - model.commit(); - } - else { - model.dispose(); - } + public static boolean isLibraryCanBeUsedAsJavaRuntime(@Nullable Library library) { + if (library == null) { + return false; + } - afterSetUp.run(); - - if (!jdkAnnotationsArePresent(module)) { - addJdkAnnotations(module); - } + for (VirtualFile root : library.getFiles(OrderRootType.CLASSES)) { + if (root.getName().equals(KOTLIN_RUNTIME_JAR)) { + return true; } - }); + } + + return false; } - @Nullable - static Library findOrCreateRuntimeLibrary(@NotNull Project project, @NotNull FindRuntimeLibraryHandler handler) { - final LibraryTable table = ProjectLibraryTable.getInstance(project); - final Library kotlinRuntime = table.getLibraryByName(LIBRARY_NAME); - if (kotlinRuntime != null) { - for (VirtualFile root : kotlinRuntime.getFiles(OrderRootType.CLASSES)) { - if (root.getName().equals(KOTLIN_RUNTIME_JAR)) { - return kotlinRuntime; - } - } - } + //@Nullable + //public static Library createRuntimeLibrary(final LibraryTable table, @NotNull final String libName, FindRuntimeLibraryHandler handler) { + // File runtimePath = PathUtil.getKotlinPathsForIdeaPlugin().getRuntimePath(); + // if (!runtimePath.exists()) { + // handler.runtimePathDoesNotExist(runtimePath); + // return null; + // } + // + // final File targetJar = handler.getRuntimeJarPath(); + // if (targetJar == null) return null; + // try { + // FileUtil.copy(runtimePath, targetJar); + // VirtualFile jarVfs = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetJar); + // if (jarVfs != null) { + // jarVfs.refresh(false, false); + // } + // } + // catch (IOException e) { + // handler.ioExceptionOnCopyingJar(e); + // return null; + // } + // + // + // return ApplicationManager.getApplication().runWriteAction(new Computable() { + // @Override + // public Library compute() { + // LibraryTableBase.ModifiableModel modifiableModel = table.getModifiableModel(); + // final String name = getUniqueLibraryName(libName, modifiableModel); + // Library library = modifiableModel.createLibrary(name); + // final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx)library.getModifiableModel(); + // model.addRoot(VfsUtil.getUrlForLibraryRoot(targetJar), OrderRootType.CLASSES); + // model.addRoot(VfsUtil.getUrlForLibraryRoot(targetJar) + "src", OrderRootType.SOURCES); + // model.commit(); + // modifiableModel.commit(); + // return library; + // } + // }); + //} - File runtimePath = PathUtil.getKotlinPathsForIdeaPlugin().getRuntimePath(); - if (!runtimePath.exists()) { - handler.runtimePathDoesNotExist(runtimePath); - return null; - } - - final File targetJar = handler.getRuntimeJarPath(); - if (targetJar == null) return null; - try { - FileUtil.copy(runtimePath, targetJar); - VirtualFile jarVfs = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetJar); - if (jarVfs != null) { - jarVfs.refresh(false, false); - } - } - catch (IOException e) { - handler.ioExceptionOnCopyingJar(e); - return null; - } - - return ApplicationManager.getApplication().runWriteAction(new Computable() { + private static String getUniqueLibraryName(final String baseName, final LibraryTable.ModifiableModel model) { + return UniqueNameGenerator.generateUniqueName(baseName, "", "", " (", ")", new Condition() { @Override - public Library compute() { - Library result = kotlinRuntime == null - ? table.createLibrary("KotlinRuntime") - : kotlinRuntime; - - Library.ModifiableModel model = result.getModifiableModel(); - model.addRoot(VfsUtil.getUrlForLibraryRoot(targetJar), OrderRootType.CLASSES); - model.addRoot(VfsUtil.getUrlForLibraryRoot(targetJar) + "src", OrderRootType.SOURCES); - model.commit(); - return result; + public boolean value(String s) { + return model.getLibraryByName(s) == null; } }); } @@ -312,14 +302,14 @@ public class KotlinRuntimeLibraryUtil { return kotlinRuntimeJar; } - public static abstract class FindRuntimeLibraryHandler { - @Nullable - public abstract File getRuntimeJarPath(); - - public void runtimePathDoesNotExist(@NotNull File path) { - } - - public void ioExceptionOnCopyingJar(@NotNull IOException e) { - } - } + //public static abstract class FindRuntimeLibraryHandler { + // @Nullable + // public abstract File getRuntimeJarPath(); + // + // public void runtimePathDoesNotExist(@NotNull File path) { + // } + // + // public void ioExceptionOnCopyingJar(@NotNull IOException e) { + // } + //} }