184e8c4525b71de34da62fc29bb1bbe337b27ab2
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / classpath / ProjectStructureChooseLibrariesDialog.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.classpath;
3
4 import com.intellij.ide.JavaUiBundle;
5 import com.intellij.ide.projectView.PresentationData;
6 import com.intellij.ide.util.treeView.NodeDescriptor;
7 import com.intellij.openapi.application.Application;
8 import com.intellij.openapi.project.Project;
9 import com.intellij.openapi.roots.impl.libraries.LibraryTableImplUtil;
10 import com.intellij.openapi.roots.libraries.Library;
11 import com.intellij.openapi.roots.libraries.LibraryTable;
12 import com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager;
13 import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
14 import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
15 import com.intellij.openapi.util.SystemInfo;
16 import com.intellij.ui.SimpleTextAttributes;
17 import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog;
18 import org.jetbrains.annotations.NotNull;
19 import org.jetbrains.annotations.Nullable;
20
21 import javax.swing.*;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.KeyEvent;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.function.Predicate;
29
30 public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTablesDialog {
31   private final ClasspathPanel myClasspathPanel;
32   private final StructureConfigurableContext myContext;
33   private final Predicate<Library> myAcceptedLibraries;
34   private final List<Library> myCreatedModuleLibraries = new ArrayList<>();
35   private JButton myCreateLibraryButton;
36
37   public ProjectStructureChooseLibrariesDialog(ClasspathPanel classpathPanel,
38                                                StructureConfigurableContext context,
39                                                java.util.function.Predicate<Library> acceptedLibraries) {
40     super(classpathPanel.getComponent(), "Choose Libraries", classpathPanel.getProject(), true);
41     myClasspathPanel = classpathPanel;
42     myContext = context;
43     myAcceptedLibraries = acceptedLibraries;
44     setOKButtonText(JavaUiBundle.message("button.add.selected"));
45     init();
46   }
47
48   @Override
49   protected void doOKAction() {
50     super.doOKAction();
51     removeCreatedModuleLibraries(getSelectedLibraries());
52   }
53
54   @Override
55   public void doCancelAction() {
56     super.doCancelAction();
57     removeCreatedModuleLibraries(Collections.emptyList());
58   }
59
60   private void removeCreatedModuleLibraries(Collection<Library> selected) {
61     for (Library library : myCreatedModuleLibraries) {
62       if (!selected.contains(library)) {
63         myClasspathPanel.getRootModel().getModuleLibraryTable().removeLibrary(library);
64       }
65     }
66   }
67
68   @Override
69   protected void collectChildren(Object element, List<Object> result) {
70     if (element instanceof Application && !myCreatedModuleLibraries.isEmpty()) {
71       result.add(myClasspathPanel.getRootModel().getModuleLibraryTable());
72     }
73     super.collectChildren(element, result);
74   }
75
76   @Override
77   protected Library @NotNull [] getLibraries(@NotNull LibraryTable table) {
78     if (table.getTableLevel().equals(LibraryTableImplUtil.MODULE_LEVEL)) {
79       return myCreatedModuleLibraries.toArray(Library.EMPTY_ARRAY);
80     }
81     final LibrariesModifiableModel model = getLibrariesModifiableModel(table);
82     if (model == null) return Library.EMPTY_ARRAY;
83     return model.getLibraries();
84   }
85
86   @Nullable
87   private LibrariesModifiableModel getLibrariesModifiableModel(LibraryTable table) {
88     return table != null ? myContext.myLevel2Providers.get(table.getTableLevel()) : null;
89   }
90
91   @Override
92   protected boolean acceptsElement(Object element) {
93     if (element instanceof Library) {
94       final Library library = (Library)element;
95       return myAcceptedLibraries.test(library);
96     }
97     return true;
98   }
99
100   @NotNull
101   private String getLibraryName(@NotNull Library library) {
102     final LibrariesModifiableModel model = getLibrariesModifiableModel(library.getTable());
103     if (model != null) {
104       if (model.hasLibraryEditor(library)) {
105         return model.getLibraryEditor(library).getName();
106       }
107     }
108     return library.getName();
109   }
110
111   @Override
112   protected Action @NotNull [] createActions() {
113     if (SystemInfo.isMac) {
114       return new Action[]{getCancelAction(), new CreateNewLibraryAction(), getOKAction()};
115     }
116     return new Action[]{getOKAction(), new CreateNewLibraryAction(), getCancelAction()};
117   }
118
119   @Override
120   protected JButton createJButtonForAction(Action action) {
121     final JButton button = super.createJButtonForAction(action);
122     if (action instanceof CreateNewLibraryAction) {
123       myCreateLibraryButton = button;
124     }
125     return button;
126   }
127
128   @Override
129   protected LibrariesTreeNodeBase<Library> createLibraryDescriptor(NodeDescriptor parentDescriptor,
130                                                                    Library library) {
131     final String libraryName = getLibraryName(library);
132     return new LibraryEditorDescriptor(getProject(), parentDescriptor, library, libraryName, myContext);
133   }
134
135   private static class LibraryEditorDescriptor extends LibrariesTreeNodeBase<Library> {
136     protected LibraryEditorDescriptor(final Project project, final NodeDescriptor parentDescriptor, final Library element,
137                                       String libraryName, StructureConfigurableContext context) {
138       super(project, parentDescriptor, element);
139       final PresentationData templatePresentation = getTemplatePresentation();
140       Icon icon = LibraryPresentationManager.getInstance().getNamedLibraryIcon(element, context);
141       templatePresentation.setIcon(icon);
142       templatePresentation.addText(libraryName, SimpleTextAttributes.REGULAR_ATTRIBUTES);
143     }
144   }
145
146   private final class CreateNewLibraryAction extends DialogWrapperAction {
147     private CreateNewLibraryAction() {
148       super(JavaUiBundle.message("dialog.title.new.library"));
149       putValue(MNEMONIC_KEY, KeyEvent.VK_N);
150     }
151
152     @Override
153     protected void doAction(ActionEvent e) {
154       AddNewLibraryDependencyAction.chooseTypeAndCreate(myClasspathPanel, myContext, myCreateLibraryButton,
155                                                         new AddNewLibraryDependencyAction.LibraryCreatedCallback() {
156                                                           @Override
157                                                           public void libraryCreated(@NotNull Library library) {
158                                                             if (library.getTable() == null) {
159                                                               myCreatedModuleLibraries.add(library);
160                                                             }
161                                                             queueUpdateAndSelect(library);
162                                                           }
163                                                         });
164     }
165   }
166 }