libraries already added to module aren't suggested in 'new custom library' action

This commit is contained in:
nik
2010-09-22 13:16:56 +04:00
parent 163bff3772
commit d157c8f54f
8 changed files with 108 additions and 67 deletions
+1
View File
@@ -13,6 +13,7 @@
<orderEntry type="module" module-name="java-impl" />
<orderEntry type="library" name="XmlRPC" level="project" />
<orderEntry type="library" name="OroMatcher" level="project" />
<orderEntry type="library" name="Guava" level="project" />
</component>
</module>
@@ -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<Library> {
}
private boolean hasLibraries() {
final Condition<Library> condition = getNotAddedLibrariesCondition();
final Predicate<Library> 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<Library> {
return new ExistingLibraryChooser();
}
private Condition<Library> getNotAddedLibrariesCondition() {
final OrderEntry[] orderEntries = myClasspathPanel.getRootModel().getOrderEntries();
final Set<Library> result = new HashSet<Library>(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<Library>() {
@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<Library> {
private List<Library> mySelectedLibraries;
@@ -127,8 +100,9 @@ class AddLibraryAction extends AddItemPopupAction<Library> {
}
public void doChoose() {
final Predicate<Library> condition = LibraryEditingUtil.getNotAddedLibrariesCondition(myClasspathPanel.getRootModel());
ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel.getComponent(), myClasspathPanel.getProject(), myContext,
getNotAddedLibrariesCondition(), myNewLibraryAction);
condition, myNewLibraryAction);
dialog.show();
mySelectedLibraries = dialog.getSelectedLibraries();
}
@@ -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<Library> myAcceptedLibraries;
private Predicate<Library> myAcceptedLibraries;
private AddNewLibraryItemAction myNewLibraryAction;
public ProjectStructureChooseLibrariesDialog(JComponent parentComponent,
@Nullable Project project,
StructureConfigurableContext context,
Condition<Library> acceptedLibraries, AddNewLibraryItemAction newLibraryAction) {
Predicate<Library> 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;
}
@@ -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<LibraryKind<?>> kinds = new HashSet<LibraryKind<?>>(creator.getSuitableKinds());
List<Library> suitableLibraries = LibraryPresentationManager.getInstance().getLibraries(kinds, context.getProject(), context);
if (suitableLibraries.isEmpty()) {
final Predicate<Library> notAddedLibrariesCondition = LibraryEditingUtil.getNotAddedLibrariesCondition(context.getModulesConfigurator().getRootModel(module));
final Collection<Library> 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));
}
@@ -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<Library> 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<Library> getNotAddedLibrariesCondition(final ModuleRootModel rootModel) {
final OrderEntry[] orderEntries = rootModel.getOrderEntries();
final Set<Library> result = new HashSet<Library>(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<Library>() {
@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;
}
};
}
}
@@ -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;
}
@@ -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;
}
@@ -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<Library> 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);