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
;
34 import java
.util
.List
;
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
);
62 public CompositePackagingElement
<?
> getParentElement(PackagingElement
<?
> element
) {
63 return myParentElements
.get(element
);
67 public CompositePackagingElementNode
getParentNode() {
71 public List
<E
> getPackagingElements() {
72 return myPackagingElements
;
76 public E
getElementIfSingle() {
77 return myPackagingElements
.size() == 1 ? myPackagingElements
.get(0) : null;
81 public Object
[] getEqualityObjects() {
82 return myPackagingElements
.toArray(new Object
[myPackagingElements
.size()]);
86 protected SimpleNode
[] buildChildren() {
90 public E
getFirstElement() {
91 return myPackagingElements
.get(0);
95 protected void update(PresentationData presentation
) {
96 final String message
= ((ArtifactEditorContextImpl
)myContext
).getValidationManager().getProblem(this);
97 if (message
== null) {
98 super.update(presentation
);
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
);
121 public Collection
<PackagingNodeSource
> getNodeSources() {
122 return myNodeSources
.values();
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() {
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())) {