Search in sources :

Example 1 with DefaultLibraryRootsComponentDescriptor

use of com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor in project azure-tools-for-java by Microsoft.

the class SparkLibraryDescription method getSparkSDKConfigurationFromLocalFile.

private NewLibraryConfiguration getSparkSDKConfigurationFromLocalFile() {
    FileChooserDescriptor chooserDescriptor = new FileChooserDescriptor(false, false, true, false, true, false);
    chooserDescriptor.setTitle("Select Spark SDK");
    String pluginPath = PluginUtil.getPluginRootDirectory();
    VirtualFile pluginVfs = LocalFileSystem.getInstance().findFileByPath(pluginPath);
    VirtualFile chooseFile = FileChooser.chooseFile(chooserDescriptor, null, pluginVfs);
    if (chooseFile == null) {
        return null;
    }
    this.localPath = chooseFile.getPath();
    final List<OrderRoot> roots = RootDetectionUtil.detectRoots(Arrays.asList(chooseFile), null, null, new DefaultLibraryRootsComponentDescriptor());
    if (roots.isEmpty()) {
        return null;
    }
    return new NewLibraryConfiguration(LibraryTypeServiceImpl.suggestLibraryName(roots), SparkLibraryType.getInstance(), new SparkLibraryProperties()) {

        @Override
        public void addRoots(@NotNull LibraryEditor libraryEditor) {
            libraryEditor.addRoots(roots);
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DefaultLibraryRootsComponentDescriptor(com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor) LibraryEditor(com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot) NotNull(org.jetbrains.annotations.NotNull) NewLibraryConfiguration(com.intellij.openapi.roots.libraries.NewLibraryConfiguration)

Example 2 with DefaultLibraryRootsComponentDescriptor

use of com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor in project android by JetBrains.

the class CreateLibraryFromFilesAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final Project project = getEventProject(e);
    if (project == null)
        return;
    if (!AndroidProjectInfo.getInstance(project).requiresAndroidModel()) {
        myDelegate.actionPerformed(e);
        return;
    }
    final List<VirtualFile> jars = getRoots(e);
    if (jars.isEmpty()) {
        return;
    }
    final List<OrderRoot> roots = RootDetectionUtil.detectRoots(jars, null, project, new DefaultLibraryRootsComponentDescriptor());
    new CreateGradleLibraryFromFilesDialog(project, roots).show();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) DefaultLibraryRootsComponentDescriptor(com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot)

Example 3 with DefaultLibraryRootsComponentDescriptor

use of com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor in project intellij-community by JetBrains.

the class MarkLibraryRootAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = getEventProject(e);
    if (project == null)
        return;
    final List<VirtualFile> jars = getRoots(e);
    if (jars.isEmpty())
        return;
    final List<OrderRoot> roots = RootDetectionUtil.detectRoots(jars, null, project, new DefaultLibraryRootsComponentDescriptor());
    new CreateLibraryFromFilesDialog(project, roots).show();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) DefaultLibraryRootsComponentDescriptor(com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot)

Example 4 with DefaultLibraryRootsComponentDescriptor

use of com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor in project azure-tools-for-java by Microsoft.

the class ManifestFileUtilsEx method selectMainClass.

@Nullable
public PsiClass selectMainClass(@Nullable VirtualFile jarFile) {
    if (jarFile == null) {
        return null;
    }
    final TreeClassChooserFactory chooserFactory = TreeClassChooserFactory.getInstance(myProject);
    final GlobalSearchScope searchScope = GlobalSearchScope.everythingScope(myProject);
    // TODO: the following code is used to find initialClassName in the jar. When user specified initialClassName
    // in the main-class-selection textfield and clicked the main-class-selection button, the filter result in the dialog
    // should be the initialClass. Currently we don't enable this method since exception happens with the following code.
    // final PsiClass aClass = initialClassName != null ? JavaPsiFacade.getInstance(project).findClass(initialClassName, searchScope) : null;
    final TreeClassChooser chooser = chooserFactory.createWithInnerClassesScopeChooser("Select Main Class", searchScope, new MainClassFilter(jarFile.getPath()), null);
    ((TreeJavaClassChooserDialog) chooser).getWindow().addWindowListener(new WindowAdapter() {

        // These fields are recorded to help remove the artifact and the module.
        @Nullable
        private String localArtifactLibraryName;

        @Nullable
        private String localArtifactModuleName;

        @Override
        public void windowOpened(WindowEvent e) {
            // remove old jar and add new jar to project dependency
            WriteAction.run(() -> addJarToModuleDependency(jarFile));
            super.windowOpened(e);
        }

        @Override
        public void windowClosed(WindowEvent e) {
            WriteAction.run(() -> removeModuleAndJar());
            super.windowClosed(e);
        }

        private void addJarToModuleDependency(@NotNull VirtualFile jarFile) {
            try {
                final Module module = createJarPackingModule();
                final List<OrderRoot> myRoots = RootDetectionUtil.detectRoots(Arrays.asList(jarFile), null, myProject, new DefaultLibraryRootsComponentDescriptor());
                final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
                // find a unique library name in the module
                final String libraryName = LibraryEditingUtil.suggestNewLibraryName(modifiableModel.getModuleLibraryTable().getModifiableModel(), jarFile.getName());
                // add library to the model of the new-created module
                LibrariesContainerFactory.createContainer(modifiableModel).createLibrary(libraryName, LibrariesContainer.LibraryLevel.MODULE, myRoots);
                modifiableModel.commit();
                localArtifactModuleName = module.getName();
                localArtifactLibraryName = libraryName;
            } catch (Exception ex) {
                log().warn(String.format("Failed to add the user selected jar(%s) into module dependency: %s", jarFile.getPath(), ex.toString()));
            }
        }

        private void removeModuleAndJar() {
            assert (localArtifactLibraryName != null && localArtifactModuleName != null) : String.format("Can't get module name or library name. module:%s, library:%s", localArtifactModuleName, localArtifactLibraryName);
            try {
                final Module module = ModuleManager.getInstance(myProject).findModuleByName(localArtifactModuleName);
                // remove library from the model of the module
                final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
                modifiableModel.getModuleLibraryTable().removeLibrary(modifiableModel.getModuleLibraryTable().getLibraryByName(localArtifactLibraryName));
                modifiableModel.commit();
                // remove module from project
                ModuleManager.getInstance(myProject).disposeModule(module);
                localArtifactModuleName = null;
                localArtifactLibraryName = null;
            } catch (Exception ex) {
                log().warn(String.format("Failed to remove jar(%s) from module(%s): %s", localArtifactLibraryName, localArtifactModuleName, ex.toString()));
            }
        }
    });
    chooser.showDialog();
    return chooser.getSelected();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TreeClassChooser(com.intellij.ide.util.TreeClassChooser) DefaultLibraryRootsComponentDescriptor(com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor) WindowAdapter(java.awt.event.WindowAdapter) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) TreeClassChooserFactory(com.intellij.ide.util.TreeClassChooserFactory) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) WindowEvent(java.awt.event.WindowEvent) List(java.util.List) Module(com.intellij.openapi.module.Module) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable)

Aggregations

DefaultLibraryRootsComponentDescriptor (com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)3 Project (com.intellij.openapi.project.Project)2 TreeClassChooser (com.intellij.ide.util.TreeClassChooser)1 TreeClassChooserFactory (com.intellij.ide.util.TreeClassChooserFactory)1 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)1 Module (com.intellij.openapi.module.Module)1 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)1 NewLibraryConfiguration (com.intellij.openapi.roots.libraries.NewLibraryConfiguration)1 LibraryEditor (com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 Nullable (com.microsoft.azuretools.azurecommons.helpers.Nullable)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)1