distinguish annotation interfaces in Ctrl-hover popup (IDEADEV-40633)
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / packaging / SelectedPackagingElements.java
blob1fd9fd307cde51f41a876d8ff20e95211f0334a3
1 package com.intellij.openapi.roots.ui.configuration.packaging;
3 import com.intellij.openapi.deployment.ContainerElement;
4 import com.intellij.openapi.deployment.LibraryLink;
5 import com.intellij.openapi.project.ProjectBundle;
6 import com.intellij.openapi.roots.OrderRootType;
7 import com.intellij.openapi.roots.libraries.Library;
8 import com.intellij.openapi.ui.Messages;
9 import com.intellij.openapi.util.Comparing;
10 import com.intellij.openapi.vfs.VirtualFile;
11 import com.intellij.util.containers.BidirectionalMap;
12 import com.intellij.util.containers.ContainerUtil;
13 import org.jetbrains.annotations.Nullable;
15 import java.util.*;
17 /**
18 * @author nik
20 class SelectedPackagingElements {
21 private final List<ContainerElement> myContainerElements = new ArrayList<ContainerElement>();
22 private final Set<PackagingArtifact> myOwners = new HashSet<PackagingArtifact>();
23 private final Collection<LibraryLink> myParentLibraryLinks;
25 public SelectedPackagingElements(final PackagingTreeNode[] treeNodes) {
26 BidirectionalMap<VirtualFile, LibraryLink> selectedFiles = new BidirectionalMap<VirtualFile, LibraryLink>();
27 Set<LibraryLink> fullySelectedLibraries = new HashSet<LibraryLink>();
28 for (PackagingTreeNode treeNode : treeNodes) {
29 ContainerElement containerElement = treeNode.getContainerElement();
30 if (containerElement == null) continue;
32 if (treeNode instanceof LibraryFileNode) {
33 LibraryFileNode node = (LibraryFileNode)treeNode;
34 selectedFiles.put(node.getFile(), node.getLibraryLink());
36 else if (containerElement instanceof LibraryLink) {
37 fullySelectedLibraries.add((LibraryLink)containerElement);
39 PackagingArtifact owner = treeNode.getOwner();
40 if (owner != null && owner.getContainerElement() != null) {
41 myOwners.add(owner);
43 else {
44 myContainerElements.add(containerElement);
48 for (LibraryLink libraryLink : selectedFiles.values()) {
49 Library library = libraryLink.getLibrary();
50 if (library != null) {
51 VirtualFile[] roots = library.getFiles(OrderRootType.CLASSES);
52 List<VirtualFile> files = selectedFiles.getKeysByValue(libraryLink);
53 if (Comparing.haveEqualElements(files, Arrays.asList(roots))) {
54 fullySelectedLibraries.add(libraryLink);
59 for (LibraryLink libraryLink : fullySelectedLibraries) {
60 selectedFiles.removeValue(libraryLink);
62 myParentLibraryLinks = selectedFiles.values();
63 ContainerUtil.removeDuplicates(myContainerElements);
66 public Collection<LibraryLink> getParentLibraryLinks() {
67 return myParentLibraryLinks;
70 public List<ContainerElement> getContainerElements() {
71 return myContainerElements;
74 public Set<PackagingArtifact> getOwners() {
75 return myOwners;
78 public boolean showRemovingWarning(final PackagingEditorImpl editor) {
79 Set<PackagingArtifact> owners = getOwners();
80 Collection<LibraryLink> parentLibraryLinks = getParentLibraryLinks();
81 if (!parentLibraryLinks.isEmpty()) {
82 StringBuilder librariesNames = new StringBuilder();
83 for (LibraryLink link : parentLibraryLinks) {
84 if (librariesNames.length() > 0) librariesNames.append(", ");
85 librariesNames.append('\'').append(link.getPresentableName()).append('\'');
87 String message = ProjectBundle.message("message.text.individial.files.cannot.be.removed.from.packaging.do.you.want.to.remove.the.whole.libraries",
88 librariesNames.toString(), parentLibraryLinks.size());
89 int answer = Messages.showYesNoDialog(editor.getMainPanel(), message, ProjectBundle.message("dialog.title.packaging.remove.included"), null);
90 if (answer != 0) {
91 return false;
95 if (!owners.isEmpty()) {
96 String message;
97 if (owners.size() == 1 && getContainerElements().isEmpty()) {
98 PackagingArtifact artifact = owners.iterator().next();
99 message = ProjectBundle.message("message.text.packaging.selected.item.belongs.to.0.do.you.want.to.exlude.1.from.2",
100 artifact.getDisplayName(), artifact.getDisplayName(), editor.getRootArtifact().getDisplayName());
102 else {
103 StringBuilder ownersBuffer = new StringBuilder();
104 for (PackagingArtifact owner : owners) {
105 if (ownersBuffer.length() > 0) {
106 ownersBuffer.append(", ");
108 ownersBuffer.append(owner.getDisplayName());
110 message = ProjectBundle.message("message.text.packaging.do.you.want.to.exlude.0.from.1", ownersBuffer, editor.getRootArtifact().getDisplayName());
112 int answer = Messages.showYesNoDialog(editor.getMainPanel(), message, ProjectBundle.message("dialog.title.packaging.remove.included"), null);
113 if (answer != 0) {
114 return false;
117 return true;
120 @Nullable
121 public PackagingElementsToEditInfo getElementsToEdit(final PackagingEditorPolicy policy) {
122 Set<ContainerElement> elements = new HashSet<ContainerElement>(myContainerElements);
123 for (PackagingArtifact owner : myOwners) {
124 ContainerElement element = owner.getContainerElement();
125 if (element != null) {
126 elements.add(element);
129 if (elements.isEmpty()) {
130 return null;
132 if (elements.size() == 1) {
133 return new PackagingElementsToEditInfo(elements.iterator().next(), policy);
136 return new PackagingElementsToEditInfo(elements, policy);