merged 'add existing library' and 'add new library' actions in classpath editor
authornik <Nikolay.Chashnikov@jetbrains.com>
Wed, 15 Sep 2010 14:49:53 +0000 (18:49 +0400)
committernik <Nikolay.Chashnikov@jetbrains.com>
Wed, 15 Sep 2010 14:50:29 +0000 (18:50 +0400)
12 files changed:
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddItemPopupAction.java
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddLibraryAction.java [moved from java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseExistingLibraryAction.java with 63% similarity]
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddNewLibraryItemAction.java [new file with mode: 0644]
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddSingleEntryModuleLibraryAction.java [new file with mode: 0644]
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseAndAddAction.java [new file with mode: 0644]
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ClasspathElementChooser.java [moved from java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ClasspathElementChooserDialog.java with 92% similarity]
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ClasspathPanelImpl.java
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateSingleEntryModuleLibraryChooser.java [moved from java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateSingleEntryModuleLibraryDialog.java with 95% similarity]
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/NewLibraryChooser.java [moved from java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateLibraryDialog.java with 96% similarity]
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ProjectStructureChooseLibrariesDialog.java
platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesFromTablesDialog.java
platform/platform-resources-en/src/messages/ProjectBundle.properties

index c34ba828a9d05ae3928ac46fee4a645aa5928696..a6aa3092a96fc4db0cacaf18643a10610586c93a 100644 (file)
  */
 package com.intellij.openapi.roots.ui.configuration.classpath;
 
-import com.intellij.openapi.util.Disposer;
-import org.jetbrains.annotations.Nullable;
-
 import javax.swing.*;
-import java.util.ArrayList;
-import java.util.List;
 
 /**
 * @author nik
 */
