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