migrated to artifacts
[fedora-idea.git] / java / compiler / impl / src / com / intellij / packaging / impl / elements / ArtifactElementType.java
blobb8c882aa76e46f3b55600a1b3560e68b1396e0fd
1 package com.intellij.packaging.impl.elements;
3 import com.intellij.openapi.compiler.CompilerBundle;
4 import com.intellij.openapi.project.Project;
5 import com.intellij.packaging.artifacts.Artifact;
6 import com.intellij.packaging.artifacts.ArtifactManager;
7 import com.intellij.packaging.artifacts.ArtifactPointerManager;
8 import com.intellij.packaging.elements.CompositePackagingElement;
9 import com.intellij.packaging.elements.PackagingElementType;
10 import com.intellij.packaging.impl.artifacts.ArtifactUtil;
11 import com.intellij.packaging.impl.artifacts.PlainArtifactType;
12 import com.intellij.packaging.ui.ArtifactEditorContext;
13 import com.intellij.util.Processor;
14 import org.jetbrains.annotations.NotNull;
16 import javax.swing.*;
17 import java.util.*;
19 /**
20 * @author nik
22 public class ArtifactElementType extends PackagingElementType<ArtifactPackagingElement> {
23 public static final ArtifactElementType ARTIFACT_ELEMENT_TYPE = new ArtifactElementType();
25 ArtifactElementType() {
26 super("artifact", CompilerBundle.message("element.type.name.artifact"));
29 @Override
30 public Icon getCreateElementIcon() {
31 return PlainArtifactType.ARTIFACT_ICON;
34 @Override
35 public boolean canCreate(@NotNull ArtifactEditorContext context, @NotNull Artifact artifact) {
36 return !getAvailableArtifacts(context, artifact).isEmpty();
39 @NotNull
40 public List<? extends ArtifactPackagingElement> chooseAndCreate(@NotNull ArtifactEditorContext context, @NotNull Artifact artifact,
41 @NotNull CompositePackagingElement<?> parent) {
42 final Project project = context.getProject();
43 List<Artifact> artifacts = context.chooseArtifacts(getAvailableArtifacts(context, artifact), CompilerBundle.message("dialog.title.choose.artifacts"));
44 final List<ArtifactPackagingElement> elements = new ArrayList<ArtifactPackagingElement>();
45 for (Artifact selected : artifacts) {
46 elements.add(new ArtifactPackagingElement(project, ArtifactPointerManager.getInstance(project).create(selected.getName())));
48 return elements;
51 @NotNull
52 public static List<? extends Artifact> getAvailableArtifacts(@NotNull final ArtifactEditorContext context, @NotNull final Artifact artifact) {
53 final Set<Artifact> result = new HashSet<Artifact>(Arrays.asList(context.getArtifactModel().getArtifacts()));
54 ArtifactUtil.processPackagingElements(artifact, ARTIFACT_ELEMENT_TYPE, new Processor<ArtifactPackagingElement>() {
55 public boolean process(ArtifactPackagingElement artifactPackagingElement) {
56 result.remove(artifactPackagingElement.findArtifact(context));
57 return true;
59 }, context, true);
60 result.remove(artifact);
61 final Iterator<Artifact> iterator = result.iterator();
62 while (iterator.hasNext()) {
63 Artifact another = iterator.next();
64 final boolean notContainThis =
65 ArtifactUtil.processPackagingElements(another, ARTIFACT_ELEMENT_TYPE, new Processor<ArtifactPackagingElement>() {
66 public boolean process(ArtifactPackagingElement element) {
67 return !artifact.getName().equals(element.getArtifactName());
69 }, context, true);
70 if (!notContainThis) {
71 iterator.remove();
74 final ArrayList<Artifact> list = new ArrayList<Artifact>(result);
75 Collections.sort(list, ArtifactManager.ARTIFACT_COMPARATOR);
76 return list;
79 @NotNull
80 public ArtifactPackagingElement createEmpty(@NotNull Project project) {
81 return new ArtifactPackagingElement(project);