3f5ae43edb170591295ce4d66bbd72d0d64d1589
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / libraryEditor / CreateNewLibraryAction.java
1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.openapi.roots.ui.configuration.libraryEditor;
3
4 import com.intellij.ide.JavaUiBundle;
5 import com.intellij.openapi.actionSystem.AnAction;
6 import com.intellij.openapi.actionSystem.AnActionEvent;
7 import com.intellij.openapi.application.WriteAction;
8 import com.intellij.openapi.project.DumbAwareAction;
9 import com.intellij.openapi.project.Project;
10 import com.intellij.openapi.roots.impl.libraries.LibraryEx;
11 import com.intellij.openapi.roots.libraries.Library;
12 import com.intellij.openapi.roots.libraries.LibraryType;
13 import com.intellij.openapi.roots.libraries.LibraryTypeService;
14 import com.intellij.openapi.roots.libraries.NewLibraryConfiguration;
15 import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable;
16 import com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil;
17 import com.intellij.openapi.roots.ui.configuration.projectRoot.BaseLibrariesConfigurable;
18 import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
19 import com.intellij.openapi.roots.ui.configuration.projectRoot.ModuleStructureConfigurable;
20 import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectLibrariesConfigurable;
21 import com.intellij.openapi.ui.MasterDetailsComponent;
22 import com.intellij.openapi.vfs.VirtualFile;
23 import com.intellij.util.PlatformIcons;
24 import org.jetbrains.annotations.NotNull;
25 import org.jetbrains.annotations.Nullable;
26
27 import javax.swing.*;
28 import javax.swing.tree.DefaultMutableTreeNode;
29 import javax.swing.tree.TreeNode;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33
34 public final class CreateNewLibraryAction extends DumbAwareAction {
35   private final @Nullable LibraryType myType;
36   private final BaseLibrariesConfigurable myLibrariesConfigurable;
37   private final Project myProject;
38
39   private CreateNewLibraryAction(@NotNull String text, @Nullable Icon icon, @Nullable LibraryType type, @NotNull BaseLibrariesConfigurable librariesConfigurable, final @NotNull Project project) {
40     super(text, null, icon);
41     myType = type;
42     myLibrariesConfigurable = librariesConfigurable;
43     myProject = project;
44   }
45
46   @Nullable
47   public static Library createLibrary(@Nullable final LibraryType type, @NotNull final JComponent parentComponent,
48                                       @NotNull final Project project, @NotNull final LibrariesModifiableModel modifiableModel) {
49     final NewLibraryConfiguration configuration = createNewLibraryConfiguration(type, parentComponent, project);
50     if (configuration == null) return null;
51     final LibraryType<?> libraryType = configuration.getLibraryType();
52     final Library library = modifiableModel.createLibrary(
53       LibraryEditingUtil.suggestNewLibraryName(modifiableModel, configuration.getDefaultLibraryName()),
54       libraryType != null ? libraryType.getKind() : null);
55
56     final NewLibraryEditor editor = new NewLibraryEditor(libraryType, configuration.getProperties());
57     configuration.addRoots(editor);
58     final Library.ModifiableModel model = library.getModifiableModel();
59     editor.applyTo((LibraryEx.ModifiableModelEx)model);
60     WriteAction.run(() -> model.commit());
61     return library;
62   }
63
64   @Nullable
65   public static NewLibraryConfiguration createNewLibraryConfiguration(@Nullable LibraryType type, @NotNull JComponent parentComponent, @NotNull Project project) {
66     final NewLibraryConfiguration configuration;
67     final VirtualFile baseDir = project.getBaseDir();
68     if (type != null) {
69       configuration = type.createNewLibrary(parentComponent, baseDir, project);
70     }
71     else {
72       configuration = LibraryTypeService
73         .getInstance().createLibraryFromFiles(new DefaultLibraryRootsComponentDescriptor(), parentComponent, baseDir, null, project);
74     }
75     if (configuration == null) return null;
76     return configuration;
77   }
78
79   public static AnAction[] createActionOrGroup(@NotNull String text, @NotNull BaseLibrariesConfigurable librariesConfigurable, final @NotNull Project project) {
80     final LibraryType<?>[] extensions = LibraryType.EP_NAME.getExtensions();
81     List<LibraryType<?>> suitableTypes = new ArrayList<>();
82     if (librariesConfigurable instanceof ProjectLibrariesConfigurable || !project.isDefault()) {
83       final ModuleStructureConfigurable configurable = ModuleStructureConfigurable.getInstance(project);
84       for (LibraryType<?> extension : extensions) {
85         if (!LibraryEditingUtil.getSuitableModules(configurable, extension.getKind(), null).isEmpty()) {
86           suitableTypes.add(extension);
87         }
88       }
89     }
90     else {
91       Collections.addAll(suitableTypes, extensions);
92     }
93
94     if (suitableTypes.isEmpty()) {
95       return new AnAction[]{new CreateNewLibraryAction(text, PlatformIcons.LIBRARY_ICON, null, librariesConfigurable, project)};
96     }
97     List<AnAction> actions = new ArrayList<>();
98     actions.add(new CreateNewLibraryAction(JavaUiBundle.message("create.default.library.type.action.name"), PlatformIcons.LIBRARY_ICON, null, librariesConfigurable, project));
99     for (LibraryType<?> type : suitableTypes) {
100       final String actionName = type.getCreateActionName();
101       if (actionName != null) {
102         actions.add(new CreateNewLibraryAction(actionName, type.getIcon(null), type, librariesConfigurable, project));
103       }
104     }
105     return actions.toArray(AnAction.EMPTY_ARRAY);
106   }
107
108   @Override
109   public void actionPerformed(@NotNull AnActionEvent e) {
110     Library library =
111       createLibrary(myType, myLibrariesConfigurable.getTree(), myProject, myLibrariesConfigurable.getModelProvider().getModifiableModel());
112     if (library == null) return;
113
114     final BaseLibrariesConfigurable rootConfigurable = ProjectStructureConfigurable.getInstance(myProject).getConfigurableFor(library);
115     final DefaultMutableTreeNode
116       libraryNode = MasterDetailsComponent.findNodeByObject((TreeNode)rootConfigurable.getTree().getModel().getRoot(), library);
117     rootConfigurable.selectNodeInTree(libraryNode);
118     LibraryEditingUtil.showDialogAndAddLibraryToDependencies(library, myProject, true);
119   }
120 }