find usages & validation in project structure reworked
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / artifacts / nodes / PackagingElementNode.java
blobcc7e19c8d1c6317d877bf53ada42808b6a558e01
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.roots.ui.configuration.artifacts.nodes;
18 import com.intellij.ide.projectView.PresentationData;
19 import com.intellij.openapi.editor.markup.EffectType;
20 import com.intellij.openapi.editor.markup.TextAttributes;
21 import com.intellij.openapi.roots.ui.configuration.artifacts.ArtifactEditorContextImpl;
22 import com.intellij.openapi.util.MultiValuesMap;
23 import com.intellij.packaging.elements.CompositePackagingElement;
24 import com.intellij.packaging.elements.PackagingElement;
25 import com.intellij.packaging.ui.ArtifactEditorContext;
26 import com.intellij.ui.SimpleTextAttributes;
27 import com.intellij.ui.treeStructure.SimpleNode;
28 import com.intellij.util.SmartList;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 import java.awt.*;
33 import java.util.*;
34 import java.util.List;
36 /**
37 * @author nik
39 public class PackagingElementNode<E extends PackagingElement<?>> extends ArtifactsTreeNode {
40 private final List<E> myPackagingElements;
41 private final Map<PackagingElement<?>, CompositePackagingElement<?>> myParentElements = new HashMap<PackagingElement<?>, CompositePackagingElement<?>>(1);
42 private final MultiValuesMap<PackagingElement<?>, PackagingNodeSource> myNodeSources = new MultiValuesMap<PackagingElement<?>, PackagingNodeSource>();
43 private final CompositePackagingElementNode myParentNode;
45 public PackagingElementNode(@NotNull E packagingElement, ArtifactEditorContext context, @Nullable CompositePackagingElementNode parentNode,
46 @Nullable CompositePackagingElement<?> parentElement,
47 @NotNull Collection<PackagingNodeSource> nodeSources) {
48 super(context, parentNode, packagingElement.createPresentation(context));
49 myParentNode = parentNode;
50 myParentElements.put(packagingElement, parentElement);
51 myNodeSources.putAll(packagingElement, nodeSources);
52 myPackagingElements = new SmartList<E>();
53 doAddElement(packagingElement);
56 private void doAddElement(E packagingElement) {
57 myPackagingElements.add(packagingElement);
58 ((ArtifactEditorContextImpl)myContext).getValidationManager().elementAddedToNode(this, packagingElement);
61 @Nullable
62 public CompositePackagingElement<?> getParentElement(PackagingElement<?> element) {
63 return myParentElements.get(element);
66 @Nullable
67 public CompositePackagingElementNode getParentNode() {
68 return myParentNode;
71 public List<E> getPackagingElements() {
72 return myPackagingElements;
75 @Nullable
76 public E getElementIfSingle() {
77 return myPackagingElements.size() == 1 ? myPackagingElements.get(0) : null;
80 @Override
81 public Object[] getEqualityObjects() {
82 return myPackagingElements.toArray(new Object[myPackagingElements.size()]);
85 @Override
86 protected SimpleNode[] buildChildren() {
87 return NO_CHILDREN;
90 public E getFirstElement() {
91 return myPackagingElements.get(0);
94 @Override
95 protected void update(PresentationData presentation) {
96 final String message = ((ArtifactEditorContextImpl)myContext).getValidationManager().getProblem(this);
97 if (message == null) {
98 super.update(presentation);
99 return;
102 getElementPresentation().render(presentation, addErrorHighlighting(SimpleTextAttributes.REGULAR_ATTRIBUTES),
103 addErrorHighlighting(SimpleTextAttributes.GRAY_ATTRIBUTES));
104 presentation.setTooltip(message);
107 private SimpleTextAttributes addErrorHighlighting(SimpleTextAttributes attributes) {
108 final TextAttributes textAttributes = attributes.toTextAttributes();
109 textAttributes.setEffectType(EffectType.WAVE_UNDERSCORE);
110 textAttributes.setEffectColor(Color.RED);
111 return SimpleTextAttributes.fromTextAttributes(textAttributes);
114 void addElement(PackagingElement<?> element, CompositePackagingElement parentElement, Collection<PackagingNodeSource> nodeSource) {
115 doAddElement((E)element);
116 myParentElements.put(element, parentElement);
117 myNodeSources.putAll(element, nodeSource);
120 @NotNull
121 public Collection<PackagingNodeSource> getNodeSources() {
122 return myNodeSources.values();
125 @NotNull
126 public Collection<PackagingNodeSource> getNodeSource(@NotNull PackagingElement<?> element) {
127 final Collection<PackagingNodeSource> nodeSources = myNodeSources.get(element);
128 return nodeSources != null ? nodeSources : Collections.<PackagingNodeSource>emptyList();
131 public ArtifactEditorContext getContext() {
132 return myContext;
135 @Nullable
136 public CompositePackagingElementNode findCompositeChild(@NotNull String name) {
137 final SimpleNode[] children = getChildren();
138 for (SimpleNode child : children) {
139 if (child instanceof CompositePackagingElementNode) {
140 final CompositePackagingElementNode composite = (CompositePackagingElementNode)child;
141 if (name.equals(composite.getFirstElement().getName())) {
142 return composite;
146 return null;