*/
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;
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();
}
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
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() {
public void doChoose() {
ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel.getComponent(), myClasspathPanel.getProject(), myContext,
- getAlreadyAddedLibraries());
+ getNotAddedLibrariesCondition(), myNewLibraryAction);
dialog.show();
mySelectedLibraries = dialog.getSelectedLibraries();
}
--- /dev/null
+/*
+ * 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());
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+}
--- /dev/null
+/*
+ * 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();
+}
/**
* @author nik
*/
-interface ClasspathElementChooserDialog<T> extends Disposable {
+interface ClasspathElementChooser<T> extends Disposable {
List<T> getChosenElements();
void doChoose();
boolean isOK();
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;
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"));
}
}
);
}
}
- 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);
}
return new RelativePoint(myEntryTable, location);
}
}
+
}
/**
* @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;
/**
* @author nik
*/
-class CreateLibraryDialog implements ClasspathElementChooserDialog<Library> {
+class NewLibraryChooser implements ClasspathElementChooser<Library> {
private boolean myIsOk;
private final ModifiableRootModel myRootModel;
private Library myChosenLibrary;
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;
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;
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();
}
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;
}
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) {
}
}
+ 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();
+ }
+ }
}
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@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);
}
}
}
}
}
- 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
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