reworked 'new library' button in 'add library to dependencies' dialog: now it adds created library to the libraries tree in the dialog

This commit is contained in:
nik
2011-10-05 10:07:49 +04:00
parent 0328cbfe24
commit bf83c0936e
14 changed files with 165 additions and 59 deletions
@@ -23,13 +23,16 @@ import com.intellij.openapi.roots.OrderEntry;
import com.intellij.openapi.roots.impl.libraries.LibraryTableBase;
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.ui.classpath.ChooseLibrariesFromTablesDialog;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@@ -53,7 +56,12 @@ class AddLibraryAction extends AddItemPopupAction<Library> {
@Override
public PopupStep createSubStep() {
return AddNewLibraryItemAction.createChooseTypeStep(myClasspathPanel, myContext, null);
return LibraryEditingUtil.createChooseTypeStep(myClasspathPanel, new ParameterizedRunnable<LibraryType>() {
@Override
public void run(LibraryType libraryType) {
new AddNewLibraryItemAction(myClasspathPanel, myContext, libraryType).execute();
}
});
}
@Override
@@ -88,12 +96,17 @@ class AddLibraryAction extends AddItemPopupAction<Library> {
final OrderEntry[] orderEntries = rootModel.getOrderEntries();
for (OrderEntry orderEntry : orderEntries) {
if (orderEntry instanceof LibraryOrderEntry) {
if (item.getName().equals(((LibraryOrderEntry)orderEntry).getLibraryName())) {
final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)orderEntry;
if (item.equals(libraryOrderEntry.getLibrary())) {
return ClasspathTableItem.createLibItem(libraryOrderEntry, myContext);
}
if (item.getName().equals(libraryOrderEntry.getLibraryName())) {
if (orderEntry.isValid()) {
Messages.showErrorDialog(ProjectBundle.message("classpath.message.library.already.added",item.getName()),
Messages.showErrorDialog(ProjectBundle.message("classpath.message.library.already.added", item.getName()),
ProjectBundle.message("classpath.title.adding.dependency"));
return null;
} else {
}
else {
rootModel.removeOrderEntry(orderEntry);
}
}
@@ -112,6 +125,7 @@ class AddLibraryAction extends AddItemPopupAction<Library> {
}
class ExistingLibraryChooser implements ClasspathElementChooser<Library> {
@NotNull
public List<Library> chooseElements() {
final Predicate<Library> condition = LibraryEditingUtil.getNotAddedLibrariesCondition(myClasspathPanel.getRootModel());
ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel, myContext,
@@ -15,19 +15,15 @@
*/
package com.intellij.openapi.roots.ui.configuration.classpath;
import com.intellij.ide.IdeBundle;
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.LibraryType;
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil;
import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.intellij.util.PlatformIcons;
import com.intellij.util.ParameterizedRunnable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -38,7 +34,7 @@ import javax.swing.*;
*/
class AddNewLibraryItemAction extends ChooseAndAddAction<Library> {
private final StructureConfigurableContext myContext;
private LibraryType myLibraryType;
private final LibraryType myLibraryType;
public AddNewLibraryItemAction(final ClasspathPanel classpathPanel,
StructureConfigurableContext context, LibraryType libraryType) {
@@ -64,45 +60,34 @@ class AddNewLibraryItemAction extends ChooseAndAddAction<Library> {
return new NewLibraryChooser(myClasspathPanel.getProject(), myClasspathPanel.getRootModel(), myLibraryType, myContext, myClasspathPanel.getComponent());
}
public static void chooseTypeAndExecute(final ClasspathPanel classpathPanel,
final StructureConfigurableContext context,
final DialogWrapper parentDialog,
JButton contextButton) {
public static void chooseTypeAndCreate(final ClasspathPanel classpathPanel,
final StructureConfigurableContext context,
final JButton contextButton, @NotNull final LibraryCreatedCallback callback) {
if (LibraryEditingUtil.hasSuitableTypes(classpathPanel)) {
final ListPopup popup = JBPopupFactory.getInstance().createListPopup(createChooseTypeStep(classpathPanel, context, parentDialog));
final ListPopup popup = JBPopupFactory.getInstance().createListPopup(LibraryEditingUtil.createChooseTypeStep(classpathPanel, new ParameterizedRunnable<LibraryType>() {
@Override
public void run(LibraryType libraryType) {
doCreateLibrary(classpathPanel, context, callback, contextButton, libraryType);
}
}));
popup.showUnderneathOf(contextButton);
}
else {
if (parentDialog != null) parentDialog.close(DialogWrapper.CANCEL_EXIT_CODE);
new AddNewLibraryItemAction(classpathPanel, context, null).execute();
doCreateLibrary(classpathPanel, context, callback, contextButton, null);
}
}
public static BaseListPopupStep<LibraryType> createChooseTypeStep(final ClasspathPanel classpathPanel,
final StructureConfigurableContext context,
final @Nullable DialogWrapper parentDialog) {
return new BaseListPopupStep<LibraryType>("Select Library Type", LibraryEditingUtil.getSuitableTypes(classpathPanel)) {
@NotNull
@Override
public String getTextFor(LibraryType value) {
return value != null ? value.getCreateActionName() : IdeBundle.message("create.default.library.type.action.name");
}
private static void doCreateLibrary(ClasspathPanel classpathPanel,
StructureConfigurableContext context,
LibraryCreatedCallback callback, final JComponent component, final @Nullable LibraryType libraryType) {
final NewLibraryChooser chooser = new NewLibraryChooser(classpathPanel.getProject(), classpathPanel.getRootModel(), libraryType, context, component);
final Library library = chooser.createLibrary();
if (library != null) {
callback.libraryCreated(library);
}
}
@Override
public Icon getIconFor(LibraryType aValue) {
return aValue != null ? aValue.getIcon() : PlatformIcons.LIBRARY_ICON;
}
@Override
public PopupStep onChosen(final LibraryType selectedValue, boolean finalChoice) {
return doFinalStep(new Runnable() {
@Override
public void run() {
if (parentDialog != null) parentDialog.close(DialogWrapper.CANCEL_EXIT_CODE);
new AddNewLibraryItemAction(classpathPanel, context, selectedValue).execute();
}
});
}
};
interface LibraryCreatedCallback {
void libraryCreated(@NotNull Library library);
}
}
@@ -15,11 +15,14 @@
*/
package com.intellij.openapi.roots.ui.configuration.classpath;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* @author nik
*/
interface ClasspathElementChooser<T> {
@NotNull
List<T> chooseElements();
}
@@ -688,6 +688,7 @@ public class ClasspathPanelImpl extends JPanel implements ClasspathPanel {
myDescription = description;
}
@NotNull
public List<Module> chooseElements() {
ChooseModulesDialog dialog = new ChooseModulesDialog(ClasspathPanelImpl.this, myItems, myTitle, myDescription);
dialog.show();
@@ -32,6 +32,7 @@ import com.intellij.openapi.roots.ui.configuration.libraryEditor.RootDetectionUt
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.*;
@@ -44,7 +45,6 @@ public class CreateModuleLibraryChooser implements ClasspathElementChooser<Libra
private final Module myModule;
private final LibraryTable.ModifiableModel myModuleLibrariesModel;
private final HashMap<LibraryRootsComponentDescriptor,LibraryType> myLibraryTypes;
private LibraryType myLibraryType;
private final DefaultLibraryRootsComponentDescriptor myDefaultDescriptor;
public CreateModuleLibraryChooser(ClasspathPanel classpathPanel, LibraryTable.ModifiableModel moduleLibraryModel) {
@@ -111,6 +111,7 @@ public class CreateModuleLibraryChooser implements ClasspathElementChooser<Libra
return false;
}
@NotNull
public List<Library> chooseElements() {
final FileChooserDescriptor chooserDescriptor;
final List<Pair<LibraryRootsComponentDescriptor, FileChooserDescriptor>> descriptors = new ArrayList<Pair<LibraryRootsComponentDescriptor, FileChooserDescriptor>>();
@@ -16,7 +16,6 @@
package com.intellij.openapi.roots.ui.configuration.classpath;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
@@ -28,10 +27,12 @@ import com.intellij.openapi.roots.libraries.LibraryType;
import com.intellij.openapi.roots.ui.configuration.libraryEditor.CreateNewLibraryDialog;
import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor;
import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
@@ -54,7 +55,13 @@ class NewLibraryChooser implements ClasspathElementChooser<Library> {
myProject = project;
}
@NotNull
public List<Library> chooseElements() {
return ContainerUtil.createMaybeSingletonList(createLibrary());
}
@Nullable
public Library createLibrary() {
final NewLibraryEditor libraryEditor;
if (myLibraryType == null) {
libraryEditor = new NewLibraryEditor();
@@ -73,8 +80,8 @@ class NewLibraryChooser implements ClasspathElementChooser<Library> {
dialog.setContextModule(contextModule);
dialog.show();
if (dialog.isOK()) {
return Collections.singletonList(dialog.createLibrary());
return dialog.createLibrary();
}
return Collections.emptyList();
return null;
}
}
@@ -18,7 +18,9 @@ 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.application.Application;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.impl.libraries.LibraryTableImplUtil;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager;
@@ -27,18 +29,24 @@ import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigur
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* @author nik
*/
public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTablesDialog {
private final ClasspathPanel myClasspathPanel;
private StructureConfigurableContext myContext;
private Predicate<Library> myAcceptedLibraries;
private final StructureConfigurableContext myContext;
private final Predicate<Library> myAcceptedLibraries;
private final List<Library> myCreatedModuleLibraries = new ArrayList<Library>();
private JButton myCreateLibraryButton;
public ProjectStructureChooseLibrariesDialog(ClasspathPanel classpathPanel,
@@ -52,16 +60,48 @@ public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTa
init();
}
@Override
protected void doOKAction() {
super.doOKAction();
removeCreatedModuleLibraries(getSelectedLibraries());
}
@Override
public void doCancelAction() {
super.doCancelAction();
removeCreatedModuleLibraries(Collections.<Library>emptyList());
}
private void removeCreatedModuleLibraries(Collection<Library> selected) {
for (Library library : myCreatedModuleLibraries) {
if (!selected.contains(library)) {
myClasspathPanel.getRootModel().getModuleLibraryTable().removeLibrary(library);
}
}
}
@Override
protected void collectChildren(Object element, List<Object> result) {
if (element instanceof Application && !myCreatedModuleLibraries.isEmpty()) {
result.add(myClasspathPanel.getRootModel().getModuleLibraryTable());
}
super.collectChildren(element, result);
}
@NotNull
@Override
protected Library[] getLibraries(@NotNull LibraryTable table) {
if (table.getTableLevel().equals(LibraryTableImplUtil.MODULE_LEVEL)) {
return myCreatedModuleLibraries.toArray(new Library[myCreatedModuleLibraries.size()]);
}
final LibrariesModifiableModel model = getLibrariesModifiableModel(table);
if (model == null) return Library.EMPTY_ARRAY;
return model.getLibraries();
}
@Nullable
private LibrariesModifiableModel getLibrariesModifiableModel(LibraryTable table) {
return myContext.myLevel2Providers.get(table.getTableLevel());
return table != null ? myContext.myLevel2Providers.get(table.getTableLevel()) : null;
}
@Override
@@ -129,7 +169,16 @@ public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTa
@Override
protected void doAction(ActionEvent e) {
AddNewLibraryItemAction.chooseTypeAndExecute(myClasspathPanel, myContext, ProjectStructureChooseLibrariesDialog.this, myCreateLibraryButton);
AddNewLibraryItemAction.chooseTypeAndCreate(myClasspathPanel, myContext, myCreateLibraryButton,
new AddNewLibraryItemAction.LibraryCreatedCallback() {
@Override
public void libraryCreated(@NotNull Library library) {
if (library.getTable() == null) {
myCreatedModuleLibraries.add(library);
}
queueUpdateAndSelect(library);
}
});
}
}
}
@@ -16,6 +16,7 @@
package com.intellij.openapi.roots.ui.configuration.libraries;
import com.google.common.base.Predicate;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
@@ -31,12 +32,17 @@ import com.intellij.openapi.roots.libraries.*;
import com.intellij.openapi.roots.libraries.ui.OrderRoot;
import com.intellij.openapi.roots.ui.configuration.classpath.ClasspathPanel;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.ParameterizedRunnable;
import com.intellij.util.PathUtil;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.*;
/**
@@ -167,4 +173,30 @@ public class LibraryEditingUtil {
public static boolean hasSuitableTypes(ClasspathPanel panel) {
return getSuitableTypes(panel).size() > 1;
}
public static BaseListPopupStep<LibraryType> createChooseTypeStep(final ClasspathPanel classpathPanel,
final ParameterizedRunnable<LibraryType> action) {
return new BaseListPopupStep<LibraryType>(IdeBundle.message("popup.title.select.library.type"), getSuitableTypes(classpathPanel)) {
@NotNull
@Override
public String getTextFor(LibraryType value) {
return value != null ? value.getCreateActionName() : IdeBundle.message("create.default.library.type.action.name");
}
@Override
public Icon getIconFor(LibraryType aValue) {
return aValue != null ? aValue.getIcon() : PlatformIcons.LIBRARY_ICON;
}
@Override
public PopupStep onChosen(final LibraryType selectedValue, boolean finalChoice) {
return doFinalStep(new Runnable() {
@Override
public void run() {
action.run(selectedValue);
}
});
}
};
}
}
@@ -35,8 +35,8 @@ import java.util.List;
*/
public class CreateNewLibraryDialog extends LibraryEditorDialogBase {
private final StructureConfigurableContext myContext;
private NewLibraryEditor myLibraryEditor;
private ComboBox myLibraryLevelCombobox;
private final NewLibraryEditor myLibraryEditor;
private final ComboBox myLibraryLevelCombobox;
public CreateNewLibraryDialog(@NotNull JComponent parent, @NotNull StructureConfigurableContext context, @NotNull NewLibraryEditor libraryEditor,
@NotNull List<LibraryTable> libraryTables, int selectedTable) {
@@ -82,7 +82,7 @@ public class CreateNewLibraryDialog extends LibraryEditorDialogBase {
@Override
protected void addNorthComponents(FormBuilder formBuilder) {
formBuilder.addLabeledComponent("Level:", myLibraryLevelCombobox);
formBuilder.addLabeledComponent("&Level:", myLibraryLevelCombobox);
}
protected boolean shouldCheckName(String newName) {