diff --git a/java/idea-ui/idea-ui.iml b/java/idea-ui/idea-ui.iml
index 4a549c503788..4e561138bc52 100644
--- a/java/idea-ui/idea-ui.iml
+++ b/java/idea-ui/idea-ui.iml
@@ -13,6 +13,7 @@
+
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddLibraryAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddLibraryAction.java
index bdfb56a66e15..1c4103c6bb04 100644
--- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddLibraryAction.java
+++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddLibraryAction.java
@@ -15,23 +15,21 @@
*/
package com.intellij.openapi.roots.ui.configuration.classpath;
+import com.google.common.base.Predicate;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.roots.LibraryOrderEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.OrderEntry;
-import com.intellij.openapi.roots.impl.libraries.LibraryImpl;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
+import com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil;
import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
import com.intellij.openapi.ui.Messages;
-import com.intellij.openapi.util.Condition;
import com.intellij.util.Icons;
import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog;
import org.jetbrains.annotations.Nullable;
-import java.util.HashSet;
import java.util.List;
-import java.util.Set;
/**
* @author nik
@@ -58,10 +56,10 @@ class AddLibraryAction extends AddItemPopupAction {
}
private boolean hasLibraries() {
- final Condition condition = getNotAddedLibrariesCondition();
+ final Predicate condition = LibraryEditingUtil.getNotAddedLibrariesCondition(myClasspathPanel.getRootModel());
for (LibraryTable table : ChooseLibrariesFromTablesDialog.getLibraryTables(myClasspathPanel.getProject(), true)) {
for (Library library : table.getLibraries()) {
- if (condition.value(library)) {
+ if (condition.apply(library)) {
return true;
}
}
@@ -94,31 +92,6 @@ class AddLibraryAction extends AddItemPopupAction {
return new ExistingLibraryChooser();
}
- private Condition getNotAddedLibrariesCondition() {
- final OrderEntry[] orderEntries = myClasspathPanel.getRootModel().getOrderEntries();
- final Set result = new HashSet(orderEntries.length);
- for (OrderEntry orderEntry : orderEntries) {
- if (orderEntry instanceof LibraryOrderEntry && orderEntry.isValid()) {
- final LibraryImpl library = (LibraryImpl)((LibraryOrderEntry)orderEntry).getLibrary();
- if (library != null) {
- final Library source = library.getSource();
- result.add(source != null ? source : library);
- }
- }
- }
- return new Condition() {
- @Override
- public boolean value(Library library) {
- if (result.contains(library)) return false;
- if (library instanceof LibraryImpl) {
- final Library source = ((LibraryImpl)library).getSource();
- if (source != null && result.contains(source)) return false;
- }
- return true;
- }
- };
- }
-
class ExistingLibraryChooser implements ClasspathElementChooser {
private List mySelectedLibraries;
@@ -127,8 +100,9 @@ class AddLibraryAction extends AddItemPopupAction {
}
public void doChoose() {
+ final Predicate condition = LibraryEditingUtil.getNotAddedLibrariesCondition(myClasspathPanel.getRootModel());
ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel.getComponent(), myClasspathPanel.getProject(), myContext,
- getNotAddedLibrariesCondition(), myNewLibraryAction);
+ condition, myNewLibraryAction);
dialog.show();
mySelectedLibraries = dialog.getSelectedLibraries();
}
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ProjectStructureChooseLibrariesDialog.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ProjectStructureChooseLibrariesDialog.java
index 4af2475d5b4a..33d4d136749b 100644
--- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ProjectStructureChooseLibrariesDialog.java
+++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ProjectStructureChooseLibrariesDialog.java
@@ -15,6 +15,7 @@
*/
package com.intellij.openapi.roots.ui.configuration.classpath;
+import com.google.common.base.Predicate;
import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.util.treeView.NodeDescriptor;
import com.intellij.openapi.project.Project;
@@ -23,7 +24,6 @@ import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
-import com.intellij.openapi.util.Condition;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog;
import org.jetbrains.annotations.NotNull;
@@ -38,13 +38,13 @@ import java.awt.event.KeyEvent;
*/
public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTablesDialog {
private StructureConfigurableContext myContext;
- private Condition myAcceptedLibraries;
+ private Predicate myAcceptedLibraries;
private AddNewLibraryItemAction myNewLibraryAction;
public ProjectStructureChooseLibrariesDialog(JComponent parentComponent,
@Nullable Project project,
StructureConfigurableContext context,
- Condition acceptedLibraries, AddNewLibraryItemAction newLibraryAction) {
+ Predicate acceptedLibraries, AddNewLibraryItemAction newLibraryAction) {
super(parentComponent, "Choose Libraries", project, true);
myContext = context;
myAcceptedLibraries = acceptedLibraries;
@@ -69,7 +69,7 @@ public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTa
protected boolean acceptsElement(Object element) {
if (element instanceof Library) {
final Library library = (Library)element;
- return myAcceptedLibraries.value(library);
+ return myAcceptedLibraries.apply(library);
}
return true;
}
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraries/CreateCustomLibraryAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraries/CreateCustomLibraryAction.java
index deb556a705b2..617d5c51969f 100644
--- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraries/CreateCustomLibraryAction.java
+++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraries/CreateCustomLibraryAction.java
@@ -15,6 +15,8 @@
*/
package com.intellij.openapi.roots.ui.configuration.libraries;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
@@ -90,14 +92,16 @@ public class CreateCustomLibraryAction extends DumbAwareAction {
for (CustomLibraryCreator creator : CustomLibraryCreator.EP_NAME.getExtensions()) {
final HashSet> kinds = new HashSet>(creator.getSuitableKinds());
List suitableLibraries = LibraryPresentationManager.getInstance().getLibraries(kinds, context.getProject(), context);
- if (suitableLibraries.isEmpty()) {
+ final Predicate notAddedLibrariesCondition = LibraryEditingUtil.getNotAddedLibrariesCondition(context.getModulesConfigurator().getRootModel(module));
+ final Collection librariesToAdd = Collections2.filter(suitableLibraries, notAddedLibrariesCondition);
+ if (librariesToAdd.isEmpty()) {
actions.add(new CreateCustomLibraryAction(creator.getDisplayName(), creator, context, moduleStructureConfigurable, module));
}
else {
final DefaultActionGroup group = new DefaultActionGroup(creator.getDisplayName(), true);
group.getTemplatePresentation().setIcon(creator.getIcon());
group.add(new CreateCustomLibraryAction("New...", creator, context, moduleStructureConfigurable, module));
- for (Library library : suitableLibraries) {
+ for (Library library : librariesToAdd) {
Icon icon = LibraryPresentationManager.getInstance().getNamedLibraryIcon(library, context);
group.add(new AddExistingCustomLibraryAction(library, icon, context, moduleStructureConfigurable, module));
}
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraries/LibraryEditingUtil.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraries/LibraryEditingUtil.java
new file mode 100644
index 000000000000..638c39b250c7
--- /dev/null
+++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraries/LibraryEditingUtil.java
@@ -0,0 +1,86 @@
+/*
+ * 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.libraries;
+
+import com.google.common.base.Predicate;
+import com.intellij.openapi.roots.LibraryOrderEntry;
+import com.intellij.openapi.roots.ModuleRootModel;
+import com.intellij.openapi.roots.OrderEntry;
+import com.intellij.openapi.roots.impl.libraries.LibraryImpl;
+import com.intellij.openapi.roots.libraries.Library;
+import com.intellij.openapi.roots.libraries.LibraryTable;
+import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * @author nik
+ */
+public class LibraryEditingUtil {
+ private LibraryEditingUtil() {
+ }
+
+ public static boolean libraryAlreadyExists(LibraryTable.ModifiableModel table, String libraryName) {
+ for (Iterator it = table.getLibraryIterator(); it.hasNext(); ) {
+ final Library library = it.next();
+ final String libName;
+ if (table instanceof LibrariesModifiableModel){
+ libName = ((LibrariesModifiableModel)table).getLibraryEditor(library).getName();
+ }
+ else {
+ libName = library.getName();
+ }
+ if (libraryName.equals(libName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static String suggestNewLibraryName(LibraryTable.ModifiableModel table) {
+ final String name = "Unnamed";
+ String candidataName = name;
+ for (int idx = 1; libraryAlreadyExists(table, candidataName); candidataName = name + (idx++));
+ return candidataName;
+ }
+
+ public static Predicate getNotAddedLibrariesCondition(final ModuleRootModel rootModel) {
+ final OrderEntry[] orderEntries = rootModel.getOrderEntries();
+ final Set result = new HashSet(orderEntries.length);
+ for (OrderEntry orderEntry : orderEntries) {
+ if (orderEntry instanceof LibraryOrderEntry && orderEntry.isValid()) {
+ final LibraryImpl library = (LibraryImpl)((LibraryOrderEntry)orderEntry).getLibrary();
+ if (library != null) {
+ final Library source = library.getSource();
+ result.add(source != null ? source : library);
+ }
+ }
+ }
+ return new Predicate() {
+ @Override
+ public boolean apply(Library library) {
+ if (result.contains(library)) return false;
+ if (library instanceof LibraryImpl) {
+ final Library source = ((LibraryImpl)library).getSource();
+ if (source != null && result.contains(source)) return false;
+ }
+ return true;
+ }
+ };
+ }
+}
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/CreateNewLibraryAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/CreateNewLibraryAction.java
index 3c0fbe8827e0..414d42d6f6fd 100644
--- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/CreateNewLibraryAction.java
+++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/CreateNewLibraryAction.java
@@ -23,6 +23,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable;
+import com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil;
import com.intellij.openapi.roots.ui.configuration.projectRoot.BaseLibrariesConfigurable;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
import com.intellij.openapi.roots.ui.configuration.projectRoot.ModuleStructureConfigurable;
@@ -55,7 +56,7 @@ public class CreateNewLibraryAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent e) {
final LibrariesModifiableModel modifiableModel = myModelProvider.getModifiableModel();
- final String initial = LibraryRootsComponent.suggestNewLibraryName(modifiableModel);
+ final String initial = LibraryEditingUtil.suggestNewLibraryName(modifiableModel);
final String prompt = ProjectBundle.message("library.name.prompt");
final String title = ProjectBundle.message("library.create.library.action").replaceAll(String.valueOf(UIUtil.MNEMONIC), "");
final Icon icon = Messages.getQuestionIcon();
@@ -68,7 +69,7 @@ public class CreateNewLibraryAction extends AnAction {
Messages.showErrorDialog(ProjectBundle.message("library.name.not.specified.error"), ProjectBundle.message("library.name.not.specified.title"));
return false;
}
- if (LibraryRootsComponent.libraryAlreadyExists(modifiableModel, inputString)) {
+ if (LibraryEditingUtil.libraryAlreadyExists(modifiableModel, inputString)) {
Messages.showErrorDialog(ProjectBundle.message("library.name.already.exists.error", inputString), ProjectBundle.message("library.name.already.exists.title"));
return false;
}
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryEditorDialogBase.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryEditorDialogBase.java
index bb9fc847f5c9..d99235ffaec7 100644
--- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryEditorDialogBase.java
+++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryEditorDialogBase.java
@@ -19,6 +19,7 @@ import com.intellij.openapi.actionSystem.DataKey;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.roots.impl.ModuleLibraryTable;
import com.intellij.openapi.roots.libraries.LibraryTable;
+import com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Comparing;
@@ -75,7 +76,7 @@ public abstract class LibraryEditorDialogBase extends DialogWrapper {
Messages.showErrorDialog(ProjectBundle.message("library.name.not.specified.error", newName), ProjectBundle.message("library.name.not.specified.title"));
return false;
}
- if (LibraryRootsComponent.libraryAlreadyExists(tableModifiableModel, newName)) {
+ if (LibraryEditingUtil.libraryAlreadyExists(tableModifiableModel, newName)) {
Messages.showErrorDialog(ProjectBundle.message("library.name.already.exists.error", newName), ProjectBundle.message("library.name.already.exists.title"));
return false;
}
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryRootsComponent.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryRootsComponent.java
index 5d9ac32c6f1a..5245fdff9023 100644
--- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryRootsComponent.java
+++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryRootsComponent.java
@@ -32,12 +32,10 @@ import com.intellij.openapi.roots.JavadocOrderRootType;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.impl.libraries.LibraryImpl;
import com.intellij.openapi.roots.libraries.Library;
-import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.LibraryUtil;
import com.intellij.openapi.roots.ui.configuration.ModuleEditor;
import com.intellij.openapi.roots.ui.configuration.PathUIUtils;
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager;
-import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
import com.intellij.openapi.roots.ui.configuration.projectRoot.ModuleStructureConfigurable;
import com.intellij.openapi.ui.ex.MultiLineLabel;
import com.intellij.openapi.ui.popup.JBPopupFactory;
@@ -134,30 +132,6 @@ public class LibraryRootsComponent implements Disposable {
return createComponent(null, libraryEditor);
}
- public static boolean libraryAlreadyExists(LibraryTable.ModifiableModel table, String libraryName) {
- for (Iterator it = table.getLibraryIterator(); it.hasNext(); ) {
- final Library library = it.next();
- final String libName;
- if (table instanceof LibrariesModifiableModel){
- libName = ((LibrariesModifiableModel)table).getLibraryEditor(library).getName();
- }
- else {
- libName = library.getName();
- }
- if (libraryName.equals(libName)) {
- return true;
- }
- }
- return false;
- }
-
- public static String suggestNewLibraryName(LibraryTable.ModifiableModel table) {
- final String name = "Unnamed";
- String candidataName = name;
- for (int idx = 1; libraryAlreadyExists(table, candidataName); candidataName = name + (idx++));
- return candidataName;
- }
-
private void init(AbstractTreeStructure treeStructure) {
myTree = new Tree(new DefaultTreeModel(new DefaultMutableTreeNode()));
myTree.setRootVisible(false);