-abstract class AddItemPopupAction<ItemType> extends ClasspathPanelAction {
+abstract class AddItemPopupAction<ItemType> extends ChooseAndAddAction<ItemType> {
   private final String myTitle;
   private final Icon myIcon;
   private final int myIndex;
@@ -49,38 +44,4 @@ abstract class AddItemPopupAction<ItemType> extends ClasspathPanelAction {
     return myIndex;
   }
 
-  @Override
-  public void run() {
-    final ClasspathElementChooserDialog<ItemType> dialog = createChooserDialog();
-    if (dialog == null) {
-      return;
-    }
-    try {
-      dialog.doChoose();
-      if (!dialog.isOK()) {
-        return;
-      }
-      final List<ItemType> chosen = dialog.getChosenElements();
-      if (chosen.isEmpty()) {
-        return;
-      }
-      List<ClasspathTableItem> toAdd = new ArrayList<ClasspathTableItem>();
-      for (ItemType item : chosen) {
-        final ClasspathTableItem tableItem = createTableItem(item);
-        if (tableItem != null) {
-          toAdd.add(tableItem);
-        }
-      }
-      myClasspathPanel.addItems(toAdd);
-    }
-    finally {
-      Disposer.dispose(dialog);
-    }
-  }
-
-  @Nullable
-  protected abstract ClasspathTableItem createTableItem(final ItemType item);
-
-  @Nullable
-  protected abstract ClasspathElementChooserDialog<ItemType> createChooserDialog();
 }
similarity index 63%
rename from java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseExistingLibraryAction.java
rename to java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddLibraryAction.java
index 47a8269f549f21dad37779a23d9c4ee7e9b2c864..bdfb56a66e150415e397079376b94c4a3f5c86b7 100644 (file)
@@ -21,24 +21,52 @@ 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.projectRoot.StructureConfigurableContext;
 import com.intellij.openapi.ui.Messages;
+import com.intellij.openapi.util.Condition;
 import com.intellij.util.Icons;
-import com.intellij.util.containers.ContainerUtil;
+import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog;
 import org.jetbrains.annotations.Nullable;
 
-import java.util.*;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
 /**
 * @author nik
 */
-class ChooseExistingLibraryAction extends AddItemPopupAction<Library> {
+class AddLibraryAction extends AddItemPopupAction<Library> {
   private StructureConfigurableContext myContext;
+  private AddNewLibraryItemAction myNewLibraryAction;
 
-  public ChooseExistingLibraryAction(ClasspathPanel classpathPanel, final int index, final String title,
-                                     final StructureConfigurableContext context) {
+  public AddLibraryAction(ClasspathPanel classpathPanel, final int index, final String title,
+                          final StructureConfigurableContext context) {
     super(classpathPanel, index, title, Icons.LIBRARY_ICON);
     myContext = context;
+    myNewLibraryAction = new AddNewLibraryItemAction(classpathPanel, context);
+  }
+
+  @Override
+  public void run() {
+    if (hasLibraries()) {
+      super.run();
+    }
+    else {
+      myNewLibraryAction.run();
+    }
+  }
+
+  private boolean hasLibraries() {
+    final Condition<Library> condition = getNotAddedLibrariesCondition();
+    for (LibraryTable table : ChooseLibrariesFromTablesDialog.getLibraryTables(myClasspathPanel.getProject(), true)) {
+      for (Library library : table.getLibraries()) {
+        if (condition.value(library)) {
+          return true;
+        }
+      }
+    }
+    return false;
   }
 
   @Nullable
@@ -62,25 +90,36 @@ class ChooseExistingLibraryAction extends AddItemPopupAction<Library> {
     return ClasspathTableItem.createLibItem(rootModel.addLibraryEntry(item));
   }
 
-  protected ClasspathElementChooserDialog<Library> createChooserDialog() {
-    return new MyChooserDialog();
+  protected ClasspathElementChooser<Library> createChooser() {
+    return new ExistingLibraryChooser();
   }
 
-  private Collection<Library> getAlreadyAddedLibraries() {
+  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) {
-          ContainerUtil.addIfNotNull(result, library.getSource());
+          final Library source = library.getSource();
+          result.add(source != null ? source : library);
         }
       }
     }
-    return result;
+    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 MyChooserDialog implements ClasspathElementChooserDialog<Library> {
+  class ExistingLibraryChooser implements ClasspathElementChooser<Library> {
     private List<Library> mySelectedLibraries;
 
     public List<Library> getChosenElements() {
@@ -89,7 +128,7 @@ class ChooseExistingLibraryAction extends AddItemPopupAction<Library> {
 
     public void doChoose() {
       ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel.getComponent(), myClasspathPanel.getProject(), myContext,
-                                                                                               getAlreadyAddedLibraries());
+                                                                                               getNotAddedLibrariesCondition(), myNewLibraryAction);
       dialog.show();
       mySelectedLibraries = dialog.getSelectedLibraries();
     }
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddNewLibraryItemAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddNewLibraryItemAction.java
new file mode 100644 (file)
index 0000000..55179b2
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * 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.openapi.roots.LibraryOrderEntry;
+import com.intellij.openapi.roots.OrderEntry;
+import com.intellij.openapi.roots.libraries.Library;
+import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
+
+/**
+* @author nik
+*/
+class AddNewLibraryItemAction extends ChooseAndAddAction<Library> {
+  private final StructureConfigurableContext myContext;
+
+  public AddNewLibraryItemAction(final ClasspathPanel classpathPanel,
+                                 StructureConfigurableContext context) {
+    super(classpathPanel);
+    myContext = context;
+  }
+
+  protected ClasspathTableItem createTableItem(final Library item) {
+    final OrderEntry[] entries = myClasspathPanel.getRootModel().getOrderEntries();
+    for (OrderEntry entry : entries) {
+      if (entry instanceof LibraryOrderEntry) {
+        final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)entry;
+        if (item.equals(libraryOrderEntry.getLibrary())) {
+          return ClasspathTableItem.createLibItem(libraryOrderEntry);
+        }
+      }
+    }
+    return ClasspathTableItem.createLibItem(myClasspathPanel.getRootModel().addLibraryEntry(item));
+  }
+
+  protected ClasspathElementChooser<Library> createChooser() {
+    return new NewLibraryChooser(myClasspathPanel.getProject(), myClasspathPanel.getRootModel(), myContext, myClasspathPanel.getComponent());
+  }
+}
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddSingleEntryModuleLibraryAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddSingleEntryModuleLibraryAction.java
new file mode 100644 (file)
index 0000000..480c5ab
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * 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.openapi.diagnostic.Logger;
+import com.intellij.openapi.project.ProjectBundle;
+import com.intellij.openapi.roots.LibraryOrderEntry;
+import com.intellij.openapi.roots.OrderEntry;
+import com.intellij.openapi.roots.libraries.Library;
+import com.intellij.openapi.roots.libraries.LibraryTable;
+import com.intellij.util.Icons;
+
+/**
+* @author nik
+*/
+class AddSingleEntryModuleLibraryAction extends AddItemPopupAction<Library> {
+  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.roots.ui.configuration.classpath.AddSingleEntryModuleLibraryAction");
+
+  public AddSingleEntryModuleLibraryAction(final ClasspathPanel classpathPanel, int actionIndex) {
+    super(classpathPanel, actionIndex, ProjectBundle.message("classpath.add.simple.module.library.action"), Icons.JAR_ICON);
+  }
+
+  protected ClasspathTableItem createTableItem(final Library item) {
+    final OrderEntry[] entries = myClasspathPanel.getRootModel().getOrderEntries();
+    for (OrderEntry entry : entries) {
+      if (entry instanceof LibraryOrderEntry) {
+        final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)entry;
+        if (item.equals(libraryOrderEntry.getLibrary())) {
+          return ClasspathTableItem.createLibItem(libraryOrderEntry);
+        }
+      }
+    }
+    LOG.error("Unknown library " + item);
+    return null;
+  }
+
+  protected ClasspathElementChooser<Library> createChooser() {
+    final LibraryTable.ModifiableModel moduleLibraryModel = myClasspathPanel.getRootModel().getModuleLibraryTable().getModifiableModel();
+    return new CreateSingleEntryModuleLibraryChooser(myClasspathPanel, moduleLibraryModel);
+  }
+}
diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseAndAddAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseAndAddAction.java
new file mode 100644 (file)
index 0000000..7cdb4f5
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * 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.openapi.util.Disposer;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author nik
+ */
+public abstract class ChooseAndAddAction<ItemType> extends ClasspathPanelAction {
+  public ChooseAndAddAction(ClasspathPanel classpathPanel) {
+    super(classpathPanel);
+  }
+
+  @Override
+  public void run() {
+    final ClasspathElementChooser<ItemType> dialog = createChooser();
+    if (dialog == null) {
+      return;
+    }
+    try {
+      dialog.doChoose();
+      if (!dialog.isOK()) {
+        return;
+      }
+      final List<ItemType> chosen = dialog.getChosenElements();
+      if (chosen.isEmpty()) {
+        return;
+      }
+      List<ClasspathTableItem> toAdd = new ArrayList<ClasspathTableItem>();
+      for (ItemType item : chosen) {
+        final ClasspathTableItem tableItem = createTableItem(item);
+        if (tableItem != null) {
+          toAdd.add(tableItem);
+        }
+      }
+      myClasspathPanel.addItems(toAdd);
+    }
+    finally {
+      Disposer.dispose(dialog);
+    }
+  }
+
+  @Nullable
+  protected abstract ClasspathTableItem createTableItem(final ItemType item);
+
+  @Nullable
+  protected abstract ClasspathElementChooser<ItemType> createChooser();
+}
similarity index 92%
rename from java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ClasspathElementChooserDialog.java
rename to java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ClasspathElementChooser.java
index 56befffab2f7eafd7f42603a3ea206bb4332d5df..691776cec60d46285f8b629987ed1f6ab01af5c1 100644 (file)
@@ -22,7 +22,7 @@ import java.util.List;
 /**
 * @author nik
 */
-interface ClasspathElementChooserDialog<T> extends Disposable {
+interface ClasspathElementChooser<T> extends Disposable {
   List<T> getChosenElements();
   void doChoose();
   boolean isOK();
index 62a8f2b1b200dea33d971827556e27f52e46bb41..4368359e1b1bb59aee0b9dea91cf25de5e8fe063 100644 (file)
@@ -52,7 +52,6 @@ import com.intellij.openapi.wm.IdeFocusManager;
 import com.intellij.ui.*;
 import com.intellij.ui.awt.RelativePoint;
 import com.intellij.util.EventDispatcher;
-import com.intellij.util.Icons;
 import com.intellij.util.ui.Table;
 import org.jetbrains.annotations.NotNull;
 
@@ -461,60 +460,20 @@ public class ClasspathPanelImpl extends JPanel implements ClasspathPanel {
       final StructureConfigurableContext context = ProjectStructureConfigurable.getInstance(myState.getProject()).getContext();
       int actionIndex = 1;
       final List<AddItemPopupAction<?>> actions = new ArrayList<AddItemPopupAction<?>>();
-      actions.add(
-        new AddItemPopupAction<Library>(this, actionIndex++, ProjectBundle.message("classpath.add.simple.module.library.action"), Icons.JAR_ICON) {
-          protected ClasspathTableItem createTableItem(final Library item) {
-            final OrderEntry[] entries = getRootModel().getOrderEntries();
-            for (OrderEntry entry : entries) {
-              if (entry instanceof LibraryOrderEntry) {
-                final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)entry;
-                if (item.equals(libraryOrderEntry.getLibrary())) {
-                  return ClasspathTableItem.createLibItem(libraryOrderEntry);
-                }
-              }
-            }
-            LOG.error("Unknown library " + item);
-            return null;
-          }
-
-          protected ClasspathElementChooserDialog<Library> createChooserDialog() {
-            final LibraryTable.ModifiableModel moduleLibraryModel = getRootModel().getModuleLibraryTable().getModifiableModel();
-            return new CreateSingleEntryModuleLibraryDialog(ClasspathPanelImpl.this, moduleLibraryModel);
-          }
-        });
-      actions.add(
-        new AddItemPopupAction<Library>(this, actionIndex++, ProjectBundle.message("classpath.add.new.library.action"), Icons.LIBRARY_ICON) {
-          protected ClasspathTableItem createTableItem(final Library item) {
-            final OrderEntry[] entries = getRootModel().getOrderEntries();
-            for (OrderEntry entry : entries) {
-              if (entry instanceof LibraryOrderEntry) {
-                final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)entry;
-                if (item.equals(libraryOrderEntry.getLibrary())) {
-                  return ClasspathTableItem.createLibItem(libraryOrderEntry);
-                }
-              }
-            }
-            return ClasspathTableItem.createLibItem(getRootModel().addLibraryEntry(item));
-          }
-
-          protected ClasspathElementChooserDialog<Library> createChooserDialog() {
-            return new CreateLibraryDialog(ClasspathPanelImpl.this.getProject(), getRootModel(), context, ClasspathPanelImpl.this.getComponent());
-          }
-        });
-      actions.add(new ChooseExistingLibraryAction(this, actionIndex++, ProjectBundle.message("classpath.add.existing.library.action"), context));
-
+      actions.add(new AddSingleEntryModuleLibraryAction(this, actionIndex++));
+      actions.add(new AddLibraryAction(this, actionIndex++, ProjectBundle.message("classpath.add.library.action"), context));
       actions.add(new AddItemPopupAction<Module>(this, actionIndex, ProjectBundle.message("classpath.add.module.dependency.action"),
                                                  StdModuleTypes.JAVA.getNodeIcon(false)) {
           protected ClasspathTableItem createTableItem(final Module item) {
             return ClasspathTableItem.createItem(getRootModel().addModuleOrderEntry(item));
           }
-          protected ClasspathElementChooserDialog<Module> createChooserDialog() {
+          protected ClasspathElementChooser<Module> createChooser() {
             final List<Module> chooseItems = getDependencyModules();
             if (chooseItems.isEmpty()) {
               Messages.showMessageDialog(ClasspathPanelImpl.this, ProjectBundle.message("message.no.module.dependency.candidates"), getTitle(), Messages.getInformationIcon());
               return null;
             }
-            return new ChooseModulesToAddDialog(chooseItems, ProjectBundle.message("classpath.chooser.title.add.module.dependency"));
+            return new ModuleChooser(chooseItems, ProjectBundle.message("classpath.chooser.title.add.module.dependency"));
           }
         }
       );
@@ -677,8 +636,8 @@ public class ClasspathPanelImpl extends JPanel implements ClasspathPanel {
     }
   }
 
-  private class ChooseModulesToAddDialog extends ChooseModulesDialog implements ClasspathElementChooserDialog<Module> {
-    public ChooseModulesToAddDialog(final List<Module> items, final String title) {
+  private class ModuleChooser extends ChooseModulesDialog implements ClasspathElementChooser<Module> {
+    public ModuleChooser(final List<Module> items, final String title) {
       super(ClasspathPanelImpl.this, items, title);
     }
 
@@ -734,4 +693,5 @@ public class ClasspathPanelImpl extends JPanel implements ClasspathPanel {
       return new RelativePoint(myEntryTable, location);
     }
   }
+
 }
@@ -35,12 +35,12 @@ import java.util.List;
 /**
 * @author nik
 */
-class CreateSingleEntryModuleLibraryDialog implements ClasspathElementChooserDialog<Library> {
+class CreateSingleEntryModuleLibraryChooser implements ClasspathElementChooser<Library> {
   private ClasspathPanel myClasspathPanel;
   private final LibraryTable.ModifiableModel myModuleLibrariesModel;
   private VirtualFile[] myChosenFiles;
 
-  public CreateSingleEntryModuleLibraryDialog(ClasspathPanel classpathPanel,
+  public CreateSingleEntryModuleLibraryChooser(ClasspathPanel classpathPanel,
                                               final LibraryTable.ModifiableModel moduleLibrariesModel) {
     myClasspathPanel = classpathPanel;
     myModuleLibrariesModel = moduleLibrariesModel;
similarity index 96%
rename from java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateLibraryDialog.java
rename to java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/NewLibraryChooser.java
index 143316d10f3d15bc68861306d510062c805c933b..9ded4cc81940e7d604a11b1011b62deeb3188852 100644 (file)
@@ -35,7 +35,7 @@ import java.util.List;
 /**
 * @author nik
 */
-class CreateLibraryDialog implements ClasspathElementChooserDialog<Library> {
+class NewLibraryChooser implements ClasspathElementChooser<Library> {
   private boolean myIsOk;
   private final ModifiableRootModel myRootModel;
   private Library myChosenLibrary;
@@ -43,7 +43,7 @@ class CreateLibraryDialog implements ClasspathElementChooserDialog<Library> {
   private final JComponent myParentComponent;
   private final Project myProject;
 
-  public CreateLibraryDialog(final Project project,
+  public NewLibraryChooser(final Project project,
                              final ModifiableRootModel rootModel,
                              StructureConfigurableContext context, final JComponent parentComponent) {
     myRootModel = rootModel;
index 646bd391f4f59763ca7fe26c28b3fe4753f7646a..5079e27690917ba0527583d899881352f23b1eca 100644 (file)
@@ -18,11 +18,11 @@ package com.intellij.openapi.roots.ui.configuration.classpath;
 import com.intellij.ide.projectView.PresentationData;
 import com.intellij.ide.util.treeView.NodeDescriptor;
 import com.intellij.openapi.project.Project;
-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 com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
+import com.intellij.openapi.util.Condition;
 import com.intellij.ui.SimpleTextAttributes;
 import com.intellij.util.Icons;
 import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog;
@@ -30,22 +30,26 @@ import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
-import java.util.Collection;
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
 
 /**
  * @author nik
  */
 public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTablesDialog {
   private StructureConfigurableContext myContext;
-  private Collection<Library> myAlreadyAddedLibraries;
+  private Condition<Library> myAcceptedLibraries;
+  private AddNewLibraryItemAction myNewLibraryAction;
 
   public ProjectStructureChooseLibrariesDialog(JComponent parentComponent,
                                                @Nullable Project project,
                                                StructureConfigurableContext context,
-                                               Collection<Library> alreadyAddedLibraries) {
+                                               Condition<Library> acceptedLibraries, AddNewLibraryItemAction newLibraryAction) {
     super(parentComponent, "Choose Libraries", project, true);
     myContext = context;
-    myAlreadyAddedLibraries = alreadyAddedLibraries;
+    myAcceptedLibraries = acceptedLibraries;
+    myNewLibraryAction = newLibraryAction;
+    setOKButtonText("Add Selected");
     init();
   }
 
@@ -65,11 +69,7 @@ public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTa
   protected boolean acceptsElement(Object element) {
     if (element instanceof Library) {
       final Library library = (Library)element;
-      if (myAlreadyAddedLibraries.contains(library)) return false;
-      if (library instanceof LibraryImpl) {
-        final Library source = ((LibraryImpl)library).getSource();
-        if (source != null && myAlreadyAddedLibraries.contains(source)) return false;
-      }
+      return myAcceptedLibraries.value(library);
     }
     return true;
   }
@@ -85,6 +85,16 @@ public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTa
     return library.getName();
   }
 
+  @Override
+  protected Action[] createActions() {
+    return new Action[]{getCancelAction()};
+  }
+
+  @Override
+  protected Action[] createLeftSideActions() {
+    return new Action[]{getOKAction(), new CreateNewLibraryAction()};
+  }
+
   @Override
   protected LibrariesTreeNodeBase<Library> createLibraryDescriptor(NodeDescriptor parentDescriptor,
                                                                    Library library) {
@@ -102,4 +112,16 @@ public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTa
     }
   }
 
+  private class CreateNewLibraryAction extends DialogWrapperAction {
+    private CreateNewLibraryAction() {
+      super("New Library...");
+      putValue(MNEMONIC_KEY, KeyEvent.VK_N);
+    }
+
+    @Override
+    protected void doAction(ActionEvent e) {
+      close(CANCEL_EXIT_CODE);
+      myNewLibraryAction.execute();
+    }
+  }
 }
index 7862eb43c22ae6c6f7634092dbb7dfee1362904b..0d8ca8c06ac846540e976593450205b737ffe1b2 100644 (file)
@@ -24,6 +24,7 @@ import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -74,14 +75,9 @@ public class ChooseLibrariesFromTablesDialog extends ChooseLibrariesDialogBase {
   @Override
   protected void collectChildren(Object element, List<Object> result) {
     if (element instanceof Application) {
-      final LibraryTablesRegistrar registrar = LibraryTablesRegistrar.getInstance();
-      if (myProject != null) {
-        addLibraryTable(result, registrar.getLibraryTable(myProject));
-      }
-      addLibraryTable(result, registrar.getLibraryTable());
-      if (myShowCustomLibraryTables) {
-        for (LibraryTable table : registrar.getCustomLibraryTables()) {
-          addLibraryTable(result, table);
+      for (LibraryTable table : getLibraryTables(myProject, myShowCustomLibraryTables)) {
+        if (hasLibraries(table)) {
+          result.add(table);
         }
       }
     }
@@ -90,10 +86,29 @@ public class ChooseLibrariesFromTablesDialog extends ChooseLibrariesDialogBase {
     }
   }
 
-  private void addLibraryTable(List<Object> result, LibraryTable table) {
-    if (getLibraries(table).length > 0) {
-      result.add(table);
+  public static List<LibraryTable> getLibraryTables(final Project project, final boolean showCustomLibraryTables) {
+    final List<LibraryTable> tables = new ArrayList<LibraryTable>();
+    final LibraryTablesRegistrar registrar = LibraryTablesRegistrar.getInstance();
+    if (project != null) {
+      tables.add(registrar.getLibraryTable(project));
+    }
+    tables.add(registrar.getLibraryTable());
+    if (showCustomLibraryTables) {
+      for (LibraryTable table : registrar.getCustomLibraryTables()) {
+        tables.add(table);
+      }
+    }
+    return tables;
+  }
+
+  private boolean hasLibraries(LibraryTable table) {
+    final Library[] libraries = getLibraries(table);
+    for (Library library : libraries) {
+      if (acceptsElement(library)) {
+        return true;
+      }
     }
+    return false;
   }
 
   @Override
index 381b94822d68b64d291f37148c5fd2000dee4f63..6d1534f2badf40b05c190c464d846c84dce4bd74 100644 (file)
@@ -219,7 +219,7 @@ library.name.already.exists.title=Library Already Exists
 library.create.library.action=Create &Library...
 classpath.add.new.library.action=New Library...
 classpath.add.simple.module.library.action=Single-Entry Module Library...
-classpath.add.existing.library.action=Existing Library...
+classpath.add.library.action=Library...
 classpath.add.module.dependency.action=Module Dependency...
 classpath.chooser.title.add.module.dependency=Choose Dependent Modules
 classpath.title.adding.dependency=Adding dependency