From b26be44c93eb3ff25d93787a6ec6ac0fa2a46e1c Mon Sep 17 00:00:00 2001 From: Alexey Pegov Date: Wed, 13 Oct 2010 13:36:38 +0400 Subject: [PATCH] IDEA-59669 AE at LibraryImpl.getFiles() on removing just created library (third case) --- .../BaseLibrariesConfigurable.java | 64 +++++++++++++++++-- .../BaseStructureConfigurable.java | 6 +- .../ModuleStructureConfigurable.java | 16 +++++ .../openapi/ui/MasterDetailsComponent.java | 34 ++++++++-- 4 files changed, 105 insertions(+), 15 deletions(-) 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 77842d8111db..0a4674e705b4 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,9 +27,9 @@ 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.artifacts.UsageInArtifact; 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.roots.ui.configuration.projectRoot.daemon.*; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.NamedConfigurable; @@ -186,13 +186,65 @@ public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurabl public abstract BaseLibrariesConfigurable getOppositeGroup(); - protected void removeLibrary(final Library library) { + protected boolean removeLibrary(final Library library) { final LibraryTable table = library.getTable(); if (table != null) { - getModelProvider().getModifiableModel().removeLibrary(library); - myContext.getDaemonAnalyzer().removeElement(new LibraryProjectStructureElement(myContext, library)); - // TODO: myContext.invalidateModules(myContext.myLibraryDependencyCache.get(library.getName())); + final Collection usages = myContext.getDaemonAnalyzer().getUsages(getSelectedElement()); + if (usages.size() > 0) { + final List modules = new ArrayList(); + final List artifacts = new ArrayList(); + for (final ProjectStructureElementUsage usage : usages) { + if (usage instanceof UsageInModuleClasspath) { + modules.add(usage.getPresentableName()); + } else if (usage instanceof UsageInArtifact) { + artifacts.add(usage.getPresentableName()); + } else { + LOG.error("Unknown usage: " + usage.getClass().getName()); + } + } + + final StringBuilder sb = new StringBuilder("Library \""); + sb.append(library.getName()).append("\" is used in "); + if (modules.size() > 0) { + if (modules.size() == 1) { + sb.append("module ").append("\"").append(modules.get(0)).append("\""); + } else { + sb.append(modules.size()).append(" modules"); + } + } + + if (artifacts.size() > 0) { + sb.append(modules.size() > 0 ? " and in " : "."); + + if (modules.size() == 1) { + sb.append("artifact ").append("\"").append(artifacts.get(0)).append("\"."); + } else { + sb.append(artifacts.size()).append(" artifacts."); + } + } + + sb.append("\n\nAre you sure you want to delete this library?"); + + if (DialogWrapper.OK_EXIT_CODE == Messages.showOkCancelDialog(myProject, sb.toString(), + "Confirm library deletion", Messages.getQuestionIcon())) { + + final ModuleStructureConfigurable rootConfigurable = ModuleStructureConfigurable.getInstance(myProject); + for (final ProjectStructureElementUsage usage : usages) { + if (usage instanceof UsageInModuleClasspath) { + rootConfigurable.removeLibraryOrderEntry(((ModuleProjectStructureElement)usage.getContainingElement()).getModule(), library); + } else if (usage instanceof UsageInArtifact) { + // TODO[nik] implement me + } + } + + getModelProvider().getModifiableModel().removeLibrary(library); + myContext.getDaemonAnalyzer().removeElement(new LibraryProjectStructureElement(myContext, library)); + return true; + } + } } + + return false; } protected diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseStructureConfigurable.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseStructureConfigurable.java index 2a93273184a0..ea570efe6f72 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseStructureConfigurable.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseStructureConfigurable.java @@ -403,7 +403,7 @@ public abstract class BaseStructureConfigurable extends MasterDetailsComponent i if (removeFacet((Facet)editableObject).isEmpty()) return false; } else if (editableObject instanceof Library) { - removeLibrary((Library)editableObject); + if (!removeLibrary((Library)editableObject)) return false; } else if (editableObject instanceof Artifact) { removeArtifact((Artifact)editableObject); @@ -416,8 +416,8 @@ public abstract class BaseStructureConfigurable extends MasterDetailsComponent i } - protected void removeLibrary(Library library) { - + protected boolean removeLibrary(Library library) { + return false; } protected void removeFacetNodes(@NotNull List facets) { diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ModuleStructureConfigurable.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ModuleStructureConfigurable.java index bfc9a0df69ff..c89cf77288ba 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ModuleStructureConfigurable.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ModuleStructureConfigurable.java @@ -417,6 +417,22 @@ public class ModuleStructureConfigurable extends BaseStructureConfigurable imple } } + public void removeLibraryOrderEntry(final Module module, final Library library) { + final ModuleEditor moduleEditor = myContext.myModulesConfigurator.getModuleEditor(module); + LOG.assertTrue(moduleEditor != null, "Current module editor was not initialized"); + final ModifiableRootModel modelProxy = moduleEditor.getModifiableRootModelProxy(); + final OrderEntry[] entries = modelProxy.getOrderEntries(); + for (OrderEntry entry : entries) { + if (entry instanceof LibraryOrderEntry && Comparing.strEqual(entry.getPresentableName(), library.getName())) { + modelProxy.removeOrderEntry(entry); + break; + } + } + + myContext.getDaemonAnalyzer().queueUpdate(new ModuleProjectStructureElement(myContext, module)); + myTree.repaint(); + } + public void addLibraryOrderEntry(final Module module, final Library library) { Component parent = WindowManager.getInstance().suggestParentWindow(module.getProject()); diff --git a/platform/platform-api/src/com/intellij/openapi/ui/MasterDetailsComponent.java b/platform/platform-api/src/com/intellij/openapi/ui/MasterDetailsComponent.java index 4b562edba59d..f7fea3927e78 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/MasterDetailsComponent.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/MasterDetailsComponent.java @@ -678,12 +678,34 @@ public abstract class MasterDetailsComponent implements Configurable, DetailsCom namedConfigurable.disposeUIResources(); } - if (parentNode != null && idx != -1) { - TreeUtil - .selectInTree((DefaultMutableTreeNode)(idx < parentNode.getChildCount() ? parentNode.getChildAt(idx) : parentNode), true, myTree); - } - else { - TreeUtil.selectFirstNode(myTree); + if (paths.length > 0) { + if (parentNode != null && idx != -1) { + DefaultMutableTreeNode toSelect = null; + if (idx < parentNode.getChildCount()) { + toSelect = (DefaultMutableTreeNode) parentNode.getChildAt(idx); + } else { + if (idx > 0 && parentNode.getChildCount() > 0) { + if (idx - 1 < parentNode.getChildCount()) { + toSelect = (DefaultMutableTreeNode) parentNode.getChildAt(idx - 1); + } else { + toSelect = (DefaultMutableTreeNode) parentNode.getFirstChild(); + } + } else { + if (parentNode.isRoot() && myTree.isRootVisible()) { + toSelect = parentNode; + } else if (parentNode.getChildCount() > 0) { + toSelect = (DefaultMutableTreeNode) parentNode.getFirstChild(); + } + } + } + + if (toSelect != null) { + TreeUtil.selectInTree(toSelect, true, myTree); + } + } + else { + TreeUtil.selectFirstNode(myTree); + } } }