summaryrefslogtreecommitdiff
path: root/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddLibraryDependencyAction.java
blob: a671153aaab494b5217890eae285d69400704d03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright 2000-2010 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 com.intellij.openapi.roots.ui.configuration.classpath;

import com.intellij.facet.impl.ProjectFacetsConfigurator;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.LibraryType;
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.util.ParameterizedRunnable;
import com.intellij.util.PlatformIcons;
import com.intellij.util.containers.Predicate;
import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* @author nik
*/
class AddLibraryDependencyAction extends AddItemPopupAction<Library> {
  private final StructureConfigurableContext myContext;

  public AddLibraryDependencyAction(ClasspathPanel classpathPanel, final int index, final String title,
                                    final StructureConfigurableContext context) {
    super(classpathPanel, index, title, PlatformIcons.LIBRARY_ICON);
    myContext = context;
  }

  @Override
  public boolean hasSubStep() {
    return !hasLibraries() && LibraryEditingUtil.hasSuitableTypes(myClasspathPanel);
  }

  @Override
  public PopupStep createSubStep() {
    return LibraryEditingUtil.createChooseTypeStep(myClasspathPanel, new ParameterizedRunnable<LibraryType>() {
      @Override
      public void run(LibraryType libraryType) {
        new AddNewLibraryDependencyAction(myClasspathPanel, myContext, libraryType).execute();
      }
    });
  }

  @Override
  public void run() {
    if (hasLibraries()) {
      super.run();
    }
    else {
      new AddNewLibraryDependencyAction(myClasspathPanel, myContext, null).run();
    }
  }

  private boolean hasLibraries() {
    final Predicate<Library> condition = getNotAddedSuitableLibrariesCondition();
    for (LibraryTable table : ChooseLibrariesFromTablesDialog.getLibraryTables(myClasspathPanel.getProject(), true)) {
      final LibrariesModifiableModel model = myContext.myLevel2Providers.get(table.getTableLevel());
      if (model != null) {
        for (Library library : model.getLibraries()) {
          if (condition.apply(library)) {
            return true;
          }
        }
      }
    }
    return false;
  }

  private Predicate<Library> getNotAddedSuitableLibrariesCondition() {
    ProjectFacetsConfigurator facetsConfigurator = myContext.getModulesConfigurator().getFacetsConfigurator();
    return LibraryEditingUtil.getNotAddedSuitableLibrariesCondition(myClasspathPanel.getRootModel(), facetsConfigurator);
  }

  @Override
  @Nullable
  protected ClasspathTableItem<?> createTableItem(final Library item) {
    // clear invalid order entry corresponding to added library if any
    final ModifiableRootModel rootModel = myClasspathPanel.getRootModel();
    final OrderEntry[] orderEntries = rootModel.getOrderEntries();
    for (OrderEntry orderEntry : orderEntries) {
      if (orderEntry instanceof LibraryOrderEntry) {
        final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)orderEntry;
        if (item.equals(libraryOrderEntry.getLibrary())) {
          return ClasspathTableItem.createLibItem(libraryOrderEntry, myContext);
        }
        String name = item.getName();
        if (name != null && name.equals(libraryOrderEntry.getLibraryName())) {
          if (orderEntry.isValid()) {
            Messages.showErrorDialog(ProjectBundle.message("classpath.message.library.already.added", item.getName()),
                                     ProjectBundle.message("classpath.title.adding.dependency"));
            return null;
          }
          else {
            rootModel.removeOrderEntry(orderEntry);
          }
        }
      }
    }
    final LibraryOrderEntry orderEntry = rootModel.addLibraryEntry(item);
    orderEntry.setScope(LibraryDependencyScopeSuggester.getDefaultScope(item));
    return ClasspathTableItem.createLibItem(orderEntry, myContext);
  }

  @Override
  protected ClasspathElementChooser<Library> createChooser() {
    return new ExistingLibraryChooser();
  }

  class ExistingLibraryChooser implements ClasspathElementChooser<Library> {
    @Override
    @NotNull
    public List<Library> chooseElements() {
      ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel, myContext,
                                                                                               getNotAddedSuitableLibrariesCondition());
      dialog.show();
      return dialog.getSelectedLibraries();
    }
  }
}