diff --git a/java/idea-ui/src/com/intellij/facet/impl/ui/libraries/EditLibraryDialog.java b/java/idea-ui/src/com/intellij/facet/impl/ui/libraries/EditLibraryDialog.java index 33f61e2cb61e..3e8373fd6c0c 100644 --- a/java/idea-ui/src/com/intellij/facet/impl/ui/libraries/EditLibraryDialog.java +++ b/java/idea-ui/src/com/intellij/facet/impl/ui/libraries/EditLibraryDialog.java @@ -16,9 +16,7 @@ package com.intellij.facet.impl.ui.libraries; import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.roots.libraries.LibraryTable; -import com.intellij.openapi.roots.libraries.LibraryTablePresentation; -import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.ExistingLibraryEditor; import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryTableEditor; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.util.Disposer; @@ -43,28 +41,7 @@ public class EditLibraryDialog extends DialogWrapper { mySettings = settings; final Library library = settings.getOrCreateLibrary(); - myLibraryTableEditor = LibraryTableEditor.editLibrary(new LibraryTableModifiableModelProvider() { - - @Override - public LibraryTable.ModifiableModel getModifiableModel() { - return library.getTable().getModifiableModel(); - } - - @Override - public String getTableLevel() { - return library.getTable().getTableLevel(); - } - - @Override - public LibraryTablePresentation getLibraryTablePresentation() { - return library.getTable().getPresentation(); - } - - @Override - public boolean isLibraryTableEditable() { - return false; - } - }, library); + myLibraryTableEditor = LibraryTableEditor.editLibrary(new ExistingLibraryEditor(library, null)); Disposer.register(getDisposable(), myLibraryTableEditor); diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/LibraryTableEditorForm.form b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/LibraryTableEditorForm.form index 0ed59096fb3a..a3a2d35e1d02 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/LibraryTableEditorForm.form +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/LibraryTableEditorForm.form @@ -8,7 +8,7 @@ - + @@ -16,17 +16,9 @@ - - - - - - - - - + @@ -34,12 +26,12 @@ - + - + @@ -47,7 +39,7 @@ - + @@ -55,7 +47,7 @@ - + @@ -63,23 +55,15 @@ - + - - - - - - - - - + @@ -87,7 +71,7 @@ - + @@ -95,7 +79,7 @@ - + diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/LibraryTableModifiableModelProvider.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/LibraryTableModifiableModelProvider.java index a6f7561050cc..988ae306beb2 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/LibraryTableModifiableModelProvider.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/LibraryTableModifiableModelProvider.java @@ -16,15 +16,9 @@ package com.intellij.openapi.roots.ui.configuration; import com.intellij.openapi.roots.libraries.LibraryTable; -import com.intellij.openapi.roots.libraries.LibraryTablePresentation; public interface LibraryTableModifiableModelProvider { LibraryTable.ModifiableModel getModifiableModel(); - String getTableLevel(); - - LibraryTablePresentation getLibraryTablePresentation(); - - boolean isLibraryTableEditable(); } diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ModuleEditor.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ModuleEditor.java index fe641f733c88..8af238b834d5 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ModuleEditor.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ModuleEditor.java @@ -409,7 +409,7 @@ public abstract class ModuleEditor implements Place.Navigator, Disposable { } } - private class LibraryTableInvocationHandler implements InvocationHandler { + private class LibraryTableInvocationHandler implements InvocationHandler, ProxyDelegateAccessor { private final LibraryTable myDelegateTable; @NonNls private final Set myCheckedNames = new HashSet(Arrays.asList("removeLibrary" /*,"createLibrary"*/)); @@ -450,6 +450,10 @@ public abstract class ModuleEditor implements Place.Navigator, Disposable { } } + @Override + public Object getDelegate() { + return myDelegateTable; + } } private class LibraryInvocationHandler implements InvocationHandler, ProxyDelegateAccessor { @@ -478,7 +482,7 @@ public abstract class ModuleEditor implements Place.Navigator, Disposable { } } - private class LibraryModifiableModelInvocationHandler implements InvocationHandler { + private class LibraryModifiableModelInvocationHandler implements InvocationHandler, ProxyDelegateAccessor { private final Library.ModifiableModel myDelegateModel; LibraryModifiableModelInvocationHandler(Library.ModifiableModel delegateModel) { @@ -499,9 +503,14 @@ public abstract class ModuleEditor implements Place.Navigator, Disposable { } } } + + @Override + public Object getDelegate() { + return myDelegateModel; + } } - private class LibraryTableModelInvocationHandler implements InvocationHandler { + private class LibraryTableModelInvocationHandler implements InvocationHandler, ProxyDelegateAccessor { private final LibraryTable.ModifiableModel myDelegateModel; LibraryTableModelInvocationHandler(LibraryTable.ModifiableModel delegateModel) { @@ -537,6 +546,11 @@ public abstract class ModuleEditor implements Place.Navigator, Disposable { } } } + + @Override + public Object getDelegate() { + return myDelegateModel; + } } public interface ProxyDelegateAccessor { diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddItemPopupAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddItemPopupAction.java index 179a66d7d59f..c34ba828a9d0 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddItemPopupAction.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/AddItemPopupAction.java @@ -74,9 +74,7 @@ abstract class AddItemPopupAction extends ClasspathPanelAction { myClasspathPanel.addItems(toAdd); } finally { - if (dialog instanceof ChooseNamedLibraryAction.MyChooserDialog) { - Disposer.dispose(dialog); - } + Disposer.dispose(dialog); } } diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseNamedLibraryAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseExistingLibraryAction.java similarity index 67% rename from java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseNamedLibraryAction.java rename to java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseExistingLibraryAction.java index 78aa9bc6e593..47a8269f549f 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseNamedLibraryAction.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseExistingLibraryAction.java @@ -21,11 +21,10 @@ 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.ui.configuration.LibraryTableModifiableModelProvider; -import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryTableEditor; +import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext; import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.util.Disposer; import com.intellij.util.Icons; +import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.Nullable; import java.util.*; @@ -33,15 +32,13 @@ import java.util.*; /** * @author nik */ -class ChooseNamedLibraryAction extends AddItemPopupAction { - private final LibraryTableModifiableModelProvider myLibraryTableModelProvider; +class ChooseExistingLibraryAction extends AddItemPopupAction { + private StructureConfigurableContext myContext; - public ChooseNamedLibraryAction(ClasspathPanel classpathPanel, - final int index, - final String title, - final LibraryTableModifiableModelProvider libraryTable) { + public ChooseExistingLibraryAction(ClasspathPanel classpathPanel, final int index, final String title, + final StructureConfigurableContext context) { super(classpathPanel, index, title, Icons.LIBRARY_ICON); - myLibraryTableModelProvider = libraryTable; + myContext = context; } @Nullable @@ -76,7 +73,7 @@ class ChooseNamedLibraryAction extends AddItemPopupAction { if (orderEntry instanceof LibraryOrderEntry && orderEntry.isValid()) { final LibraryImpl library = (LibraryImpl)((LibraryOrderEntry)orderEntry).getLibrary(); if (library != null) { - result.add(library.getSource()); + ContainerUtil.addIfNotNull(result, library.getSource()); } } } @@ -84,27 +81,21 @@ class ChooseNamedLibraryAction extends AddItemPopupAction { } class MyChooserDialog implements ClasspathElementChooserDialog { - private final LibraryTableEditor myEditor; - private Library[] myLibraries; - - MyChooserDialog(){ - myEditor = LibraryTableEditor.editLibraryTable(myLibraryTableModelProvider, myClasspathPanel.getProject()); - Disposer.register(this, myEditor); - } + private List mySelectedLibraries; public List getChosenElements() { - final List chosen = new ArrayList(Arrays.asList(myLibraries)); - chosen.removeAll(getAlreadyAddedLibraries()); - return chosen; + return mySelectedLibraries; } public void doChoose() { - final Iterator iter = myLibraryTableModelProvider.getModifiableModel().getLibraryIterator(); - myLibraries = myEditor.openDialog(myClasspathPanel.getComponent(), iter.hasNext()? Collections.singleton((Library)iter.next()) : Collections.emptyList(), false); + ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel.getComponent(), myClasspathPanel.getProject(), myContext, + getAlreadyAddedLibraries()); + dialog.show(); + mySelectedLibraries = dialog.getSelectedLibraries(); } public boolean isOK() { - return myLibraries != null; + return mySelectedLibraries != null && !mySelectedLibraries.isEmpty(); } public void dispose() { diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ClasspathPanelImpl.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ClasspathPanelImpl.java index a64350a8ffb3..62a8f2b1b200 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ClasspathPanelImpl.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ClasspathPanelImpl.java @@ -27,14 +27,13 @@ import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.*; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.libraries.LibraryTable; -import com.intellij.openapi.roots.libraries.LibraryTablePresentation; import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; import com.intellij.openapi.roots.ui.configuration.ModuleConfigurationState; import com.intellij.openapi.roots.ui.configuration.ModulesProvider; import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable; import com.intellij.openapi.roots.ui.configuration.dependencyAnalysis.AnalyzeDependenciesDialog; import com.intellij.openapi.roots.ui.configuration.libraryEditor.ChooseModulesDialog; -import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryTableEditor; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.EditExistingLibraryDialog; import com.intellij.openapi.roots.ui.configuration.projectRoot.FindUsagesInProjectStructureActionBase; import com.intellij.openapi.roots.ui.configuration.projectRoot.ModuleStructureConfigurable; import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext; @@ -65,8 +64,10 @@ import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import java.awt.*; import java.awt.event.*; -import java.util.*; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class ClasspathPanelImpl extends JPanel implements ClasspathPanel { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.roots.ui.configuration.classpath.ClasspathPanel"); @@ -381,25 +382,14 @@ public class ClasspathPanelImpl extends JPanel implements ClasspathPanel { return moduleLibraryTable.getModifiableModel(); } - public String getTableLevel() { - return moduleLibraryTable.getTableLevel(); - } - - public LibraryTablePresentation getLibraryTablePresentation() { - return moduleLibraryTable.getPresentation(); - } - - public boolean isLibraryTableEditable() { - return false; - } }; } else { - provider = ProjectStructureConfigurable.getInstance(myState.getProject()).getContext().createModifiableModelProvider(table.getTableLevel(), false); + provider = ProjectStructureConfigurable.getInstance(myState.getProject()).getContext().createModifiableModelProvider(table.getTableLevel()); } - final LibraryTableEditor editor = LibraryTableEditor.editLibrary(provider, library, myState.getProject()); - editor.addFileChooserContext(LangDataKeys.MODULE_CONTEXT, getRootModel().getModule()); - editor.openDialog(ClasspathPanelImpl.this, Collections.singletonList(library), true); + EditExistingLibraryDialog dialog = EditExistingLibraryDialog.createDialog(ClasspathPanelImpl.this, provider, library, myState.getProject()); + dialog.addFileChooserContext(LangDataKeys.MODULE_CONTEXT, getRootModel().getModule()); + dialog.show(); myEntryTable.repaint(); ModuleStructureConfigurable.getInstance(myState.getProject()).getTree().repaint(); } @@ -488,11 +478,12 @@ public class ClasspathPanelImpl extends JPanel implements ClasspathPanel { } protected ClasspathElementChooserDialog createChooserDialog() { - return new ChooseModuleLibrariesDialog(ClasspathPanelImpl.this, getRootModel().getModuleLibraryTable(), null); + final LibraryTable.ModifiableModel moduleLibraryModel = getRootModel().getModuleLibraryTable().getModifiableModel(); + return new CreateSingleEntryModuleLibraryDialog(ClasspathPanelImpl.this, moduleLibraryModel); } }); actions.add( - new AddItemPopupAction(this, actionIndex++, ProjectBundle.message("classpath.add.module.library.action"), Icons.JAR_ICON) { + new AddItemPopupAction(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) { @@ -503,22 +494,14 @@ public class ClasspathPanelImpl extends JPanel implements ClasspathPanel { } } } - LOG.error("Unknown library " + item); - return null; + return ClasspathTableItem.createLibItem(getRootModel().addLibraryEntry(item)); } protected ClasspathElementChooserDialog createChooserDialog() { - return new CreateModuleLibraryDialog(ClasspathPanelImpl.this, getRootModel().getModuleLibraryTable()); + return new CreateLibraryDialog(ClasspathPanelImpl.this.getProject(), getRootModel(), context, ClasspathPanelImpl.this.getComponent()); } }); - actions.add(new ChooseNamedLibraryAction(this, actionIndex++, ProjectBundle.message("classpath.add.project.library.action"), - context.getProjectLibrariesProvider(true))); - actions.add(new ChooseNamedLibraryAction(this, actionIndex++, ProjectBundle.message("classpath.add.global.library.action"), - context.getGlobalLibrariesProvider(true))); - - for (final LibraryTableModifiableModelProvider provider : context.getCustomLibrariesProviders(true)) { - actions.add(new ChooseNamedLibraryAction(this, actionIndex++, provider.getLibraryTablePresentation().getDisplayName(false) + "...", provider)); - } + actions.add(new ChooseExistingLibraryAction(this, actionIndex++, ProjectBundle.message("classpath.add.existing.library.action"), context)); actions.add(new AddItemPopupAction(this, actionIndex, ProjectBundle.message("classpath.add.module.dependency.action"), StdModuleTypes.JAVA.getNodeIcon(false)) { diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateLibraryDialog.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateLibraryDialog.java new file mode 100644 index 000000000000..143316d10f3d --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateLibraryDialog.java @@ -0,0 +1,83 @@ +/* + * 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.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; +import com.intellij.openapi.roots.ModifiableRootModel; +import com.intellij.openapi.roots.libraries.Library; +import com.intellij.openapi.roots.libraries.LibraryTable; +import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.CreateNewLibraryDialog; +import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext; + +import javax.swing.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** +* @author nik +*/ +class CreateLibraryDialog implements ClasspathElementChooserDialog { + private boolean myIsOk; + private final ModifiableRootModel myRootModel; + private Library myChosenLibrary; + private StructureConfigurableContext myContext; + private final JComponent myParentComponent; + private final Project myProject; + + public CreateLibraryDialog(final Project project, + final ModifiableRootModel rootModel, + StructureConfigurableContext context, final JComponent parentComponent) { + myRootModel = rootModel; + myContext = context; + myParentComponent = parentComponent; + myProject = project; + } + + public List getChosenElements() { + return myChosenLibrary == null? Collections.emptyList() : Collections.singletonList(myChosenLibrary); + } + + public void doChoose() { + final LibraryTablesRegistrar registrar = LibraryTablesRegistrar.getInstance(); + List tables = Arrays.asList(myRootModel.getModuleLibraryTable(), + registrar.getLibraryTable(myProject), + registrar.getLibraryTable()); + CreateNewLibraryDialog dialog = CreateNewLibraryDialog.createDialog(myParentComponent, myProject, this, tables, 1); + final Module contextModule = DataKeys.MODULE_CONTEXT.getData(DataManager.getInstance().getDataContext(myParentComponent)); + dialog.addFileChooserContext(LangDataKeys.MODULE_CONTEXT, contextModule); + dialog.show(); + myIsOk = dialog.isOK(); + if (myIsOk) { + myChosenLibrary = dialog.createLibrary(myContext.getModifiableLibraryTable(dialog.getSelectedTable())); + } + else { + myChosenLibrary = null; + } + } + + public boolean isOK() { + return myIsOk; + } + + public void dispose() { + } +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateModuleLibraryDialog.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateModuleLibraryDialog.java deleted file mode 100644 index 5640df01ffe8..000000000000 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateModuleLibraryDialog.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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.ide.DataManager; -import com.intellij.openapi.actionSystem.DataKeys; -import com.intellij.openapi.actionSystem.LangDataKeys; -import com.intellij.openapi.module.Module; -import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.roots.libraries.LibraryTable; -import com.intellij.openapi.roots.libraries.LibraryTablePresentation; -import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; -import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryTableEditor; - -import java.util.Collections; -import java.util.List; - -/** -* @author nik -*/ -class CreateModuleLibraryDialog implements ClasspathElementChooserDialog { - private boolean myIsOk; - private final ClasspathPanel myClasspathPanel; - private final LibraryTable myLibraryTable; - private Library myChosenLibrary; - - public CreateModuleLibraryDialog(ClasspathPanel classpathPanel, final LibraryTable libraryTable) { - myClasspathPanel = classpathPanel; - myLibraryTable = libraryTable; - } - - public List getChosenElements() { - return myChosenLibrary == null? Collections.emptyList() : Collections.singletonList(myChosenLibrary); - } - - public void doChoose() { - final LibraryTable.ModifiableModel libraryModifiableModel = myLibraryTable.getModifiableModel(); - final LibraryTableModifiableModelProvider provider = new LibraryTableModifiableModelProvider() { - public LibraryTable.ModifiableModel getModifiableModel() { - return libraryModifiableModel; - } - - public String getTableLevel() { - return myLibraryTable.getTableLevel(); - } - - public LibraryTablePresentation getLibraryTablePresentation() { - return myLibraryTable.getPresentation(); - } - - public boolean isLibraryTableEditable() { - return false; - } - }; - final Library library = myLibraryTable.createLibrary(); - final LibraryTableEditor editor = LibraryTableEditor.editLibrary(provider, library, myClasspathPanel.getProject()); - final Module contextModule = DataKeys.MODULE_CONTEXT.getData(DataManager.getInstance().getDataContext(myClasspathPanel.getComponent())); - editor.addFileChooserContext(LangDataKeys.MODULE_CONTEXT, contextModule); - myIsOk = editor.openDialog(myClasspathPanel.getComponent(), Collections.singletonList(library), true) != null; - if (myIsOk) { - myChosenLibrary = library; - } - else { - myChosenLibrary = null; - libraryModifiableModel.removeLibrary(library); - } - } - - public boolean isOK() { - return myIsOk; - } - - public void dispose() { - } -} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseModuleLibrariesDialog.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateSingleEntryModuleLibraryDialog.java similarity index 67% rename from java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseModuleLibrariesDialog.java rename to java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateSingleEntryModuleLibraryDialog.java index e4b7970fc954..87a854dcf46c 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ChooseModuleLibrariesDialog.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/CreateSingleEntryModuleLibraryDialog.java @@ -17,34 +17,33 @@ package com.intellij.openapi.roots.ui.configuration.classpath; import com.intellij.ide.DataManager; import com.intellij.openapi.actionSystem.LangDataKeys; +import com.intellij.openapi.fileChooser.FileChooser; import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.module.Module; import com.intellij.openapi.roots.OrderRootType; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.libraries.LibraryTable; -import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryFileChooser; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.containers.ContainerUtil; -import org.jetbrains.annotations.Nullable; +import javax.swing.*; import java.awt.*; -import java.awt.List; import java.util.*; +import java.util.List; /** * @author nik */ -class ChooseModuleLibrariesDialog extends LibraryFileChooser implements ClasspathElementChooserDialog { - private Pair myLastChosen; - private final LibraryTable myLibraryTable; - @Nullable private final VirtualFile myFileToSelect; +class CreateSingleEntryModuleLibraryDialog implements ClasspathElementChooserDialog { + private ClasspathPanel myClasspathPanel; + private final LibraryTable.ModifiableModel myModuleLibrariesModel; + private VirtualFile[] myChosenFiles; - public ChooseModuleLibrariesDialog(Component parent, final LibraryTable libraryTable, final VirtualFile fileToSelect) { - super(createFileChooserDescriptor(parent), parent, false, null); - myLibraryTable = libraryTable; - myFileToSelect = fileToSelect; + public CreateSingleEntryModuleLibraryDialog(ClasspathPanel classpathPanel, + final LibraryTable.ModifiableModel moduleLibrariesModel) { + myClasspathPanel = classpathPanel; + myModuleLibrariesModel = moduleLibrariesModel; } private static FileChooserDescriptor createFileChooserDescriptor(Component parent) { @@ -54,18 +53,17 @@ class ChooseModuleLibrariesDialog extends LibraryFileChooser implements Classpat return descriptor; } - public java.util.List getChosenElements() { - if (myLastChosen == null) { + public List getChosenElements() { + if (myChosenFiles == null) { return Collections.emptyList(); } - final VirtualFile[] files = filterAlreadyAdded(myLastChosen.getSecond()); + final VirtualFile[] files = filterAlreadyAdded(myChosenFiles); if (files.length == 0) { return Collections.emptyList(); } - final LibraryTable.ModifiableModel modifiableModel = myLibraryTable.getModifiableModel(); - final java.util.List addedLibraries = new ArrayList(files.length); + final List addedLibraries = new ArrayList(files.length); for (VirtualFile file : files) { - final Library library = modifiableModel.createLibrary(null); + final Library library = myModuleLibrariesModel.createLibrary(null); final Library.ModifiableModel libModel = library.getModifiableModel(); libModel.addRoot(file, OrderRootType.CLASSES); libModel.commit(); @@ -80,7 +78,7 @@ class ChooseModuleLibrariesDialog extends LibraryFileChooser implements Classpat } final Set chosenFilesSet = new HashSet(Arrays.asList(files)); final Set alreadyAdded = new HashSet(); - final Library[] libraries = myLibraryTable.getLibraries(); + final Library[] libraries = myModuleLibrariesModel.getLibraries(); for (Library library : libraries) { ContainerUtil.addAll(alreadyAdded, library.getFiles(OrderRootType.CLASSES)); } @@ -89,6 +87,16 @@ class ChooseModuleLibrariesDialog extends LibraryFileChooser implements Classpat } public void doChoose() { - myLastChosen = chooseNameAndFiles(myFileToSelect); + final JComponent parent = myClasspathPanel.getComponent(); + myChosenFiles = FileChooser.chooseFiles(parent, createFileChooserDescriptor(parent)); + } + + @Override + public boolean isOK() { + return myChosenFiles != null && myChosenFiles.length > 0; + } + + @Override + public void dispose() { } } 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 new file mode 100644 index 000000000000..56871dcef63a --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/classpath/ProjectStructureChooseLibrariesDialog.java @@ -0,0 +1,105 @@ +/* + * 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.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.ui.SimpleTextAttributes; +import com.intellij.util.Icons; +import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.util.Collection; + +/** + * @author nik + */ +public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTablesDialog { + private StructureConfigurableContext myContext; + private Collection myAlreadyAddedLibraries; + + public ProjectStructureChooseLibrariesDialog(JComponent parentComponent, + @Nullable Project project, + StructureConfigurableContext context, + Collection alreadyAddedLibraries) { + super(parentComponent, "Choose Libraries", project); + myContext = context; + myAlreadyAddedLibraries = alreadyAddedLibraries; + init(); + } + + @NotNull + @Override + protected Library[] getLibraries(@NotNull LibraryTable table) { + final LibrariesModifiableModel model = getLibrariesModifiableModel(table); + if (model == null) return Library.EMPTY_ARRAY; + return model.getLibraries(); + } + + private LibrariesModifiableModel getLibrariesModifiableModel(LibraryTable table) { + return myContext.myLevel2Providers.get(table.getTableLevel()); + } + + @Override + 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 true; + } + + @NotNull + private String getLibraryName(@NotNull Library library) { + final LibrariesModifiableModel model = getLibrariesModifiableModel(library.getTable()); + if (model != null) { + if (model.hasLibraryEditor(library)) { + return model.getLibraryEditor(library).getName(); + } + } + return library.getName(); + } + + @Override + protected LibrariesTreeNodeBase createLibraryDescriptor(NodeDescriptor parentDescriptor, + Library library) { + final String libraryName = getLibraryName(library); + return new LibraryEditorDescriptor(getProject(), parentDescriptor, library, libraryName); + } + + private static class LibraryEditorDescriptor extends LibrariesTreeNodeBase { + protected LibraryEditorDescriptor(final Project project, final NodeDescriptor parentDescriptor, final Library element, + String libraryName) { + super(project, parentDescriptor, element); + final PresentationData templatePresentation = getTemplatePresentation(); + templatePresentation.setIcons(Icons.LIBRARY_ICON); + templatePresentation.addText(libraryName, SimpleTextAttributes.REGULAR_ATTRIBUTES); + } + } + +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/AbstractLibraryTreeStructure.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/AbstractLibraryTreeStructure.java deleted file mode 100644 index 5bdea37c95dd..000000000000 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/AbstractLibraryTreeStructure.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2000-2009 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.libraryEditor; - -import com.intellij.ide.util.treeView.AbstractTreeStructure; -import com.intellij.ide.util.treeView.NodeDescriptor; -import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.roots.libraries.Library; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.Arrays; - -/** - * @author nik - */ -public abstract class AbstractLibraryTreeStructure extends AbstractTreeStructure { - protected NodeDescriptor myRootElementDescriptor; - protected final LibraryTableEditor myParentEditor; - - public AbstractLibraryTreeStructure(LibraryTableEditor parentElement) { - myParentEditor = parentElement; - } - - public void commit() { - } - - public boolean hasSomethingToCommit() { - return false; - } - - protected Object[] buildItems(LibraryTableTreeContentElement parent, Library library, OrderRootType orderRootType) { - ArrayList items = new ArrayList(); - final LibraryEditor libraryEditor = myParentEditor.getLibraryEditor(library); - final String[] urls = libraryEditor.getUrls(orderRootType).clone(); - Arrays.sort(urls, LibraryTableEditor.ourUrlComparator); - for (String url : urls) { - items.add(new ItemElement(parent, library, url, orderRootType, libraryEditor.isJarDirectory(url), libraryEditor.isValid(url, orderRootType))); - } - return items.toArray(); - } - - public Object getParentElement(Object element) { - Object rootElement = getRootElement(); - if (element == rootElement) { - return null; - } - if (element instanceof LibraryTableTreeContentElement) { - return ((LibraryTableTreeContentElement)element).getParent(); - } - return rootElement; - } - - @NotNull - public NodeDescriptor createDescriptor(Object element, NodeDescriptor parentDescriptor) { - if (element == getRootElement()) { - return myRootElementDescriptor; - } - if (element instanceof LibraryTableTreeContentElement) { - return ((LibraryTableTreeContentElement)element).createDescriptor(parentDescriptor, myParentEditor); - } - return null; - } -} 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 new file mode 100644 index 000000000000..8a8f2a03e56c --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/CreateNewLibraryAction.java @@ -0,0 +1,112 @@ +/* + * 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.libraryEditor; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.module.Module; +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.projectRoot.BaseLibrariesConfigurable; +import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel; +import com.intellij.openapi.roots.ui.configuration.projectRoot.ModuleStructureConfigurable; +import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureLibraryTableModifiableModelProvider; +import com.intellij.openapi.ui.InputValidator; +import com.intellij.openapi.ui.MasterDetailsComponent; +import com.intellij.openapi.ui.Messages; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.ui.UIUtil; + +import javax.swing.*; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.TreeNode; +import java.util.ArrayList; +import java.util.List; + +/** +* @author nik +*/ +public class CreateNewLibraryAction extends AnAction { + private StructureLibraryTableModifiableModelProvider myModelProvider; + private Project myProject; + + public CreateNewLibraryAction(String text, StructureLibraryTableModifiableModelProvider modelProvider, final Project project) { + super(text); + myModelProvider = modelProvider; + myProject = project; + } + + @Override + public void actionPerformed(AnActionEvent e) { + final LibrariesModifiableModel modifiableModel = myModelProvider.getModifiableModel(); + final String initial = LibraryTableEditor.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(); + final String libraryName = Messages.showInputDialog(myProject, prompt, title, icon, initial, new InputValidator() { + public boolean checkInput(final String inputString) { + return true; + } + public boolean canClose(final String inputString) { + if (inputString.length() == 0) { + Messages.showErrorDialog(ProjectBundle.message("library.name.not.specified.error"), ProjectBundle.message("library.name.not.specified.title")); + return false; + } + if (LibraryTableEditor.libraryAlreadyExists(modifiableModel, inputString)) { + Messages.showErrorDialog(ProjectBundle.message("library.name.already.exists.error", inputString), ProjectBundle.message("library.name.already.exists.title")); + return false; + } + return true; + } + }); + if (libraryName == null) return; + final Library library = modifiableModel.createLibrary(libraryName); + if (myProject != null){ + final BaseLibrariesConfigurable rootConfigurable = ProjectStructureConfigurable.getInstance(myProject).getConfigurableFor(library); + final ExistingLibraryEditor libraryEditor = modifiableModel.getLibraryEditor(library); + if (libraryEditor.hasChanges()) { + ApplicationManager.getApplication().runWriteAction(new Runnable(){ + public void run() { + libraryEditor.commit(); //update lib node + } + }); + } + final DefaultMutableTreeNode + libraryNode = MasterDetailsComponent.findNodeByObject((TreeNode)rootConfigurable.getTree().getModel().getRoot(), library); + rootConfigurable.selectNodeInTree(libraryNode); + appendLibraryToModules(ModuleStructureConfigurable.getInstance(myProject), library); + } + } + + private void appendLibraryToModules(final ModuleStructureConfigurable rootConfigurable, final Library libraryToSelect) { + final List modules = new ArrayList(); + ContainerUtil.addAll(modules, rootConfigurable.getModules()); + final ChooseModulesDialog dlg = new ChooseModulesDialog(myProject, + modules, ProjectBundle.message("choose.modules.dialog.title"), + ProjectBundle + .message("choose.modules.dialog.description", libraryToSelect.getName())); + dlg.show(); + if (dlg.isOK()) { + final List choosenModules = dlg.getChosenElements(); + for (Module module : choosenModules) { + rootConfigurable.addLibraryOrderEntry(module, libraryToSelect); + } + } + } +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/CreateNewLibraryDialog.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/CreateNewLibraryDialog.java new file mode 100644 index 000000000000..c5ab576bd9cc --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/CreateNewLibraryDialog.java @@ -0,0 +1,92 @@ +/* + * 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.libraryEditor; + +import com.intellij.openapi.Disposable; +import com.intellij.openapi.application.Result; +import com.intellij.openapi.application.WriteAction; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.libraries.Library; +import com.intellij.openapi.roots.libraries.LibraryTable; +import com.intellij.openapi.ui.ComboBox; +import com.intellij.openapi.util.Disposer; +import com.intellij.util.ui.FormBuilder; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.awt.*; +import java.util.List; + +/** + * @author nik + */ +public class CreateNewLibraryDialog extends LibraryEditorDialogBase { + private NewLibraryEditor myLibraryEditor; + private ComboBox myLibraryLevelCombobox; + + public static CreateNewLibraryDialog createDialog(JComponent parent, @Nullable Project project, @NotNull Disposable parentDisposable, + @NotNull List libraryTables, + int selectedTable) { + NewLibraryEditor libraryEditor = new NewLibraryEditor(); + Disposer.register(parentDisposable, libraryEditor); + return new CreateNewLibraryDialog(parent, project, libraryEditor, libraryTables, selectedTable); + } + + private CreateNewLibraryDialog(@NotNull JComponent parent, @Nullable Project project, @NotNull NewLibraryEditor libraryEditor, + @NotNull List libraryTables, int selectedTable) { + super(parent, LibraryTableEditor.editLibrary(project, libraryEditor)); + myLibraryEditor = libraryEditor; + final DefaultComboBoxModel model = new DefaultComboBoxModel(); + for (LibraryTable table : libraryTables) { + model.addElement(table); + } + myLibraryLevelCombobox = new ComboBox(model); + myLibraryLevelCombobox.setSelectedIndex(selectedTable); + myLibraryLevelCombobox.setRenderer(new DefaultListCellRenderer() { + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + final Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + if (value instanceof LibraryTable) { + setText(((LibraryTable)value).getPresentation().getDisplayName(false)); + } + return component; + } + }); + init(); + } + + public LibraryTable getSelectedTable() { + return (LibraryTable)myLibraryLevelCombobox.getSelectedItem(); + } + + public Library createLibrary(final @NotNull LibraryTable.ModifiableModel modifiableModel) { + final Library library = modifiableModel.createLibrary(myLibraryEditor.getName()); + final Library.ModifiableModel model = library.getModifiableModel(); + myLibraryEditor.apply(model); + new WriteAction() { + protected void run(final Result result) { + model.commit(); + } + }.execute(); + return library; + } + + @Override + protected void addNorthComponents(FormBuilder formBuilder) { + formBuilder.addLabeledComponent("Level:", myLibraryLevelCombobox); + } +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/EditExistingLibraryDialog.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/EditExistingLibraryDialog.java new file mode 100644 index 000000000000..269e5ecc692f --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/EditExistingLibraryDialog.java @@ -0,0 +1,82 @@ +/* + * 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.libraryEditor; + +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.libraries.Library; +import com.intellij.openapi.roots.libraries.LibraryTable; +import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; +import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel; +import com.intellij.openapi.util.Disposer; +import org.jetbrains.annotations.Nullable; + +import java.awt.*; + +/** + * @author nik + */ +public class EditExistingLibraryDialog extends LibraryEditorDialogBase { + private ExistingLibraryEditor myLibraryEditor; + private boolean myCommitChanges; + private LibraryTable.ModifiableModel myTableModifiableModel; + + public static EditExistingLibraryDialog createDialog(Component parent, + LibraryTableModifiableModelProvider modelProvider, + Library library, + @Nullable Project project) { + LibraryTable.ModifiableModel modifiableModel = modelProvider.getModifiableModel(); + boolean commitChanges = false; + ExistingLibraryEditor libraryEditor; + if (modifiableModel instanceof LibrariesModifiableModel) { + libraryEditor = ((LibrariesModifiableModel)modifiableModel).getLibraryEditor(library); + } + else { + libraryEditor = new ExistingLibraryEditor(library, null); + commitChanges = true; + } + return new EditExistingLibraryDialog(parent, modifiableModel, project, libraryEditor, commitChanges); + } + + private EditExistingLibraryDialog(Component parent, + LibraryTable.ModifiableModel tableModifiableModel, + @Nullable Project project, ExistingLibraryEditor libraryEditor, boolean commitChanges) { + super(parent, LibraryTableEditor.editLibrary(project, libraryEditor)); + myTableModifiableModel = tableModifiableModel; + myLibraryEditor = libraryEditor; + myCommitChanges = commitChanges; + if (commitChanges) { + Disposer.register(getDisposable(), libraryEditor); + } + init(); + } + + @Override + protected boolean validateAndApply() { + if (!super.validateAndApply()) { + return false; + } + + if (myCommitChanges) { + myLibraryEditor.commit(); + } + return true; + } + + @Override + protected LibraryTable.ModifiableModel getTableModifiableModel() { + return myTableModifiableModel; + } +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/ExistingLibraryEditor.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/ExistingLibraryEditor.java new file mode 100644 index 000000000000..1ba3c491c250 --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/ExistingLibraryEditor.java @@ -0,0 +1,130 @@ +/* + * Copyright 2000-2009 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.libraryEditor; + +import com.intellij.openapi.roots.OrderRootType; +import com.intellij.openapi.roots.libraries.Library; +import com.intellij.openapi.util.Disposer; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.Nullable; + +public class ExistingLibraryEditor implements LibraryEditor { + private final Library myLibrary; + private final LibraryEditorListener myListener; + private String myLibraryName = null; + private Library.ModifiableModel myModel = null; + + public ExistingLibraryEditor(Library library, @Nullable LibraryEditorListener listener) { + myLibrary = library; + myListener = listener; + } + + public Library getLibrary() { + return myLibrary; + } + + @Override + public String getName() { + if (myLibraryName != null) { + return myLibraryName; + } + return myLibrary.getName(); + } + + public void dispose() { + if (myModel != null) { + // dispose if wasn't committed + Disposer.dispose(myModel); + } + } + + @Override + public String[] getUrls(OrderRootType rootType) { + if (myModel != null) { + return myModel.getUrls(rootType); + } + return myLibrary.getUrls(rootType); + } + + @Override + public VirtualFile[] getFiles(OrderRootType rootType) { + if (myModel != null) { + return myModel.getFiles(rootType); + } + return myLibrary.getFiles(rootType); + } + + @Override + public void setName(String name) { + String oldName = getModel().getName(); + myLibraryName = name; + getModel().setName(name); + if (myListener != null) { + myListener.libraryRenamed(myLibrary, oldName, name); + } + } + + @Override + public void addRoot(VirtualFile file, OrderRootType rootType) { + getModel().addRoot(file, rootType); + } + + @Override + public void addJarDirectory(VirtualFile file, boolean recursive) { + getModel().addJarDirectory(file, recursive); + } + + @Override + public void removeRoot(String url, OrderRootType rootType) { + while (getModel().removeRoot(url, rootType)) ; + } + + public void commit() { + if (myModel != null) { + myModel.commit(); + myModel = null; + myLibraryName = null; + } + } + + public Library.ModifiableModel getModel() { + if (myModel == null) { + myModel = myLibrary.getModifiableModel(); + } + return myModel; + } + + @Override + public boolean hasChanges() { + return myModel != null && myModel.isChanged(); + } + + @Override + public boolean isJarDirectory(String url) { + if (myModel != null) { + return myModel.isJarDirectory(url); + } + return myLibrary.isJarDirectory(url); + } + + @Override + public boolean isValid(final String url, final OrderRootType orderRootType) { + if (myModel != null) { + return myModel.isValid(url, orderRootType); + } + return myLibrary.isValid(url, orderRootType); + } +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/ItemElement.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/ItemElement.java index 42e6bcc7a6cc..164752114399 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/ItemElement.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/ItemElement.java @@ -17,20 +17,18 @@ package com.intellij.openapi.roots.ui.configuration.libraryEditor; import com.intellij.ide.util.treeView.NodeDescriptor; import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.roots.libraries.Library; class ItemElement extends LibraryTableTreeContentElement { private final LibraryTableTreeContentElement myParent; - private final Library myLibrary; private final String myUrl; private final OrderRootType myRootType; private final boolean myIsJarDirectory; private final boolean myValid; - public ItemElement(LibraryTableTreeContentElement parent, Library library, String url, OrderRootType rootType, final boolean isJarDirectory, boolean isValid) { + public ItemElement(LibraryTableTreeContentElement parent, String url, OrderRootType rootType, final boolean isJarDirectory, + boolean isValid) { myParent = parent; - myLibrary = library; myUrl = url; myRootType = rootType; myIsJarDirectory = isJarDirectory; @@ -65,17 +63,12 @@ class ItemElement extends LibraryTableTreeContentElement { return myRootType; } - public Library getLibrary() { - return myLibrary; - } - public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof ItemElement)) return false; final ItemElement itemElement = (ItemElement)o; - if (!myLibrary.equals(itemElement.myLibrary)) return false; if (!myParent.equals(itemElement.myParent)) return false; if (!myRootType.equals(itemElement.myRootType)) return false; if (!myUrl.equals(itemElement.myUrl)) return false; @@ -86,7 +79,6 @@ class ItemElement extends LibraryTableTreeContentElement { public int hashCode() { int result; result = myParent.hashCode(); - result = 29 * result + myLibrary.hashCode(); result = 29 * result + myUrl.hashCode(); result = 29 * result + myRootType.hashCode(); return result; diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryEditor.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryEditor.java index a14a4e827b99..499215d0caac 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryEditor.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryEditor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -17,115 +17,29 @@ package com.intellij.openapi.roots.ui.configuration.libraryEditor; import com.intellij.openapi.Disposable; import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.roots.impl.libraries.LibraryEx; -import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.util.Disposer; import com.intellij.openapi.vfs.VirtualFile; -import org.jetbrains.annotations.NotNull; -public class LibraryEditor implements Disposable { - private final Library myLibrary; - private final LibraryEditorListener myListener; - private String myLibraryName = null; - private Library.ModifiableModel myModel = null; +/** + * @author nik + */ +public interface LibraryEditor extends Disposable { + String getName(); - public LibraryEditor(Library library, @NotNull LibraryEditorListener listener) { - myLibrary = library; - myListener = listener; - } + String[] getUrls(OrderRootType rootType); - public String getName() { - if (myLibraryName != null) { - return myLibraryName; - } - return myLibrary.getName(); - } + VirtualFile[] getFiles(OrderRootType rootType); - public void dispose() { - if (myModel != null) { - // dispose if wasn't committed - Disposer.dispose(myModel); - } - } + void setName(String name); - public String[] getUrls(OrderRootType rootType) { - if (myModel != null) { - return myModel.getUrls(rootType); - } - return myLibrary.getUrls(rootType); - } + void addRoot(VirtualFile file, OrderRootType rootType); - public VirtualFile[] getFiles(OrderRootType rootType) { - if (myModel != null) { - return myModel.getFiles(rootType); - } - return myLibrary.getFiles(rootType); - } + void addJarDirectory(VirtualFile file, boolean recursive); - public void setName(String name) { - String oldName = getModel().getName(); - myLibraryName = name; - getModel().setName(name); - myListener.libraryRenamed(myLibrary, oldName, name); - } + void removeRoot(String url, OrderRootType rootType); - public void addRoot(String url, OrderRootType rootType) { - getModel().addRoot(url, rootType); - } + boolean hasChanges(); - public void addRoot(VirtualFile file, OrderRootType rootType) { - getModel().addRoot(file, rootType); - } + boolean isJarDirectory(String url); - public void addJarDirectory(String url, boolean recursive) { - getModel().addJarDirectory(url, recursive); - } - - public void addJarDirectory(VirtualFile file, boolean recursive) { - getModel().addJarDirectory(file, recursive); - } - - public void removeRoot(String url, OrderRootType rootType) { - while (getModel().removeRoot(url, rootType)) ; - } - - public void commit() { - if (myModel != null) { - myModel.commit(); - myModel = null; - myLibraryName = null; - } - } - - public Library.ModifiableModel getModel() { - if (myModel == null) { - myModel = myLibrary.getModifiableModel(); - } - return myModel; - } - - public boolean hasChanges() { - return myModel != null && myModel.isChanged(); - } - - public boolean isJarDirectory(String url) { - if (myModel != null) { - return myModel.isJarDirectory(url); - } - return myLibrary.isJarDirectory(url); - } - - public boolean allPathsValid(OrderRootType orderRootType) { - if (myModel != null) { - return ((LibraryEx.ModifiableModelEx)myModel).allPathsValid(orderRootType); - } - return ((LibraryEx)myLibrary).allPathsValid(orderRootType); - } - - public boolean isValid(final String url, final OrderRootType orderRootType) { - if (myModel != null) { - return myModel.isValid(url, orderRootType); - } - return myLibrary.isValid(url, orderRootType); - } + boolean isValid(String url, OrderRootType orderRootType); } 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 new file mode 100644 index 000000000000..43593821ed48 --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryEditorDialogBase.java @@ -0,0 +1,112 @@ +/* + * 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.libraryEditor; + +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.ui.DialogWrapper; +import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.util.Disposer; +import com.intellij.util.ui.FormBuilder; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.awt.*; + +/** +* @author nik +*/ +public abstract class LibraryEditorDialogBase extends DialogWrapper { + private JTextField myNameField; + private LibraryTableEditor myLibraryTableEditor; + + public LibraryEditorDialogBase(final Component parent, final LibraryTableEditor libraryTableEditor) { + super(parent, true); + myLibraryTableEditor = libraryTableEditor; + setTitle(ProjectBundle.message("library.configure.title")); + Disposer.register(getDisposable(), myLibraryTableEditor); + } + + public void addFileChooserContext(DataKey key, T value) { + myLibraryTableEditor.addFileChooserContext(key, value); + } + + protected String getDimensionServiceKey() { + return "#com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditorDialog"; + } + + public JComponent getPreferredFocusedComponent() { + return myNameField; + } + + protected final void doOKAction() { + if (!validateAndApply()) { + return; + } + super.doOKAction(); + } + + protected boolean validateAndApply() { + final String currentName = myLibraryTableEditor.getLibraryEditor().getName(); + String newName = myNameField.getText().trim(); + if (newName.length() == 0) { + newName = null; + } + if (!Comparing.equal(newName, currentName)) { + final LibraryTable.ModifiableModel tableModifiableModel = getTableModifiableModel(); + if (tableModifiableModel != null && !(tableModifiableModel instanceof ModuleLibraryTable)) { + if (newName == null) { + Messages.showErrorDialog(ProjectBundle.message("library.name.not.specified.error", newName), ProjectBundle.message("library.name.not.specified.title")); + return false; + } + if (LibraryTableEditor.libraryAlreadyExists(tableModifiableModel, newName)) { + Messages.showErrorDialog(ProjectBundle.message("library.name.already.exists.error", newName), ProjectBundle.message("library.name.already.exists.title")); + return false; + } + } + myLibraryTableEditor.renameLibrary(newName); + } + return true; + } + + @Nullable + protected LibraryTable.ModifiableModel getTableModifiableModel() { + return null; + } + + protected JComponent createNorthPanel() { + FormBuilder formBuilder = new FormBuilder(); + String currentName = myLibraryTableEditor.getLibraryEditor().getName(); + myNameField = new JTextField(currentName); + formBuilder.addLabeledComponent("&Name:", myNameField); + addNorthComponents(formBuilder); + myNameField.selectAll(); + + final JPanel panel = formBuilder.getPanel(); + panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); + return panel; + } + + protected void addNorthComponents(FormBuilder formBuilder) { + } + + protected JComponent createCenterPanel() { + return myLibraryTableEditor.getComponent(); + } +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryElement.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryElement.java index 07ec1515d38c..93922c349189 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryElement.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryElement.java @@ -17,25 +17,18 @@ package com.intellij.openapi.roots.ui.configuration.libraryEditor; import com.intellij.ide.util.treeView.NodeDescriptor; import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.roots.libraries.Library; public class LibraryElement extends LibraryTableTreeContentElement { - private final Library myLibrary; private final LibraryTableEditor myParentEditor; private final boolean myHasInvalidPaths; - public LibraryElement(Library library, LibraryTableEditor parentEditor, final boolean hasInvalidPaths) { - myLibrary = library; + public LibraryElement(LibraryTableEditor parentEditor, final boolean hasInvalidPaths) { myParentEditor = parentEditor; myHasInvalidPaths = hasInvalidPaths; } - public Library getLibrary() { - return myLibrary; - } - public boolean isAnonymous() { - final String name = myParentEditor.getLibraryEditor(myLibrary).getName(); + final String name = myParentEditor.getLibraryEditor().getName(); return name == null; } @@ -45,22 +38,11 @@ public class LibraryElement extends LibraryTableTreeContentElement { public boolean equals(Object o) { if (this == o) return true; - if (!(o instanceof LibraryElement)) { - return false; - } - - final LibraryElement libraryElement = (LibraryElement)o; - - - if (!myLibrary.equals(libraryElement.myLibrary)) { - return false; - } - - return true; + return o instanceof LibraryElement; } public int hashCode() { - return myLibrary.hashCode(); + return 0; } public LibraryTableTreeContentElement getParent() { diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryElementDescriptor.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryElementDescriptor.java index 68871923126c..6662d033d41a 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryElementDescriptor.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryElementDescriptor.java @@ -18,7 +18,6 @@ package com.intellij.openapi.roots.ui.configuration.libraryEditor; import com.intellij.ide.util.treeView.NodeDescriptor; import com.intellij.openapi.project.ProjectBundle; import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.Icons; @@ -37,10 +36,9 @@ class LibraryElementDescriptor extends NodeDescriptor { } public boolean update() { - final Library library = myElement.getLibrary(); final String name; final Icon icon; - final LibraryEditor libraryEditor = myParentEditor.getLibraryEditor(library); + final LibraryEditor libraryEditor = myParentEditor.getLibraryEditor(); if (myElement.isAnonymous()) { final VirtualFile[] files = libraryEditor.getFiles(OrderRootType.CLASSES); if (files.length > 0) { diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryFileChooser.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryFileChooser.java deleted file mode 100644 index 85373c8191fd..000000000000 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryFileChooser.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright 2000-2009 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.libraryEditor; - -import com.intellij.openapi.fileChooser.FileChooserDescriptor; -import com.intellij.openapi.fileChooser.ex.FileChooserDialogImpl; -import com.intellij.openapi.project.ProjectBundle; -import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.util.Pair; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.ui.FieldPanel; -import com.intellij.util.ui.UIUtil; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; -import javax.swing.event.TreeSelectionEvent; -import javax.swing.event.TreeSelectionListener; -import java.awt.*; - -public class LibraryFileChooser extends FileChooserDialogImpl { - private JTextField myNameField; - private final boolean myInputName; - private final LibraryTableEditor myParentEditor; - private boolean myNameChangedByUser = false; - - public LibraryFileChooser(FileChooserDescriptor chooserDescriptor, - Component parent, - boolean inputName, - LibraryTableEditor parentEditor) { - super(chooserDescriptor, parent); - myInputName = inputName; - myParentEditor = parentEditor; - } - - public String getName() { - if (myNameField != null) { - final String name = myNameField.getText().trim(); - return name.length() > 0 ? name : null; - } - return null; - } - - private void setName(String name) { - if (myNameField != null) { - final boolean savedValue = myNameChangedByUser; - try { - myNameField.setText(name); - } - finally { - myNameChangedByUser = savedValue; - } - } - } - - public JComponent getPreferredFocusedComponent() { - return myInputName ? myNameField : super.getPreferredFocusedComponent(); - } - - protected JComponent createCenterPanel() { - final JComponent centerPanel = super.createCenterPanel(); - if (!myInputName) { - return centerPanel; - } - - final JPanel panel = new JPanel(new BorderLayout()); - panel.add(centerPanel, BorderLayout.CENTER); - - final FieldPanel fieldPanel = FieldPanel.create(ProjectBundle.message("library.name.prompt"), null); - fieldPanel.getFieldLabel().setFont(UIUtil.getLabelFont().deriveFont(Font.BOLD)); - myNameField = fieldPanel.getTextField(); - myNameField.getDocument().addDocumentListener(new DocumentListener() { - public void changedUpdate(DocumentEvent e) { - myNameChangedByUser = true; - } - - public void insertUpdate(DocumentEvent e) { - myNameChangedByUser = true; - } - - public void removeUpdate(DocumentEvent e) { - myNameChangedByUser = true; - } - }); - panel.add(fieldPanel, BorderLayout.NORTH); - - myFileSystemTree.getTree().addTreeSelectionListener(new TreeSelectionListener() { - public void valueChanged(TreeSelectionEvent e) { - if (myNameField == null || myNameChangedByUser) { - return; - } - final VirtualFile[] selectedFiles = getSelectedFiles(); - setName(selectedFiles.length == 1 ? selectedFiles[0].getNameWithoutExtension() : ""); - } - }); - return panel; - } - - protected void doOKAction() { - if (!validateData()) { - return; - } - - super.doOKAction(); - } - - private boolean validateData() { - JComponent componentToFocus = null; - try { - final VirtualFile[] chosenFiles = getSelectedFiles(); - if (chosenFiles != null && chosenFiles.length > 0) { - if (myInputName) { - final String name = getName(); - if (name == null) { - Messages.showErrorDialog(myNameField, ProjectBundle.message("library.name.not.specified.error"), - ProjectBundle.message("library.name.not.specified.title")); - componentToFocus = myNameField; - return false; - } - if (myParentEditor.libraryAlreadyExists(name)) { - Messages.showErrorDialog(myNameField, ProjectBundle.message("library.name.already.exists.error", name), - ProjectBundle.message("library.name.already.exists.title")); - componentToFocus = myNameField; - return false; - } - } - } - else { - Messages.showErrorDialog(ProjectBundle.message("library.files.not.selected.error"), - ProjectBundle.message("library.files.not.selected.title")); - componentToFocus = myFileSystemTree.getTree(); - return false; - } - return true; - } - finally { - if (componentToFocus != null) { - final JComponent _componentToFocus = componentToFocus; - SwingUtilities.invokeLater(new Runnable() { - public void run() { - _componentToFocus.requestFocus(); - } - }); - } - } - } - - public Pair chooseNameAndFiles(@Nullable VirtualFile toSelect) { - VirtualFile[] chosenFiles = choose(toSelect, null); - return new Pair(getName(), chosenFiles); - } - - public Pair chooseNameAndFiles() { - return chooseNameAndFiles(null); - } -} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryRootsData.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryRootsData.java new file mode 100644 index 000000000000..6b7bebf62e13 --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryRootsData.java @@ -0,0 +1,24 @@ +/* + * 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.libraryEditor; + +/** + * @author nik + */ +public class LibraryRootsData { + + +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableEditor.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableEditor.java index a8124ef78e0a..6b6e7066d627 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableEditor.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableEditor.java @@ -21,7 +21,6 @@ import com.intellij.ide.util.treeView.NodeDescriptor; import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.DataKey; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.application.ModalityState; import com.intellij.openapi.fileChooser.FileChooser; import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.module.Module; @@ -32,27 +31,22 @@ import com.intellij.openapi.roots.AnnotationOrderRootType; 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.impl.libraries.LibraryTableImplUtil; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.libraries.LibraryTable; -import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar; import com.intellij.openapi.roots.libraries.LibraryUtil; -import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; import com.intellij.openapi.roots.ui.configuration.ModuleEditor; import com.intellij.openapi.roots.ui.configuration.PathUIUtils; -import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable; -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; -import com.intellij.openapi.ui.DialogWrapper; -import com.intellij.openapi.ui.InputValidator; -import com.intellij.openapi.ui.MasterDetailsComponent; -import com.intellij.openapi.ui.Messages; 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.openapi.util.*; +import com.intellij.openapi.util.Disposer; +import com.intellij.openapi.util.IconLoader; +import com.intellij.openapi.util.NullableComputable; +import com.intellij.openapi.util.Ref; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.*; import com.intellij.openapi.vfs.ex.http.HttpFileSystem; import com.intellij.ui.ScrollPaneFactory; @@ -61,7 +55,6 @@ import com.intellij.ui.treeStructure.Tree; import com.intellij.util.ArrayUtil; import com.intellij.util.Icons; import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -70,7 +63,6 @@ import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; -import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; import java.awt.*; import java.awt.event.ActionEvent; @@ -85,13 +77,11 @@ import java.util.List; * @author Eugene Zhuravlev * Date: Jan 11, 2004 */ -public class LibraryTableEditor implements Disposable, LibraryEditorListener { +public class LibraryTableEditor implements Disposable { static final UrlComparator ourUrlComparator = new UrlComparator(); private JPanel myPanel; - private JButton myAddLibraryButton; private JButton myRemoveButton; - private JButton myRenameLibraryButton; private JButton myAttachClassesButton; private JButton myAttachJarDirectoriesButton; private JButton myAttachSourcesButton; @@ -101,57 +91,46 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { private JButton myAttachAnnotationsButton; private JButton myAttachMoreButton; private Tree myTree; - private final Map myLibraryToEditorMap = new HashMap(); - - private final LibraryTableModifiableModelProvider myLibraryTableProvider; - private final boolean myEditingModuleLibraries; private LibraryTableTreeBuilder myTreeBuilder; - private LibraryTable.ModifiableModel myTableModifiableModel; private static final Icon INVALID_ITEM_ICON = IconLoader.getIcon("/nodes/ppInvalid.png"); private static final Icon JAR_DIRECTORY_ICON = IconLoader.getIcon("/nodes/jarDirectory.png"); private final Collection myListeners = new ArrayList(); - private final List myLibraryEditorListeners = new ArrayList(); @Nullable private final Project myProject; private final Map myFileChooserUserData = new HashMap(); + private final LibraryEditor myLibraryEditor; - private LibraryTableEditor(LibraryTableModifiableModelProvider provider, Project project){ + private LibraryTableEditor(Project project, + LibraryEditor libraryEditor){ myProject = project; - myLibraryTableProvider = provider; - myTableModifiableModel = myLibraryTableProvider.getModifiableModel(); - final String tableLevel = provider.getTableLevel(); - myEditingModuleLibraries = LibraryTableImplUtil.MODULE_LEVEL.equals(tableLevel); - if (!provider.isLibraryTableEditable()) { - myAddLibraryButton.setVisible(false); - myRenameLibraryButton.setVisible(false); + myLibraryEditor = libraryEditor; + } + + public static LibraryTableEditor editLibrary(final @Nullable Project project, @NotNull LibraryEditor libraryEditor) { + LibraryTableEditor tableEditor = new LibraryTableEditor(project, libraryEditor); + tableEditor.init(new LibraryTreeStructure(tableEditor)); + if (project != null) { + Disposer.register(project, tableEditor); } - } - - public static LibraryTableEditor editLibraryTable(LibraryTableModifiableModelProvider provider, Project project) { - LibraryTableEditor result = new LibraryTableEditor(provider,project); - result.init(new LibraryTableTreeStructure(result)); - return result; - } - - public static LibraryTableEditor editLibrary(final LibraryTableModifiableModelProvider provider, - final Library library, - final Project project) { - LibraryTableEditor tableEditor = new LibraryTableEditor(provider,project); - tableEditor.init(new LibraryTreeStructure(tableEditor, library)); - Disposer.register(project, tableEditor); return tableEditor; } - public static LibraryTableEditor editLibrary(LibraryTableModifiableModelProvider provider, Library library) { - LibraryTableEditor result = new LibraryTableEditor(provider,null); - result.init(new LibraryTreeStructure(result, library)); - return result; + public static LibraryTableEditor editLibrary(@NotNull LibraryEditor libraryEditor) { + return editLibrary(null, libraryEditor); } - private static boolean libraryAlreadyExists(LibraryTable.ModifiableModel table, String libraryName) { + public static boolean libraryAlreadyExists(LibraryTable.ModifiableModel table, String libraryName) { for (Iterator it = table.getLibraryIterator(); it.hasNext(); ) { - if (libraryName.equals(it.next().getName())) { + 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; } } @@ -177,16 +156,7 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { myTreePanel.setLayout(new BorderLayout()); myTreePanel.add(ScrollPaneFactory.createScrollPane(myTree), BorderLayout.CENTER); - myAddLibraryButton.setText(myEditingModuleLibraries? ProjectBundle.message("library.add.jar.directory.action") : - ProjectBundle.message("library.create.library.action")); - myAddLibraryButton.addActionListener(new AddLibraryAction()); myRemoveButton.addActionListener(new RemoveAction()); - if (myEditingModuleLibraries) { - myRenameLibraryButton.setVisible(false); - } - else if (myRenameLibraryButton.isVisible()){ - myRenameLibraryButton.addActionListener(new RenameLibraryAction()); - } myAttachClassesButton.addActionListener(new AttachClassesAction()); myAttachJarDirectoriesButton.addActionListener(new AttachJarDirectoriesAction()); myAttachSourcesButton.addActionListener(new AttachSourcesAction()); @@ -195,107 +165,39 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { myAttachAnnotationsButton.addActionListener(new AttachAnnotationsAction()); final LibraryTableAttachHandler[] handlers = LibraryTableAttachHandler.EP_NAME.getExtensions(); - if (handlers.length == 0 || myProject == null) myAttachMoreButton.setVisible(false); - else if (handlers.length == 1) { - myAttachMoreButton.setText(handlers[0].getLongName()); + final LibraryEditor libraryEditor = getLibraryEditor(); + if (handlers.length == 0 || myProject == null || !(libraryEditor instanceof ExistingLibraryEditor)) { + myAttachMoreButton.setVisible(false); + } + else { + myAttachMoreButton.addActionListener(new AttachMoreAction(handlers, (ExistingLibraryEditor)libraryEditor)); + if (handlers.length == 1) { + myAttachMoreButton.setText(handlers[0].getLongName()); + } } - myAttachMoreButton.addActionListener(new AttachMoreAction(handlers)); treeSelectionListener.updateButtons(); - Disposer.register(this, myTreeBuilder); } + public Tree getTree() { + return myTree; + } + public JComponent getComponent() { return myPanel; } - public void selectLibrary(Library library, boolean expand) { - LibraryTableTreeContentElement element = new LibraryElement(library, this, false); - myTreeBuilder.updateAndSelect(element); - } - public void addFileChooserContext(DataKey key, T value) { myFileChooserUserData.put(key, value); } - public LibraryEditor getLibraryEditor(Library library) { - if (myTableModifiableModel instanceof LibrariesModifiableModel){ - return ((LibrariesModifiableModel)myTableModifiableModel).getLibraryEditor(library); - } - LibraryEditor libraryEditor = myLibraryToEditorMap.get(library); - if (libraryEditor == null) { - libraryEditor = new LibraryEditor(library, this); - myLibraryToEditorMap.put(library, libraryEditor); - } - return libraryEditor; - } - - private void removeLibrary(Library library) { - final LibraryEditor libraryEditor = myLibraryToEditorMap.remove(library); - if (libraryEditor != null) Disposer.dispose(libraryEditor); - myTableModifiableModel.removeLibrary(library); - if (myProject != null){ - ModuleStructureConfigurable.getInstance(myProject).fireItemsChangeListener(library); - } - } - - /** - * Should call this method in order to commit all the changes that were done by the editor - */ - public void commitChanges() { - ApplicationManager.getApplication().runWriteAction(new Runnable() { - public void run() { - for (LibraryEditor libraryEditor : myLibraryToEditorMap.values()) { - libraryEditor.commit(); - Disposer.dispose(libraryEditor); - } - myTableModifiableModel.commit(); - } - }); - myTableModifiableModel = myLibraryTableProvider.getModifiableModel(); - myLibraryToEditorMap.clear(); - } - - public void cancelChanges() { - for (LibraryEditor libraryEditor : new ArrayList(myLibraryToEditorMap.values())) { - Disposer.dispose(libraryEditor); - } - - myLibraryToEditorMap.clear(); + public LibraryEditor getLibraryEditor() { + return myLibraryEditor; } public boolean hasChanges() { - if (myTableModifiableModel.isChanged()) { - return true; - } - for (final Library library : myLibraryToEditorMap.keySet()) { - final LibraryEditor libraryEditor = myLibraryToEditorMap.get(library); - if (libraryEditor.hasChanges()) { - return true; - } - } - return false; - } - - public void addLibraryEditorListener(@NotNull LibraryEditorListener listener) { - myLibraryEditorListeners.add(listener); - } - - public void libraryRenamed(@NotNull Library library, String oldName, String newName) { - for (LibraryEditorListener listener : myLibraryEditorListeners) { - listener.libraryRenamed(library, oldName, newName); - } - } - - public Library[] getLibraries() { - return myTableModifiableModel.getLibraries(); - } - - @Nullable - private Object getSelectedElement() { - final TreePath selectionPath = myTreeBuilder.getTree().getSelectionPath(); - return getPathElement(selectionPath); + return getLibraryEditor().hasChanges(); } private Object[] getSelectedElements() { @@ -334,157 +236,16 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { return element; } - @Nullable - private Library getSelectedLibrary() { - if (myTreeBuilder != null && myTreeBuilder.getTreeStructure() instanceof LibraryTreeStructure) { - return ((LibraryTreeStructure)myTreeBuilder.getTreeStructure()).getLibrary(); - } else { - return convertElementToLibrary(getSelectedElement()); - } - } - - private Library[] getSelectedLibraries() { - final List libs = new ArrayList(); - final Object[] selectedElements = getSelectedElements(); - for (Object selectedElement : selectedElements) { - final Library library = convertElementToLibrary(selectedElement); - if (library != null) { - libs.add(library); - } - } - return libs.toArray(new Library[libs.size()]); - } - - @Nullable - private static Library convertElementToLibrary(Object selectedElement) { - LibraryElement libraryElement = null; - if (selectedElement instanceof LibraryElement) { - libraryElement = (LibraryElement)selectedElement; - } - else if (selectedElement instanceof ItemElement) { - selectedElement = ((ItemElement)selectedElement).getParent(); - } - if (selectedElement instanceof ClassesElement) { - libraryElement = ((ClassesElement)selectedElement).getParent(); - } - else if (selectedElement instanceof SourcesElement) { - libraryElement = ((SourcesElement)selectedElement).getParent(); - } - else if (selectedElement instanceof JavadocElement) { - libraryElement = ((JavadocElement)selectedElement).getParent(); - } - return libraryElement != null? libraryElement.getLibrary() : null; - } - - public void renameLibrary(Library library, String newName) { - if (library == null) { - return; - } - final LibraryEditor libraryEditor = getLibraryEditor(library); + public void renameLibrary(String newName) { + final LibraryEditor libraryEditor = getLibraryEditor(); libraryEditor.setName(newName); librariesChanged(false); } - /** - * @return true if Ok button was pressed on dialog close, false otherwise - */ - @Nullable - public Library[] openDialog(final Component parent, final Collection selection, final boolean expandSelectedItems) { - final MyDialogWrapper dialogWrapper = new MyDialogWrapper(parent); - if (selection != null) { - ApplicationManager.getApplication().invokeLater(new Runnable() { - public void run() { - for (final Library library : selection) { - selectLibrary(library, expandSelectedItems); - } - } - }, ModalityState.stateForComponent(dialogWrapper.getContentPane())); - } - dialogWrapper.show(); - return dialogWrapper.isOK() ? dialogWrapper.getSelectedLibraries() : null; - } - - public ActionListener createAddLibraryAction(boolean select){ - return new AddLibraryAction(select); - } - public void dispose() { myTreeBuilder = null; } - private class AddLibraryAction implements ActionListener { - private boolean myNeedSelection; - - public AddLibraryAction() { - this(false); - } - - public AddLibraryAction(boolean select) { - myNeedSelection = select; - } - - public void actionPerformed(ActionEvent e) { - final String initial = suggestNewLibraryName(myTableModifiableModel); - final String prompt = ProjectBundle.message("library.name.prompt"); - final String title = myAddLibraryButton.getText().replaceAll(String.valueOf(UIUtil.MNEMONIC), ""); - final Icon icon = Messages.getQuestionIcon(); - final String libraryName = Messages.showInputDialog(myProject, prompt, title, icon, initial, new InputValidator() { - public boolean checkInput(final String inputString) { - return true; - } - public boolean canClose(final String inputString) { - if (inputString.length() == 0) { - Messages.showErrorDialog(ProjectBundle.message("library.name.not.specified.error"), ProjectBundle.message("library.name.not.specified.title")); - return false; - } - if (libraryAlreadyExists(myTableModifiableModel, inputString)) { - Messages.showErrorDialog(ProjectBundle.message("library.name.already.exists.error", inputString), ProjectBundle.message("library.name.already.exists.title")); - return false; - } - return true; - } - }); - if (libraryName == null) return; - final Library library = myTableModifiableModel.createLibrary(libraryName); - selectLibrary(library, true); - if (myProject != null){ - final BaseLibrariesConfigurable rootConfigurable = ProjectStructureConfigurable.getInstance(myProject).getConfigurableFor(library); - final LibraryEditor libraryEditor = getLibraryEditor(library); - if (libraryEditor.hasChanges()) { - ApplicationManager.getApplication().runWriteAction(new Runnable(){ - public void run() { - libraryEditor.commit(); //update lib node - } - }); - } - final DefaultMutableTreeNode libraryNode = MasterDetailsComponent.findNodeByObject((TreeNode)rootConfigurable.getTree().getModel().getRoot(), library); - if (myNeedSelection){ - rootConfigurable.selectNodeInTree(libraryNode); - if (!myEditingModuleLibraries) { - appendLibraryToModules(ModuleStructureConfigurable.getInstance(myProject), library); - } - } - } - librariesChanged(true); - } - - private void appendLibraryToModules(final ModuleStructureConfigurable rootConfigurable, final Library libraryToSelect) { - final List modules = new ArrayList(); - ContainerUtil.addAll(modules, rootConfigurable.getModules()); - final ChooseModulesDialog dlg = new ChooseModulesDialog(myProject, - modules, ProjectBundle.message("choose.modules.dialog.title"), - ProjectBundle - .message("choose.modules.dialog.description", libraryToSelect.getName())); - dlg.show(); - if (dlg.isOK()) { - final List choosenModules = dlg.getChosenElements(); - for (Module module : choosenModules) { - rootConfigurable.addLibraryOrderEntry(module, libraryToSelect); - } - } - } - } - private abstract class AttachItemAction implements ActionListener { private final FileChooserDescriptor myDescriptor; private VirtualFile myLastChosen = null; @@ -510,31 +271,28 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { } public final void actionPerformed(ActionEvent e) { - final Library library = getSelectedLibrary(); - if (library != null) { - myDescriptor.setTitle(getTitle()); - myDescriptor.setDescription(getDescription()); - for (Map.Entry entry : myFileChooserUserData.entrySet()) { - myDescriptor.putUserData(entry.getKey(), entry.getValue()); - } - VirtualFile toSelect = getFileToSelect(library); - final VirtualFile[] attachedFiles = - attachFiles(library, scanForActualRoots(FileChooser.chooseFiles(myPanel, myDescriptor, toSelect)), getRootType(), - addAsJarDirectories()); - if (attachedFiles.length > 0) { - myLastChosen = attachedFiles[0]; - } + myDescriptor.setTitle(getTitle()); + myDescriptor.setDescription(getDescription()); + for (Map.Entry entry : myFileChooserUserData.entrySet()) { + myDescriptor.putUserData(entry.getKey(), entry.getValue()); + } + VirtualFile toSelect = getFileToSelect(); + final VirtualFile[] attachedFiles = + attachFiles(scanForActualRoots(FileChooser.chooseFiles(myPanel, myDescriptor, toSelect)), getRootType(), + addAsJarDirectories()); + if (attachedFiles.length > 0) { + myLastChosen = attachedFiles[0]; } fireLibrariesChanged(); myTree.requestFocus(); } @Nullable - private VirtualFile getFileToSelect(Library library) { + private VirtualFile getFileToSelect() { VirtualFile toSelect = myLastChosen; if (toSelect == null) { for (OrderRootType orderRootType : OrderRootType.getAllPersistentTypes()) { - final VirtualFile[] existingRoots = library.getFiles(orderRootType); + final VirtualFile[] existingRoots = getLibraryEditor().getFiles(orderRootType); if (existingRoots.length > 0) { VirtualFile existingRoot = existingRoots [0]; if (existingRoot.getFileSystem() instanceof JarFileSystem) { @@ -552,9 +310,10 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { } } } - if (toSelect == null && Comparing.strEqual(myLibraryTableProvider.getTableLevel(), LibraryTablesRegistrar.PROJECT_LEVEL)) { + if (toSelect == null) { final Project project = myProject; if (project != null) { + //todo[nik] perhaps we shouldn't select project base dir if global library is edited toSelect = project.getBaseDir(); } } @@ -562,12 +321,12 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { } } - private VirtualFile[] attachFiles(final Library library, final VirtualFile[] files, final OrderRootType rootType, final boolean isJarDirectories) { - final VirtualFile[] filesToAttach = filterAlreadyAdded(library, files, rootType); + private VirtualFile[] attachFiles(final VirtualFile[] files, final OrderRootType rootType, final boolean isJarDirectories) { + final VirtualFile[] filesToAttach = filterAlreadyAdded(files, rootType); if (filesToAttach.length > 0) { ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { - final LibraryEditor libraryEditor = getLibraryEditor(library); + final LibraryEditor libraryEditor = getLibraryEditor(); for (VirtualFile file : filesToAttach) { if (isJarDirectories) { libraryEditor.addJarDirectory(file, false); @@ -576,9 +335,6 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { libraryEditor.addRoot(file, rootType); } } - if (myEditingModuleLibraries) { - commitChanges(); - } } }); myTreeBuilder.updateFromRoot(); @@ -586,23 +342,14 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { return filesToAttach; } - private VirtualFile[] filterAlreadyAdded(Library lib, VirtualFile[] files, final OrderRootType rootType) { + private VirtualFile[] filterAlreadyAdded(VirtualFile[] files, final OrderRootType rootType) { if (files == null || files.length == 0) { return VirtualFile.EMPTY_ARRAY; } final Set chosenFilesSet = new HashSet(Arrays.asList(files)); final Set alreadyAdded = new HashSet(); - if (lib == null) { - final Library[] libraries = myTableModifiableModel.getLibraries(); - for (Library library : libraries) { - final VirtualFile[] libraryFiles = getLibraryEditor(library).getFiles(rootType); - ContainerUtil.addAll(alreadyAdded, libraryFiles); - } - } - else { - final VirtualFile[] libraryFiles = getLibraryEditor(lib).getFiles(rootType); - ContainerUtil.addAll(alreadyAdded, libraryFiles); - } + final VirtualFile[] libraryFiles = getLibraryEditor().getFiles(rootType); + ContainerUtil.addAll(alreadyAdded, libraryFiles); chosenFilesSet.removeAll(alreadyAdded); return VfsUtil.toVirtualFileArray(chosenFilesSet); } @@ -614,12 +361,12 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { } protected String getTitle() { - final Library selectedLibrary = getSelectedLibrary(); - if (selectedLibrary != null) { - return ProjectBundle.message("library.attach.classes.to.library.action", getLibraryEditor(selectedLibrary).getName()); + final String name = getLibraryEditor().getName(); + if (StringUtil.isEmpty(name)) { + return ProjectBundle.message("library.attach.classes.action"); } else { - return ProjectBundle.message("library.attach.classes.action"); + return ProjectBundle.message("library.attach.classes.to.library.action", name); } } @@ -643,12 +390,12 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { } protected String getTitle() { - final Library selectedLibrary = getSelectedLibrary(); - if (selectedLibrary != null) { - return ProjectBundle.message("library.attach.jar.directory.to.library.action", getLibraryEditor(selectedLibrary).getName()); + final String name = getLibraryEditor().getName(); + if (StringUtil.isEmpty(name)) { + return ProjectBundle.message("library.attach.jar.directory.action"); } else { - return ProjectBundle.message("library.attach.jar.directory.action"); + return ProjectBundle.message("library.attach.jar.directory.to.library.action", name); } } @@ -714,12 +461,9 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { private class AttachUrlJavadocAction implements ActionListener { public void actionPerformed(ActionEvent e) { - final Library library = getSelectedLibrary(); - if (library != null) { - final VirtualFile vFile = Util.showSpecifyJavadocUrlDialog(myPanel); - if (vFile != null) { - attachFiles(library, new VirtualFile[] {vFile}, JavadocOrderRootType.getInstance(), false); - } + final VirtualFile vFile = Util.showSpecifyJavadocUrlDialog(myPanel); + if (vFile != null) { + attachFiles(new VirtualFile[] {vFile}, JavadocOrderRootType.getInstance(), false); } myTree.requestFocus(); } @@ -734,18 +478,11 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { for (Object selectedElement : selectedElements) { - if (selectedElement instanceof LibraryElement) { - removeLibrary(((LibraryElement)selectedElement).getLibrary()); - } - else if (selectedElement instanceof ItemElement) { + if (selectedElement instanceof ItemElement) { final ItemElement itemElement = (ItemElement)selectedElement; - final Library library = itemElement.getLibrary(); - getLibraryEditor(library).removeRoot(itemElement.getUrl(), itemElement.getRootType()); + getLibraryEditor().removeRoot(itemElement.getUrl(), itemElement.getRootType()); } } - if (myEditingModuleLibraries) { - commitChanges(); - } } }); librariesChanged(true); @@ -776,136 +513,6 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { myListeners.remove(listener); } - private class RenameLibraryAction implements ActionListener { - public void actionPerformed(ActionEvent e) { - final Library selectedLibrary = getSelectedLibrary(); - if (selectedLibrary == null) { - return; - } - final String currentName = selectedLibrary.getName(); - final String newName = Messages.showInputDialog(myTree, ProjectBundle.message("library.rename.prompt"), - ProjectBundle.message("library.rename.title", getLibraryEditor(selectedLibrary).getName()), Messages.getQuestionIcon(), getLibraryEditor(selectedLibrary).getName(), new InputValidator() { - public boolean checkInput(String inputString) { - return true; - } - public boolean canClose(String libraryName) { - if (!currentName.equals(libraryName)) { - if (libraryAlreadyExists(libraryName)) { - Messages.showErrorDialog(ProjectBundle.message("library.name.already.exists.error", libraryName), - ProjectBundle.message("library.name.already.exists.title")); - return false; - } - } - return true; - } - }); - renameLibrary(selectedLibrary, newName); - } - } - - boolean libraryAlreadyExists(String libraryName) { - for (Iterator it = myTableModifiableModel.getLibraryIterator(); it.hasNext(); ) { - final Library lib = (Library)it.next(); - final LibraryEditor editor = getLibraryEditor(lib); - final String libName = editor != null ? editor.getName() : lib.getName(); - if (libraryName.equals(libName)) { - return true; - } - } - return false; - } - - private class MyDialogWrapper extends DialogWrapper { - private JTextField myNameField; - private Library[] mySelectedLibraries; - - public MyDialogWrapper(final Component parent) { - super(parent, true); - setTitle(myLibraryTableProvider.getLibraryTablePresentation().getLibraryTableEditorTitle()); - init(); - - Disposer.register(getDisposable(), LibraryTableEditor.this); - } - - @SuppressWarnings({"RefusedBequest"}) - protected String getDimensionServiceKey() { - return "#com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryTableEditor.MyDialogWrapper"; - } - - @SuppressWarnings({"RefusedBequest"}) - public JComponent getPreferredFocusedComponent() { - if (myNameField != null) { - return myNameField; - } - return myTree; - } - - protected void doOKAction() { - mySelectedLibraries = LibraryTableEditor.this.getSelectedLibraries(); - - if (myNameField != null) { - final Library library = getSelectedLibrary(); - final String currentName = getLibraryEditor(library).getName(); - String newName = myNameField.getText().trim(); - if (newName.length() == 0) { - newName = null; - } - if (!Comparing.equal(newName, currentName)) { - if (!myEditingModuleLibraries) { - if (newName == null) { - Messages.showErrorDialog(ProjectBundle.message("library.name.not.specified.error", newName), ProjectBundle.message("library.name.not.specified.title")); - return; - } - if (libraryAlreadyExists(newName)) { - Messages.showErrorDialog(ProjectBundle.message("library.name.already.exists.error", newName), ProjectBundle.message("library.name.already.exists.title")); - return; - } - } - renameLibrary(library, newName); - } - } - commitChanges(); - super.doOKAction(); - } - - protected JComponent createNorthPanel() { - if (myTreeBuilder.getTreeStructure() instanceof LibraryTreeStructure) { - final Library library = getSelectedLibrary(); - final JPanel panel = new JPanel(new BorderLayout()); - panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); - final LibraryEditor libraryEditor = getLibraryEditor(library); - String currentName = libraryEditor.getName(); - /* - if (currentName == null || currentName.length() == 0) { - final String[] urls = libraryEditor.getUrls(OrderRootType.CLASSES); - if (urls.length > 0) { - String url = urls[0]; - final int idx = url.lastIndexOf('/'); - if (idx) - } - } - */ - myNameField = new JTextField(currentName); - panel.add(myNameField, BorderLayout.CENTER); - final JLabel label = new JLabel("Name: "); - panel.add(label, BorderLayout.WEST); - label.setLabelFor(myNameField); - label.setDisplayedMnemonic('N'); - myNameField.selectAll(); - return panel; - } - return super.createNorthPanel(); - } - - protected JComponent createCenterPanel() { - return LibraryTableEditor.this.getComponent(); - } - - public Library[] getSelectedLibraries() { - return mySelectedLibraries; - } - } - private class MyTreeSelectionListener implements TreeSelectionListener { public void valueChanged(TreeSelectionEvent e) { updateButtons(); @@ -913,35 +520,18 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { public void updateButtons() { final Object[] selectedElements = getSelectedElements(); - final Class elementsClass = getElementsClass(selectedElements); - myRemoveButton.setEnabled( - elementsClass != null && - !(elementsClass.isAssignableFrom(ClassesElement.class) || elementsClass.equals(SourcesElement.class) || elementsClass.isAssignableFrom(JavadocElement.class) || elementsClass.isAssignableFrom(AnnotationElement.class)) - && (myLibraryTableProvider.isLibraryTableEditable() || !elementsClass.isAssignableFrom(LibraryElement.class)) + final Class elementsClass = getElementsClass(selectedElements); + myRemoveButton.setEnabled(elementsClass != null && + !(elementsClass.isAssignableFrom(ClassesElement.class) || elementsClass.equals(SourcesElement.class) || elementsClass.isAssignableFrom(JavadocElement.class) || elementsClass.isAssignableFrom(AnnotationElement.class)) ); - myRenameLibraryButton.setEnabled(selectedElements.length == 1 && elementsClass != null && elementsClass.equals(LibraryElement.class)); - if (elementsClass != null && elementsClass.isAssignableFrom(ItemElement.class)) { - myRemoveButton.setText(ProjectBundle.message("library.detach.action")); - } - else { - myRemoveButton.setText(ProjectBundle.message("library.detach.action")); - } - boolean attachActionsEnabled = selectedElements.length == 1 || getSelectedLibrary() != null; - myAttachClassesButton.setEnabled(attachActionsEnabled); - myAttachJavadocsButton.setEnabled(attachActionsEnabled); - myAttachUrlJavadocsButton.setEnabled(attachActionsEnabled); - myAttachSourcesButton.setEnabled(attachActionsEnabled); - myAttachAnnotationsButton.setEnabled(attachActionsEnabled); - myAttachJarDirectoriesButton.setEnabled(attachActionsEnabled); - myAttachMoreButton.setEnabled(attachActionsEnabled); } @Nullable - private Class getElementsClass(Object[] elements) { + private Class getElementsClass(Object[] elements) { if (elements.length == 0) { return null; } - Class cls = null; + Class cls = null; for (Object element : elements) { if (cls == null) { cls = element.getClass(); @@ -1052,19 +642,20 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { private class AttachMoreAction implements ActionListener { private final LibraryTableAttachHandler[] myHandlers; + private final ExistingLibraryEditor myLibraryEditor; - public AttachMoreAction(LibraryTableAttachHandler[] handlers) { + public AttachMoreAction(LibraryTableAttachHandler[] handlers, final ExistingLibraryEditor libraryEditor) { myHandlers = handlers; + myLibraryEditor = libraryEditor; } public void actionPerformed(ActionEvent e) { - final Library library = getSelectedLibrary(); - assert library != null; final Ref modelRef = Ref.create(null); final NullableComputable computable = new NullableComputable() { public Library.ModifiableModel compute() { if (myTreeBuilder == null) { // The following lines were born in severe pain & suffering, please respect + final Library library = myLibraryEditor.getLibrary(); final InvocationHandler invocationHandler = Proxy.isProxyClass(library.getClass())? Proxy.getInvocationHandler(library) : null; final Library realLibrary = invocationHandler instanceof ModuleEditor.ProxyDelegateAccessor? (Library)((ModuleEditor.ProxyDelegateAccessor)invocationHandler) .getDelegate() : library; @@ -1076,7 +667,7 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { return model; } else { - return getLibraryEditor(library).getModel(); + return myLibraryEditor.getModel(); } } }; @@ -1089,7 +680,7 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { public void run() { if (myTreeBuilder != null) myTreeBuilder.queueUpdate(); if (myProject != null) { - ModuleStructureConfigurable.getInstance(myProject).fireItemsChangeListener(library); + ModuleStructureConfigurable.getInstance(myProject).fireItemsChangeListener(myLibraryEditor.getLibrary()); } } }); @@ -1119,7 +710,6 @@ public class LibraryTableEditor implements Disposable, LibraryEditorListener { public PopupStep onChosen(final LibraryTableAttachHandler handler, final boolean finalChoice) { ApplicationManager.getApplication().invokeLater(new Runnable() { public void run() { - getSelectedLibrary(); handler.performAttach(myProject, computable).doWhenProcessed(successRunnable).doWhenRejected(rejectRunnable); } }); diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableTreeContentElement.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableTreeContentElement.java index da2b2cacb9d0..0bed99458b4c 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableTreeContentElement.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableTreeContentElement.java @@ -20,7 +20,6 @@ import com.intellij.openapi.roots.OrderRootType; import org.jetbrains.annotations.Nullable; public abstract class LibraryTableTreeContentElement { - // empty, just to serve as a base for all tree structure elements public abstract LibraryTableTreeContentElement getParent(); diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableTreeStructure.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableTreeStructure.java deleted file mode 100644 index a3dd827c2d0c..000000000000 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTableTreeStructure.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2000-2009 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.libraryEditor; - -import com.intellij.ide.util.treeView.NodeDescriptor; -import com.intellij.openapi.project.ProjectBundle; -import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.roots.ui.configuration.LibrariesAlphaComparator; -import com.intellij.openapi.roots.ui.configuration.OrderRootTypeUIFactory; -import com.intellij.util.ArrayUtil; - -import java.util.ArrayList; -import java.util.Arrays; - -class LibraryTableTreeStructure extends AbstractLibraryTreeStructure { - private final Object myRootElement = new Object(); - - public LibraryTableTreeStructure(LibraryTableEditor parentEditor) { - super(parentEditor); - myRootElementDescriptor = new NodeDescriptor(null, null) { - public boolean update() { - myName = ProjectBundle.message("library.root.node"); - return false; - } - public Object getElement() { - return myRootElement; - } - }; - } - - public Object getRootElement() { - return myRootElement; - } - - public Object[] getChildElements(Object element) { - if (element == myRootElement) { - final Library[] libraries = myParentEditor.getLibraries(); - Arrays.sort(libraries, LibrariesAlphaComparator.INSTANCE); - LibraryElement[] elements = new LibraryElement[libraries.length]; - for (int idx = 0; idx < libraries.length; idx++) { - final Library library = libraries[idx]; - boolean allPathsValid = true; - for (OrderRootType type : OrderRootType.getAllTypes()) { - allPathsValid &= allPathsValid(library, type); - } - elements[idx] = new LibraryElement(library, myParentEditor, !allPathsValid); - } - return elements; - } - - if (element instanceof LibraryElement) { - final LibraryElement libraryItemElement = (LibraryElement)element; - ArrayList elements = new ArrayList(3); - final Library library = libraryItemElement.getLibrary(); - - final LibraryEditor parentEditor = myParentEditor.getLibraryEditor(library); - for (OrderRootType type : OrderRootType.getAllTypes()) { - final String[] urls = parentEditor.getUrls(type); - if (urls.length > 0) { - elements.add(OrderRootTypeUIFactory.FACTORY.getByKey(type).createElement(libraryItemElement)); - } - } - return elements.toArray(); - } - - if (element instanceof LibraryTableTreeContentElement) { - final LibraryTableTreeContentElement contentElement = (LibraryTableTreeContentElement)element; - final LibraryTableTreeContentElement parentElement = contentElement.getParent(); - if (parentElement instanceof LibraryElement) { - return buildItems(contentElement, ((LibraryElement)parentElement).getLibrary(), contentElement.getOrderRootType()); - } - } - return ArrayUtil.EMPTY_OBJECT_ARRAY; - } - - private boolean allPathsValid(Library library, OrderRootType orderRootType) { - return myParentEditor.getLibraryEditor(library).allPathsValid(orderRootType); - } - -} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTreeStructure.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTreeStructure.java index 4362bebd5c63..756ed767248d 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTreeStructure.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/LibraryTreeStructure.java @@ -15,21 +15,25 @@ */ package com.intellij.openapi.roots.ui.configuration.libraryEditor; +import com.intellij.ide.util.treeView.AbstractTreeStructure; import com.intellij.ide.util.treeView.NodeDescriptor; import com.intellij.openapi.project.ProjectBundle; import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.ui.configuration.OrderRootTypeUIFactory; import com.intellij.util.ArrayUtil; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.Arrays; -public class LibraryTreeStructure extends AbstractLibraryTreeStructure { +public class LibraryTreeStructure extends AbstractTreeStructure { private final LibraryElement myRootElement; + protected NodeDescriptor myRootElementDescriptor; + protected final LibraryTableEditor myParentEditor; - public LibraryTreeStructure(LibraryTableEditor parentElement, Library library) { - super(parentElement); - myRootElement = new LibraryElement(library, myParentEditor, false); + public LibraryTreeStructure(LibraryTableEditor parentElement) { + myParentEditor = parentElement; + myRootElement = new LibraryElement(myParentEditor, false); myRootElementDescriptor = new NodeDescriptor(null, null) { public boolean update() { myName = ProjectBundle.message("library.root.node"); @@ -48,8 +52,7 @@ public class LibraryTreeStructure extends AbstractLibraryTreeStructure { public Object[] getChildElements(Object element) { if (element == myRootElement) { ArrayList elements = new ArrayList(3); - final Library library = myRootElement.getLibrary(); - final LibraryEditor parentEditor = myParentEditor.getLibraryEditor(library); + final LibraryEditor parentEditor = myParentEditor.getLibraryEditor(); for (OrderRootType type : OrderRootType.getAllTypes()) { final String[] urls = parentEditor.getUrls(type); if (urls.length > 0) { @@ -63,13 +66,49 @@ public class LibraryTreeStructure extends AbstractLibraryTreeStructure { final LibraryTableTreeContentElement contentElement = (LibraryTableTreeContentElement)element; final LibraryTableTreeContentElement parentElement = contentElement.getParent(); if (parentElement instanceof LibraryElement) { - return buildItems(contentElement, ((LibraryElement)parentElement).getLibrary(), contentElement.getOrderRootType()); + return buildItems(contentElement, contentElement.getOrderRootType()); } } return ArrayUtil.EMPTY_OBJECT_ARRAY; } - public Library getLibrary() { - return myRootElement.getLibrary(); + public void commit() { + } + + public boolean hasSomethingToCommit() { + return false; + } + + protected Object[] buildItems(LibraryTableTreeContentElement parent, OrderRootType orderRootType) { + ArrayList items = new ArrayList(); + final LibraryEditor libraryEditor = myParentEditor.getLibraryEditor(); + final String[] urls = libraryEditor.getUrls(orderRootType).clone(); + Arrays.sort(urls, LibraryTableEditor.ourUrlComparator); + for (String url : urls) { + items.add(new ItemElement(parent, url, orderRootType, libraryEditor.isJarDirectory(url), libraryEditor.isValid(url, orderRootType))); + } + return items.toArray(); + } + + public Object getParentElement(Object element) { + Object rootElement = getRootElement(); + if (element == rootElement) { + return null; + } + if (element instanceof LibraryTableTreeContentElement) { + return ((LibraryTableTreeContentElement)element).getParent(); + } + return rootElement; + } + + @NotNull + public NodeDescriptor createDescriptor(Object element, NodeDescriptor parentDescriptor) { + if (element == getRootElement()) { + return myRootElementDescriptor; + } + if (element instanceof LibraryTableTreeContentElement) { + return ((LibraryTableTreeContentElement)element).createDescriptor(parentDescriptor, myParentEditor); + } + return null; } } diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/NewLibraryEditor.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/NewLibraryEditor.java new file mode 100644 index 000000000000..2ed87b43deee --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/NewLibraryEditor.java @@ -0,0 +1,117 @@ +/* + * 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.libraryEditor; + +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.vfs.VirtualFile; +import com.intellij.openapi.vfs.pointers.VirtualFilePointer; +import com.intellij.openapi.vfs.pointers.VirtualFilePointerContainer; +import com.intellij.openapi.vfs.pointers.VirtualFilePointerManager; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author nik + */ +public class NewLibraryEditor implements LibraryEditor { + private String myLibraryName; + private final Map myRoots; + private final Map myJarDirectories = new HashMap(); + + public NewLibraryEditor() { + myRoots = new HashMap(); + for (OrderRootType rootType : OrderRootType.getAllTypes()) { + myRoots.put(rootType, VirtualFilePointerManager.getInstance().createContainer(this)); + } + } + + @Override + public String getName() { + return myLibraryName; + } + + @Override + public String[] getUrls(OrderRootType rootType) { + return myRoots.get(rootType).getUrls(); + } + + @Override + public VirtualFile[] getFiles(OrderRootType rootType) { + return LibraryImpl.getRootFiles(myRoots.get(rootType), myJarDirectories); + } + + @Override + public void setName(String name) { + myLibraryName = name; + } + + @Override + public void addRoot(VirtualFile file, OrderRootType rootType) { + myRoots.get(rootType).add(file); + } + + @Override + public void addJarDirectory(VirtualFile file, boolean recursive) { + myRoots.get(OrderRootType.CLASSES).add(file); + myJarDirectories.put(file.getUrl(), recursive); + } + + @Override + public void removeRoot(String url, OrderRootType rootType) { + final VirtualFilePointerContainer container = myRoots.get(rootType); + final VirtualFilePointer pointer = container.findByUrl(url); + if (pointer != null) { + myJarDirectories.remove(pointer.getUrl()); + container.remove(pointer); + } + } + + @Override + public boolean hasChanges() { + return true; + } + + @Override + public boolean isJarDirectory(String url) { + return myJarDirectories.containsKey(url); + } + + @Override + public boolean isValid(String url, OrderRootType orderRootType) { + final VirtualFilePointer pointer = myRoots.get(orderRootType).findByUrl(url); + return pointer != null && pointer.isValid(); + } + + @Override + public void dispose() { + } + + public void apply(@NotNull Library.ModifiableModel model) { + model.setName(myLibraryName); + for (Map.Entry entry : myRoots.entrySet()) { + for (String url : entry.getValue().getUrls()) { + model.addRoot(url, entry.getKey()); + } + } + for (Map.Entry entry : myJarDirectories.entrySet()) { + model.addJarDirectory(entry.getKey(), entry.getValue()); + } + } +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseLibrariesConfigurable.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseLibrariesConfigurable.java index 2d3079a3e5c0..472db8b275a4 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseLibrariesConfigurable.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseLibrariesConfigurable.java @@ -27,15 +27,13 @@ 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.ui.configuration.LibraryTableModifiableModelProvider; -import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryTableEditor; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.CreateNewLibraryAction; import com.intellij.openapi.roots.ui.configuration.projectRoot.daemon.LibraryProjectStructureElement; import com.intellij.openapi.roots.ui.configuration.projectRoot.daemon.ProjectStructureElement; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.NamedConfigurable; import com.intellij.openapi.ui.TextFieldWithBrowseButton; -import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtil; @@ -98,7 +96,7 @@ public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurabl } protected void loadTree() { - createLibrariesNode(myContext.createModifiableModelProvider(myLevel, false)); + createLibrariesNode(myContext.createModifiableModelProvider(myLevel)); } @NotNull @@ -118,7 +116,7 @@ public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurabl return result; } - private void createLibrariesNode(final LibraryTableModifiableModelProvider modelProvider) { + private void createLibrariesNode(final StructureLibraryTableModifiableModelProvider modelProvider) { final Library[] libraries = modelProvider.getModifiableModel().getLibraries(); for (Library library : libraries) { myRoot.add(new MyNode(new LibraryConfigurable(modelProvider, library, myProject, TREE_UPDATER))); @@ -149,7 +147,7 @@ public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurabl if (table != null) { final String level = table.getTableLevel(); final LibraryConfigurable configurable = - new LibraryConfigurable(myContext.createModifiableModelProvider(level, false), library, myProject, TREE_UPDATER); + new LibraryConfigurable(myContext.createModifiableModelProvider(level), library, myProject, TREE_UPDATER); final MyNode node = new MyNode(configurable); addNode(node, myRoot); myContext.getDaemonAnalyzer().queueUpdate(new LibraryProjectStructureElement(myContext, library)); @@ -175,29 +173,23 @@ public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurabl setPopup(false); } + @NotNull public AnAction[] getChildren(@Nullable final AnActionEvent e) { - return new AnAction[]{new AnAction(getAddText()) { - public void actionPerformed(AnActionEvent e) { - final LibraryTableEditor editor = LibraryTableEditor.editLibraryTable(getModelProvider(false), myProject); - editor.addLibraryEditorListener(myContext); - editor.createAddLibraryAction(true).actionPerformed(null); - Disposer.dispose(editor); - } - }}; + return new AnAction[]{new CreateNewLibraryAction(getAddText(), getModelProvider(), myProject)}; } }; } protected abstract String getAddText(); - public abstract LibraryTableModifiableModelProvider getModelProvider(final boolean editable); + public abstract StructureLibraryTableModifiableModelProvider getModelProvider(); public abstract BaseLibrariesConfigurable getOppositeGroup(); protected void removeLibrary(final Library library) { final LibraryTable table = library.getTable(); if (table != null) { - getModelProvider(true).getModifiableModel().removeLibrary(library); + getModelProvider().getModifiableModel().removeLibrary(library); myContext.getDaemonAnalyzer().removeElement(new LibraryProjectStructureElement(myContext, library)); // TODO: myContext.invalidateModules(myContext.myLibraryDependencyCache.get(library.getName())); } @@ -266,7 +258,7 @@ public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurabl LOG.assertTrue(library != null); - final LibraryTable.ModifiableModel libsModel = configurable.getModelProvider(true).getModifiableModel(); + final LibraryTable.ModifiableModel libsModel = configurable.getModelProvider().getModifiableModel(); final Library lib = libsModel.createLibrary(myNameTf.getText()); final Library.ModifiableModel model = ((LibrariesModifiableModel)libsModel).getLibraryEditor(lib).getModel(); for (OrderRootType type : OrderRootType.getAllTypes()) { diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/GlobalLibrariesConfigurable.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/GlobalLibrariesConfigurable.java index 7730b1eb78fc..de9e38f82b2e 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/GlobalLibrariesConfigurable.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/GlobalLibrariesConfigurable.java @@ -21,7 +21,6 @@ import com.intellij.openapi.components.Storage; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectBundle; import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar; -import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.Nullable; @@ -63,8 +62,8 @@ public class GlobalLibrariesConfigurable extends BaseLibrariesConfigurable { return ServiceManager.getService(project, GlobalLibrariesConfigurable.class); } - public LibraryTableModifiableModelProvider getModelProvider(final boolean editable) { - return myContext.getGlobalLibrariesProvider(editable); + public StructureLibraryTableModifiableModelProvider getModelProvider() { + return myContext.getGlobalLibrariesProvider(); } public BaseLibrariesConfigurable getOppositeGroup() { diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainerFactory.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainerFactory.java index 820eba5e0bba..a5d4723d38af 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainerFactory.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainerFactory.java @@ -248,10 +248,10 @@ public class LibrariesContainerFactory { @Nullable private LibraryTableModifiableModelProvider getProvider(LibraryLevel libraryLevel) { if (libraryLevel == LibraryLevel.PROJECT) { - return myContext.getProjectLibrariesProvider(false); + return myContext.getProjectLibrariesProvider(); } else if (libraryLevel == LibraryLevel.GLOBAL) { - return myContext.getGlobalLibrariesProvider(false); + return myContext.getGlobalLibrariesProvider(); } else { return null; @@ -264,12 +264,12 @@ public class LibrariesContainerFactory { @NotNull public VirtualFile[] getLibraryFiles(@NotNull final Library library, @NotNull final OrderRootType rootType) { - LibrariesModifiableModel projectLibrariesModel = (LibrariesModifiableModel)myContext.getProjectLibrariesProvider(false).getModifiableModel(); + LibrariesModifiableModel projectLibrariesModel = myContext.getProjectLibrariesProvider().getModifiableModel(); if (projectLibrariesModel.hasLibraryEditor(library)) { LibraryEditor libraryEditor = projectLibrariesModel.getLibraryEditor(library); return libraryEditor.getFiles(rootType); } - LibrariesModifiableModel globalLibraries = (LibrariesModifiableModel)myContext.getGlobalLibrariesProvider(false).getModifiableModel(); + LibrariesModifiableModel globalLibraries = myContext.getGlobalLibrariesProvider().getModifiableModel(); if (globalLibraries.hasLibraryEditor(library)) { LibraryEditor libraryEditor = globalLibraries.getLibraryEditor(library); return libraryEditor.getFiles(rootType); diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesModifiableModel.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesModifiableModel.java index 17d813c7b4af..be95b31d7d2b 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesModifiableModel.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesModifiableModel.java @@ -21,6 +21,7 @@ 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.ProjectStructureConfigurable; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.ExistingLibraryEditor; import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor; import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditorListener; import com.intellij.openapi.util.Disposer; @@ -34,7 +35,7 @@ import java.util.*; */ public class LibrariesModifiableModel implements LibraryTable.ModifiableModel { - private final Map myLibrary2EditorMap = new HashMap(); + private final Map myLibrary2EditorMap = new HashMap(); private final Set myRemovedLibraries = new HashSet(); private LibraryTable.ModifiableModel myLibrariesModifiableModel; @@ -96,15 +97,16 @@ public class LibrariesModifiableModel implements LibraryTable.ModifiableModel { } public void deferredCommit(){ - for (LibraryEditor libraryEditor : new ArrayList(myLibrary2EditorMap.values())) { + final List libraryEditors = new ArrayList(myLibrary2EditorMap.values()); + myLibrary2EditorMap.clear(); + for (ExistingLibraryEditor libraryEditor : libraryEditors) { libraryEditor.commit(); // TODO: is seems like commit will recreate the editor, but it should not Disposer.dispose(libraryEditor); } - if (!(myLibrary2EditorMap.isEmpty() && myRemovedLibraries.isEmpty())) { + if (!libraryEditors.isEmpty() || !myRemovedLibraries.isEmpty()) { getLibrariesModifiableModel().commit(); myLibrariesModifiableModel = null; } - myLibrary2EditorMap.clear(); myRemovedLibraries.clear(); } @@ -116,20 +118,20 @@ public class LibrariesModifiableModel implements LibraryTable.ModifiableModel { return myLibrary2EditorMap.containsKey(library); } - public LibraryEditor getLibraryEditor(Library library){ + public ExistingLibraryEditor getLibraryEditor(Library library){ final Library source = ((LibraryImpl)library).getSource(); if (source != null) { return getLibraryEditor(source); } - LibraryEditor libraryEditor = myLibrary2EditorMap.get(library); + ExistingLibraryEditor libraryEditor = myLibrary2EditorMap.get(library); if (libraryEditor == null){ libraryEditor = createLibraryEditor(library); } return libraryEditor; } - private LibraryEditor createLibraryEditor(final Library library) { - final LibraryEditor libraryEditor = new LibraryEditor(library, myLibraryEditorListener); + private ExistingLibraryEditor createLibraryEditor(final Library library) { + final ExistingLibraryEditor libraryEditor = new ExistingLibraryEditor(library, myLibraryEditorListener); myLibrary2EditorMap.put(library, libraryEditor); return libraryEditor; } diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibraryConfigurable.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibraryConfigurable.java index d4f78dfd237f..260b93c19da2 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibraryConfigurable.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibraryConfigurable.java @@ -21,12 +21,10 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectBundle; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.libraries.LibraryTable; -import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor; import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryTableEditor; import com.intellij.openapi.roots.ui.configuration.projectRoot.daemon.LibraryProjectStructureElement; import com.intellij.openapi.roots.ui.configuration.projectRoot.daemon.ProjectStructureElement; -import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.IconLoader; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.Nullable; @@ -42,17 +40,17 @@ public class LibraryConfigurable extends ProjectStructureElementConfigurable getCustomLibrariesProviders(final boolean tableEditable) { + public List getCustomLibrariesProviders() { return ContainerUtil.map2List(LibraryTablesRegistrar.getInstance().getCustomLibraryTables(), new NotNullFunction() { @NotNull public LibraryTableModifiableModelProvider fun(final LibraryTable libraryTable) { - return createModifiableModelProvider(libraryTable.getTableLevel(), tableEditable); + return createModifiableModelProvider(libraryTable.getTableLevel()); } }); } + public LibraryTable.ModifiableModel getModifiableLibraryTable(@NotNull LibraryTable table) { + final String tableLevel = table.getTableLevel(); + if (tableLevel.equals(LibraryTableImplUtil.MODULE_LEVEL)) { + return table.getModifiableModel(); + } + return myLevel2Providers.get(tableLevel); + } @Nullable public Library getLibrary(final String libraryName, final String libraryLevel) { @@ -187,4 +177,5 @@ public class StructureConfigurableContext implements Disposable, LibraryEditorLi public void clear() { myLevel2Providers.clear(); } + } diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/StructureLibraryTableModifiableModelProvider.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/StructureLibraryTableModifiableModelProvider.java new file mode 100644 index 000000000000..23ab6647bfd0 --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/StructureLibraryTableModifiableModelProvider.java @@ -0,0 +1,37 @@ +/* + * 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.projectRoot; + +import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; + +/** +* @author nik +*/ +public class StructureLibraryTableModifiableModelProvider implements LibraryTableModifiableModelProvider { + private final String myLevel; + private final StructureConfigurableContext myContext; + + public StructureLibraryTableModifiableModelProvider(String level, + final StructureConfigurableContext context) { + myLevel = level; + myContext = context; + } + + public LibrariesModifiableModel getModifiableModel() { + return myContext.myLevel2Providers.get(myLevel); + } + +} diff --git a/platform/lang-api/src/com/intellij/openapi/roots/libraries/Library.java b/platform/lang-api/src/com/intellij/openapi/roots/libraries/Library.java index 9d172e85dff6..ee342b92fa66 100644 --- a/platform/lang-api/src/com/intellij/openapi/roots/libraries/Library.java +++ b/platform/lang-api/src/com/intellij/openapi/roots/libraries/Library.java @@ -27,6 +27,8 @@ import org.jetbrains.annotations.NotNull; * @author dsl */ public interface Library extends JDOMExternalizable, Disposable { + Library[] EMPTY_ARRAY = new Library[0]; + String getName(); @NotNull String[] getUrls(@NotNull OrderRootType rootType); diff --git a/platform/lang-impl/src/com/intellij/openapi/roots/impl/libraries/LibraryImpl.java b/platform/lang-impl/src/com/intellij/openapi/roots/impl/libraries/LibraryImpl.java index 091ccf348d86..a8ba709b3549 100644 --- a/platform/lang-impl/src/com/intellij/openapi/roots/impl/libraries/LibraryImpl.java +++ b/platform/lang-impl/src/com/intellij/openapi/roots/impl/libraries/LibraryImpl.java @@ -141,10 +141,14 @@ public class LibraryImpl implements LibraryEx.ModifiableModelEx, LibraryEx { @NotNull public VirtualFile[] getFiles(@NotNull OrderRootType rootType) { assert !isDisposed(); + return getRootFiles(myRoots.get(rootType), myJarDirectories); + } + + public static VirtualFile[] getRootFiles(final VirtualFilePointerContainer container, final Map jarDirectories) { final List expanded = new ArrayList(); - for (VirtualFile file : myRoots.get(rootType).getFiles()) { + for (VirtualFile file : container.getFiles()) { if (file.isDirectory()) { - final Boolean expandRecursively = myJarDirectories.get(file.getUrl()); + final Boolean expandRecursively = jarDirectories.get(file.getUrl()); if (expandRecursively != null) { addChildren(file, expanded, expandRecursively.booleanValue()); continue; diff --git a/platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesDialog.java b/platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesDialogBase.java similarity index 59% rename from platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesDialog.java rename to platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesDialogBase.java index aee863130502..d7a0fc13c43a 100644 --- a/platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesDialog.java +++ b/platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesDialogBase.java @@ -3,6 +3,7 @@ package com.intellij.util.ui.classpath; import com.intellij.ide.CommonActionsManager; import com.intellij.ide.DefaultTreeExpander; import com.intellij.ide.TreeExpander; +import com.intellij.ide.projectView.PresentationData; import com.intellij.ide.util.treeView.AbstractTreeBuilder; import com.intellij.ide.util.treeView.AbstractTreeStructure; import com.intellij.ide.util.treeView.NodeDescriptor; @@ -22,14 +23,17 @@ 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.libraries.LibraryTablesRegistrar; +import com.intellij.openapi.roots.ui.util.CellAppearance; import com.intellij.openapi.roots.ui.util.OrderEntryCellAppearanceUtils; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.text.StringUtil; import com.intellij.ui.ScrollPaneFactory; import com.intellij.ui.SimpleColoredComponent; +import com.intellij.ui.SimpleTextAttributes; import com.intellij.ui.treeStructure.SimpleNode; import com.intellij.ui.treeStructure.SimpleTree; +import com.intellij.ui.treeStructure.SimpleTreeBuilder; import com.intellij.ui.treeStructure.WeightBasedComparator; import com.intellij.util.CommonProcessors; import com.intellij.util.Icons; @@ -55,24 +59,38 @@ import java.util.List; * @author Gregory.Shrago */ -public class ChooseLibrariesDialog extends DialogWrapper{ - +public abstract class ChooseLibrariesDialogBase extends DialogWrapper { private final SimpleTree myTree = new SimpleTree(); private AbstractTreeBuilder myBuilder; - private List myResult; - private final Map myLibraryMap = new THashMap(); + private final Map myParentsMap = new THashMap(); - protected ChooseLibrariesDialog(final Project project, final String title) { - super(project, false); + public ChooseLibrariesDialogBase(final JComponent parentComponent, final String title) { + super(parentComponent, false); setTitle(title); - init(); + } + + @Override + protected void init() { + super.init(); updateOKAction(); } + private static String notEmpty(String nodeText) { + return StringUtil.isNotEmpty(nodeText) ? nodeText : ""; + } + @Override protected String getDimensionServiceKey() { - return "#com.intellij.javaee.module.view.dataSource.ChooseLibrariesDialog"; + return "#com.intellij.util.ui.classpath.ChooseLibrariesDialog"; + } + + protected int getLibraryTableWeight(@NotNull LibraryTable libraryTable) { + return 0; + } + + protected boolean isAutoExpandLibraryTable(@NotNull LibraryTable libraryTable) { + return false; } @Override @@ -122,8 +140,8 @@ public class ChooseLibrariesDialog extends DialogWrapper{ @Nullable protected JComponent createCenterPanel() { - myBuilder = new AbstractTreeBuilder(myTree, new DefaultTreeModel(new DefaultMutableTreeNode()), - new MyStructure(ProjectManager.getInstance().getDefaultProject()), + myBuilder = new SimpleTreeBuilder(myTree, new DefaultTreeModel(new DefaultMutableTreeNode()), + new MyStructure(getProject()), WeightBasedComparator.FULL_INSTANCE); myBuilder.initRootNode(); @@ -154,15 +172,51 @@ public class ChooseLibrariesDialog extends DialogWrapper{ return ScrollPaneFactory.createScrollPane(myTree); } + @NotNull + protected Project getProject() { + return ProjectManager.getInstance().getDefaultProject(); + } + + protected LibrariesTreeNodeBase createLibraryDescriptor(NodeDescriptor parentDescriptor, Library library) { + return new LibraryDescriptor(getProject(), parentDescriptor, library); + } + + protected void collectChildren(Object element, final List result) { + if (element instanceof Application) { + Collections.addAll(result, ProjectManager.getInstance().getOpenProjects()); + final LibraryTablesRegistrar instance = LibraryTablesRegistrar.getInstance(); + result.add(instance.getLibraryTable()); //1 + result.addAll(instance.getCustomLibraryTables()); //2 + } + else if (element instanceof Project) { + Collections.addAll(result, ModuleManager.getInstance((Project)element).getModules()); + result.add(LibraryTablesRegistrar.getInstance().getLibraryTable((Project)element)); + } + else if (element instanceof LibraryTable) { + Collections.addAll(result, ((LibraryTable)element).getLibraries()); + } + else if (element instanceof Module) { + for (OrderEntry entry : ModuleRootManager.getInstance((Module)element).getOrderEntries()) { + if (entry instanceof LibraryOrderEntry) { + final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)entry; + if (LibraryTableImplUtil.MODULE_LEVEL.equals(libraryOrderEntry.getLibraryLevel())) { + final Library library = libraryOrderEntry.getLibrary(); + result.add(library); + } + } + } + } + } + protected void dispose() { Disposer.dispose(myBuilder); super.dispose(); } - private static class MyNode extends SimpleNode { + protected static class LibrariesTreeNodeBase extends SimpleNode { private final T myElement; - private MyNode(Project project, NodeDescriptor parentDescriptor, T element) { + protected LibrariesTreeNodeBase(Project project, NodeDescriptor parentDescriptor, T element) { super(project, parentDescriptor); myElement = element; } @@ -185,37 +239,36 @@ public class ChooseLibrariesDialog extends DialogWrapper{ public Object[] getEqualityObjects() { return new Object[] {myElement}; } + + @Override + protected void update(PresentationData presentation) { + //todo[nik] this is workaround for bug in getTemplatePresentation().setIcons() + presentation.setOpenIcon(getTemplatePresentation().getIcon(true)); + presentation.setClosedIcon(getTemplatePresentation().getIcon(false)); + } } - private static class RootDescriptor extends MyNode { + private static class RootDescriptor extends LibrariesTreeNodeBase { protected RootDescriptor(final Project project) { super(project, null, ApplicationManager.getApplication()); } } - private static class ProjectDescriptor extends MyNode { + private static class ProjectDescriptor extends LibrariesTreeNodeBase { protected ProjectDescriptor(final Project project, final Project element) { super(project, null, element); - } - - @Override - protected void doUpdate() { - setIcons(Icons.PROJECT_ICON, Icons.PROJECT_ICON); - final String nodeText = getElement().getName(); - setNodeText(StringUtil.isNotEmpty(nodeText) ? nodeText : "", null, false); + getTemplatePresentation().setIcons(Icons.PROJECT_ICON); + getTemplatePresentation().addText(notEmpty(getElement().getName()), SimpleTextAttributes.REGULAR_ATTRIBUTES); } } - private static class ModuleDescriptor extends MyNode { + private static class ModuleDescriptor extends LibrariesTreeNodeBase { protected ModuleDescriptor(final Project project, final NodeDescriptor parentDescriptor, final Module element) { super(project, parentDescriptor, element); - } - - @Override - protected void doUpdate() { - setIcons(getElement().getModuleType().getNodeIcon(false), getElement().getModuleType().getNodeIcon(true)); - final String nodeText = getElement().getName(); - setNodeText(StringUtil.isNotEmpty(nodeText) ? nodeText : "", null, false); + final PresentationData templatePresentation = getTemplatePresentation(); + templatePresentation.setClosedIcon(element.getModuleType().getNodeIcon(false)); + templatePresentation.setOpenIcon(element.getModuleType().getNodeIcon(true)); + templatePresentation.addText(notEmpty(element.getName()), SimpleTextAttributes.REGULAR_ATTRIBUTES); } @Override @@ -224,37 +277,38 @@ public class ChooseLibrariesDialog extends DialogWrapper{ } } - private static class LibraryDescriptor extends MyNode { - private final Icon myIcon; - + private static class LibraryDescriptor extends LibrariesTreeNodeBase { protected LibraryDescriptor(final Project project, final NodeDescriptor parentDescriptor, final Library element) { super(project, parentDescriptor, element); + final CellAppearance appearance = OrderEntryCellAppearanceUtils.forLibrary(element); final SimpleColoredComponent coloredComponent = new SimpleColoredComponent(); - OrderEntryCellAppearanceUtils.forLibrary(getElement()).customize(coloredComponent); - myIcon = coloredComponent.getIcon(); - } - - @Override - protected void doUpdate() { - setIcons(myIcon, myIcon); - final String nodeText = OrderEntryCellAppearanceUtils.forLibrary(getElement()).getText(); - setNodeText(StringUtil.isNotEmpty(nodeText) ? nodeText : "", null, false); + appearance.customize(coloredComponent); + final PresentationData templatePresentation = getTemplatePresentation(); + templatePresentation.setIcons(coloredComponent.getIcon()); + templatePresentation.addText(notEmpty(appearance.getText()), SimpleTextAttributes.REGULAR_ATTRIBUTES); } } - private static class NamedDescriptor extends MyNode { + private static class LibraryTableDescriptor extends LibrariesTreeNodeBase { private final int myWeight; + private boolean myAutoExpand; - protected NamedDescriptor(final Project project, final NodeDescriptor parentDescriptor, final LibraryTable table, final int weight) { + protected LibraryTableDescriptor(final Project project, + final NodeDescriptor parentDescriptor, + final LibraryTable table, + final int weight, + boolean autoExpand) { super(project, parentDescriptor, table); myWeight = weight; + myAutoExpand = autoExpand; + getTemplatePresentation().setIcons(Icons.LIBRARY_ICON); + final String nodeText = table.getPresentation().getDisplayName(true); + getTemplatePresentation().addText(notEmpty(nodeText), SimpleTextAttributes.REGULAR_ATTRIBUTES); } @Override - protected void doUpdate() { - setIcons(Icons.DIRECTORY_CLOSED_ICON, Icons.DIRECTORY_OPEN_ICON); - final String nodeText = getElement().getPresentation().getDisplayName(true); - setNodeText(StringUtil.isNotEmpty(nodeText) ? nodeText : "", null, false); + public boolean isAutoExpandNode() { + return myAutoExpand; } @Override @@ -277,37 +331,14 @@ public class ChooseLibrariesDialog extends DialogWrapper{ @Override public Object[] getChildElements(Object element) { - final ArrayList result = new ArrayList(); - if (element instanceof Application) { - Collections.addAll(result, ProjectManager.getInstance().getOpenProjects()); - final LibraryTablesRegistrar instance = LibraryTablesRegistrar.getInstance(); - result.add(instance.getLibraryTable()); //1 - result.addAll(instance.getCustomLibraryTables()); //2 - } - else if (element instanceof Project) { - Collections.addAll(result, ModuleManager.getInstance((Project)element).getModules()); - result.add(LibraryTablesRegistrar.getInstance().getLibraryTable((Project)element)); - } - else if (element instanceof LibraryTable) { - Collections.addAll(result, ((LibraryTable)element).getLibraries()); - } - else if (element instanceof Module) { - for (OrderEntry entry : ModuleRootManager.getInstance((Module)element).getOrderEntries()) { - if (entry instanceof LibraryOrderEntry) { - final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)entry; - if (LibraryTableImplUtil.MODULE_LEVEL.equals(libraryOrderEntry.getLibraryLevel())) { - final Library library = libraryOrderEntry.getLibrary(); - result.add(library); - } - } - } - } + final List result = new ArrayList(); + collectChildren(element, result); final Iterator it = result.iterator(); while (it.hasNext()) { if (!acceptsElement(it.next())) it.remove(); } for (Object o : result) { - myLibraryMap.put(o, element); + myParentsMap.put(o, element); } return result.toArray(); } @@ -317,8 +348,8 @@ public class ChooseLibrariesDialog extends DialogWrapper{ if (element instanceof Application) return null; if (element instanceof Project) return ApplicationManager.getApplication(); if (element instanceof Module) return ((Module)element).getProject(); - if (element instanceof LibraryTable) return myLibraryMap.get(element); - if (element instanceof Library) return myLibraryMap.get(element); + if (element instanceof LibraryTable) return myParentsMap.get(element); + if (element instanceof Library) return myParentsMap.get(element); throw new AssertionError(); } @@ -328,8 +359,13 @@ public class ChooseLibrariesDialog extends DialogWrapper{ if (element instanceof Application) return new RootDescriptor(myProject); if (element instanceof Project) return new ProjectDescriptor(myProject, (Project)element); if (element instanceof Module) return new ModuleDescriptor(myProject, parentDescriptor, (Module)element); - if (element instanceof LibraryTable) return new NamedDescriptor(myProject, parentDescriptor, (LibraryTable)element, 0); - if (element instanceof Library) return new LibraryDescriptor(myProject, parentDescriptor, (Library)element); + if (element instanceof LibraryTable) { + final LibraryTable libraryTable = (LibraryTable)element; + return new LibraryTableDescriptor(myProject, parentDescriptor, libraryTable, + getLibraryTableWeight(libraryTable), + isAutoExpandLibraryTable(libraryTable)); + } + if (element instanceof Library) return createLibraryDescriptor(parentDescriptor, (Library)element); throw new AssertionError(); } diff --git a/platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesFromTablesDialog.java b/platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesFromTablesDialog.java new file mode 100644 index 000000000000..22ebff226eb3 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/util/ui/classpath/ChooseLibrariesFromTablesDialog.java @@ -0,0 +1,103 @@ +/* + * 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.util.ui.classpath; + +import com.intellij.openapi.application.Application; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.libraries.Library; +import com.intellij.openapi.roots.libraries.LibraryTable; +import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.util.Collections; +import java.util.List; + +/** + * @author nik + */ +public class ChooseLibrariesFromTablesDialog extends ChooseLibrariesDialogBase { + private @Nullable Project myProject; + + public ChooseLibrariesFromTablesDialog(JComponent parentComponent, String title, @Nullable Project project) { + super(parentComponent, title); + myProject = project; + } + + @NotNull + @Override + protected Project getProject() { + if (myProject != null) { + return myProject; + } + return super.getProject(); + } + + @Override + protected JComponent createNorthPanel() { + return null; + } + + @Override + protected void collectChildren(Object element, List result) { + if (element instanceof Application) { + final LibraryTablesRegistrar registrar = LibraryTablesRegistrar.getInstance(); + if (myProject != null) { + addLibraryTable(result, registrar.getLibraryTable(myProject)); + } + addLibraryTable(result, registrar.getLibraryTable()); + for (LibraryTable table : registrar.getCustomLibraryTables()) { + addLibraryTable(result, table); + } + } + else if (element instanceof LibraryTable) { + Collections.addAll(result, getLibraries((LibraryTable)element)); + } + } + + private void addLibraryTable(List result, LibraryTable table) { + if (getLibraries(table).length > 0) { + result.add(table); + } + } + + @Override + protected int getLibraryTableWeight(@NotNull LibraryTable libraryTable) { + if (isProjectLibraryTable(libraryTable)) return 0; + if (isApplicationLibraryTable(libraryTable)) return 1; + return 2; + } + + private static boolean isApplicationLibraryTable(LibraryTable libraryTable) { + return libraryTable.equals(LibraryTablesRegistrar.getInstance().getLibraryTable()); + } + + private boolean isProjectLibraryTable(LibraryTable libraryTable) { + final LibraryTablesRegistrar registrar = LibraryTablesRegistrar.getInstance(); + return myProject != null && libraryTable.equals(registrar.getLibraryTable(myProject)); + } + + @Override + protected boolean isAutoExpandLibraryTable(@NotNull LibraryTable libraryTable) { + return isApplicationLibraryTable(libraryTable) || isProjectLibraryTable(libraryTable); + } + + @NotNull + protected Library[] getLibraries(@NotNull LibraryTable table) { + return table.getLibraries(); + } +} diff --git a/platform/lang-impl/src/com/intellij/util/ui/classpath/SimpleClasspathPanel.java b/platform/lang-impl/src/com/intellij/util/ui/classpath/SimpleClasspathPanel.java index 71564a9bede7..23a7247ea746 100644 --- a/platform/lang-impl/src/com/intellij/util/ui/classpath/SimpleClasspathPanel.java +++ b/platform/lang-impl/src/com/intellij/util/ui/classpath/SimpleClasspathPanel.java @@ -13,7 +13,6 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.fileChooser.ex.FileChooserDialogImpl; import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.ProjectBundle; -import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.roots.OrderRootType; import com.intellij.openapi.roots.impl.libraries.LibraryTableImplUtil; import com.intellij.openapi.roots.libraries.Library; @@ -294,27 +293,7 @@ public class SimpleClasspathPanel extends JPanel { @NotNull protected List doChoose() { final Set existingFiles = getVirtualFiles(); - final ChooseLibrariesDialog dialog = new ChooseLibrariesDialog(ProjectManager.getInstance().getDefaultProject(), "Choose Existing Libraries") { - @Override - protected boolean acceptsElement(final Object element) { - if (!(element instanceof Library)) return true; - final Library library = (Library)element; - return !existingFiles.containsAll(Arrays.asList(library.getFiles(OrderRootType.CLASSES))); - } - - @Override - protected JComponent createCenterPanel() { - final JPanel panel = new JPanel(new BorderLayout()); - panel.add(super.createCenterPanel(), BorderLayout.CENTER); - final MultiLineLabel label = new MultiLineLabel("Please note that project-level and module-level libraries will not be\n\n" + - " added as a whole but will be converted to jars and folders instead."); - label.setIcon(Messages.getWarningIcon()); - label.setIcon(Messages.getWarningIcon()); - panel.add(label, BorderLayout.SOUTH); - return panel; - } - - }; + final ChooseLibrariesDialogBase dialog = new SimpleClasspathChooseLibrariesDialog(SimpleClasspathPanel.this, existingFiles); dialog.show(); final List libraries = dialog.getSelectedLibraries(); final ArrayList result = new ArrayList(); @@ -326,6 +305,36 @@ public class SimpleClasspathPanel extends JPanel { }); return actions.toArray(new PopupAction[actions.size()]); } + + } + + private static class SimpleClasspathChooseLibrariesDialog extends ChooseLibrariesDialogBase { + private final Set myExistingFiles; + + public SimpleClasspathChooseLibrariesDialog(JComponent parentComponent, Set existingFiles) { + super(parentComponent, "Choose Existing Libraries"); + myExistingFiles = existingFiles; + init(); + } + + @Override + protected boolean acceptsElement(final Object element) { + if (!(element instanceof Library)) return true; + final Library library = (Library)element; + return !myExistingFiles.containsAll(Arrays.asList(library.getFiles(OrderRootType.CLASSES))); + } + + @Override + protected JComponent createCenterPanel() { + final JPanel panel = new JPanel(new BorderLayout()); + panel.add(super.createCenterPanel(), BorderLayout.CENTER); + final MultiLineLabel label = new MultiLineLabel("Please note that project-level and module-level libraries will not be\n\n" + + " added as a whole but will be converted to jars and folders instead."); + label.setIcon(Messages.getWarningIcon()); + label.setIcon(Messages.getWarningIcon()); + panel.add(label, BorderLayout.SOUTH); + return panel; + } } diff --git a/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapper.java b/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapper.java index df94e909aede..f67e40b1fc7f 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapper.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapper.java @@ -472,6 +472,7 @@ public abstract class DialogWrapper { * north of the dialog's content pane. The implementation can return null * value. In this case there will be no input panel. */ + @Nullable protected JComponent createNorthPanel() { return null; } diff --git a/platform/platform-resources-en/src/messages/ProjectBundle.properties b/platform/platform-resources-en/src/messages/ProjectBundle.properties index d5a349737c6d..381b94822d68 100644 --- a/platform/platform-resources-en/src/messages/ProjectBundle.properties +++ b/platform/platform-resources-en/src/messages/ProjectBundle.properties @@ -154,7 +154,6 @@ module.libraries.target.jdk.project.radio=&Project SDK:
This SDK module.libraries.target.jdk.module.radio=&Module SDK: module.libraries.target.jdk.select.title=Select Project JDK module.libraries.unknown.item=Unknown -module.libraries.rename.button=&Rename Library module.libraries.javadoc.url.button=Specify Documentation &URL... module.libraries.remove.button=R&emove module.libraries.javadoc.attach.button=Attach D&ocumentation... @@ -217,21 +216,16 @@ library.name.not.specified.error=Please enter library name library.name.not.specified.title=Library Name Not Specified library.name.already.exists.error=Library \"{0}\" already exists library.name.already.exists.title=Library Already Exists -library.files.not.selected.error=Please select files or directories to be added to the library -library.files.not.selected.title=Library Files Not Selected library.create.library.action=Create &Library... -library.add.jar.directory.action=Add &Jar/Directory... -classpath.add.module.library.action=Module Library... +classpath.add.new.library.action=New Library... classpath.add.simple.module.library.action=Single-Entry Module Library... -classpath.add.project.library.action=Project Library... -classpath.add.global.library.action=Global Library... +classpath.add.existing.library.action=Existing Library... classpath.add.module.dependency.action=Module Dependency... classpath.chooser.title.add.module.dependency=Choose Dependent Modules classpath.title.adding.dependency=Adding dependency classpath.message.library.already.added=Library ''{0}'' already added to this module message.no.module.dependency.candidates=Found no modules to depend on -library.rename.prompt=Enter new library name -library.rename.title=Rename library \"{0}\" +library.configure.title=Configure Library library.configure.module.title=Configure Module Library library.configure.project.title=Configure Project Libraries library.configure.global.title=Configure Global Libraries diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties b/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties index e66443073c3a..de67aad80b6e 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties @@ -312,7 +312,6 @@ break.outside.loop=The break statement with named labels is only allowed inside ######################################################################################################################## # Facet configuration ######################################################################################################################## -groovy.sdk.configuration=Groovy SDK configuration new.button.label=New groovy.sdk.configuration.label=Groovy SDK:
Choose Groovy SDK specific for this module add.new.groovy.lib=New SDK diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/AbstractGroovyLibraryManager.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/AbstractGroovyLibraryManager.java index a3f0a4cf9aff..24dcc31ddaa2 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/AbstractGroovyLibraryManager.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/AbstractGroovyLibraryManager.java @@ -85,8 +85,8 @@ public abstract class AbstractGroovyLibraryManager extends LibraryManager { LibraryTable.ModifiableModel globalModel = null; if (inModuleSettings) { globalModel = project != null && inProject ? - ProjectLibrariesConfigurable.getInstance(project).getModelProvider(true).getModifiableModel() : - GlobalLibrariesConfigurable.getInstance(project).getModelProvider(true).getModifiableModel(); + ProjectLibrariesConfigurable.getInstance(project).getModelProvider().getModifiableModel() : + GlobalLibrariesConfigurable.getInstance(project).getModelProvider().getModifiableModel(); assert globalModel != null; library = globalModel.createLibrary(name); model = ((LibrariesModifiableModel)globalModel).getLibraryEditor(library).getModel(); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/GroovySupportConfigurable.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/GroovySupportConfigurable.java index b8e9c93dc8d7..146f0e959d26 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/GroovySupportConfigurable.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/GroovySupportConfigurable.java @@ -34,7 +34,6 @@ import com.intellij.util.containers.CollectionFactory; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.config.ui.GroovyFacetEditor; -import org.jetbrains.plugins.groovy.config.ui.ManagedLibrariesEditor; import org.jetbrains.plugins.groovy.util.LibrariesUtil; import javax.swing.*; @@ -68,7 +67,7 @@ public class GroovySupportConfigurable extends FrameworkSupportConfigurable { if (library == null) { cleanUndefinedGroovyLibrary(rootModel, (LibraryOrderEntry)entry); } else { - final LibraryManager manager = ManagedLibrariesEditor.findManagerFor(library, managers.toArray(new LibraryManager[managers.size()]), container); + final LibraryManager manager = LibraryManager.findManagerFor(library, managers.toArray(new LibraryManager[managers.size()]), container); if (manager != null) { @SuppressWarnings({"NonConstantStringShouldBeStringBuffer"}) String message = "There is already a " + manager.getLibraryCategoryName() + " library"; diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/LibraryManager.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/LibraryManager.java index ea31c69b94f9..2b61e44847a5 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/LibraryManager.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/LibraryManager.java @@ -31,6 +31,23 @@ import javax.swing.*; */ public abstract class LibraryManager { + @Nullable + public static LibraryManager findManagerFor(@NotNull Library library, final LibraryManager[] managers, final LibrariesContainer container) { + for (final LibraryManager manager : managers) { + final String name = library.getName(); + if (name != null && manager.managesName(name) && manager.managesLibrary(library, container)) { + return manager; + } + } + + for (final LibraryManager manager : managers) { + if (manager.managesLibrary(library, container)) { + return manager; + } + } + return null; + } + public abstract boolean managesLibrary(@NotNull Library library, LibrariesContainer container); @Nullable diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetEditor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetEditor.java index 77806ad32726..2a76341b8ca5 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetEditor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetEditor.java @@ -111,7 +111,7 @@ public class GroovyFacetEditor { final AbstractGroovyLibraryManager[] managers = AbstractGroovyLibraryManager.EP_NAME.getExtensions(); Set> libs = new TreeSet>(LIBRARY_COMPARATOR); for (Library library : container.getAllLibraries()) { - final LibraryManager manager = ManagedLibrariesEditor.findManagerFor(library, managers, container); + final LibraryManager manager = LibraryManager.findManagerFor(library, managers, container); if (myAcceptableManager.isInstance(manager)) { libs.add(Pair.create(library, manager)); } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetTab.form b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetTab.form deleted file mode 100644 index 49c3920e4008..000000000000 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetTab.form +++ /dev/null @@ -1,38 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetTab.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetTab.java deleted file mode 100644 index a2e7b0f4936e..000000000000 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/GroovyFacetTab.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2000-2009 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 org.jetbrains.plugins.groovy.config.ui; - -import com.intellij.facet.ui.FacetEditorContext; -import com.intellij.facet.ui.FacetEditorTab; -import com.intellij.facet.ui.FacetValidatorsManager; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.util.Condition; -import com.intellij.openapi.vfs.VirtualFile; -import org.jetbrains.annotations.Nls; -import org.jetbrains.plugins.groovy.GroovyBundle; -import org.jetbrains.plugins.groovy.config.AbstractGroovyLibraryManager; -import org.jetbrains.plugins.groovy.util.LibrariesUtil; - -import javax.swing.*; - -/** - * @author peter - */ -public class GroovyFacetTab extends FacetEditorTab { - - public static final Logger LOG = Logger.getInstance("org.jetbrains.plugins.groovy.config.ui.GroovyFacetTab"); - - private JPanel myPanel; - private JPanel myManagedLibrariesPanel; - private final ManagedLibrariesEditor myManagedLibrariesEditor; - - public GroovyFacetTab(final FacetEditorContext editorContext, FacetValidatorsManager validatorsManager) { - myManagedLibrariesEditor = new ManagedLibrariesEditor(editorContext, validatorsManager, AbstractGroovyLibraryManager.EP_NAME); - - myManagedLibrariesPanel.add(myManagedLibrariesEditor.getComponent()); - - myManagedLibrariesEditor.shouldHaveLibrary(new Condition() { - public boolean value(Library libraryManager) { - final VirtualFile[] files = editorContext.getLibraryFiles(libraryManager, OrderRootType.CLASSES); - return LibrariesUtil.getGroovyLibraryHome(files) != null; - } - }, "Groovy-containing libraries are missing"); - } - - @Nls - public String getDisplayName() { - return GroovyBundle.message("groovy.sdk.configuration"); - } - - public JComponent createComponent() { - return myPanel; - } - - public boolean isModified() { - return false; - } - - public void apply() { - } - - public void reset() { - myManagedLibrariesEditor.updateLibraryList(); - } - - public void disposeUIResources() { - } - -} diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/ManagedLibrariesEditor.form b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/ManagedLibrariesEditor.form deleted file mode 100644 index 2d6461c75b81..000000000000 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/ManagedLibrariesEditor.form +++ /dev/null @@ -1,68 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/ManagedLibrariesEditor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/ManagedLibrariesEditor.java deleted file mode 100644 index 0d7a0f66fe9d..000000000000 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/config/ui/ManagedLibrariesEditor.java +++ /dev/null @@ -1,464 +0,0 @@ -/* - * Copyright 2000-2009 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 org.jetbrains.plugins.groovy.config.ui; - -import com.intellij.facet.impl.ui.FacetContextChangeListener; -import com.intellij.facet.impl.ui.FacetEditorContextBase; -import com.intellij.facet.ui.*; -import com.intellij.openapi.actionSystem.*; -import com.intellij.openapi.extensions.ExtensionPointName; -import com.intellij.openapi.module.Module; -import com.intellij.openapi.project.DumbAware; -import com.intellij.openapi.roots.LibraryOrderEntry; -import com.intellij.openapi.roots.ModifiableRootModel; -import com.intellij.openapi.roots.OrderEntry; -import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.roots.libraries.LibraryTable; -import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; -import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable; -import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryTableEditor; -import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer; -import com.intellij.openapi.roots.ui.configuration.projectRoot.ModuleStructureConfigurable; -import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.ui.popup.JBPopupFactory; -import com.intellij.openapi.ui.popup.ListSeparator; -import com.intellij.openapi.ui.popup.PopupStep; -import com.intellij.openapi.ui.popup.util.BaseListPopupStep; -import com.intellij.openapi.util.Condition; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.ui.ColoredListCellRenderer; -import com.intellij.ui.PopupHandler; -import com.intellij.ui.SimpleTextAttributes; -import com.intellij.util.Function; -import com.intellij.util.Icons; -import com.intellij.util.containers.CollectionFactory; -import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.containers.MultiMap; -import com.intellij.util.containers.hash.HashSet; -import gnu.trove.THashSet; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.plugins.groovy.config.LibraryManager; - -import javax.swing.*; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.*; - -/** - * @author peter - */ -public class ManagedLibrariesEditor { - private JButton myAddButton; - private JButton myRemoveButton; - private JList myLibraryList; - private JButton myModuleDeps; - private JPanel myComponent; - - private final FacetEditorContext myEditorContext; - private final FacetValidatorsManager myValidatorsManager; - private final DefaultListModel myListModel; - private final LibrariesContainer myLibrariesContainer; - private final LibraryManager[] myManagers; - private final Module myModule; - - public ManagedLibrariesEditor(final FacetEditorContext editorContext, FacetValidatorsManager validatorsManager, final ExtensionPointName epName) { - myEditorContext = editorContext; - myValidatorsManager = validatorsManager; - myModule = myEditorContext.getModule(); - myLibrariesContainer = getFacetEditorContext().getContainer(); - myManagers = epName.getExtensions(); - - myListModel = new DefaultListModel(); - - setUpLibraryList(); - installPopup(); - installButtonListeners(editorContext); - - getFacetEditorContext().addFacetContextChangeListener(new FacetContextChangeListener() { - public void moduleRootsChanged(ModifiableRootModel rootModel) { - updateLibraryList(); - } - - public void facetModelChanged(@NotNull Module module) { - } - }); - } - - private void installButtonListeners(final FacetEditorContext editorContext) { - myAddButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - //noinspection unchecked - performAddAction(myAddButton); - } - }); - myRemoveButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - deleteSelectedLibraries(); - } - }); - myModuleDeps.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - ModuleStructureConfigurable.getInstance(editorContext.getProject()).selectOrderEntry(myModule, null); - } - }); - } - - public void shouldHaveLibrary(final Condition filter, final String errorMessage) { - myValidatorsManager.registerValidator(new FacetEditorValidator() { - @Override - public ValidationResult check() { - for (ManagedLibrary library : getUsedLibraries()) { - if (filter.value(library.library)) { - return ValidationResult.OK; - } - } - - return new ValidationResult(errorMessage, new FacetConfigurationQuickFix() { - @Override - public void run(JComponent place) { - performAddAction(place); - } - }); - } - }); - - } - - public FacetEditorContextBase getFacetEditorContext() { - return (FacetEditorContextBase)myEditorContext; - } - - public JPanel getComponent() { - return myComponent; - } - - private void performAddAction(final JComponent component) { - final Set managed = getUsedLibraries(); - final Set usedLibraries = ContainerUtil.map2Set(managed, new Function() { - public Library fun(ManagedLibrary managedLibrary) { - return managedLibrary.library; - } - }); - - final MultiMap libs = new MultiMap(); - - final List toAdd = CollectionFactory.arrayList(); - final Map separators = CollectionFactory.newTroveMap(); - - for (Library library : myLibrariesContainer.getAllLibraries()) { - if (!usedLibraries.contains(library)) { - final LibraryManager manager = findManagerFor(library, myManagers, myLibrariesContainer); - if (manager != null) { - libs.putValue(manager, new ManagedLibrary(library, manager)); - } - } - } - - for (LibraryManager manager : myManagers) { - boolean separatorSet = false; - for (ManagedLibrary library : libs.get(manager)) { - if (!separatorSet) { - separators.put(library, new ListSeparator(manager.getLibraryCategoryName())); - separatorSet = true; - } - toAdd.add(library); - } - if (!separatorSet) { - separators.put(manager, new ListSeparator(manager.getLibraryCategoryName())); - } - toAdd.add(manager); - } - - JBPopupFactory.getInstance().createListPopup(new ManagedLibrariesPopupStep(toAdd, separators)).showUnderneathOf(component); - } - - private void deleteSelectedLibraries() { - Set toDelete = new HashSet(); - for (final Object value : myLibraryList.getSelectedValues()) { - toDelete.add(((ManagedLibrary) value).library); - } - doRemoveLibraries(toDelete); - updateLibraryList(); - } - - private void doRemoveLibraries(Set toDelete) { - final ModifiableRootModel rootModel = myEditorContext.getModifiableRootModel(); - for (OrderEntry entry : rootModel.getOrderEntries()) { - if (entry instanceof LibraryOrderEntry) { - final Library library = ((LibraryOrderEntry)entry).getLibrary(); - if (library != null && toDelete.remove(library)) { - rootModel.removeOrderEntry(entry); - } - } - } - } - - private void setUpLibraryList() { - myLibraryList.setModel(myListModel); - - myLibraryList.setCellRenderer(new ColoredListCellRenderer() { - protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) { - ManagedLibrary ml = (ManagedLibrary)value; - setIcon(ml.manager.getIcon()); - append(ml.getLibraryName()); - final String version = ml.version; - if (StringUtil.isNotEmpty(version)) { - append(" (version " + version + ")", SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES); - } - } - }); - - myLibraryList.getSelectionModel().addListSelectionListener(new ListSelectionListener() { - public void valueChanged(ListSelectionEvent e) { - myRemoveButton.setEnabled(hasSelection()); - } - }); - } - - private void installPopup() { - DefaultActionGroup actionGroup = new DefaultActionGroup(); - actionGroup.add(addShortcut(IdeActions.ACTION_EDIT_SOURCE, new ManagedLibraryAction("&Edit library...", false) { - - @Override - public void actionPerformed(AnActionEvent e) { - final Object value = myLibraryList.getSelectedValue(); - if (value instanceof ManagedLibrary) { - final Library library = ((ManagedLibrary)value).library; - final LibraryTable table = library.getTable(); - if (table == null) { - return; - } - final LibraryTableModifiableModelProvider provider = - ProjectStructureConfigurable.getInstance(myModule.getProject()).getContext().createModifiableModelProvider(table.getTableLevel(), false); - LibraryTableEditor.editLibrary(provider, library, myModule.getProject()).openDialog(myComponent, Collections.singletonList(library), true); - } - - } - })); - actionGroup.add(addShortcut(IdeActions.ACTION_GOTO_DECLARATION, new ManagedLibraryAction("&Show in module dependencies", false) { - @Override - public void actionPerformed(AnActionEvent e) { - final Object value = myLibraryList.getSelectedValue(); - if (value instanceof ManagedLibrary) { - for (OrderEntry entry : myEditorContext.getModifiableRootModel().getOrderEntries()) { - if (entry instanceof LibraryOrderEntry) { - final LibraryOrderEntry orderEntry = (LibraryOrderEntry)entry; - if (((ManagedLibrary)value).library.equals(orderEntry.getLibrary())) { - ModuleStructureConfigurable.getInstance(myEditorContext.getProject()).selectOrderEntry(myModule, orderEntry); - return; - } - } - } - } - - } - })); - actionGroup.add(addShortcut(IdeActions.ACTION_DELETE, new ManagedLibraryAction("R&emove...", true) { - @Override - public void actionPerformed(AnActionEvent e) { - deleteSelectedLibraries(); - } - })); - PopupHandler.installPopupHandler(myLibraryList, actionGroup, ActionPlaces.UNKNOWN, ActionManager.getInstance()); - } - - private ManagedLibraryAction addShortcut(final String fromAction, ManagedLibraryAction action) { - action.registerCustomShortcutSet(ActionManager.getInstance().getAction(fromAction).getShortcutSet(), myLibraryList); - return action; - } - - private boolean hasSelection() { - return myLibraryList.getSelectedIndices().length > 0; - } - - private Set getUsedLibraries() { - final LinkedHashSet libraries = new LinkedHashSet(); - for (OrderEntry entry : myEditorContext.getModifiableRootModel().getOrderEntries()) { - if (entry instanceof LibraryOrderEntry) { - final Library library = ((LibraryOrderEntry)entry).getLibrary(); - if (library != null) { - final LibraryManager manager = findManagerFor(library, myManagers, myLibrariesContainer); - if (manager != null) { - libraries.add(new ManagedLibrary(library, manager)); - } - } - } - } - return libraries; - } - - @Nullable - public static LibraryManager findManagerFor(@NotNull Library library, final LibraryManager[] managers, final LibrariesContainer container) { - for (final LibraryManager manager : managers) { - final String name = library.getName(); - if (name != null && manager.managesName(name) && manager.managesLibrary(library, container)) { - return manager; - } - } - - for (final LibraryManager manager : managers) { - if (manager.managesLibrary(library, container)) { - return manager; - } - } - return null; - } - - @Nullable - public Library findUsedLibrary(Class managedBy) { - for (final ManagedLibrary library : getUsedLibraries()) { - if (managedBy.isInstance(library.manager)) { - return library.library; - } - } - return null; - } - - public void updateLibraryList() { - myListModel.clear(); - Set usedManagers = new HashSet(); - for (final ManagedLibrary library : getUsedLibraries()) { - myListModel.addElement(library); - usedManagers.add(library.manager); - } - - myValidatorsManager.validate(); - } - - private class ManagedLibrary { - final Library library; - final LibraryManager manager; - @Nullable final String version; - - private ManagedLibrary(@NotNull Library library, @NotNull LibraryManager manager) { - this.library = library; - this.manager = manager; - version = manager.getLibraryVersion(library, myLibrariesContainer); - } - - public String getLibraryName() { - final String s = library.getName(); - return s == null ? "" : s; - } - } - - - private class ManagedLibrariesPopupStep extends BaseListPopupStep { - private final Map mySeparators; - - public ManagedLibrariesPopupStep(List toAdd, Map separators) { - super(null, toAdd); - mySeparators = separators; - } - - @NotNull - @Override - public String getTextFor(Object value) { - if (value instanceof ManagedLibrary) { - final ManagedLibrary ml = (ManagedLibrary)value; - final String name = ml.getLibraryName(); - - //todo make it gray - final String version = ml.version; - if (StringUtil.isNotEmpty(version)) { - return name + " (version " + version + ")"; - } - return name; - } - return ((LibraryManager) value).getAddActionText(); - } - - @Override - public Icon getIconFor(Object value) { - if (value instanceof ManagedLibrary) { - return Icons.LIBRARY_ICON; - } - return ((LibraryManager) value).getIcon(); - } - - @Override - public ListSeparator getSeparatorAbove(Object value) { - return mySeparators.get(value); - } - - @Override - public PopupStep onChosen(final Object selectedValue, boolean finalChoice) { - return doFinalStep(new Runnable() { - public void run() { - if (selectedValue instanceof ManagedLibrary) { - final ManagedLibrary managedLibrary = (ManagedLibrary)selectedValue; - addLibraryCheckingExistings(managedLibrary.manager, managedLibrary.library); - } - else if (selectedValue instanceof LibraryManager) { - final LibraryManager manager = (LibraryManager)selectedValue; - final Library library = manager.createLibrary(myEditorContext); - if (library != null) { - addLibraryCheckingExistings(manager, library); - } - } - - updateLibraryList(); - } - }); - } - - private void addLibraryCheckingExistings(LibraryManager manager, Library library) { - for (ManagedLibrary existing : getUsedLibraries()) { - if (existing.manager == manager) { - @SuppressWarnings({"NonConstantStringShouldBeStringBuffer"}) - String message = "There is already a " + manager.getLibraryCategoryName() + " library"; - final String version = manager.getLibraryVersion(existing.library, myLibrariesContainer); - if (StringUtil.isNotEmpty(version)) { - message += " of version " + version; - } - message += ".\n Do you want to replace the existing one?"; - final String replace = "&Replace"; - final int result = - Messages.showDialog(myComponent, message, "Library already exists", new String[]{replace, "&Add", "&Cancel"}, 0, null); - if (result == 2 || result < 0) { - return; //cancel or escape - } - - if (result == 0) { - doRemoveLibraries(new THashSet(Arrays.asList(existing.library))); - } - } - } - - myEditorContext.getModifiableRootModel().addLibraryEntry(library); - } - } - - private abstract class ManagedLibraryAction extends AnAction implements DumbAware { - private final boolean myOnMultiSelection; - - protected ManagedLibraryAction(String text, boolean onMultiSelection) { - super(text); - myOnMultiSelection = onMultiSelection; - } - - @Override - public void update(AnActionEvent e) { - final int selCount = myLibraryList.getSelectedIndices().length; - e.getPresentation().setEnabled(myOnMultiSelection ? selCount > 0 : selCount == 1); - } - - } - -} diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenUIModifiableModelsProvider.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenUIModifiableModelsProvider.java index c88ae4457b7d..551c35ae3316 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenUIModifiableModelsProvider.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenUIModifiableModelsProvider.java @@ -44,7 +44,7 @@ public class MavenUIModifiableModelsProvider extends MavenBaseModifiableModelsPr myModifiableArtifactModel = modifiableArtifactModel; ProjectLibrariesConfigurable configurable = ProjectLibrariesConfigurable.getInstance(project); - myLibrariesModel = (LibrariesModifiableModel)configurable.getModelProvider(true).getModifiableModel(); + myLibrariesModel = (LibrariesModifiableModel)configurable.getModelProvider().getModifiableModel(); } @Override